Observing Supabase calls
Count and time every request the server makes to Supabase with observedFetch.
observedFetch from better-supabase/server wraps a fetch so each request
reports its method, URL, status and duration. Pass the result as the fetch
option of createServer, and every call the server's clients make to the Data
API, Auth and Storage goes through it:
import { createServer, observedFetch } from "better-supabase/server";
import { betterSupabase } from "./index";
export const bsb = createServer(betterSupabase, {
fetch: observedFetch({
onCall: ({ method, url, status, durationMs, error }) => {
console.log(method, url, status ?? error, `${Math.round(durationMs)}ms`);
},
}),
});The URL has no query string, because it can carry tokens. When the request
throws, status is undefined and error holds the error's name, and the
original error is rethrown. The observer runs after the request settled, and an
observer that throws is ignored, so it can never change a response. Wrap
another fetch (a retrying or tracing one) with the fetch option, and
replace the clock in tests with now.
The Express example counts the calls by status this way.
Last updated on