wcz-layout
Version:
1 lines • 1.85 kB
Source Map (JSON)
{"version":3,"file":"createApi-CF4ZL82B.mjs","names":[],"sources":["../src/lib/createApi.ts"],"sourcesContent":["import { createServerOnlyFn } from \"@tanstack/react-start\";\nimport axios, { AxiosError, type AxiosInstance } from \"axios\";\nimport https from \"node:https\";\nimport type { serverEnv } from \"virtual:wcz-layout\";\nimport { scopes as definedScopes } from \"virtual:wcz-layout\";\nimport { getApiToken } from \"~/lib/auth/tokens\";\n\nexport interface ApiOptions {\n baseURL: keyof typeof serverEnv & `${string}BASE_URL${string}`;\n scopeKey: keyof typeof definedScopes;\n}\n\nconst httpsAgent = new https.Agent({\n rejectUnauthorized: false,\n});\n\nexport const createApi = createServerOnlyFn(\n async ({ baseURL, scopeKey }: ApiOptions): Promise<AxiosInstance> => {\n const resolvedBaseURL = process.env[baseURL];\n if (!resolvedBaseURL) throw new Error(`\"${baseURL}\" env variable is not set.`);\n\n const accessToken = await getApiToken(scopeKey);\n\n const api = axios.create({\n baseURL: resolvedBaseURL,\n httpsAgent,\n headers: { Authorization: `Bearer ${accessToken}` },\n });\n\n api.interceptors.response.use(undefined, (error: unknown) => {\n if (error instanceof AxiosError)\n throw new Error(error.response?.data?.message ?? error.message);\n throw error;\n });\n\n return api;\n },\n);\n"],"mappings":";;;;;AAYA,MAAM,aAAa,IAAI,MAAM,MAAM,EACjC,oBAAoB,MACtB,CAAC;AAED,MAAa,YAAY,mBACvB,OAAO,EAAE,SAAS,eAAmD;CACnE,MAAM,kBAAkB,QAAQ,IAAI;CACpC,IAAI,CAAC,iBAAiB,MAAM,IAAI,MAAM,IAAI,QAAQ,2BAA2B;CAE7E,MAAM,cAAc,MAAM,YAAY,QAAQ;CAE9C,MAAM,MAAM,MAAM,OAAO;EACvB,SAAS;EACT;EACA,SAAS,EAAE,eAAe,UAAU,cAAc;CACpD,CAAC;CAED,IAAI,aAAa,SAAS,IAAI,KAAA,IAAY,UAAmB;EAC3D,IAAI,iBAAiB,YACnB,MAAM,IAAI,MAAM,MAAM,UAAU,MAAM,WAAW,MAAM,OAAO;EAChE,MAAM;CACR,CAAC;CAED,OAAO;AACT,CACF"}