Skip to content

Commit 9c4e157

Browse files
authored
Merge pull request #2436 from Shrugsy/examples/renovate-kitchen-sink
2 parents 666f182 + c04e2b3 commit 9c4e157

File tree

15 files changed

+123
-123
lines changed

15 files changed

+123
-123
lines changed

examples/query/react/kitchen-sink/public/mockServiceWorker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* tslint:disable */
33

44
/**
5-
* Mock Service Worker (0.39.2).
5+
* Mock Service Worker (0.41.1).
66
* @see https://github.com/mswjs/msw
77
* - Please do NOT modify this file.
88
* - Please do NOT serve this file on production.

examples/query/react/kitchen-sink/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function App() {
2727
<Routes>
2828
<Route path="/" element={<TimeList />} />
2929
<Route path="/counters" element={<CounterList />} />
30-
<Route path="/posts" element={<PostsManager />} />
30+
<Route path="/posts/*" element={<PostsManager />} />
3131
<Route path="/bundleSplitting" element={<Lazy />} />
3232
</Routes>
3333
</div>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { createApi, fetchBaseQuery, retry } from '@reduxjs/toolkit/query/react'
2+
import { RootState } from '../store'
3+
4+
// Create our baseQuery instance
5+
const baseQuery = fetchBaseQuery({
6+
baseUrl: '/',
7+
prepareHeaders: (headers, { getState }) => {
8+
// By default, if we have a token in the store, let's use that for authenticated requests
9+
const token = (getState() as RootState).auth.token
10+
if (token) {
11+
headers.set('authentication', `Bearer ${token}`)
12+
}
13+
return headers
14+
},
15+
})
16+
17+
const baseQueryWithRetry = retry(baseQuery, { maxRetries: 6 })
18+
19+
/**
20+
* Create a base API to inject endpoints into elsewhere.
21+
* Components using this API should import from the injected site,
22+
* in order to get the appropriate types,
23+
* and to ensure that the file injecting the endpoints is loaded
24+
*/
25+
export const api = createApi({
26+
/**
27+
* `reducerPath` is optional and will not be required by most users.
28+
* This is useful if you have multiple API definitions,
29+
* e.g. where each has a different domain, with no interaction between endpoints.
30+
* Otherwise, a single API definition should be used in order to support tag invalidation,
31+
* among other features
32+
*/
33+
reducerPath: 'splitApi',
34+
/**
35+
* A bare bones base query would just be `baseQuery: fetchBaseQuery({ baseUrl: '/' })`
36+
*/
37+
baseQuery: baseQueryWithRetry,
38+
/**
39+
* Tag types must be defined in the original API definition
40+
* for any tags that would be provided by injected endpoints
41+
*/
42+
tagTypes: ['Time', 'Posts', 'Counter'],
43+
/**
44+
* This api has endpoints injected in adjacent files,
45+
* which is why no endpoints are shown below.
46+
* If you want all endpoints defined in the same file, they could be included here instead
47+
*/
48+
endpoints: () => ({}),
49+
})
50+
51+
export const enhancedApi = api.enhanceEndpoints({
52+
endpoints: () => ({
53+
getPost: () => 'test',
54+
}),
55+
})

examples/query/react/kitchen-sink/src/app/services/counter.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
1+
import { api } from './api'
22

33
interface CountResponse {
44
count: number
55
}
66

7-
export const counterApi = createApi({
8-
reducerPath: 'counterApi',
9-
baseQuery: fetchBaseQuery(),
10-
tagTypes: ['Counter'],
7+
export const counterApi = api.injectEndpoints({
118
endpoints: (build) => ({
129
getCount: build.query<CountResponse, void>({
1310
query: () => 'count',

examples/query/react/kitchen-sink/src/app/services/split/post.ts renamed to examples/query/react/kitchen-sink/src/app/services/post.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { emptySplitApi, Post } from '.'
1+
import { api } from './api'
2+
import { Post } from './posts'
23

3-
export const apiWithPost = emptySplitApi.injectEndpoints({
4+
export const postApi = api.injectEndpoints({
45
endpoints: (build) => ({
56
addPost: build.mutation<Post, Partial<Post>>({
67
query(body) {

examples/query/react/kitchen-sink/src/app/services/posts.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { createApi, fetchBaseQuery, retry } from '@reduxjs/toolkit/query/react'
2-
import { RootState } from '../store'
1+
import { retry } from '@reduxjs/toolkit/query/react'
2+
import { api } from './api'
33

44
export interface Post {
55
id: number
@@ -16,25 +16,7 @@ export interface User {
1616
phone: string
1717
}
1818

19-
// Create our baseQuery instance
20-
const baseQuery = fetchBaseQuery({
21-
baseUrl: '/',
22-
prepareHeaders: (headers, { getState }) => {
23-
// By default, if we have a token in the store, let's use that for authenticated requests
24-
const token = (getState() as RootState).auth.token
25-
if (token) {
26-
headers.set('authentication', `Bearer ${token}`)
27-
}
28-
return headers
29-
},
30-
})
31-
32-
const baseQueryWithRetry = retry(baseQuery, { maxRetries: 6 })
33-
34-
export const postApi = createApi({
35-
reducerPath: 'postsApi', // We only specify this because there are many services. This would not be common in most applications
36-
baseQuery: baseQueryWithRetry,
37-
tagTypes: ['Posts'],
19+
export const postsApi = api.injectEndpoints({
3820
endpoints: (build) => ({
3921
login: build.mutation<{ token: string; user: User }, any>({
4022
query: (credentials: any) => ({
@@ -102,8 +84,8 @@ export const {
10284
useLoginMutation,
10385
useUpdatePostMutation,
10486
useGetErrorProneQuery,
105-
} = postApi
87+
} = postsApi
10688

10789
export const {
10890
endpoints: { login, getPost },
109-
} = postApi
91+
} = postsApi

examples/query/react/kitchen-sink/src/app/services/split/index.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

examples/query/react/kitchen-sink/src/app/services/split/posts.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/query/react/kitchen-sink/src/app/services/times.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
1+
import { api } from './api'
22

33
interface TimeResponse {
44
time: string
55
}
66

7-
export const timeApi = createApi({
8-
reducerPath: 'timeApi',
9-
baseQuery: fetchBaseQuery(),
10-
tagTypes: ['Time'],
7+
export const timeApi = api.injectEndpoints({
118
endpoints: (build) => ({
129
getTime: build.query<TimeResponse, string>({
1310
query: (id) => `time/${id}`,
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
11
import { configureStore, ConfigureStoreOptions } from '@reduxjs/toolkit'
22
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'
3-
import { counterApi } from './services/counter'
4-
import { postApi } from './services/posts'
5-
import { timeApi } from './services/times'
3+
import { api } from './services/api'
64
import polling from '../features/polling/pollingSlice'
7-
import { splitApi } from './services/split'
85
import auth from '../features/auth/authSlice'
96

107
export const createStore = (
118
options?: ConfigureStoreOptions['preloadedState'] | undefined
129
) =>
1310
configureStore({
1411
reducer: {
15-
[counterApi.reducerPath]: counterApi.reducer,
16-
[postApi.reducerPath]: postApi.reducer,
17-
[timeApi.reducerPath]: timeApi.reducer,
18-
[splitApi.reducerPath]: splitApi.reducer,
12+
[api.reducerPath]: api.reducer,
1913
polling,
2014
auth,
2115
},
2216
middleware: (getDefaultMiddleware) =>
23-
getDefaultMiddleware().concat(
24-
counterApi.middleware,
25-
postApi.middleware,
26-
timeApi.middleware,
27-
splitApi.middleware
28-
),
17+
getDefaultMiddleware().concat(api.middleware),
2918
...options,
3019
})
3120

3221
export const store = createStore()
3322

3423
export type AppDispatch = typeof store.dispatch
35-
export const useAppDispatch = () => useDispatch<AppDispatch>()
24+
export const useAppDispatch: () => AppDispatch = useDispatch
3625
export type RootState = ReturnType<typeof store.getState>
3726
export const useTypedSelector: TypedUseSelectorHook<RootState> = useSelector

0 commit comments

Comments
 (0)