{"version":3,"file":"createApi-CLYWCzR2.mjs","names":["createServerOnlyFn","axios","AxiosError","AxiosInstance","https","serverEnv","scopes","definedScopes","getAccessToken","getAppToken","getSessionUser","ApiOptions","baseURL","scopeKey","httpsAgent","Agent","rejectUnauthorized","createApi","Promise","resolvedBaseURL","process","env","Error","accessToken","api","create","headers","Authorization","interceptors","response","use","undefined","error","data","message"],"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 { getAccessToken, getAppToken } from \"~/lib/auth/tokens\";\nimport { getSessionUser } from \"~/lib/auth/user\";\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 getSessionUser())\n      ? await getAccessToken(scopeKey)\n      : await getAppToken(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":";;;;;;;AAaA,MAAMc,aAAa,IAAIV,MAAMW,MAAM,EACjCC,oBAAoB,MACtB,CAAC;AAED,MAAaC,YAAYjB,mBACvB,OAAO,EAAEY,SAASC,eAAmD;CACnE,MAAMM,kBAAkBC,QAAQC,IAAIT;CACpC,IAAI,CAACO,iBAAiB,MAAM,IAAIG,MAAM,IAAIV,QAAO,2BAA4B;CAE7E,MAAMW,cAAe,MAAMb,eAAe,IACtC,MAAMF,eAAeK,QAAQ,IAC7B,MAAMJ,YAAYI,QAAQ;CAE9B,MAAMW,MAAMvB,MAAMwB,OAAO;EACvBb,SAASO;EACTL;EACAY,SAAS,EAAEC,eAAe,UAAUJ,cAAc;CACpD,CAAC;CAEDC,IAAII,aAAaC,SAASC,IAAIC,KAAAA,IAAYC,UAAmB;EAC3D,IAAIA,iBAAiB9B,YACnB,MAAM,IAAIoB,MAAMU,MAAMH,UAAUI,MAAMC,WAAWF,MAAME,OAAO;EAChE,MAAMF;CACR,CAAC;CAED,OAAOR;AACT,CACF"}