All files / src/routes/api/documents $documentId.ts

20% Statements 1/5
100% Branches 0/0
0% Functions 0/1
20% Lines 1/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38        1x                                                                  
// A document file, after the access check in receipt-response.server.ts.
// `?download=1` sends it as an attachment.
import { createFileRoute } from '@tanstack/react-router'
 
export const Route = createFileRoute('/api/documents/$documentId')({
  server: {
    handlers: {
      // Imported on use: the modules need the Workers runtime, and the route
      // tree is also loaded where there is none (unit tests, the client).
      GET: async ({ request, params }) => {
        const [
          { documentResponse },
          { documentRepository },
          { sharingRepository },
          { getRequestContext },
        ] = await Promise.all([
          import('#/features/documents/document-response.server'),
          import('#/features/documents/document.repository.server'),
          import('#/features/sharing/sharing.repository.server'),
          import('#/server/platform/context.server'),
        ])
        const context = getRequestContext()
        const session = await context.auth.getSession()
        return documentResponse(
          {
            userId: session?.user.id,
            documents: documentRepository(context.db),
            sharing: sharingRepository(context.db),
            storage: context.storage,
          },
          params.documentId,
          { download: new URL(request.url).searchParams.has('download') },
        )
      },
    },
  },
})