UNPKG

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