MCPcopy
hub / github.com/fosrl/pangolin / makeApiRequest

Function makeApiRequest

src/actions/server.ts:71–197  ·  view source on GitHub ↗
(
    url: string,
    method: "GET" | "POST",
    body?: any,
    additionalHeaders: Record<string, string> = {}
)

Source from the content-addressed store, hash-verified

69}
70
71async function makeApiRequest<T>(
72 url: string,
73 method: "GET" | "POST",
74 body?: any,
75 additionalHeaders: Record<string, string> = {}
76): Promise<ResponseT<T>> {
77 // Get existing cookies to forward
78 const allCookies = await cookies();
79 const cookieHeader = allCookies.toString();
80
81 const headersList = await reqHeaders();
82 const host = headersList.get("host");
83 const xForwardedFor = headersList.get("x-forwarded-for");
84
85 const headers: Record<string, string> = {
86 "Content-Type": "application/json",
87 "X-CSRF-Token": "x-csrf-protection",
88 ...(xForwardedFor ? { "X-Forwarded-For": xForwardedFor } : {}),
89 ...(cookieHeader && { Cookie: cookieHeader }),
90 ...additionalHeaders
91 };
92
93 let res: Response;
94 try {
95 res = await fetch(url, {
96 method,
97 headers,
98 body: body ? JSON.stringify(body) : undefined,
99 cache: "no-store"
100 });
101 } catch (fetchError) {
102 console.error("API request failed:", fetchError);
103 return {
104 data: null,
105 success: false,
106 error: true,
107 message: "Failed to connect to server. Please try again.",
108 status: 0
109 };
110 }
111
112 // Handle Set-Cookie header
113 const rawSetCookie = res.headers.get("set-cookie");
114 if (rawSetCookie) {
115 try {
116 const { name, value, options } = parseSetCookieString(
117 rawSetCookie,
118 host || undefined
119 );
120 const allCookies = await cookies();
121 allCookies.set(name, value, options);
122 } catch (cookieError) {
123 console.error("Failed to parse Set-Cookie header:", cookieError);
124 // Continue without setting cookies rather than failing
125 }
126 }
127
128 let responseData;

Callers

nothing calls this directly

Calls 3

parseSetCookieStringFunction · 0.85
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected