Skip to content

Commit 3b981c5

Browse files
committed
Refactor loader functions to enhance error handling and move to different file
1 parent 3bea2ee commit 3b981c5

File tree

2 files changed

+64
-38
lines changed

2 files changed

+64
-38
lines changed
Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1 @@
1-
import { type LoaderFunctionArgs } from "react-router";
2-
3-
import { DapperTs } from "@thunderstore/dapper-ts";
4-
import { ApiError, type GenericApiError } from "@thunderstore/thunderstore-api";
5-
6-
import { getSessionTools } from "cyberstorm/security/publicEnvVariables";
7-
8-
export function makeTeamSettingsTabLoader<T>(
9-
dataFetcher: (dapper: DapperTs, teamName: string) => Promise<T>
10-
) {
11-
return async function clientLoader({ params }: LoaderFunctionArgs) {
12-
const teamName = params.namespaceId!;
13-
14-
try {
15-
const dapper = setupDapper();
16-
const data = await dataFetcher(dapper, teamName);
17-
return { teamName, ...data };
18-
} catch (error) {
19-
if (error instanceof ApiError) {
20-
const status = error.response.status;
21-
const statusText =
22-
(error.responseJson as GenericApiError)?.detail ??
23-
error.response.statusText;
24-
throw new Response(statusText, { status, statusText });
25-
}
26-
throw error;
27-
}
28-
};
29-
}
30-
31-
const setupDapper = () => {
32-
const tools = getSessionTools();
33-
const config = tools?.getConfig();
34-
return new DapperTs(() => ({
35-
apiHost: config?.apiHost,
36-
sessionId: config?.sessionId,
37-
}));
38-
};
1+
export { makeTeamSettingsTabLoader } from "cyberstorm/utils/getLoaderTools";
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { type LoaderFunctionArgs } from "react-router";
2+
import { DapperTs } from "@thunderstore/dapper-ts";
3+
import {
4+
getPublicEnvVariables,
5+
getSessionTools,
6+
} from "cyberstorm/security/publicEnvVariables";
7+
import { ApiError, type GenericApiError } from "@thunderstore/thunderstore-api";
8+
import { throwUserFacingPayloadResponse } from "cyberstorm/utils/errors/userFacingErrorResponse";
9+
10+
export function getLoaderTools() {
11+
let dapper: DapperTs;
12+
let sessionTools: ReturnType<typeof getSessionTools> | undefined;
13+
if (import.meta.env.SSR) {
14+
const publicEnvVariables = getPublicEnvVariables(["VITE_API_URL"]);
15+
dapper = new DapperTs(() => {
16+
return {
17+
apiHost: publicEnvVariables.VITE_API_URL,
18+
sessionId: undefined,
19+
};
20+
});
21+
} else {
22+
sessionTools = getSessionTools();
23+
const sessionConfig = sessionTools?.getConfig();
24+
dapper = new DapperTs(() => {
25+
return {
26+
apiHost: sessionConfig?.apiHost,
27+
sessionId: sessionConfig?.sessionId,
28+
};
29+
});
30+
}
31+
return { dapper, sessionTools };
32+
}
33+
34+
export function makeTeamSettingsTabLoader<T>(
35+
dataFetcher: (dapper: DapperTs, teamName: string) => Promise<T>
36+
) {
37+
return async function clientLoader({ params }: LoaderFunctionArgs) {
38+
const teamName = params.namespaceId;
39+
if (!teamName) {
40+
throwUserFacingPayloadResponse({
41+
headline: "Team not found.",
42+
description: "We could not find the requested team.",
43+
category: "not_found",
44+
status: 404,
45+
});
46+
}
47+
48+
try {
49+
const { dapper } = getLoaderTools();
50+
const data = await dataFetcher(dapper, teamName);
51+
return { teamName, ...data };
52+
} catch (error) {
53+
if (error instanceof ApiError) {
54+
const status = error.response.status;
55+
const statusText =
56+
(error.responseJson as GenericApiError)?.detail ??
57+
error.response.statusText;
58+
throw new Response(statusText, { status, statusText });
59+
}
60+
throw error;
61+
}
62+
};
63+
}

0 commit comments

Comments
 (0)