1 | import type { Action } from 'redux'
|
2 | import type {
|
3 | IsUnknownOrNonInferrable,
|
4 | IfMaybeUndefined,
|
5 | IfVoid,
|
6 | IsAny,
|
7 | } from './tsHelpers'
|
8 | import isPlainObject from './isPlainObject'
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export type PayloadAction<
|
22 | P = void,
|
23 | T extends string = string,
|
24 | M = never,
|
25 | E = never
|
26 | > = {
|
27 | payload: P
|
28 | type: T
|
29 | } & ([M] extends [never]
|
30 | ? {}
|
31 | : {
|
32 | meta: M
|
33 | }) &
|
34 | ([E] extends [never]
|
35 | ? {}
|
36 | : {
|
37 | error: E
|
38 | })
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | export type PrepareAction<P> =
|
48 | | ((...args: any[]) => { payload: P })
|
49 | | ((...args: any[]) => { payload: P; meta: any })
|
50 | | ((...args: any[]) => { payload: P; error: any })
|
51 | | ((...args: any[]) => { payload: P; meta: any; error: any })
|
52 |
|
53 | /**
|
54 | * Internal version of `ActionCreatorWithPreparedPayload`. Not to be used externally.
|
55 | *
|
56 | * @internal
|
57 | */
|
58 | export type _ActionCreatorWithPreparedPayload<
|
59 | PA extends PrepareAction<any> | void,
|
60 | T extends string = string
|
61 | > = PA extends PrepareAction<infer P>
|
62 | ? ActionCreatorWithPreparedPayload<
|
63 | Parameters<PA>,
|
64 | P,
|
65 | T,
|
66 | ReturnType<PA> extends {
|
67 | error: infer E
|
68 | }
|
69 | ? E
|
70 | : never,
|
71 | ReturnType<PA> extends {
|
72 | meta: infer M
|
73 | }
|
74 | ? M
|
75 | : never
|
76 | >
|
77 | : void
|
78 |
|
79 | /**
|
80 | * Basic type for all action creators.
|
81 | *
|
82 | * @inheritdoc {redux#ActionCreator}
|
83 | */
|
84 | interface BaseActionCreator<P, T extends string, M = never, E = never> {
|
85 | type: T
|
86 | match: (action: Action<unknown>) => action is PayloadAction<P, T, M, E>
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | export interface ActionCreatorWithPreparedPayload<
|
103 | Args extends unknown[],
|
104 | P,
|
105 | T extends string = string,
|
106 | E = never,
|
107 | M = never
|
108 | > extends BaseActionCreator<P, T, M, E> {
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | (...args: Args): PayloadAction<P, T, M, E>
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | export interface ActionCreatorWithOptionalPayload<P, T extends string = string>
|
125 | extends BaseActionCreator<P, T> {
|
126 | |
127 |
|
128 |
|
129 |
|
130 |
|
131 | (payload?: P): PayloadAction<P, T>
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | export interface ActionCreatorWithoutPayload<T extends string = string>
|
142 | extends BaseActionCreator<undefined, T> {
|
143 | |
144 |
|
145 |
|
146 |
|
147 | (): PayloadAction<undefined, T>
|
148 | }
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | export interface ActionCreatorWithPayload<P, T extends string = string>
|
158 | extends BaseActionCreator<P, T> {
|
159 | |
160 |
|
161 |
|
162 |
|
163 | (payload: P): PayloadAction<P, T>
|
164 | }
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | export interface ActionCreatorWithNonInferrablePayload<
|
174 | T extends string = string
|
175 | > extends BaseActionCreator<unknown, T> {
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 | <PT extends unknown>(payload: PT): PayloadAction<PT, T>
|
182 | }
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | export type PayloadActionCreator<
|
194 | P = void,
|
195 | T extends string = string,
|
196 | PA extends PrepareAction<P> | void = void
|
197 | > = IfPrepareActionMethodProvided<
|
198 | PA,
|
199 | _ActionCreatorWithPreparedPayload<PA, T>,
|
200 |
|
201 | IsAny<
|
202 | P,
|
203 | ActionCreatorWithPayload<any, T>,
|
204 | IsUnknownOrNonInferrable<
|
205 | P,
|
206 | ActionCreatorWithNonInferrablePayload<T>,
|
207 |
|
208 | IfVoid<
|
209 | P,
|
210 | ActionCreatorWithoutPayload<T>,
|
211 |
|
212 | IfMaybeUndefined<
|
213 | P,
|
214 | ActionCreatorWithOptionalPayload<P, T>,
|
215 |
|
216 | ActionCreatorWithPayload<P, T>
|
217 | >
|
218 | >
|
219 | >
|
220 | >
|
221 | >
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | export function createAction<P = void, T extends string = string>(
|
237 | type: T
|
238 | ): PayloadActionCreator<P, T>
|
239 |
|
240 | /**
|
241 | * A utility function to create an action creator for the given action type
|
242 | * string. The action creator accepts a single argument, which will be included
|
243 | * in the action object as a field called payload. The action creator function
|
244 | * will also have its toString() overriden so that it returns the action type,
|
245 | * allowing it to be used in reducer logic that is looking for that action type.
|
246 | *
|
247 | * @param type The action type to use for created actions.
|
248 | * @param prepare (optional) a method that takes any number of arguments and returns { payload } or { payload, meta }.
|
249 | * If this is given, the resulting action creator will pass its arguments to this method to calculate payload & meta.
|
250 | *
|
251 | * @public
|
252 | */
|
253 | export function createAction<
|
254 | PA extends PrepareAction<any>,
|
255 | T extends string = string
|
256 | >(
|
257 | type: T,
|
258 | prepareAction: PA
|
259 | ): PayloadActionCreator<ReturnType<PA>['payload'], T, PA>
|
260 |
|
261 | export function createAction(type: string, prepareAction?: Function): any {
|
262 | function actionCreator(...args: any[]) {
|
263 | if (prepareAction) {
|
264 | let prepared = prepareAction(...args)
|
265 | if (!prepared) {
|
266 | throw new Error('prepareAction did not return an object')
|
267 | }
|
268 |
|
269 | return {
|
270 | type,
|
271 | payload: prepared.payload,
|
272 | ...('meta' in prepared && { meta: prepared.meta }),
|
273 | ...('error' in prepared && { error: prepared.error }),
|
274 | }
|
275 | }
|
276 | return { type, payload: args[0] }
|
277 | }
|
278 |
|
279 | actionCreator.toString = () => `${type}`
|
280 |
|
281 | actionCreator.type = type
|
282 |
|
283 | actionCreator.match = (action: Action<unknown>): action is PayloadAction =>
|
284 | action.type === type
|
285 |
|
286 | return actionCreator
|
287 | }
|
288 |
|
289 | export function isFSA(action: unknown): action is {
|
290 | type: string
|
291 | payload?: unknown
|
292 | error?: unknown
|
293 | meta?: unknown
|
294 | } {
|
295 | return (
|
296 | isPlainObject(action) &&
|
297 | typeof (action as any).type === 'string' &&
|
298 | Object.keys(action).every(isValidKey)
|
299 | )
|
300 | }
|
301 |
|
302 | function isValidKey(key: string) {
|
303 | return ['type', 'payload', 'error', 'meta'].indexOf(key) > -1
|
304 | }
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | export function getType<T extends string>(
|
317 | actionCreator: PayloadActionCreator<any, T>
|
318 | ): T {
|
319 | return `${actionCreator}` as T
|
320 | }
|
321 |
|
322 |
|
323 |
|
324 | type IfPrepareActionMethodProvided<
|
325 | PA extends PrepareAction<any> | void,
|
326 | True,
|
327 | False
|
328 | > = PA extends (...args: any[]) => any ? True : False
|