Skip to content

Commit

Permalink
fix: logs
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jan 10, 2025
1 parent 51699f9 commit 316db1c
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
@@ -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 extends string = never>({
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)

Check failure on line 59 in src/api/index.ts

View workflow job for this annotation

GitHub Actions / main / test / test-js-code

Unsafe argument of type `any` assigned to a parameter of type `string | FetchArgs`
},
tagTypes: [...defaultTagTypes, ...tagTypes],
endpoints: () => ({}),
})

return api.injectEndpoints({
endpoints: build => ({
logout: buildLogoutEndpoint<null, null>(api, build),
}),
})
}

const api = createApi({
tagTypes: ["Contributor", "AgreementSignature"],
Expand Down

0 comments on commit 316db1c

Please sign in to comment.