MCPcopy Index your code
hub / github.com/sqlchat/sqlchat / handler

Function handler

src/pages/api/usage.ts:10–38  ·  view source on GitHub ↗
(req: NextApiRequest, res: NextApiResponse)

Source from the content-addressed store, hash-verified

8import { Quota } from "@/types";
9
10const handler = async (req: NextApiRequest, res: NextApiResponse) => {
11 if (req.method !== "GET" && req.method !== "POST") {
12 return res.status(405).json([]);
13 }
14
15 const endUser = await getEndUser(req, res);
16
17 // Get from server session if available
18 const serverSession = await getServerSession(req, res, authOptions);
19 let subscripion = serverSession?.user?.subscription;
20 if (!subscripion) {
21 subscripion = await getSubscriptionByEmail(endUser);
22 }
23
24 let usage = 0;
25 if (req.method === "GET") {
26 usage = await getCurrentMonthUsage(endUser);
27 } else if (req.method === "POST") {
28 const model = getModel((req.headers["x-openai-model"] as string) || "");
29 usage = await addUsage(endUser, model.cost_per_call);
30 }
31
32 const quota: Quota = {
33 current: usage,
34 limit: subscripion.quota,
35 };
36
37 res.status(200).json(quota);
38};
39
40export default handler;

Callers

nothing calls this directly

Calls 5

getEndUserFunction · 0.90
getSubscriptionByEmailFunction · 0.90
getCurrentMonthUsageFunction · 0.90
getModelFunction · 0.90
addUsageFunction · 0.90

Tested by

no test coverage detected