Functions
Supabase provides Edge functions to execute code on-demand in the cloud
Prerequisites
A rest client to trigger functions, E.g.
Running Locally
If developing functions locally run via
yarn nx run picsa-server:supabase functions serve
Authorization
Functions may expect a user to pass auth credentials with the function. A list of available credentials can be found via
yarn nx run picsa-server:supabase status
Output will provide keys for both anonymous and service role user
anon key: eyJhbGciOiJIUzI1N....
service_role key: eyJhbGciOiJI...
Either of these can be included in the headers of your http rest client when calling functions.
Usually there will be an Auth
option with the request, use this to set a Bearer token
with the value Output
E.g. in Insomnia:
Develop New Functions
Supabase recommends using a smaller number of functions, where each function can serve multiple purposes
As such all functions related to a specific product, e.g. the dashboard, should be grouped into a single parent function as seen in
To add a new function simply create the .ts
file and add a new endpoint to call that function from the main index, e.g.
apps/picsa-server/supabase/functions/dashboard/index.ts
switch (endpoint) {
case "my-endpoint":
return myFunction(req);
// ...
}
my-function.ts
export const myFunction = async (req: Request) => {
return new Response("it works");
};
To return more structured responses you may want to use the JSONResponse
and ErrorResponse
utility methods included locally
Run via Rest Client
Simply make a request to the named endpoint, e.g.
http://localhost:54321/functions/v1/dashboard/my-function
Unit Testing
Whilst serving functions tests can be executed by
yarn nx run picsa-server:test-functions
In order to run tests that connect to supabase an additional .env.local
file should be populated in the the functions directory with SUPABASE_ANON_KEY credentials
Tests are written using the Behavior-Driven Development module
Functions cli supports additional arguments such as --watch
yarn nx run picsa-server:test-functions --watch
Production Deployment
supabase functions deploy