Debian Codegraph @

install/freeipa-webui/node_modules/@reduxjs/toolkit/src/configureStore.ts

raw
1 import type {
2 Reducer,
3 ReducersMapObject,
4 Middleware,
5 Action,
6 StoreEnhancer,
7 Store,
8 UnknownAction,
9 } from 'redux'
10 import {
11 applyMiddleware,
12 createStore,
13 compose,
14 combineReducers,
15 isPlainObject,
16 } from 'redux'
17 import type { DevToolsEnhancerOptions as DevToolsOptions } from './devtoolsExtension'
18 import { composeWithDevTools } from './devtoolsExtension'
19
20 import type {
21 ThunkMiddlewareFor,
22 GetDefaultMiddleware,
23 } from './getDefaultMiddleware'
24 import { buildGetDefaultMiddleware } from './getDefaultMiddleware'
25 import type {
26 ExtractDispatchExtensions,
27 ExtractStoreExtensions,
28 ExtractStateExtensions,
29 UnknownIfNonSpecific,
30 } from './tsHelpers'
31 import type { Tuple } from './utils'
32 import type { GetDefaultEnhancers } from './getDefaultEnhancers'
33 import { buildGetDefaultEnhancers } from './getDefaultEnhancers'
34
35 /**
36 * Options for `configureStore()`.
37 *
38 * @public
39 */
40 export interface ConfigureStoreOptions<
41 S = any,
42 A extends Action = UnknownAction,
43 M extends Tuple<Middlewares<S>> = Tuple<Middlewares<S>>,
44 E extends Tuple<Enhancers> = Tuple<Enhancers>,
45 P = S,
46 > {
47 /**
48 * A single reducer function that will be used as the root reducer, or an
49 * object of slice reducers that will be passed to `combineReducers()`.
50 */
51 reducer: Reducer<S, A, P> | ReducersMapObject<S, A, P>
52
53 /**
54 * An array of Redux middleware to install, or a callback receiving `getDefaultMiddleware` and returning a Tuple of middleware.
55 * If not supplied, defaults to the set of middleware returned by `getDefaultMiddleware()`.
56 *
57 * @example `middleware: (gDM) => gDM().concat(logger, apiMiddleware, yourCustomMiddleware)`
58 * @see https://redux-toolkit.js.org/api/getDefaultMiddleware#intended-usage
59 */
60 middleware?: (getDefaultMiddleware: GetDefaultMiddleware<S>) => M
61
62 /**
63 * Whether to enable Redux DevTools integration. Defaults to `true`.
64 *
65 * Additional configuration can be done by passing Redux DevTools options
66 */
67 devTools?: boolean | DevToolsOptions
68
69 /**
70 * The initial state, same as Redux's createStore.
71 * You may optionally specify it to hydrate the state
72 * from the server in universal apps, or to restore a previously serialized
73 * user session. If you use `combineReducers()` to produce the root reducer
74 * function (either directly or indirectly by passing an object as `reducer`),
75 * this must be an object with the same shape as the reducer map keys.
76 */
77 // we infer here, and instead complain if the reducer doesn't match
78 preloadedState?: P
79
80 /**
81 * The store enhancers to apply. See Redux's `createStore()`.
82 * All enhancers will be included before the DevTools Extension enhancer.
83 * If you need to customize the order of enhancers, supply a callback
84 * function that will receive a `getDefaultEnhancers` function that returns a Tuple,
85 * and should return a Tuple of enhancers (such as `getDefaultEnhancers().concat(offline)`).
86 * If you only need to add middleware, you can use the `middleware` parameter instead.
87 */
88 enhancers?: (getDefaultEnhancers: GetDefaultEnhancers<M>) => E
89 }
90
91 export type Middlewares<S> = ReadonlyArray<Middleware<{}, S>>
92
93 type Enhancers = ReadonlyArray<StoreEnhancer>
94
95 /**
96 * A Redux store returned by `configureStore()`. Supports dispatching
97 * side-effectful _thunks_ in addition to plain actions.
98 *
99 * @public
100 */
101 export type EnhancedStore<
102 S = any,
103 A extends Action = UnknownAction,
104 E extends Enhancers = Enhancers,
105 > = ExtractStoreExtensions<E> &
106 Store<S, A, UnknownIfNonSpecific<ExtractStateExtensions<E>>>
107
108 /**
109 * A friendly abstraction over the standard Redux `createStore()` function.
110 *
111 * @param options The store configuration.
112 * @returns A configured Redux store.
113 *
114 * @public
115 */
116 export function configureStore<
117 S = any,
118 A extends Action = UnknownAction,
119 M extends Tuple<Middlewares<S>> = Tuple<[ThunkMiddlewareFor<S>]>,
120 E extends Tuple<Enhancers> = Tuple<
121 [StoreEnhancer<{ dispatch: ExtractDispatchExtensions<M> }>, StoreEnhancer]
122 >,
123 P = S,
124 >(options: ConfigureStoreOptions<S, A, M, E, P>): EnhancedStore<S, A, E> {
125 const getDefaultMiddleware = buildGetDefaultMiddleware<S>()
126
127 const {
128 reducer = undefined,
129 middleware,
130 devTools = true,
131 preloadedState = undefined,
132 enhancers = undefined,
133 } = options || {}
134
135 let rootReducer: Reducer<S, A, P>
136
137 if (typeof reducer === 'function') {
138 rootReducer = reducer
139 } else if (isPlainObject(reducer)) {
140 rootReducer = combineReducers(reducer) as unknown as Reducer<S, A, P>
141 } else {
142 throw new Error(
143 '`reducer` is a required argument, and must be a function or an object of functions that can be passed to combineReducers',
144 )
145 }
146
147 if (
148 process.env.NODE_ENV !== 'production' &&
149 middleware &&
150 typeof middleware !== 'function'
151 ) {
152 throw new Error('`middleware` field must be a callback')
153 }
154
155 let finalMiddleware: Tuple<Middlewares<S>>
156 if (typeof middleware === 'function') {
157 finalMiddleware = middleware(getDefaultMiddleware)
158
159 if (
160 process.env.NODE_ENV !== 'production' &&
161 !Array.isArray(finalMiddleware)
162 ) {
163 throw new Error(
164 'when using a middleware builder function, an array of middleware must be returned',
165 )
166 }
167 } else {
168 finalMiddleware = getDefaultMiddleware()
169 }
170 if (
171 process.env.NODE_ENV !== 'production' &&
172 finalMiddleware.some((item: any) => typeof item !== 'function')
173 ) {
174 throw new Error(
175 'each middleware provided to configureStore must be a function',
176 )
177 }
178
179 let finalCompose = compose
180
181 if (devTools) {
182 finalCompose = composeWithDevTools({
183 // Enable capture of stack traces for dispatched Redux actions
184 trace: process.env.NODE_ENV !== 'production',
185 ...(typeof devTools === 'object' && devTools),
186 })
187 }
188
189 const middlewareEnhancer = applyMiddleware(...finalMiddleware)
190
191 const getDefaultEnhancers = buildGetDefaultEnhancers<M>(middlewareEnhancer)
192
193 if (
194 process.env.NODE_ENV !== 'production' &&
195 enhancers &&
196 typeof enhancers !== 'function'
197 ) {
198 throw new Error('`enhancers` field must be a callback')
199 }
200
201 let storeEnhancers =
202 typeof enhancers === 'function'
203 ? enhancers(getDefaultEnhancers)
204 : getDefaultEnhancers()
205
206 if (process.env.NODE_ENV !== 'production' && !Array.isArray(storeEnhancers)) {
207 throw new Error('`enhancers` callback must return an array')
208 }
209 if (
210 process.env.NODE_ENV !== 'production' &&
211 storeEnhancers.some((item: any) => typeof item !== 'function')
212 ) {
213 throw new Error(
214 'each enhancer provided to configureStore must be a function',
215 )
216 }
217 if (
218 process.env.NODE_ENV !== 'production' &&
219 finalMiddleware.length &&
220 !storeEnhancers.includes(middlewareEnhancer)
221 ) {
222 console.error(
223 'middlewares were provided, but middleware enhancer was not included in final enhancers - make sure to call `getDefaultEnhancers`',
224 )
225 }
226
227 const composedEnhancer: StoreEnhancer<any> = finalCompose(...storeEnhancers)
228
229 return createStore(rootReducer, preloadedState as P, composedEnhancer)
230 }