Skip to main content

Recording api calls

Recording api calls from all api routes is done using this helper component. which takes in the req supabase(supabase client) and userId if it exist otherwise we will try to get the current user via supabase client.

getNextJsApiPath is a function that will construct a path from where it was called for ex https://mmv-dex.vercel.app/api/peer-review/create upon getting the NextApiRequest.

import { SupabaseClient } from "@supabase/supabase-js";
import { NextApiRequest } from "next";
import { DatabaseTypes } from "../types/db/db-types";

export const getNextJsApiPath = (req: NextApiRequest) => {
let api_path: string | null = (req.headers.origin || req.headers.host || "") + (req.url || "");
if (api_path.length === 0) api_path = null;
return api_path;
};

export const recordNextJsApiCall = async (
req: NextApiRequest,
supabase: SupabaseClient<DatabaseTypes>,
userId?: string | null | undefined
) => {
if (!userId) {
const {
data: { user },
} = await supabase.auth.getUser();
userId = user?.id || null;
}
const api_path = getNextJsApiPath(req);
if (!api_path) return;
return await supabase.from("api_calls").insert({ api_path, called_by: userId });
};