Skip to content

Add optional thunk configuration object #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion etc/redux-toolkit.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function createAction<P = void, T extends string = string>(type: T): Payl
export function createAction<PA extends PrepareAction<any>, T extends string = string>(type: T, prepareAction: PA): PayloadActionCreator<ReturnType<PA>['payload'], T, PA>;

// @alpha (undocumented)
export function createAsyncThunk<ActionType extends string, Returned, ActionParams = void, TA extends AsyncThunksArgs<any, any, any> = AsyncThunksArgs<unknown, unknown, Dispatch>>(type: ActionType, payloadCreator: (args: ActionParams, thunkArgs: TA) => Promise<Returned> | Returned): {
export function createAsyncThunk<ActionType extends string | ThunkActionCreatorConfig, Returned, ActionParams = void, TA extends AsyncThunksArgs<any, any, any> = AsyncThunksArgs<unknown, unknown, Dispatch>>(config: ActionType, payloadCreator: (args: ActionParams, thunkArgs: TA) => Promise<Returned> | Returned): {
(args: ActionParams): (dispatch: TA["dispatch"], getState: TA["getState"], extra: TA["extra"]) => Promise<any>;
pending: import("./createAction").ActionCreatorWithPreparedPayload<[string, ActionParams], undefined, string, never, {
args: ActionParams;
Expand Down
40 changes: 40 additions & 0 deletions src/createAsyncThunk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,46 @@ describe('createAsyncThunk', () => {
expect(thunkActionCreator.rejected.type).toBe('testType/rejected')
})

it('should accept a config object', () => {
const thunkActionCreator = createAsyncThunk(
{ type: 'testType' },
async () => 42
)

expect(thunkActionCreator.fulfilled.type).toBe('testType/fulfilled')
expect(thunkActionCreator.pending.type).toBe('testType/pending')
expect(thunkActionCreator.finished.type).toBe('testType/finished')
expect(thunkActionCreator.rejected.type).toBe('testType/rejected')
})

it('should rethrow error', async () => {
const dispatch = jest.fn()

const args = 123
let generatedRequestId = ''

const error = new Error('Panic!')

const thunkActionCreator = createAsyncThunk(
{ type: 'testType', rethrow: true },
async (args: number, { requestId }) => {
generatedRequestId = requestId
throw error
}
)

const thunkFunction = thunkActionCreator(args)

await expect(thunkFunction(dispatch, undefined, undefined)).rejects.toThrow(
error
)

expect(dispatch).toHaveBeenNthCalledWith(
2,
thunkActionCreator.rejected(error, generatedRequestId, args)
)
})

it('works without passing arguments to the payload creator', async () => {
const thunkActionCreator = createAsyncThunk('testType', async () => 42)

Expand Down
31 changes: 28 additions & 3 deletions src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,37 @@ type AsyncThunksArgs<S, E, D extends Dispatch = Dispatch> = {
requestId: string
}

interface ThunkActionCreatorConfig {
type: string
rethrow?: boolean
}

type ActionType = string | ThunkActionCreatorConfig

function isConfig(type: ActionType): type is ThunkActionCreatorConfig {
return (type as ThunkActionCreatorConfig).type !== undefined
}

function buildOptions(
config: string | ThunkActionCreatorConfig
): ThunkActionCreatorConfig {
return isConfig(config)
? config
: {
type: config,
rethrow: false
}
}

/**
*
* @param type
* @param config
* @param payloadCreator
*
* @alpha
*/
export function createAsyncThunk<
ActionType extends string,
ActionType extends string | ThunkActionCreatorConfig,
Returned,
ActionParams = void,
TA extends AsyncThunksArgs<any, any, any> = AsyncThunksArgs<
Expand All @@ -26,12 +48,14 @@ export function createAsyncThunk<
Dispatch
>
>(
type: ActionType,
config: ActionType,
payloadCreator: (
args: ActionParams,
thunkArgs: TA
) => Promise<Returned> | Returned
) {
const { type, rethrow } = buildOptions(config)

const fulfilled = createAction(
type + '/fulfilled',
(result: Returned, requestId: string, args: ActionParams) => {
Expand Down Expand Up @@ -96,6 +120,7 @@ export function createAsyncThunk<
} catch (err) {
// TODO Errors aren't serializable
dispatch(rejected(err, requestId, args))
if (rethrow) throw err
} finally {
// TODO IS there really a benefit from a "finished" action?
dispatch(finished(requestId, args))
Expand Down