Skip to content

Commit 19385d7

Browse files
committed
Update fetch.ts
1 parent 841e214 commit 19385d7

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

src/lib/api/fetch.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const mcpNameHeader = 'X-mcp';
1010
const jqHeader = 'X-jq';
1111
const contentTypeHeader = 'Content-Type';
1212

13+
/**
14+
* Attempts to parse the response body as JSON. If parsing fails, returns the raw text.
15+
*
16+
* @param {Response} res - The fetch Response object.
17+
* @returns {Promise<unknown>} The parsed JSON object or the raw text if parsing fails.
18+
*/
1319
export const parseJsonOrText = async (res: Response): Promise<unknown> => {
1420
const text = await res.text();
1521
try {
@@ -19,28 +25,46 @@ export const parseJsonOrText = async (res: Response): Promise<unknown> => {
1925
}
2026
};
2127

22-
// fetchApiServer is a wrapper around fetch that adds the necessary headers for the Crate API or the MCP API server.
28+
/**
29+
* Wrapper around fetch that adds the necessary headers for the Crate API or the MCP API server.
30+
* Handles authentication, content type, and custom headers for backend routing.
31+
* Redirects to login if the response is unauthorized (401).
32+
*
33+
* @param {string} path - The API endpoint path (appended to `/api/onboarding`).
34+
* @param {ApiConfig} config - The API configuration, including authentication and MCP/Crate context.
35+
* @param {string} [jq] - Optional jq transformation string for the proxy server.
36+
* @param {string} [httpMethod='GET'] - The HTTP method to use (GET, POST, PATCH, etc.).
37+
* @param {BodyInit} [body] - The request body, if applicable.
38+
* @returns {Promise<Response>} The fetch Response object.
39+
* @throws {APIError} Throws an APIError if the response is not ok.
40+
*/
2341
export const fetchApiServer = async (
2442
path: string,
2543
config: ApiConfig,
2644
jq?: string,
2745
httpMethod: string = 'GET',
2846
body?: BodyInit,
2947
): Promise<Response> => {
30-
// The default headers used for the fetch request.
31-
// The Authorization header is required for both the Crate API and the MCP API and the correct token is passed in the config object that is consumed outside this function from the context that has handled the OIDC flow to get a token.
48+
/**
49+
* The default headers used for the fetch request.
50+
* The Authorization header is required for both the Crate API and the MCP API and the correct token is passed in the config object that is consumed outside this function from the context that has handled the OIDC flow to get a token.
51+
*/
3252
const headers: { [key: string]: string } = {};
3353
if (httpMethod !== 'PATCH') {
3454
headers[contentTypeHeader] = 'application/json';
3555
} else {
3656
headers[contentTypeHeader] = 'application/merge-patch+json';
3757
}
3858

39-
// Set the jq header to do a jq transformation on the proxy server.
59+
/**
60+
* Set the jq header to do a jq transformation on the proxy server.
61+
*/
4062
if (jq) headers[jqHeader] = jq;
4163

42-
// If the config object has a mcpConfig, it is assumed that the request is for the MCP API server and the necessary headers are set for the backend to get the OIDC kubeconfig without exposing it to the frontend,
43-
// otherwise, the useCrateClusterHeader is set to true to indicate that the request is for the Crate
64+
/**
65+
* If the config object has a mcpConfig, it is assumed that the request is for the MCP API server and the necessary headers are set for the backend to get the OIDC kubeconfig without exposing it to the frontend,
66+
* otherwise, the useCrateClusterHeader is set to true to indicate that the request is for the Crate.
67+
*/
4468
if (config.mcpConfig !== undefined) {
4569
headers[projectNameHeader] = config.mcpConfig.projectName;
4670
headers[workspaceNameHeader] = config.mcpConfig.workspaceName;
@@ -57,7 +81,9 @@ export const fetchApiServer = async (
5781

5882
if (!res.ok) {
5983
if (res.status === 401) {
60-
// Unauthorized (token expired), redirect to the login page
84+
/**
85+
* Unauthorized (token expired), redirect to the login page.
86+
*/
6187
sessionStorage.setItem(AUTH_FLOW_SESSION_KEY, 'onboarding');
6288
window.location.replace(`/api/auth/onboarding/login?redirectTo=${encodeURIComponent(getRedirectSuffix())}`);
6389
}
@@ -69,6 +95,18 @@ export const fetchApiServer = async (
6995
return res;
7096
};
7197

98+
/**
99+
* Calls fetchApiServer and parses the response as JSON.
100+
*
101+
* @template T
102+
* @param {string} path - The API endpoint path (appended to `/api/onboarding`).
103+
* @param {ApiConfig} config - The API configuration, including authentication and MCP/Crate context.
104+
* @param {string} [jq] - Optional jq transformation string for the proxy server.
105+
* @param {string} [httpMethod='GET'] - The HTTP method to use (GET, POST, PATCH, etc.).
106+
* @param {BodyInit} [body] - The request body, if applicable.
107+
* @returns {Promise<T>} The parsed JSON response.
108+
* @throws {APIError} Throws an APIError if the response is not ok.
109+
*/
72110
export const fetchApiServerJson = async <T>(
73111
path: string,
74112
config: ApiConfig,

0 commit comments

Comments
 (0)