|
1 | | -import { getSessionTools } from "cyberstorm/security/publicEnvVariables"; |
2 | | -import { type LoaderFunctionArgs } from "react-router"; |
3 | | - |
4 | | -import { DapperTs } from "@thunderstore/dapper-ts"; |
5 | | -import { ApiError, type GenericApiError } from "@thunderstore/thunderstore-api"; |
6 | | - |
7 | | -/** |
8 | | - * TODO |
9 | | - * 1) This approach no longer handles different ApiErrors properly |
10 | | - * when the data isn't awaited in the clientLoader but returned as |
11 | | - * promises for the Suspense/Await elements to handle. Instead, any |
12 | | - * HTTP error codes are shown as 500 errors. This isn't fixed yet |
13 | | - * as it will be easier to do once upcoming project wide error |
14 | | - * handling changes are merged. |
15 | | - * 2) The purpose of this helper was to reduce boilerplate in different |
16 | | - * tab components of the team settings page. Half of that boilerplate |
17 | | - * is Dapper setup, the other is handling ApiErrors. As the latter is |
18 | | - * supposed to be handled elsewhere after the changes mentioned above, |
19 | | - * this helper might no longer have a valid reason to exist after the |
20 | | - * changes. |
21 | | - */ |
22 | | -export function makeTeamSettingsTabLoader<T>( |
23 | | - dataFetcher: (dapper: DapperTs, teamName: string) => Promise<T> |
24 | | -) { |
25 | | - return async function clientLoader({ params }: LoaderFunctionArgs) { |
26 | | - const teamName = params.namespaceId!; |
27 | | - |
28 | | - try { |
29 | | - const dapper = setupDapper(); |
30 | | - const data = await dataFetcher(dapper, teamName); |
31 | | - return { teamName, ...data }; |
32 | | - } catch (error) { |
33 | | - if (error instanceof ApiError) { |
34 | | - const status = error.response.status; |
35 | | - const statusText = |
36 | | - (error.responseJson as GenericApiError)?.detail ?? |
37 | | - error.response.statusText; |
38 | | - throw new Response(statusText, { status, statusText }); |
39 | | - } |
40 | | - throw error; |
41 | | - } |
42 | | - }; |
43 | | -} |
44 | | - |
45 | | -const setupDapper = () => { |
46 | | - const tools = getSessionTools(); |
47 | | - const config = tools?.getConfig(); |
48 | | - return new DapperTs(() => ({ |
49 | | - apiHost: config?.apiHost, |
50 | | - sessionId: config?.sessionId, |
51 | | - })); |
52 | | -}; |
| 1 | +export { makeTeamSettingsTabLoader } from "cyberstorm/utils/getLoaderTools"; |
0 commit comments