-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathapi.ts
49 lines (46 loc) · 1.72 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { createApi, fetchBaseQuery, retry } from '@reduxjs/toolkit/query/react'
import { RootState } from '../store'
// Create our baseQuery instance
const baseQuery = fetchBaseQuery({
baseUrl: '/',
prepareHeaders: (headers, { getState }) => {
// By default, if we have a token in the store, let's use that for authenticated requests
const token = (getState() as RootState).auth.token
if (token) {
headers.set('authentication', `Bearer ${token}`)
}
return headers
},
})
const baseQueryWithRetry = retry(baseQuery, { maxRetries: 6 })
/**
* Create a base API to inject endpoints into elsewhere.
* Components using this API should import from the injected site,
* in order to get the appropriate types,
* and to ensure that the file injecting the endpoints is loaded
*/
export const api = createApi({
/**
* `reducerPath` is optional and will not be required by most users.
* This is useful if you have multiple API definitions,
* e.g. where each has a different domain, with no interaction between endpoints.
* Otherwise, a single API definition should be used in order to support tag invalidation,
* among other features
*/
reducerPath: 'splitApi',
/**
* A bare bones base query would just be `baseQuery: fetchBaseQuery({ baseUrl: '/' })`
*/
baseQuery: baseQueryWithRetry,
/**
* Tag types must be defined in the original API definition
* for any tags that would be provided by injected endpoints
*/
tagTypes: ['Time', 'Posts', 'Counter'],
/**
* This api has endpoints injected in adjacent files,
* which is why no endpoints are shown below.
* If you want all endpoints defined in the same file, they could be included here instead
*/
endpoints: () => ({}),
})