Skip to content

Commit 13767f8

Browse files
committed
update docs and add type test for config
1 parent 301319d commit 13767f8

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

docs/api/createAsyncThunk.mdx

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -772,20 +772,31 @@ const UsersComponent = (props: { id: string }) => {
772772
773773
## `createAsyncThunkCreator`
774774
775+
Create a customised version of `createAsyncThunk` with defaulted options.
776+
777+
Options specified when calling `createAsyncThunk` will override options specified in `createAsyncThunkCreator`.
778+
775779
### Options
776780
777781
An object with the following optional fields:
778782
779-
- `serializeError(error: any, defaultSerializer: (error: any) => SerializedError) => GetSerializedErrorType<ThunkApiConfig>` to replace or extend the default internal serializer method with your own serialization logic.
783+
- `serializeError(error: unknown) => GetSerializedErrorType<ThunkApiConfig>` to replace or extend the default serializer method with your own serialization logic.
784+
- `idGenerator(arg: unknown) => string`: a function to use when generating the `requestId` for the request sequence. Defaults to use [nanoid](./otherExports.mdx/#nanoid), but you can implement your own ID generation logic.
780785
781786
### Return Value
782787
783-
`createAsyncThunkCreator` returns a Redux thunk action creator with customized options. Currently, the only option is `serializeError`.
788+
A version of `createAsyncThunk` that has options defaulted to the values provided.
784789
785790
### Example
786791
787792
```ts no-transpile
788-
import { createAsyncThunkCreator, SerializedError } from '@reduxjs/toolkit'
793+
import {
794+
createAsyncThunkCreator,
795+
miniSerializeError,
796+
SerializedError,
797+
} from '@reduxjs/toolkit'
798+
import { isAxiosError } from 'axios'
799+
import { v4 as uuidv4 } from 'uuid'
789800

790801
export interface AppSerializedError extends SerializedError {
791802
isAxiosError?: boolean
@@ -796,27 +807,13 @@ type ThunkApiConfig = {
796807
serializedErrorType: AppSerializedError
797808
}
798809

799-
const createAppAsyncThunkCreator = createAsyncThunkCreator<ThunkApiConfig>({
800-
serializeError(error, defaultSerializer) {
801-
const serializedError = defaultSerializer(error) as AppSerializedError
802-
serializedError.isAxiosError = error.isAxiosError
803-
return serializedError
810+
export const createAppAsyncThunk = createAsyncThunkCreator<ThunkApiConfig>({
811+
serializeError(error) {
812+
return {
813+
...miniSerializeError(error),
814+
isAxiosError: isAxiosError(error),
815+
}
804816
},
817+
idGenerator: () => uuidv4(),
805818
})
806-
807-
function createAppAsyncThunk<
808-
Returned,
809-
ThunkArg = void,
810-
T extends ThunkApiConfig = ThunkApiConfig,
811-
>(
812-
typePrefix: string,
813-
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, T>,
814-
options?: AsyncThunkOptions<ThunkArg, T>,
815-
): AsyncThunk<Returned, ThunkArg, T> {
816-
return createAppAsyncThunkCreator<Returned, ThunkArg, T>(
817-
typePrefix,
818-
payloadCreator,
819-
options,
820-
)
821-
}
822819
```

packages/toolkit/src/tests/createAsyncThunk.test-d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
Action,
23
AsyncThunk,
34
SerializedError,
45
ThunkDispatch,
@@ -7,6 +8,7 @@ import type {
78
import {
89
configureStore,
910
createAsyncThunk,
11+
createAsyncThunkCreator,
1012
createReducer,
1113
createSlice,
1214
unwrapResult,
@@ -888,4 +890,27 @@ describe('type tests', () => {
888890
expectTypeOf(ret.meta).not.toHaveProperty('extraProp')
889891
}
890892
})
893+
test('createAsyncThunkCreator', () => {
894+
const store = configureStore({
895+
reducer: (state: Action[] = [], action) => [...state, action],
896+
})
897+
898+
type RootState = ReturnType<typeof store.getState>
899+
type AppDispatch = typeof store.dispatch
900+
901+
const createAsyncThunk = createAsyncThunkCreator<{
902+
state: RootState
903+
dispatch: AppDispatch
904+
}>()
905+
906+
const thunk = createAsyncThunk(
907+
'test',
908+
(arg: string, { dispatch, getState }) => {
909+
expectTypeOf(dispatch).toEqualTypeOf<AppDispatch>()
910+
expectTypeOf(getState).toEqualTypeOf<() => RootState>()
911+
},
912+
)
913+
914+
store.dispatch(thunk('test'))
915+
})
891916
})

0 commit comments

Comments
 (0)