1 | import { isAction } from 'redux'
|
2 | import type {
|
3 | IsUnknownOrNonInferrable,
|
4 | IfMaybeUndefined,
|
5 | IfVoid,
|
6 | IsAny,
|
7 | } from './tsHelpers'
|
8 | import { hasMatchFunction } from './tsHelpers'
|
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 | > =
|
62 | PA extends PrepareAction<infer P>
|
63 | ? ActionCreatorWithPreparedPayload<
|
64 | Parameters<PA>,
|
65 | P,
|
66 | T,
|
67 | ReturnType<PA> extends {
|
68 | error: infer E
|
69 | }
|
70 | ? E
|
71 | : never,
|
72 | ReturnType<PA> extends {
|
73 | meta: infer M
|
74 | }
|
75 | ? M
|
76 | : never
|
77 | >
|
78 | : void
|
79 |
|
80 | /**
|
81 | * Basic type for all action creators.
|
82 | *
|
83 | * @inheritdoc {redux#ActionCreator}
|
84 | */
|
85 | export interface BaseActionCreator<P, T extends string, M = never, E = never> {
|
86 | type: T
|
87 | match: (action: unknown) => action is PayloadAction<P, T, M, E>
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | export interface ActionCreatorWithPreparedPayload<
|
104 | Args extends unknown[],
|
105 | P,
|
106 | T extends string = string,
|
107 | E = never,
|
108 | M = never,
|
109 | > extends BaseActionCreator<P, T, M, E> {
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 | (...args: Args): PayloadAction<P, T, M, E>
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | export interface ActionCreatorWithOptionalPayload<P, T extends string = string>
|
126 | extends BaseActionCreator<P, T> {
|
127 | |
128 |
|
129 |
|
130 |
|
131 |
|
132 | (payload?: P): PayloadAction<P, T>
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | export interface ActionCreatorWithoutPayload<T extends string = string>
|
143 | extends BaseActionCreator<undefined, T> {
|
144 | |
145 |
|
146 |
|
147 |
|
148 | (noArgument: void): PayloadAction<undefined, T>
|
149 | }
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | export interface ActionCreatorWithPayload<P, T extends string = string>
|
159 | extends BaseActionCreator<P, T> {
|
160 | |
161 |
|
162 |
|
163 |
|
164 | (payload: P): PayloadAction<P, T>
|
165 | }
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | export interface ActionCreatorWithNonInferrablePayload<
|
175 | T extends string = string,
|
176 | > extends BaseActionCreator<unknown, T> {
|
177 | |
178 |
|
179 |
|
180 |
|
181 |
|
182 | <PT extends unknown>(payload: PT): PayloadAction<PT, T>
|
183 | }
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | export type PayloadActionCreator<
|
195 | P = void,
|
196 | T extends string = string,
|
197 | PA extends PrepareAction<P> | void = void,
|
198 | > = IfPrepareActionMethodProvided<
|
199 | PA,
|
200 | _ActionCreatorWithPreparedPayload<PA, T>,
|
201 |
|
202 | IsAny<
|
203 | P,
|
204 | ActionCreatorWithPayload<any, T>,
|
205 | IsUnknownOrNonInferrable<
|
206 | P,
|
207 | ActionCreatorWithNonInferrablePayload<T>,
|
208 |
|
209 | IfVoid<
|
210 | P,
|
211 | ActionCreatorWithoutPayload<T>,
|
212 |
|
213 | IfMaybeUndefined<
|
214 | P,
|
215 | ActionCreatorWithOptionalPayload<P, T>,
|
216 |
|
217 | ActionCreatorWithPayload<P, T>
|
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() overridden so that it returns the action type.
|
245 | *
|
246 | * @param type The action type to use for created actions.
|
247 | * @param prepare (optional) a method that takes any number of arguments and returns { payload } or { payload, meta }.
|
248 | * If this is given, the resulting action creator will pass its arguments to this method to calculate payload & meta.
|
249 | *
|
250 | * @public
|
251 | */
|
252 | export function createAction<
|
253 | PA extends PrepareAction<any>,
|
254 | T extends string = string,
|
255 | >(
|
256 | type: T,
|
257 | prepareAction: PA,
|
258 | ): PayloadActionCreator<ReturnType<PA>['payload'], T, PA>
|
259 |
|
260 | export function createAction(type: string, prepareAction?: Function): any {
|
261 | function actionCreator(...args: any[]) {
|
262 | if (prepareAction) {
|
263 | let prepared = prepareAction(...args)
|
264 | if (!prepared) {
|
265 | throw new Error('prepareAction did not return an object')
|
266 | }
|
267 |
|
268 | return {
|
269 | type,
|
270 | payload: prepared.payload,
|
271 | ...('meta' in prepared && { meta: prepared.meta }),
|
272 | ...('error' in prepared && { error: prepared.error }),
|
273 | }
|
274 | }
|
275 | return { type, payload: args[0] }
|
276 | }
|
277 |
|
278 | actionCreator.toString = () => `${type}`
|
279 |
|
280 | actionCreator.type = type
|
281 |
|
282 | actionCreator.match = (action: unknown): action is PayloadAction =>
|
283 | isAction(action) && action.type === type
|
284 |
|
285 | return actionCreator
|
286 | }
|
287 |
|
288 |
|
289 |
|
290 |
|
291 | export function isActionCreator(
|
292 | action: unknown,
|
293 | ): action is BaseActionCreator<unknown, string> & Function {
|
294 | return (
|
295 | typeof action === 'function' &&
|
296 | 'type' in action &&
|
297 |
|
298 | hasMatchFunction(action as any)
|
299 | )
|
300 | }
|
301 |
|
302 |
|
303 |
|
304 |
|
305 | export function isFSA(action: unknown): action is {
|
306 | type: string
|
307 | payload?: unknown
|
308 | error?: unknown
|
309 | meta?: unknown
|
310 | } {
|
311 | return isAction(action) && Object.keys(action).every(isValidKey)
|
312 | }
|
313 |
|
314 | function isValidKey(key: string) {
|
315 | return ['type', 'payload', 'error', 'meta'].indexOf(key) > -1
|
316 | }
|
317 |
|
318 |
|
319 |
|
320 | type IfPrepareActionMethodProvided<
|
321 | PA extends PrepareAction<any> | void,
|
322 | True,
|
323 | False,
|
324 | > = PA extends (...args: any[]) => any ? True : False
|