From 316db1c80187ad7ce305e34e491b6ce81821f160 Mon Sep 17 00:00:00 2001 From: SKairinos Date: Fri, 10 Jan 2025 11:36:20 +0000 Subject: [PATCH] fix: logs --- src/api/index.ts | 71 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/src/api/index.ts b/src/api/index.ts index cc74a04..bb5de82 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,4 +1,73 @@ -import { createApi } from "codeforlife/api" +// TODO: use CFL package and delete code below +// import { createApi } from "codeforlife/api" +import { + createApi as _createApi, + fetchBaseQuery, +} from "@reduxjs/toolkit/query/react" +import { SERVICE_API_URL } from "codeforlife/settings" +import { buildLogoutEndpoint } from "codeforlife/api/endpoints/session" +import defaultTagTypes from "codeforlife/api/tagTypes" +import { getCsrfCookie } from "codeforlife/utils/auth" + +function createApi({ + tagTypes = [], +}: { + tagTypes?: readonly TagTypes[] +} = {}) { + const fetch = fetchBaseQuery({ + baseUrl: `${SERVICE_API_URL}/`, + credentials: "include", + prepareHeaders: (headers, { type }) => { + if (type === "mutation") { + const csrfToken = getCsrfCookie() + console.log(`csrfToken: ${csrfToken}`) + if (csrfToken) { + headers.set("x-csrftoken", csrfToken) + } else { + console.error("failed to set header") + } + } + + return headers + }, + }) + + const api = _createApi({ + // https://redux-toolkit.js.org/rtk-query/usage/customizing-queries#implementing-a-custom-basequery + baseQuery: async (args, api, extraOptions) => { + if (api.type === "mutation" && getCsrfCookie() === undefined) { + // Get the CSRF token. + const { error } = await fetch( + { url: "/csrf/cookie", method: "GET" }, + api, + {}, + ) + + // Validate we got the CSRF token. + if (error !== undefined) { + console.error(error) + // TODO + // window.location.href = `${PORTAL_BASE_URL}/error/500` + } + if (getCsrfCookie() === undefined) { + // TODO + // window.location.href = `${PORTAL_BASE_URL}/error/500` + } + } + + // Send the HTTP request and fetch the response. + return await fetch(args, api, extraOptions) + }, + tagTypes: [...defaultTagTypes, ...tagTypes], + endpoints: () => ({}), + }) + + return api.injectEndpoints({ + endpoints: build => ({ + logout: buildLogoutEndpoint(api, build), + }), + }) +} const api = createApi({ tagTypes: ["Contributor", "AgreementSignature"],