MCPcopy Create free account
hub / github.com/LeenHawk/gproxy / api

Function api

console/src/api/http.ts:27–60  ·  view source on GitHub ↗
(
  path: string,
  init: Omit<RequestInit, "headers"> & { headers?: Record<string, string> } = {},
)

Source from the content-addressed store, hash-verified

25}
26
27export async function api<T>(
28 path: string,
29 init: Omit<RequestInit, "headers"> & { headers?: Record<string, string> } = {},
30): Promise<T> {
31 let res: Response;
32 try {
33 res = await fetch(path, {
34 credentials: "include",
35 ...init,
36 headers: {
37 ...(typeof init.body === "string" ? { "content-type": "application/json" } : {}),
38 ...init.headers,
39 },
40 });
41 } catch {
42 throw new ApiError(0, "network", "network error");
43 }
44 if (res.status === 204) return undefined as T;
45 if (!res.ok) {
46 const retryAfterRaw = Number(res.headers.get("retry-after"));
47 const retryAfter = Number.isFinite(retryAfterRaw) && retryAfterRaw > 0 ? retryAfterRaw : undefined;
48 let type: ApiErrorType = "internal";
49 let message = res.statusText || `HTTP ${res.status}`;
50 try {
51 const body = (await res.json()) as ErrorBody;
52 if (body.error?.type) type = body.error.type as ApiErrorType;
53 if (body.error?.message) message = body.error.message;
54 } catch {
55 /* non-JSON error body (e.g. 429 from throttle) — keep defaults */
56 }
57 throw new ApiError(res.status, type, message, retryAfter);
58 }
59 return (await res.json()) as T;
60}

Callers 15

permissionsQueryFunction · 0.90
rateLimitsQueryFunction · 0.90
upsertPermissionFunction · 0.90
deletePermissionFunction · 0.90
upsertRateLimitFunction · 0.90
deleteRateLimitFunction · 0.90
upsertQuotaFunction · 0.90
deleteQuotaFunction · 0.90
loginFlowStartFunction · 0.90
loginFlowCompleteFunction · 0.90
deviceStartFunction · 0.90
devicePollFunction · 0.90

Calls 3

jsonMethod · 0.80
fetchFunction · 0.50
getMethod · 0.45

Tested by

no test coverage detected