Currently, I have a route handler that returns a stream of a PDF as the response body:
api.get('/create/pdf', async ({ request, response }) => {
const { pdfStream } = await createPdfStream() // This returns a Transform stream
response.body = pdfStream
})
I am able to get this PDF on the client by doing the following:
const response = await fetch('/create/pdf')
const blob = await response.blob()
const pdfUrl = URL.createObjectUrl(blob) // I can now use `pdfUrl` to render the PDF
However, I now want to return some additional properties from the API route and so I want to make it a JSON response instead of just returning the stream. Is this possible? I basically want to change my route handler to something like this:
api.get('/create/pdf', async ({ request, response }) => {
const { pdfStream, pdfMetadata } = await createPdfStream() // This returns a Transform stream
response.body = {
pdfStream,
pdfMetadata
}
})
But if I did this, I am not sure how I would be able to access the PDF on the client anymore as I won't be able to do response.blob()
anymore, I could only do response.json()
. Does anyone have any tips?