{"version":3,"sources":["../src/helpers/fetcher.ts"],"sourcesContent":["import { DEFAULT_SESSION_LIFETIME } from \"../constants/defaultSessionLifetime\";\nimport { ServerOptions } from \"../types/cookieHandling\";\nimport { rememberMeCookie } from \"./cookieManagement\";\nimport { dependencyContainer } from \"../library/DependencyContainer\";\n\ntype FetcherResponse<T> =\n\t| { isError: false; frontasticRequestId: string; data: T }\n\t| { isError: true; frontasticRequestId: string; error: string | Error };\n\nconst fetcher = async <T>(\n\turl: string,\n\toptions: RequestInit,\n\tserverOptions?: ServerOptions,\n\tsessionLifetime?: number\n): Promise<FetcherResponse<T>> => {\n\tdependencyContainer().throwIfDINotConfigured();\n\tlet sessionCookie = (await dependencyContainer()\n\t\t.cookieHandler()\n\t\t.getCookie(\"frontastic-session\", serverOptions)) as string;\n\tsessionCookie = sessionCookie ?? \"\";\n\tconst incomingHeaders: { [key: string]: any } = serverOptions?.req\n\t\t? { ...serverOptions.req.headers }\n\t\t: {};\n\tdelete incomingHeaders[\"host\"];\n\tdelete incomingHeaders[\"cookie\"];\n\n\toptions.headers = {\n\t\t\"Content-Type\": \"application/json\",\n\t\tAccept: \"application/json\",\n\t\t\"X-Frontastic-Access-Token\": \"APIKEY\",\n\t\t...(options.headers || {}),\n\t\t...(sessionCookie ? { \"Frontastic-Session\": sessionCookie } : {}),\n\t\t...incomingHeaders,\n\t};\n\n\tconst response: Response = await fetch(url, options);\n\tconst frontasticRequestId =\n\t\tresponse.headers.get(\"Frontastic-Request-Id\") ?? \"\";\n\n\tif (response.ok && response.headers.has(\"Frontastic-Session\")) {\n\t\tlet rememberMe = await rememberMeCookie.get();\n\t\tlet expiryDate;\n\n\t\tif (rememberMe) {\n\t\t\texpiryDate = new Date(\n\t\t\t\tDate.now() + (sessionLifetime ?? DEFAULT_SESSION_LIFETIME)\n\t\t\t);\n\t\t}\n\t\tawait dependencyContainer()\n\t\t\t.cookieHandler()\n\t\t\t.setCookie(\n\t\t\t\t\"frontastic-session\",\n\t\t\t\tresponse.headers.get(\"Frontastic-Session\")!,\n\t\t\t\t{ expires: expiryDate, ...(serverOptions ?? {}) }\n\t\t\t);\n\t}\n\n\tlet error: Error | string;\n\n\tif (response.ok) {\n\t\ttry {\n\t\t\tlet data = await response.json();\n\t\t\treturn { frontasticRequestId, data, isError: false };\n\t\t} catch (err) {\n\t\t\terror = err as Error;\n\t\t}\n\t} else {\n\t\ttry {\n\t\t\terror = await response.clone().json();\n\t\t} catch (e) {\n\t\t\terror = await response.text();\n\t\t}\n\t}\n\n\treturn { frontasticRequestId, error, isError: true };\n};\n\nexport { fetcher, FetcherResponse };\n"],"mappings":";;;;;;;;;;;AASA,IAAM,UAAU,OACf,KACA,SACA,eACA,oBACiC;AACjC,sBAAoB,EAAE,uBAAuB;AAC7C,MAAI,gBAAiB,MAAM,oBAAoB,EAC7C,cAAc,EACd,UAAU,sBAAsB,aAAa;AAC/C,kBAAgB,iBAAiB;AACjC,QAAM,kBAA0C,eAAe,MAC5D,EAAE,GAAG,cAAc,IAAI,QAAQ,IAC/B,CAAC;AACJ,SAAO,gBAAgB,MAAM;AAC7B,SAAO,gBAAgB,QAAQ;AAE/B,UAAQ,UAAU;AAAA,IACjB,gBAAgB;AAAA,IAChB,QAAQ;AAAA,IACR,6BAA6B;AAAA,IAC7B,GAAI,QAAQ,WAAW,CAAC;AAAA,IACxB,GAAI,gBAAgB,EAAE,sBAAsB,cAAc,IAAI,CAAC;AAAA,IAC/D,GAAG;AAAA,EACJ;AAEA,QAAM,WAAqB,MAAM,MAAM,KAAK,OAAO;AACnD,QAAM,sBACL,SAAS,QAAQ,IAAI,uBAAuB,KAAK;AAElD,MAAI,SAAS,MAAM,SAAS,QAAQ,IAAI,oBAAoB,GAAG;AAC9D,QAAI,aAAa,MAAM,iBAAiB,IAAI;AAC5C,QAAI;AAEJ,QAAI,YAAY;AACf,mBAAa,IAAI;AAAA,QAChB,KAAK,IAAI,KAAK,mBAAmB;AAAA,MAClC;AAAA,IACD;AACA,UAAM,oBAAoB,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