Skip to content

Commit 2d5b7db

Browse files
authored
createAsyncThunk: support for meta (reduxjs#1083)
* createAsyncThunk: support for `meta` * adjust matcher type to work around variance problems * also allow to `throw rejectedWithValue`, cleanup * add test for `mapBuilders` with the extended `createAsyncThunk` * add docs
1 parent 5d59988 commit 2d5b7db

File tree

7 files changed

+577
-95
lines changed

7 files changed

+577
-95
lines changed

docs/api/createAsyncThunk.mdx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,20 @@ The `payloadCreator` function will be called with two arguments:
8181
- `extra`: the "extra argument" given to the thunk middleware on setup, if available
8282
- `requestId`: a unique string ID value that was automatically generated to identify this request sequence
8383
- `signal`: an [`AbortController.signal` object](https://developer.mozilla.org/en-US/docs/Web/API/AbortController/signal) that may be used to see if another part of the app logic has marked this request as needing cancelation.
84-
- `rejectWithValue`: rejectWithValue is a utility function that you can `return` in your action creator to return a rejected response with a defined payload. It will pass whatever value you give it and return it in the payload of the rejected action.
84+
- `rejectWithValue(value, [meta])`: rejectWithValue is a utility function that you can `return` (or `throw`) in your action creator to return a rejected response with a defined payload and meta. It will pass whatever value you give it and return it in the payload of the rejected action. If you also pass in a `meta`, it will be merged with the existing `rejectedAction.meta`.
85+
- `fulfillWithValue(value, meta)`: fulfillWithValue is a utility function that you can `return` in your action creator to `fulfill` with a value while having the ability of adding to `fulfilledAction.meta`.
8586

8687
The logic in the `payloadCreator` function may use any of these values as needed to calculate the result.
8788

8889
### Options
8990

9091
An object with the following optional fields:
9192

92-
- `condition`: a callback that can be used to skip execution of the payload creator and all action dispatches, if desired. See [Canceling Before Execution](#canceling-before-execution) for a complete description.
93+
- `condition(arg, { getState, extra } ): boolean`: a callback that can be used to skip execution of the payload creator and all action dispatches, if desired. See [Canceling Before Execution](#canceling-before-execution) for a complete description.
9394
- `dispatchConditionRejection`: if `condition()` returns `false`, the default behavior is that no actions will be dispatched at all. If you still want a "rejected" action to be dispatched when the thunk was canceled, set this flag to `true`.
94-
- `idGenerator`: a function to use when generating the `requestId` for the request sequence. Defaults to use [nanoid](./otherExports.mdx/#nanoid).
95+
- `idGenerator(): string`: a function to use when generating the `requestId` for the request sequence. Defaults to use [nanoid](./otherExports.mdx/#nanoid).
96+
- `serializeError(error: unknown) => any` to replace the internal `miniSerializeError` method with your own serialization logic.
97+
- `getPendingMeta({ arg, requestId }, { getState, extra }): any`: a function to create an object that will be merged into the `pendingAction.meta` field.
9598

9699
## Return Value
97100

docs/usage/usage-with-typescript.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,28 @@ const lastReturnedAction = await store.dispatch(fetchUserById(3))
497497

498498
The second argument to the `payloadCreator`, known as `thunkApi`, is an object containing references to the `dispatch`, `getState`, and `extra` arguments from the thunk middleware as well as a utility function called `rejectWithValue`. If you want to use these from within the `payloadCreator`, you will need to define some generic arguments, as the types for these arguments cannot be inferred. Also, as TS cannot mix explicit and inferred generic parameters, from this point on you'll have to define the `Returned` and `ThunkArg` generic parameter as well.
499499

500-
To define the types for these arguments, pass an object as the third generic argument, with type declarations for some or all of these fields: `{dispatch?, state?, extra?, rejectValue?}`.
500+
To define the types for these arguments, pass an object as the third generic argument, with type declarations for some or all of these fields:
501+
502+
```ts
503+
type AsyncThunkConfig = {
504+
/** return type for `thunkApi.getState` */
505+
state?: unknown
506+
/** type for `thunkApi.dispatch` */
507+
dispatch?: Dispatch
508+
/** type of the `extra` argument for the thunk middleware, which will be passed in as `thunkApi.extra` */
509+
extra?: unknown
510+
/** type to be passed into `rejectWithValue`'s first argument that will end up on `rejectedAction.payload` */
511+
rejectValue?: unknown
512+
/** return type of the `serializeError` option callback */
513+
serializedErrorType?: unknown
514+
/** type to be returned from the `getPendingMeta` option callback & merged into `pendingAction.meta` */
515+
pendingMeta?: unknown
516+
/** type to be passed into the second argument of `fulfillWithValue` to finally be merged into `fulfilledAction.meta` */
517+
fulfilledMeta?: unknown
518+
/** type to be passed into the second argument of `rejectWithValue` to finally be merged into `rejectedAction.meta` */
519+
rejectedMeta?: unknown
520+
}
521+
```
501522
502523
```ts
503524
const fetchUserById = createAsyncThunk<

0 commit comments

Comments
 (0)