|
1 |
| -import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/dist/query/react' |
| 1 | +import { |
| 2 | + BaseQueryApi, |
| 3 | + createApi, |
| 4 | + fetchBaseQuery, |
| 5 | +} from '@reduxjs/toolkit/dist/query/react' |
2 | 6 |
|
3 |
| -import { FetchBaseQueryArgs } from '@reduxjs/toolkit/dist/query/fetchBaseQuery' |
| 7 | +import { |
| 8 | + FetchArgs, |
| 9 | + FetchBaseQueryArgs, |
| 10 | +} from '@reduxjs/toolkit/dist/query/fetchBaseQuery' |
4 | 11 | import { CreateApiOptions } from '@reduxjs/toolkit/dist/query/createApi'
|
5 | 12 | import { StoreStateType } from './store'
|
6 | 13 |
|
7 | 14 | const Project = require('./project')
|
8 | 15 | const _data = require('./data/base/_data.js')
|
9 | 16 |
|
| 17 | +const excludedEndpoints = [ |
| 18 | + 'register', |
| 19 | + 'createConfirmEmail', |
| 20 | + 'createResetPassword', |
| 21 | + 'createResendConfirmationEmail', |
| 22 | + 'createForgotPassword', |
| 23 | +] |
| 24 | + |
| 25 | +const refreshAccessToken = async () => { |
| 26 | + try { |
| 27 | + const response = await fetch(`${Project.api}auth/token/refresh/`, { |
| 28 | + credentials: 'include', |
| 29 | + method: 'POST', |
| 30 | + }) |
| 31 | + |
| 32 | + if (!response.ok) { |
| 33 | + throw new Error('Failed to refresh token') |
| 34 | + } |
| 35 | + |
| 36 | + return |
| 37 | + } catch (error) { |
| 38 | + console.error('Token refresh failed', error) |
| 39 | + throw error |
| 40 | + } |
| 41 | +} |
| 42 | + |
10 | 43 | export const baseApiOptions = (queryArgs?: Partial<FetchBaseQueryArgs>) => {
|
| 44 | + const baseQuery = fetchBaseQuery({ |
| 45 | + baseUrl: Project.api, |
| 46 | + credentials: Project.cookieAuthEnabled ? 'include' : undefined, |
| 47 | + prepareHeaders: async (headers, { endpoint, getState }) => { |
| 48 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 49 | + const state = getState() as StoreStateType |
| 50 | + if (!excludedEndpoints.includes(endpoint)) { |
| 51 | + try { |
| 52 | + const token = _data.token |
| 53 | + if (token && !Project.cookieAuthEnabled) { |
| 54 | + headers.set('Authorization', `Token ${token}`) |
| 55 | + } |
| 56 | + } catch (e) {} |
| 57 | + } |
| 58 | + |
| 59 | + return headers |
| 60 | + }, |
| 61 | + ...queryArgs, |
| 62 | + }) |
| 63 | + |
| 64 | + const baseQueryWithInterceptor = async ( |
| 65 | + args: string | FetchArgs, |
| 66 | + api: BaseQueryApi, |
| 67 | + extraOptions: {}, |
| 68 | + ) => { |
| 69 | + const result = await baseQuery(args, api, extraOptions) |
| 70 | + if ( |
| 71 | + Project.cookieAuthEnabled && |
| 72 | + result.error && |
| 73 | + result.error.status === 401 |
| 74 | + ) { |
| 75 | + await refreshAccessToken() |
| 76 | + return baseQuery(args, api, extraOptions) |
| 77 | + } |
| 78 | + return result |
| 79 | + } |
| 80 | + |
11 | 81 | const res: Pick<
|
12 | 82 | CreateApiOptions<any, any, any, any>,
|
13 | 83 | | 'baseQuery'
|
14 | 84 | | 'refetchOnReconnect'
|
15 | 85 | | 'refetchOnFocus'
|
16 | 86 | | 'extractRehydrationInfo'
|
17 | 87 | > = {
|
18 |
| - baseQuery: fetchBaseQuery({ |
19 |
| - baseUrl: Project.api, |
20 |
| - credentials: Project.cookieAuthEnabled ? 'include' : undefined, |
21 |
| - prepareHeaders: async (headers, { endpoint, getState }) => { |
22 |
| - // eslint-disable-next-line @typescript-eslint/no-unused-vars |
23 |
| - const state = getState() as StoreStateType |
24 |
| - if ( |
25 |
| - endpoint !== 'register' && |
26 |
| - endpoint !== 'createConfirmEmail' && |
27 |
| - endpoint !== 'createResetPassword' && |
28 |
| - endpoint !== 'createResendConfirmationEmail' && |
29 |
| - endpoint !== 'createForgotPassword' |
30 |
| - ) { |
31 |
| - try { |
32 |
| - const token = _data.token |
33 |
| - if (token && !Project.cookieAuthEnabled) { |
34 |
| - headers.set('Authorization', `Token ${token}`) |
35 |
| - } |
36 |
| - } catch (e) {} |
37 |
| - } |
38 |
| - |
39 |
| - return headers |
40 |
| - }, |
41 |
| - ...queryArgs, |
42 |
| - }), |
43 |
| - |
44 |
| - refetchOnFocus: true, |
45 |
| - refetchOnReconnect: true, |
| 88 | + baseQuery: baseQueryWithInterceptor, |
46 | 89 | }
|
| 90 | + |
47 | 91 | return res
|
48 | 92 | }
|
49 | 93 |
|
|
0 commit comments