supabase / functions-js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Body does not seem properly formated and cors error

GaryAustin1 opened this issue · comments

Bug report

The documentation shows calling a function as:

const { data: user, error } = await supabase.functions.invoke('hello', {
  responseType: 'text',
  body: { foo: 'bar' }
})

The function.js code has

const response = await this.fetch(`${this.url}/${functionName}`, {
        method: 'POST',
        headers: Object.assign({}, this.headers, headers),
        body,
      })

I would think at a minimum body should be body:body.
Very strange results occur, but basically content length is string length if {body:'string'} is used in call.
If an object is used {body:{k:'v'}, length is always 15 probably an error message for invalid object or something...

Not sure if it is related. But all calls generate cors errors:

image

To Reproduce

Follow the instructions for functions invoke in api reference.

Expected behavior

Body should be formatted correctly and no cors error (if not related)

System information

Latest supabase.js

It appears to use functions.js Edge Functions need more than the sample code shown so far.
I had to add allow headers to the return..
supabase/supabase#6267

Finally got it all to work:
Seems body: is optional in fetch? BUT the documentation shows passing in an object for body. When I passed in a json string:
body:'{"name":"gary"}' The body gets passed correctly.

Also for the browser to work with function.js I had to deal with an OPTION request and the POST request, both with allow headers like such:

import { serve } from "https://deno.land/std@0.131.0/http/server.ts";

console.log("Hello from Functions!");

serve(async (req) => {
        console.log(req.method)
        if (req.method === 'OPTIONS') {
            return new Response(
                'ok',
                {
                    headers: {
                        "Access-Control-Allow-Origin": "*",
                        "Access-Control-Allow-Methods": "POST",
                        "Access-Control-Expose-Headers": "Content-Length, X-JSON",
                        "Access-Control-Allow-Headers": "apikey,X-Client-Info, Content-Type, Authorization, Accept, Accept-Language, X-Authorization",
                    }
                }
            );
        }
        else {
            const jsonData = await req.json();
            const name = jsonData.name;
            const data = {message: `Hello there ${name}!`};
            return new Response(
                JSON.stringify(data),
                {
                    headers: {
                        "Content-Type": "application/json",
                        "Access-Control-Allow-Origin": "*",
                        "Access-Control-Allow-Methods": "POST",
                        "Access-Control-Expose-Headers": "Content-Length, X-JSON",
                        "Access-Control-Allow-Headers": "apikey,X-Client-Info, Content-Type, Authorization, Accept, Accept-Language, X-Authorization",
                    }
                }
            );
        }
});

Thanks @GaryAustin1 I've replied on the other issue: supabase/supabase#6267