@hyper-fetch/core 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -13
- package/dist/adapter/adapter.bindings.d.ts +25 -0
- package/dist/adapter/adapter.browser.d.ts +2 -0
- package/dist/{client/fetch.client.constants.d.ts → adapter/adapter.constants.d.ts} +0 -0
- package/dist/adapter/adapter.d.ts +2 -0
- package/dist/adapter/adapter.server.d.ts +2 -0
- package/dist/adapter/adapter.types.d.ts +57 -0
- package/dist/{client/fetch.client.utils.d.ts → adapter/adapter.utils.d.ts} +3 -3
- package/dist/adapter/index.d.ts +7 -0
- package/dist/cache/cache.d.ts +9 -9
- package/dist/cache/cache.types.d.ts +4 -4
- package/dist/cache/cache.utils.d.ts +3 -3
- package/dist/{builder/builder.constants.d.ts → client/client.constants.d.ts} +0 -0
- package/dist/client/client.d.ts +142 -0
- package/dist/client/client.types.d.ts +41 -0
- package/dist/client/client.utils.d.ts +10 -0
- package/dist/client/index.d.ts +4 -6
- package/dist/dispatcher/dispatcher.d.ts +23 -23
- package/dist/dispatcher/dispatcher.events.d.ts +8 -8
- package/dist/dispatcher/dispatcher.types.d.ts +10 -10
- package/dist/dispatcher/dispatcher.utils.d.ts +5 -5
- package/dist/effect/index.d.ts +2 -2
- package/dist/effect/request.effect.d.ts +14 -0
- package/dist/effect/request.effect.types.d.ts +32 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.d.ts +3 -3
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +4 -4
- package/dist/managers/index.d.ts +1 -1
- package/dist/managers/logger/logger.manager.d.ts +5 -5
- package/dist/managers/logger/logger.manager.types.d.ts +7 -7
- package/dist/managers/request/index.d.ts +4 -0
- package/dist/managers/request/request.manager.d.ts +43 -0
- package/dist/managers/request/request.manager.events.d.ts +36 -0
- package/dist/managers/{command/command.manager.types.d.ts → request/request.manager.types.d.ts} +6 -6
- package/dist/managers/{command/command.manager.utils.d.ts → request/request.manager.utils.d.ts} +0 -0
- package/dist/request/index.d.ts +3 -0
- package/dist/request/request.d.ts +98 -0
- package/dist/request/request.types.d.ts +202 -0
- package/dist/request/request.utils.d.ts +22 -0
- package/dist/types/fetch.types.d.ts +16 -16
- package/package.json +1 -1
- package/src/{client/fetch.client.bindings.ts → adapter/adapter.bindings.ts} +58 -58
- package/src/{client/fetch.client.browser.ts → adapter/adapter.browser.ts} +6 -6
- package/src/{client/fetch.client.constants.ts → adapter/adapter.constants.ts} +0 -0
- package/src/{client/fetch.client.server.ts → adapter/adapter.server.ts} +12 -12
- package/src/adapter/adapter.ts +8 -0
- package/src/adapter/adapter.types.ts +79 -0
- package/src/{client/fetch.client.utils.ts → adapter/adapter.utils.ts} +3 -3
- package/src/adapter/index.ts +7 -0
- package/src/cache/cache.ts +19 -19
- package/src/cache/cache.types.ts +4 -4
- package/src/cache/cache.utils.ts +6 -6
- package/src/{builder/builder.constants.ts → client/client.constants.ts} +0 -0
- package/src/client/client.ts +310 -0
- package/src/client/client.types.ts +52 -0
- package/src/{builder/builder.utils.ts → client/client.utils.ts} +34 -31
- package/src/client/index.ts +4 -6
- package/src/dispatcher/dispatcher.events.ts +11 -11
- package/src/dispatcher/dispatcher.ts +68 -68
- package/src/dispatcher/dispatcher.types.ts +10 -10
- package/src/dispatcher/dispatcher.utils.ts +7 -7
- package/src/effect/index.ts +2 -2
- package/src/effect/request.effect.ts +28 -0
- package/src/effect/request.effect.types.ts +35 -0
- package/src/index.ts +4 -4
- package/src/managers/index.ts +1 -1
- package/src/managers/logger/logger.manager.ts +8 -8
- package/src/managers/logger/logger.manager.types.ts +7 -7
- package/src/managers/request/index.ts +4 -0
- package/src/managers/{command/command.manager.events.ts → request/request.manager.events.ts} +47 -51
- package/src/managers/{command/command.manager.ts → request/request.manager.ts} +5 -5
- package/src/managers/request/request.manager.types.ts +25 -0
- package/src/managers/{command/command.manager.utils.ts → request/request.manager.utils.ts} +0 -0
- package/src/request/index.ts +3 -0
- package/src/{command/command.ts → request/request.ts} +188 -194
- package/src/request/request.types.ts +282 -0
- package/src/{command/command.utils.ts → request/request.utils.ts} +56 -53
- package/src/types/fetch.types.ts +18 -18
- package/dist/builder/builder.d.ts +0 -141
- package/dist/builder/builder.types.d.ts +0 -41
- package/dist/builder/builder.utils.d.ts +0 -10
- package/dist/builder/index.d.ts +0 -4
- package/dist/client/fetch.client.bindings.d.ts +0 -25
- package/dist/client/fetch.client.browser.d.ts +0 -2
- package/dist/client/fetch.client.d.ts +0 -2
- package/dist/client/fetch.client.server.d.ts +0 -2
- package/dist/client/fetch.client.types.d.ts +0 -57
- package/dist/command/command.d.ts +0 -98
- package/dist/command/command.types.d.ts +0 -202
- package/dist/command/command.utils.d.ts +0 -22
- package/dist/command/index.d.ts +0 -3
- package/dist/effect/fetch.effect.d.ts +0 -14
- package/dist/effect/fetch.effect.types.d.ts +0 -32
- package/dist/managers/command/command.manager.d.ts +0 -43
- package/dist/managers/command/command.manager.events.d.ts +0 -36
- package/dist/managers/command/index.d.ts +0 -4
- package/src/builder/builder.ts +0 -308
- package/src/builder/builder.types.ts +0 -53
- package/src/builder/index.ts +0 -4
- package/src/client/fetch.client.ts +0 -9
- package/src/client/fetch.client.types.ts +0 -83
- package/src/command/command.types.ts +0 -273
- package/src/command/index.ts +0 -3
- package/src/effect/fetch.effect.ts +0 -28
- package/src/effect/fetch.effect.types.ts +0 -35
- package/src/managers/command/command.manager.types.ts +0 -25
- package/src/managers/command/index.ts +0 -4
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NullableKeys,
|
|
3
|
+
NegativeTypes,
|
|
4
|
+
ExtractParamsType,
|
|
5
|
+
ExtractPayloadType,
|
|
6
|
+
HttpMethodsType,
|
|
7
|
+
ExtractQueryParamsType,
|
|
8
|
+
ExtractAdapterOptionsType,
|
|
9
|
+
ExtractEndpointType,
|
|
10
|
+
ExtractHasDataType,
|
|
11
|
+
ExtractHasParamsType,
|
|
12
|
+
ExtractHasQueryParamsType,
|
|
13
|
+
ExtractErrorType,
|
|
14
|
+
ExtractResponseType,
|
|
15
|
+
} from "types";
|
|
16
|
+
import { Request } from "request";
|
|
17
|
+
import { ResponseType, QueryParamsType, ProgressType } from "adapter";
|
|
18
|
+
import { RequestEventType, ResponseDetailsType } from "managers";
|
|
19
|
+
|
|
20
|
+
// Progress
|
|
21
|
+
export type AdapterProgressEventType = { total: number; loaded: number };
|
|
22
|
+
export type AdapterProgressType = {
|
|
23
|
+
progress: number;
|
|
24
|
+
timeLeft: number;
|
|
25
|
+
sizeLeft: number;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// Dump
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Dump of the request used to later recreate it
|
|
32
|
+
*/
|
|
33
|
+
export type RequestDump<
|
|
34
|
+
Request extends RequestInstance,
|
|
35
|
+
// Bellow generics provided only to overcome the typescript bugs
|
|
36
|
+
AdapterOptions = unknown,
|
|
37
|
+
QueryParams = QueryParamsType,
|
|
38
|
+
Params = ExtractParamsType<Request>,
|
|
39
|
+
> = {
|
|
40
|
+
requestOptions: RequestOptionsType<string, AdapterOptions | ExtractAdapterOptionsType<Request>>;
|
|
41
|
+
endpoint: string;
|
|
42
|
+
method: HttpMethodsType;
|
|
43
|
+
headers?: HeadersInit;
|
|
44
|
+
auth: boolean;
|
|
45
|
+
cancelable: boolean;
|
|
46
|
+
retry: number;
|
|
47
|
+
retryTime: number;
|
|
48
|
+
garbageCollection: number;
|
|
49
|
+
cache: boolean;
|
|
50
|
+
cacheTime: number;
|
|
51
|
+
queued: boolean;
|
|
52
|
+
offline: boolean;
|
|
53
|
+
disableResponseInterceptors: boolean | undefined;
|
|
54
|
+
disableRequestInterceptors: boolean | undefined;
|
|
55
|
+
options?: AdapterOptions | ExtractAdapterOptionsType<Request>;
|
|
56
|
+
data: PayloadType<ExtractPayloadType<Request>, unknown>;
|
|
57
|
+
params: Params | NegativeTypes;
|
|
58
|
+
queryParams: QueryParams | NegativeTypes;
|
|
59
|
+
abortKey: string;
|
|
60
|
+
cacheKey: string;
|
|
61
|
+
queueKey: string;
|
|
62
|
+
effectKey: string;
|
|
63
|
+
used: boolean;
|
|
64
|
+
updatedAbortKey: boolean;
|
|
65
|
+
updatedCacheKey: boolean;
|
|
66
|
+
updatedQueueKey: boolean;
|
|
67
|
+
updatedEffectKey: boolean;
|
|
68
|
+
deduplicate: boolean;
|
|
69
|
+
deduplicateTime: number;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// Request
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Configuration options for request creation
|
|
76
|
+
*/
|
|
77
|
+
export type RequestOptionsType<GenericEndpoint extends string, AdapterOptions> = {
|
|
78
|
+
/**
|
|
79
|
+
* Determine the endpoint for request request
|
|
80
|
+
*/
|
|
81
|
+
endpoint: GenericEndpoint;
|
|
82
|
+
/**
|
|
83
|
+
* Custom headers for request
|
|
84
|
+
*/
|
|
85
|
+
headers?: HeadersInit;
|
|
86
|
+
/**
|
|
87
|
+
* Should the onAuth method get called on this request
|
|
88
|
+
*/
|
|
89
|
+
auth?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Request method GET | POST | PATCH | PUT | DELETE
|
|
92
|
+
*/
|
|
93
|
+
method?: HttpMethodsType;
|
|
94
|
+
/**
|
|
95
|
+
* Should enable cancelable mode in the Dispatcher
|
|
96
|
+
*/
|
|
97
|
+
cancelable?: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Retry count when request is failed
|
|
100
|
+
*/
|
|
101
|
+
retry?: number;
|
|
102
|
+
/**
|
|
103
|
+
* Retry time delay between retries
|
|
104
|
+
*/
|
|
105
|
+
retryTime?: number;
|
|
106
|
+
/**
|
|
107
|
+
* Should we trigger garbage collection or leave data in memory
|
|
108
|
+
*/
|
|
109
|
+
garbageCollection?: number;
|
|
110
|
+
/**
|
|
111
|
+
* Should we save the response to cache
|
|
112
|
+
*/
|
|
113
|
+
cache?: boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Time for which the cache is considered up-to-date
|
|
116
|
+
*/
|
|
117
|
+
cacheTime?: number;
|
|
118
|
+
/**
|
|
119
|
+
* Should the requests from this request be send one-by-one
|
|
120
|
+
*/
|
|
121
|
+
queued?: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Do we want to store request made in offline mode for latter use when we go back online
|
|
124
|
+
*/
|
|
125
|
+
offline?: boolean;
|
|
126
|
+
/**
|
|
127
|
+
* Disable post-request interceptors
|
|
128
|
+
*/
|
|
129
|
+
disableResponseInterceptors?: boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Disable pre-request interceptors
|
|
132
|
+
*/
|
|
133
|
+
disableRequestInterceptors?: boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Additional options for your adapter, by default XHR options
|
|
136
|
+
*/
|
|
137
|
+
options?: AdapterOptions;
|
|
138
|
+
/**
|
|
139
|
+
* Key which will be used to cancel requests. Autogenerated by default.
|
|
140
|
+
*/
|
|
141
|
+
abortKey?: string;
|
|
142
|
+
/**
|
|
143
|
+
* Key which will be used to cache requests. Autogenerated by default.
|
|
144
|
+
*/
|
|
145
|
+
cacheKey?: string;
|
|
146
|
+
/**
|
|
147
|
+
* Key which will be used to queue requests. Autogenerated by default.
|
|
148
|
+
*/
|
|
149
|
+
queueKey?: string;
|
|
150
|
+
/**
|
|
151
|
+
* Key which will be used to use effects. Autogenerated by default.
|
|
152
|
+
*/
|
|
153
|
+
effectKey?: string;
|
|
154
|
+
/**
|
|
155
|
+
* Should we deduplicate two requests made at the same time into one
|
|
156
|
+
*/
|
|
157
|
+
deduplicate?: boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Time of pooling for the deduplication to be active (default 10ms)
|
|
160
|
+
*/
|
|
161
|
+
deduplicateTime?: number;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
export type PayloadMapperType<PayloadType, MappedData> = (data: PayloadType) => MappedData;
|
|
165
|
+
|
|
166
|
+
export type PayloadType<Payload, MappedData> = (MappedData extends never ? Payload : MappedData) | NegativeTypes;
|
|
167
|
+
|
|
168
|
+
export type RequestCurrentType<
|
|
169
|
+
Response,
|
|
170
|
+
Payload,
|
|
171
|
+
QueryParams,
|
|
172
|
+
ErrorType,
|
|
173
|
+
GenericEndpoint extends string,
|
|
174
|
+
AdapterOptions,
|
|
175
|
+
MappedData,
|
|
176
|
+
> = {
|
|
177
|
+
used?: boolean;
|
|
178
|
+
params?: ExtractRouteParams<GenericEndpoint> | NegativeTypes;
|
|
179
|
+
queryParams?: QueryParams | NegativeTypes;
|
|
180
|
+
data?: PayloadType<Payload, MappedData>;
|
|
181
|
+
mockCallback?: ((data: Payload) => ResponseType<Response, ErrorType>) | undefined;
|
|
182
|
+
headers?: HeadersInit;
|
|
183
|
+
updatedAbortKey?: boolean;
|
|
184
|
+
updatedCacheKey?: boolean;
|
|
185
|
+
updatedQueueKey?: boolean;
|
|
186
|
+
updatedEffectKey?: boolean;
|
|
187
|
+
} & Partial<NullableKeys<RequestOptionsType<GenericEndpoint, AdapterOptions>>>;
|
|
188
|
+
|
|
189
|
+
export type ParamType = string | number;
|
|
190
|
+
export type ParamsType = Record<string, ParamType>;
|
|
191
|
+
|
|
192
|
+
export type ExtractRouteParams<T extends string> = string extends T
|
|
193
|
+
? NegativeTypes
|
|
194
|
+
: T extends `${string}:${infer Param}/${infer Rest}`
|
|
195
|
+
? { [k in Param | keyof ExtractRouteParams<Rest>]: ParamType }
|
|
196
|
+
: T extends `${string}:${infer Param}`
|
|
197
|
+
? { [k in Param]: ParamType }
|
|
198
|
+
: NegativeTypes;
|
|
199
|
+
|
|
200
|
+
export type FetchOptionsType<AdapterOptions> = Omit<
|
|
201
|
+
Partial<RequestOptionsType<string, AdapterOptions>>,
|
|
202
|
+
"endpoint" | "method"
|
|
203
|
+
>;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* It will check if the query params are already set
|
|
207
|
+
*/
|
|
208
|
+
export type FetchQueryParamsType<
|
|
209
|
+
QueryParams extends QueryParamsType | string,
|
|
210
|
+
HasQuery extends true | false = false,
|
|
211
|
+
> = HasQuery extends true
|
|
212
|
+
? { queryParams?: NegativeTypes }
|
|
213
|
+
: {
|
|
214
|
+
queryParams?: QueryParams | string;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* If the request endpoint parameters are not filled it will throw an error
|
|
219
|
+
*/
|
|
220
|
+
export type FetchParamsType<
|
|
221
|
+
Endpoint extends string,
|
|
222
|
+
HasParams extends true | false,
|
|
223
|
+
> = ExtractRouteParams<Endpoint> extends NegativeTypes
|
|
224
|
+
? { params?: NegativeTypes }
|
|
225
|
+
: true extends HasParams
|
|
226
|
+
? { params?: NegativeTypes }
|
|
227
|
+
: { params: NonNullable<ExtractRouteParams<Endpoint>> };
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* If the request data is not filled it will throw an error
|
|
231
|
+
*/
|
|
232
|
+
export type FetchPayloadType<Payload, HasData extends true | false> = Payload extends NegativeTypes
|
|
233
|
+
? { data?: NegativeTypes }
|
|
234
|
+
: HasData extends true
|
|
235
|
+
? { data?: NegativeTypes }
|
|
236
|
+
: { data: NonNullable<Payload> };
|
|
237
|
+
|
|
238
|
+
export type RequestQueueOptions = {
|
|
239
|
+
dispatcherType?: "auto" | "fetch" | "submit";
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// Request making
|
|
243
|
+
|
|
244
|
+
export type RequestSendOptionsType<Request extends RequestInstance> = FetchQueryParamsType<
|
|
245
|
+
ExtractQueryParamsType<Request>,
|
|
246
|
+
ExtractHasQueryParamsType<Request>
|
|
247
|
+
> &
|
|
248
|
+
FetchParamsType<ExtractEndpointType<Request>, ExtractHasParamsType<Request>> &
|
|
249
|
+
FetchPayloadType<ExtractPayloadType<Request>, ExtractHasDataType<Request>> &
|
|
250
|
+
Omit<FetchOptionsType<ExtractAdapterOptionsType<Request>>, "params" | "data"> &
|
|
251
|
+
FetchSendActionsType<Request> &
|
|
252
|
+
RequestQueueOptions;
|
|
253
|
+
|
|
254
|
+
export type FetchSendActionsType<Request extends RequestInstance> = {
|
|
255
|
+
onSettle?: (requestId: string, request: Request) => void;
|
|
256
|
+
onRequestStart?: (details: RequestEventType<Request>) => void;
|
|
257
|
+
onResponseStart?: (details: RequestEventType<Request>) => void;
|
|
258
|
+
onUploadProgress?: (values: ProgressType, details: RequestEventType<Request>) => void;
|
|
259
|
+
onDownloadProgress?: (values: ProgressType, details: RequestEventType<Request>) => void;
|
|
260
|
+
onResponse?: (
|
|
261
|
+
response: ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>>,
|
|
262
|
+
details: ResponseDetailsType,
|
|
263
|
+
) => void;
|
|
264
|
+
onRemove?: (details: RequestEventType<Request>) => void;
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
export type RequestSendType<Request extends RequestInstance> =
|
|
268
|
+
RequestSendOptionsType<Request>["data"] extends NegativeTypes
|
|
269
|
+
? RequestSendOptionsType<Request>["params"] extends NegativeTypes
|
|
270
|
+
? (
|
|
271
|
+
options?: RequestSendOptionsType<Request>,
|
|
272
|
+
) => Promise<ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>>>
|
|
273
|
+
: (
|
|
274
|
+
options: RequestSendOptionsType<Request>,
|
|
275
|
+
) => Promise<ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>>>
|
|
276
|
+
: (
|
|
277
|
+
options: RequestSendOptionsType<Request>,
|
|
278
|
+
) => Promise<ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>>>;
|
|
279
|
+
|
|
280
|
+
// Instance
|
|
281
|
+
|
|
282
|
+
export type RequestInstance = Request<any, any, any, any, any, any, any, any, any, any, any>;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ProgressType, ResponseType, getErrorMessage } from "adapter";
|
|
2
|
+
import { AdapterProgressEventType, RequestInstance, RequestDump, RequestSendOptionsType } from "request";
|
|
3
3
|
import { HttpMethodsEnum } from "constants/http.constants";
|
|
4
4
|
import { canRetryRequest, Dispatcher, isFailedRequest } from "dispatcher";
|
|
5
|
-
import {
|
|
5
|
+
import { ExtractErrorType, ExtractResponseType } from "types";
|
|
6
6
|
|
|
7
7
|
export const stringifyKey = (value: unknown): string => {
|
|
8
8
|
try {
|
|
@@ -16,7 +16,7 @@ export const stringifyKey = (value: unknown): string => {
|
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
export const getProgressValue = ({ loaded, total }:
|
|
19
|
+
export const getProgressValue = ({ loaded, total }: AdapterProgressEventType): number => {
|
|
20
20
|
if (!loaded || !total) return 0;
|
|
21
21
|
return Number(((loaded * 100) / total).toFixed(0));
|
|
22
22
|
};
|
|
@@ -24,7 +24,7 @@ export const getProgressValue = ({ loaded, total }: ClientProgressEvent): number
|
|
|
24
24
|
export const getRequestEta = (
|
|
25
25
|
startDate: Date,
|
|
26
26
|
progressDate: Date,
|
|
27
|
-
{ total, loaded }:
|
|
27
|
+
{ total, loaded }: AdapterProgressEventType,
|
|
28
28
|
): { sizeLeft: number; timeLeft: number | null } => {
|
|
29
29
|
const timeElapsed = +progressDate - +startDate || 1;
|
|
30
30
|
const uploadSpeed = loaded / timeElapsed;
|
|
@@ -39,8 +39,8 @@ export const getRequestEta = (
|
|
|
39
39
|
export const getProgressData = (
|
|
40
40
|
requestStartTime: Date,
|
|
41
41
|
progressDate: Date,
|
|
42
|
-
progressEvent:
|
|
43
|
-
):
|
|
42
|
+
progressEvent: AdapterProgressEventType,
|
|
43
|
+
): ProgressType => {
|
|
44
44
|
const { total, loaded } = progressEvent;
|
|
45
45
|
if (Number.isNaN(total) || Number.isNaN(loaded)) {
|
|
46
46
|
return {
|
|
@@ -66,19 +66,19 @@ export const getProgressData = (
|
|
|
66
66
|
};
|
|
67
67
|
|
|
68
68
|
// Keys
|
|
69
|
-
export const getSimpleKey = (
|
|
70
|
-
return `${
|
|
69
|
+
export const getSimpleKey = (request: RequestInstance | RequestDump<RequestInstance>): string => {
|
|
70
|
+
return `${request.method}_${request.requestOptions.endpoint}_${request.cancelable}`;
|
|
71
71
|
};
|
|
72
72
|
|
|
73
73
|
/**
|
|
74
|
-
* Cache instance for individual
|
|
74
|
+
* Cache instance for individual request that collects individual requests responses from
|
|
75
75
|
* the same endpoint (they may differ base on the custom key, endpoint params etc)
|
|
76
|
-
* @param
|
|
76
|
+
* @param request
|
|
77
77
|
* @param useInitialValues
|
|
78
78
|
* @returns
|
|
79
79
|
*/
|
|
80
|
-
export const
|
|
81
|
-
|
|
80
|
+
export const getRequestKey = (
|
|
81
|
+
request: RequestInstance | RequestDump<RequestInstance>,
|
|
82
82
|
useInitialValues?: boolean,
|
|
83
83
|
): string => {
|
|
84
84
|
/**
|
|
@@ -92,75 +92,78 @@ export const getCommandKey = (
|
|
|
92
92
|
* queryParams: string;
|
|
93
93
|
* params: string;
|
|
94
94
|
*/
|
|
95
|
-
const methodKey = stringifyKey(
|
|
96
|
-
const endpointKey = useInitialValues ?
|
|
95
|
+
const methodKey = stringifyKey(request.method);
|
|
96
|
+
const endpointKey = useInitialValues ? request.requestOptions.endpoint : stringifyKey(request.endpoint);
|
|
97
97
|
|
|
98
98
|
return `${methodKey}_${endpointKey}`;
|
|
99
99
|
};
|
|
100
100
|
|
|
101
|
-
export const
|
|
102
|
-
|
|
101
|
+
export const getRequestDispatcher = <Request extends RequestInstance>(
|
|
102
|
+
request: Request,
|
|
103
103
|
dispatcherType: "auto" | "fetch" | "submit" = "auto",
|
|
104
104
|
): [Dispatcher, boolean] => {
|
|
105
|
-
const { fetchDispatcher, submitDispatcher } =
|
|
106
|
-
const isGet =
|
|
105
|
+
const { fetchDispatcher, submitDispatcher } = request.client;
|
|
106
|
+
const isGet = request.method === HttpMethodsEnum.get;
|
|
107
107
|
const isFetchDispatcher = (dispatcherType === "auto" && isGet) || dispatcherType === "fetch";
|
|
108
108
|
const dispatcher = isFetchDispatcher ? fetchDispatcher : submitDispatcher;
|
|
109
109
|
|
|
110
110
|
return [dispatcher, isFetchDispatcher];
|
|
111
111
|
};
|
|
112
112
|
|
|
113
|
-
export const
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
export const requestSendRequest = <Request extends RequestInstance>(
|
|
114
|
+
request: Request,
|
|
115
|
+
options?: RequestSendOptionsType<Request>,
|
|
116
|
+
) => {
|
|
117
|
+
const { requestManager } = request.client;
|
|
118
|
+
const [dispatcher] = getRequestDispatcher(request, options?.dispatcherType);
|
|
116
119
|
|
|
117
|
-
return new Promise<
|
|
118
|
-
const requestId = dispatcher.add(
|
|
119
|
-
options?.onSettle?.(requestId,
|
|
120
|
+
return new Promise<ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>>>((resolve) => {
|
|
121
|
+
const requestId = dispatcher.add(request);
|
|
122
|
+
options?.onSettle?.(requestId, request);
|
|
120
123
|
|
|
121
|
-
const unmountRequestStart =
|
|
124
|
+
const unmountRequestStart = requestManager.events.onRequestStartById<Request>(requestId, (...props) =>
|
|
122
125
|
options?.onRequestStart?.(...props),
|
|
123
126
|
);
|
|
124
127
|
|
|
125
|
-
const unmountResponseStart =
|
|
128
|
+
const unmountResponseStart = requestManager.events.onResponseStartById<Request>(requestId, (...props) =>
|
|
126
129
|
options?.onResponseStart?.(...props),
|
|
127
130
|
);
|
|
128
131
|
|
|
129
|
-
const unmountUpload =
|
|
132
|
+
const unmountUpload = requestManager.events.onUploadProgressById<Request>(requestId, (...props) =>
|
|
130
133
|
options?.onUploadProgress?.(...props),
|
|
131
134
|
);
|
|
132
135
|
|
|
133
|
-
const unmountDownload =
|
|
136
|
+
const unmountDownload = requestManager.events.onDownloadProgressById<Request>(requestId, (...props) =>
|
|
134
137
|
options?.onDownloadProgress?.(...props),
|
|
135
138
|
);
|
|
136
139
|
|
|
137
140
|
// When resolved
|
|
138
|
-
const unmountResponse =
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
);
|
|
141
|
+
const unmountResponse = requestManager.events.onResponseById<
|
|
142
|
+
ExtractResponseType<Request>,
|
|
143
|
+
ExtractErrorType<Request>
|
|
144
|
+
>(requestId, (response, details) => {
|
|
145
|
+
const isFailed = isFailedRequest(response);
|
|
146
|
+
const isOfflineStatus = request.offline && details.isOffline;
|
|
147
|
+
const willRetry = canRetryRequest(details.retries, request.retry);
|
|
148
|
+
|
|
149
|
+
// When going offline we can't handle the request as it will be postponed to later resolve
|
|
150
|
+
if (isFailed && isOfflineStatus) return;
|
|
151
|
+
|
|
152
|
+
// When request is in retry mode we need to listen for retries end
|
|
153
|
+
if (isFailed && willRetry) return;
|
|
154
|
+
|
|
155
|
+
options?.onResponse?.(response, details);
|
|
156
|
+
resolve(response);
|
|
157
|
+
|
|
158
|
+
// Unmount Listeners
|
|
159
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
160
|
+
umountAll();
|
|
161
|
+
});
|
|
159
162
|
|
|
160
163
|
// When removed from queue storage we need to clean event listeners and return proper error
|
|
161
|
-
const unmountRemoveQueueElement =
|
|
162
|
-
options
|
|
163
|
-
resolve([null, getErrorMessage("deleted") as unknown as
|
|
164
|
+
const unmountRemoveQueueElement = requestManager.events.onRemoveById<Request>(requestId, (...props) => {
|
|
165
|
+
options.onRemove?.(...props);
|
|
166
|
+
resolve([null, getErrorMessage("deleted") as unknown as ExtractErrorType<Request>, 0]);
|
|
164
167
|
|
|
165
168
|
// Unmount Listeners
|
|
166
169
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
package/src/types/fetch.types.ts
CHANGED
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ExtractRouteParams,
|
|
1
|
+
import { ResponseType } from "adapter";
|
|
2
|
+
import { ExtractRouteParams, Request, RequestInstance } from "request";
|
|
3
3
|
|
|
4
|
-
export type
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
export type ExtractAdapterReturnType<T extends RequestInstance> = ResponseType<
|
|
5
|
+
ExtractResponseType<T>,
|
|
6
|
+
ExtractErrorType<T>
|
|
7
7
|
>;
|
|
8
8
|
|
|
9
|
-
export type
|
|
9
|
+
export type ExtractResponseType<T> = T extends Request<infer D, any, any, any, any, any, any, any, any, any, any>
|
|
10
10
|
? D
|
|
11
11
|
: never;
|
|
12
12
|
|
|
13
|
-
export type
|
|
13
|
+
export type ExtractPayloadType<T> = T extends Request<any, infer D, any, any, any, any, any, any, any, any, any>
|
|
14
14
|
? D
|
|
15
15
|
: never;
|
|
16
16
|
|
|
17
|
-
export type
|
|
17
|
+
export type ExtractQueryParamsType<T> = T extends Request<any, any, infer Q, any, any, any, any, any, any, any, any>
|
|
18
18
|
? Q
|
|
19
19
|
: never;
|
|
20
20
|
|
|
21
|
-
export type
|
|
21
|
+
export type ExtractErrorType<T> = T extends Request<any, any, any, infer G, infer L, any, any, any, any, any, any>
|
|
22
22
|
? G | L
|
|
23
23
|
: never;
|
|
24
24
|
|
|
25
|
-
export type
|
|
25
|
+
export type ExtractGlobalErrorType<T> = T extends Request<any, any, any, infer E, any, any, any, any, any, any, any>
|
|
26
26
|
? E
|
|
27
27
|
: never;
|
|
28
28
|
|
|
29
|
-
export type
|
|
29
|
+
export type ExtractLocalErrorType<T> = T extends Request<any, any, any, any, infer E, any, any, any, any, any, any>
|
|
30
30
|
? E
|
|
31
31
|
: never;
|
|
32
32
|
|
|
33
|
-
export type
|
|
33
|
+
export type ExtractParamsType<T> = T extends Request<any, any, any, any, any, infer P, any, any, any, any, any>
|
|
34
34
|
? ExtractRouteParams<P>
|
|
35
35
|
: never;
|
|
36
36
|
|
|
37
|
-
export type
|
|
37
|
+
export type ExtractEndpointType<T> = T extends Request<any, any, any, any, any, infer E, any, any, any, any, any>
|
|
38
38
|
? E
|
|
39
39
|
: never;
|
|
40
40
|
|
|
41
|
-
export type
|
|
41
|
+
export type ExtractAdapterOptionsType<T> = T extends Request<any, any, any, any, any, any, infer O, any, any, any, any>
|
|
42
42
|
? O
|
|
43
43
|
: never;
|
|
44
44
|
|
|
45
|
-
export type
|
|
45
|
+
export type ExtractHasDataType<T> = T extends Request<any, any, any, any, any, any, any, infer D, any, any, any>
|
|
46
46
|
? D
|
|
47
47
|
: never;
|
|
48
48
|
|
|
49
|
-
export type
|
|
49
|
+
export type ExtractHasParamsType<T> = T extends Request<any, any, any, any, any, any, any, any, infer P, any, any>
|
|
50
50
|
? P
|
|
51
51
|
: never;
|
|
52
52
|
|
|
53
|
-
export type
|
|
53
|
+
export type ExtractHasQueryParamsType<T> = T extends Request<any, any, any, any, any, any, any, any, any, infer Q, any>
|
|
54
54
|
? Q
|
|
55
55
|
: never;
|
|
56
56
|
|
|
57
|
-
export type
|
|
57
|
+
export type ExtractHasMappedDataType<T> = T extends Request<any, any, any, any, any, any, any, any, any, any, infer M>
|
|
58
58
|
? M
|
|
59
59
|
: never;
|