UNPKG

11.2 kBTypeScriptView Raw
1import type { Dispatch, AnyAction } from 'redux';
2import type { ActionCreatorWithPreparedPayload } from './createAction';
3import type { ThunkDispatch } from 'redux-thunk';
4import type { FallbackIfUnknown, IsAny, IsUnknown } from './tsHelpers';
5export declare type BaseThunkAPI<S, E, D extends Dispatch = Dispatch, RejectedValue = undefined, RejectedMeta = unknown, FulfilledMeta = unknown> = {
6 dispatch: D;
7 getState: () => S;
8 extra: E;
9 requestId: string;
10 signal: AbortSignal;
11 rejectWithValue: IsUnknown<RejectedMeta, (value: RejectedValue) => RejectWithValue<RejectedValue, RejectedMeta>, (value: RejectedValue, meta: RejectedMeta) => RejectWithValue<RejectedValue, RejectedMeta>>;
12 fulfillWithValue: IsUnknown<FulfilledMeta, <FulfilledValue>(value: FulfilledValue) => FulfillWithMeta<FulfilledValue, FulfilledMeta>, <FulfilledValue>(value: FulfilledValue, meta: FulfilledMeta) => FulfillWithMeta<FulfilledValue, FulfilledMeta>>;
13};
14/**
15 * @public
16 */
17export interface SerializedError {
18 name?: string;
19 message?: string;
20 stack?: string;
21 code?: string;
22}
23declare class RejectWithValue<Payload, RejectedMeta> {
24 readonly payload: Payload;
25 readonly meta: RejectedMeta;
26 private readonly _type;
27 constructor(payload: Payload, meta: RejectedMeta);
28}
29declare class FulfillWithMeta<Payload, FulfilledMeta> {
30 readonly payload: Payload;
31 readonly meta: FulfilledMeta;
32 private readonly _type;
33 constructor(payload: Payload, meta: FulfilledMeta);
34}
35/**
36 * Serializes an error into a plain object.
37 * Reworked from https://github.com/sindresorhus/serialize-error
38 *
39 * @public
40 */
41export declare const miniSerializeError: (value: any) => SerializedError;
42declare type AsyncThunkConfig = {
43 state?: unknown;
44 dispatch?: Dispatch;
45 extra?: unknown;
46 rejectValue?: unknown;
47 serializedErrorType?: unknown;
48 pendingMeta?: unknown;
49 fulfilledMeta?: unknown;
50 rejectedMeta?: unknown;
51};
52declare type GetState<ThunkApiConfig> = ThunkApiConfig extends {
53 state: infer State;
54} ? State : unknown;
55declare type GetExtra<ThunkApiConfig> = ThunkApiConfig extends {
56 extra: infer Extra;
57} ? Extra : unknown;
58declare type GetDispatch<ThunkApiConfig> = ThunkApiConfig extends {
59 dispatch: infer Dispatch;
60} ? FallbackIfUnknown<Dispatch, ThunkDispatch<GetState<ThunkApiConfig>, GetExtra<ThunkApiConfig>, AnyAction>> : ThunkDispatch<GetState<ThunkApiConfig>, GetExtra<ThunkApiConfig>, AnyAction>;
61declare type GetThunkAPI<ThunkApiConfig> = BaseThunkAPI<GetState<ThunkApiConfig>, GetExtra<ThunkApiConfig>, GetDispatch<ThunkApiConfig>, GetRejectValue<ThunkApiConfig>, GetRejectedMeta<ThunkApiConfig>, GetFulfilledMeta<ThunkApiConfig>>;
62declare type GetRejectValue<ThunkApiConfig> = ThunkApiConfig extends {
63 rejectValue: infer RejectValue;
64} ? RejectValue : unknown;
65declare type GetPendingMeta<ThunkApiConfig> = ThunkApiConfig extends {
66 pendingMeta: infer PendingMeta;
67} ? PendingMeta : unknown;
68declare type GetFulfilledMeta<ThunkApiConfig> = ThunkApiConfig extends {
69 fulfilledMeta: infer FulfilledMeta;
70} ? FulfilledMeta : unknown;
71declare type GetRejectedMeta<ThunkApiConfig> = ThunkApiConfig extends {
72 rejectedMeta: infer RejectedMeta;
73} ? RejectedMeta : unknown;
74declare type GetSerializedErrorType<ThunkApiConfig> = ThunkApiConfig extends {
75 serializedErrorType: infer GetSerializedErrorType;
76} ? GetSerializedErrorType : SerializedError;
77declare type MaybePromise<T> = T | Promise<T> | (T extends any ? Promise<T> : never);
78/**
79 * A type describing the return value of the `payloadCreator` argument to `createAsyncThunk`.
80 * Might be useful for wrapping `createAsyncThunk` in custom abstractions.
81 *
82 * @public
83 */
84export declare type AsyncThunkPayloadCreatorReturnValue<Returned, ThunkApiConfig extends AsyncThunkConfig> = MaybePromise<IsUnknown<GetFulfilledMeta<ThunkApiConfig>, Returned, FulfillWithMeta<Returned, GetFulfilledMeta<ThunkApiConfig>>> | RejectWithValue<GetRejectValue<ThunkApiConfig>, GetRejectedMeta<ThunkApiConfig>>>;
85/**
86 * A type describing the `payloadCreator` argument to `createAsyncThunk`.
87 * Might be useful for wrapping `createAsyncThunk` in custom abstractions.
88 *
89 * @public
90 */
91export declare type AsyncThunkPayloadCreator<Returned, ThunkArg = void, ThunkApiConfig extends AsyncThunkConfig = {}> = (arg: ThunkArg, thunkAPI: GetThunkAPI<ThunkApiConfig>) => AsyncThunkPayloadCreatorReturnValue<Returned, ThunkApiConfig>;
92/**
93 * A ThunkAction created by `createAsyncThunk`.
94 * Dispatching it returns a Promise for either a
95 * fulfilled or rejected action.
96 * Also, the returned value contains an `abort()` method
97 * that allows the asyncAction to be cancelled from the outside.
98 *
99 * @public
100 */
101export declare type AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig extends AsyncThunkConfig> = (dispatch: GetDispatch<ThunkApiConfig>, getState: () => GetState<ThunkApiConfig>, extra: GetExtra<ThunkApiConfig>) => Promise<ReturnType<AsyncThunkFulfilledActionCreator<Returned, ThunkArg>> | ReturnType<AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig>>> & {
102 abort: (reason?: string) => void;
103 requestId: string;
104 arg: ThunkArg;
105 unwrap: () => Promise<Returned>;
106};
107declare type AsyncThunkActionCreator<Returned, ThunkArg, ThunkApiConfig extends AsyncThunkConfig> = IsAny<ThunkArg, (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>, unknown extends ThunkArg ? (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : [ThunkArg] extends [void] | [undefined] ? () => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : [void] extends [ThunkArg] ? (arg?: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : [undefined] extends [ThunkArg] ? WithStrictNullChecks<(arg?: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>, (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>> : (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>>;
108/**
109 * Options object for `createAsyncThunk`.
110 *
111 * @public
112 */
113export declare type AsyncThunkOptions<ThunkArg = void, ThunkApiConfig extends AsyncThunkConfig = {}> = {
114 /**
115 * A method to control whether the asyncThunk should be executed. Has access to the
116 * `arg`, `api.getState()` and `api.extra` arguments.
117 *
118 * @returns `false` if it should be skipped
119 */
120 condition?(arg: ThunkArg, api: Pick<GetThunkAPI<ThunkApiConfig>, 'getState' | 'extra'>): MaybePromise<boolean | undefined>;
121 /**
122 * If `condition` returns `false`, the asyncThunk will be skipped.
123 * This option allows you to control whether a `rejected` action with `meta.condition == false`
124 * will be dispatched or not.
125 *
126 * @default `false`
127 */
128 dispatchConditionRejection?: boolean;
129 serializeError?: (x: unknown) => GetSerializedErrorType<ThunkApiConfig>;
130 /**
131 * A function to use when generating the `requestId` for the request sequence.
132 *
133 * @default `nanoid`
134 */
135 idGenerator?: (arg: ThunkArg) => string;
136} & IsUnknown<GetPendingMeta<ThunkApiConfig>, {
137 /**
138 * A method to generate additional properties to be added to `meta` of the pending action.
139 *
140 * Using this optional overload will not modify the types correctly, this overload is only in place to support JavaScript users.
141 * Please use the `ThunkApiConfig` parameter `pendingMeta` to get access to a correctly typed overload
142 */
143 getPendingMeta?(base: {
144 arg: ThunkArg;
145 requestId: string;
146 }, api: Pick<GetThunkAPI<ThunkApiConfig>, 'getState' | 'extra'>): GetPendingMeta<ThunkApiConfig>;
147}, {
148 /**
149 * A method to generate additional properties to be added to `meta` of the pending action.
150 */
151 getPendingMeta(base: {
152 arg: ThunkArg;
153 requestId: string;
154 }, api: Pick<GetThunkAPI<ThunkApiConfig>, 'getState' | 'extra'>): GetPendingMeta<ThunkApiConfig>;
155}>;
156export declare type AsyncThunkPendingActionCreator<ThunkArg, ThunkApiConfig = {}> = ActionCreatorWithPreparedPayload<[
157 string,
158 ThunkArg,
159 GetPendingMeta<ThunkApiConfig>?
160], undefined, string, never, {
161 arg: ThunkArg;
162 requestId: string;
163 requestStatus: 'pending';
164} & GetPendingMeta<ThunkApiConfig>>;
165export declare type AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig = {}> = ActionCreatorWithPreparedPayload<[
166 Error | null,
167 string,
168 ThunkArg,
169 GetRejectValue<ThunkApiConfig>?,
170 GetRejectedMeta<ThunkApiConfig>?
171], GetRejectValue<ThunkApiConfig> | undefined, string, GetSerializedErrorType<ThunkApiConfig>, {
172 arg: ThunkArg;
173 requestId: string;
174 requestStatus: 'rejected';
175 aborted: boolean;
176 condition: boolean;
177} & (({
178 rejectedWithValue: false;
179} & {
180 [K in keyof GetRejectedMeta<ThunkApiConfig>]?: undefined;
181}) | ({
182 rejectedWithValue: true;
183} & GetRejectedMeta<ThunkApiConfig>))>;
184export declare type AsyncThunkFulfilledActionCreator<Returned, ThunkArg, ThunkApiConfig = {}> = ActionCreatorWithPreparedPayload<[
185 Returned,
186 string,
187 ThunkArg,
188 GetFulfilledMeta<ThunkApiConfig>?
189], Returned, string, never, {
190 arg: ThunkArg;
191 requestId: string;
192 requestStatus: 'fulfilled';
193} & GetFulfilledMeta<ThunkApiConfig>>;
194/**
195 * A type describing the return value of `createAsyncThunk`.
196 * Might be useful for wrapping `createAsyncThunk` in custom abstractions.
197 *
198 * @public
199 */
200export declare type AsyncThunk<Returned, ThunkArg, ThunkApiConfig extends AsyncThunkConfig> = AsyncThunkActionCreator<Returned, ThunkArg, ThunkApiConfig> & {
201 pending: AsyncThunkPendingActionCreator<ThunkArg, ThunkApiConfig>;
202 rejected: AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig>;
203 fulfilled: AsyncThunkFulfilledActionCreator<Returned, ThunkArg, ThunkApiConfig>;
204 typePrefix: string;
205};
206/**
207 *
208 * @param typePrefix
209 * @param payloadCreator
210 * @param options
211 *
212 * @public
213 */
214export declare function createAsyncThunk<Returned, ThunkArg = void>(typePrefix: string, payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, {}>, options?: AsyncThunkOptions<ThunkArg, {}>): AsyncThunk<Returned, ThunkArg, {}>;
215/**
216 *
217 * @param typePrefix
218 * @param payloadCreator
219 * @param options
220 *
221 * @public
222 */
223export declare function createAsyncThunk<Returned, ThunkArg, ThunkApiConfig extends AsyncThunkConfig>(typePrefix: string, payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>, options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>): AsyncThunk<Returned, ThunkArg, ThunkApiConfig>;
224interface UnwrappableAction {
225 payload: any;
226 meta?: any;
227 error?: any;
228}
229declare type UnwrappedActionPayload<T extends UnwrappableAction> = Exclude<T, {
230 error: any;
231}>['payload'];
232/**
233 * @public
234 */
235export declare function unwrapResult<R extends UnwrappableAction>(action: R): UnwrappedActionPayload<R>;
236declare type WithStrictNullChecks<True, False> = undefined extends boolean ? False : True;
237export {};