1 | import type { Dispatch, UnknownAction } from 'redux';
|
2 | import type { ActionCreatorWithPreparedPayload } from './createAction';
|
3 | import type { ThunkDispatch } from 'redux-thunk';
|
4 | import type { FallbackIfUnknown, Id, IsAny, IsUnknown, SafePromise } from './tsHelpers';
|
5 | export 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 |
|
17 |
|
18 | export interface SerializedError {
|
19 | name?: string;
|
20 | message?: string;
|
21 | stack?: string;
|
22 | code?: string;
|
23 | }
|
24 | declare class RejectWithValue<Payload, RejectedMeta> {
|
25 | readonly payload: Payload;
|
26 | readonly meta: RejectedMeta;
|
27 | private readonly _type;
|
28 | constructor(payload: Payload, meta: RejectedMeta);
|
29 | }
|
30 | declare 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 | */
|
42 | export declare const miniSerializeError: (value: any) => SerializedError;
|
43 | export 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 | };
|
53 | type GetState<ThunkApiConfig> = ThunkApiConfig extends {
|
54 | state: infer State;
|
55 | } ? State : unknown;
|
56 | type GetExtra<ThunkApiConfig> = ThunkApiConfig extends {
|
57 | extra: infer Extra;
|
58 | } ? Extra : unknown;
|
59 | type GetDispatch<ThunkApiConfig> = ThunkApiConfig extends {
|
60 | dispatch: infer Dispatch;
|
61 | } ? FallbackIfUnknown<Dispatch, ThunkDispatch<GetState<ThunkApiConfig>, GetExtra<ThunkApiConfig>, UnknownAction>> : ThunkDispatch<GetState<ThunkApiConfig>, GetExtra<ThunkApiConfig>, UnknownAction>;
|
62 | export type GetThunkAPI<ThunkApiConfig> = BaseThunkAPI<GetState<ThunkApiConfig>, GetExtra<ThunkApiConfig>, GetDispatch<ThunkApiConfig>, GetRejectValue<ThunkApiConfig>, GetRejectedMeta<ThunkApiConfig>, GetFulfilledMeta<ThunkApiConfig>>;
|
63 | type GetRejectValue<ThunkApiConfig> = ThunkApiConfig extends {
|
64 | rejectValue: infer RejectValue;
|
65 | } ? RejectValue : unknown;
|
66 | type GetPendingMeta<ThunkApiConfig> = ThunkApiConfig extends {
|
67 | pendingMeta: infer PendingMeta;
|
68 | } ? PendingMeta : unknown;
|
69 | type GetFulfilledMeta<ThunkApiConfig> = ThunkApiConfig extends {
|
70 | fulfilledMeta: infer FulfilledMeta;
|
71 | } ? FulfilledMeta : unknown;
|
72 | type GetRejectedMeta<ThunkApiConfig> = ThunkApiConfig extends {
|
73 | rejectedMeta: infer RejectedMeta;
|
74 | } ? RejectedMeta : unknown;
|
75 | type GetSerializedErrorType<ThunkApiConfig> = ThunkApiConfig extends {
|
76 | serializedErrorType: infer GetSerializedErrorType;
|
77 | } ? GetSerializedErrorType : SerializedError;
|
78 | type MaybePromise<T> = T | Promise<T> | (T extends any ? Promise<T> : never);
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | export type AsyncThunkPayloadCreatorReturnValue<Returned, ThunkApiConfig extends AsyncThunkConfig> = MaybePromise<IsUnknown<GetFulfilledMeta<ThunkApiConfig>, Returned, FulfillWithMeta<Returned, GetFulfilledMeta<ThunkApiConfig>>> | RejectWithValue<GetRejectValue<ThunkApiConfig>, GetRejectedMeta<ThunkApiConfig>>>;
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | export type AsyncThunkPayloadCreator<Returned, ThunkArg = void, ThunkApiConfig extends AsyncThunkConfig = {}> = (arg: ThunkArg, thunkAPI: GetThunkAPI<ThunkApiConfig>) => AsyncThunkPayloadCreatorReturnValue<Returned, ThunkApiConfig>;
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | export type AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig extends AsyncThunkConfig> = (dispatch: GetDispatch<ThunkApiConfig>, getState: () => GetState<ThunkApiConfig>, extra: GetExtra<ThunkApiConfig>) => SafePromise<ReturnType<AsyncThunkFulfilledActionCreator<Returned, ThunkArg>> | ReturnType<AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig>>> & {
|
103 | abort: (reason?: string) => void;
|
104 | requestId: string;
|
105 | arg: ThunkArg;
|
106 | unwrap: () => Promise<Returned>;
|
107 | };
|
108 | 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 |
|
111 |
|
112 |
|
113 |
|
114 | export type AsyncThunkOptions<ThunkArg = void, ThunkApiConfig extends AsyncThunkConfig = {}> = {
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | condition?(arg: ThunkArg, api: Pick<GetThunkAPI<ThunkApiConfig>, 'getState' | 'extra'>): MaybePromise<boolean | undefined>;
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | dispatchConditionRejection?: boolean;
|
130 | serializeError?: (x: unknown) => GetSerializedErrorType<ThunkApiConfig>;
|
131 | |
132 |
|
133 |
|
134 |
|
135 |
|
136 | idGenerator?: (arg: ThunkArg) => string;
|
137 | } & IsUnknown<GetPendingMeta<ThunkApiConfig>, {
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | getPendingMeta?(base: {
|
145 | arg: ThunkArg;
|
146 | requestId: string;
|
147 | }, api: Pick<GetThunkAPI<ThunkApiConfig>, 'getState' | 'extra'>): GetPendingMeta<ThunkApiConfig>;
|
148 | }, {
|
149 | |
150 |
|
151 |
|
152 | getPendingMeta(base: {
|
153 | arg: ThunkArg;
|
154 | requestId: string;
|
155 | }, api: Pick<GetThunkAPI<ThunkApiConfig>, 'getState' | 'extra'>): GetPendingMeta<ThunkApiConfig>;
|
156 | }>;
|
157 | export 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>>;
|
166 | export 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>))>;
|
185 | export 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 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | export 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 | settled: (action: any) => action is ReturnType<AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig> | AsyncThunkFulfilledActionCreator<Returned, ThunkArg, ThunkApiConfig>>;
|
206 | typePrefix: string;
|
207 | };
|
208 | export type OverrideThunkApiConfigs<OldConfig, NewConfig> = Id<NewConfig & Omit<OldConfig, keyof NewConfig>>;
|
209 | type CreateAsyncThunk<CurriedThunkApiConfig extends AsyncThunkConfig> = {
|
210 | |
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | <Returned, ThunkArg = void>(typePrefix: string, payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, CurriedThunkApiConfig>, options?: AsyncThunkOptions<ThunkArg, CurriedThunkApiConfig>): AsyncThunk<Returned, ThunkArg, CurriedThunkApiConfig>;
|
219 | |
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | <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>>;
|
228 | withTypes<ThunkApiConfig extends AsyncThunkConfig>(): CreateAsyncThunk<OverrideThunkApiConfigs<CurriedThunkApiConfig, ThunkApiConfig>>;
|
229 | };
|
230 | export declare const createAsyncThunk: CreateAsyncThunk<AsyncThunkConfig>;
|
231 | interface UnwrappableAction {
|
232 | payload: any;
|
233 | meta?: any;
|
234 | error?: any;
|
235 | }
|
236 | type UnwrappedActionPayload<T extends UnwrappableAction> = Exclude<T, {
|
237 | error: any;
|
238 | }>['payload'];
|
239 |
|
240 |
|
241 |
|
242 | export declare function unwrapResult<R extends UnwrappableAction>(action: R): UnwrappedActionPayload<R>;
|
243 | type WithStrictNullChecks<True, False> = undefined extends boolean ? False : True;
|
244 | export {};
|