supabase / functions-js

MIT License
61 stars 22 forks source link

Support multiple content types or allow custom response handling #70

Open matejm opened 1 year ago

matejm commented 1 year ago

Bug report

Describe the bug

Currently, @supabase/functions-js package automatically handles JSON/text/multipart-form/blob parsing. However, this is done simply by checking if Content-Type if one of three predefined options (see functions-js source code). In all other cases, response is converted to text.

Example

Supabase has defined following edge function:

// read-pdf-file
serve(async (req) => {
  // read pdf file from S3 storage and return it as a response
  const pdfFile: Blob = ...
  return new Response(
    pdfFile,
    {
      headers: {
        // set correct content type
        "Content-Type": "application/pdf",
      },
    },
  );
});

When this function is invoked via Supabase client:

const { data } = await client.functions.invoke(f, { method: 'GET' });
// data is of type string, but should be Blob

As application/pdf is not one of three predefined content types, response is converted to text (in this line).

I know it is impossible to handle all possible content types, but defaulting to text might not be the best idea. Maybe you could add something like customResponseParser, which could be handled in a similar way to customFetch option already provided.

Does this make sense or am I missing something?

alphanumericaracters commented 7 months ago

Ok, so... now i can/have a response in PDF format and not JSON??? (sorry im newby)