@hyper-fetch/core 6.0.0 → 7.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/.eslintrc.json +8 -3
- package/README.md +5 -5
- package/dist/browser/index.js +3711 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/index.d.ts +1669 -1091
- package/dist/index.js +3949 -0
- package/dist/index.js.map +7 -0
- package/package.json +16 -18
- package/src/adapter/adapter.bindings.ts +261 -165
- package/src/adapter/adapter.ts +480 -8
- package/src/adapter/adapter.types.ts +111 -139
- package/src/adapter/adapter.utils.ts +71 -25
- package/src/adapter/index.ts +1 -2
- package/src/cache/cache.events.ts +58 -14
- package/src/cache/cache.ts +158 -91
- package/src/cache/cache.types.ts +15 -25
- package/src/cache/cache.utils.ts +17 -10
- package/src/client/client.create.ts +17 -0
- package/src/client/client.ts +247 -266
- package/src/client/client.types.ts +43 -21
- package/src/client/client.utils.ts +7 -168
- package/src/client/index.ts +1 -0
- package/src/constants/http.constants.ts +6 -6
- package/src/constants/time.constants.ts +10 -10
- package/src/dispatcher/dispatcher.constants.ts +5 -5
- package/src/dispatcher/dispatcher.events.ts +85 -20
- package/src/dispatcher/dispatcher.ts +265 -199
- package/src/dispatcher/dispatcher.types.ts +6 -7
- package/src/dispatcher/dispatcher.utils.ts +26 -10
- package/src/http-adapter/http-adapter.browser.ts +101 -0
- package/src/http-adapter/http-adapter.constants.ts +8 -0
- package/src/http-adapter/http-adapter.server.ts +134 -0
- package/src/http-adapter/http-adapter.ts +11 -0
- package/src/http-adapter/http-adapter.types.ts +86 -0
- package/src/http-adapter/http-adapter.utils.ts +177 -0
- package/src/http-adapter/index.ts +4 -0
- package/src/index.ts +2 -1
- package/src/managers/app/app.manager.constants.ts +4 -4
- package/src/managers/app/app.manager.events.ts +12 -12
- package/src/managers/app/app.manager.ts +10 -7
- package/src/managers/app/app.manager.utils.ts +12 -2
- package/src/managers/logger/logger.manager.constants.ts +13 -18
- package/src/managers/logger/logger.manager.ts +37 -49
- package/src/managers/logger/logger.manager.types.ts +32 -21
- package/src/managers/logger/logger.manager.utils.ts +23 -17
- package/src/managers/request/request.manager.events.ts +222 -109
- package/src/managers/request/request.manager.ts +5 -2
- package/src/managers/request/request.manager.types.ts +35 -7
- package/src/managers/request/request.manager.utils.ts +29 -17
- package/src/mocker/mocker.ts +115 -81
- package/src/mocker/mocker.types.ts +29 -25
- package/src/plugin/index.ts +2 -0
- package/src/plugin/plugin.ts +159 -0
- package/src/plugin/plugin.types.ts +181 -0
- package/src/request/request.ts +371 -323
- package/src/request/request.types.ts +136 -145
- package/src/request/request.utils.ts +31 -30
- package/src/types/adapter.types.ts +63 -0
- package/src/types/client.types.ts +3 -3
- package/src/types/helpers.types.ts +16 -3
- package/src/types/hydration.types.ts +16 -0
- package/src/types/index.ts +3 -1
- package/src/types/request.types.ts +73 -0
- package/src/utils/emitter.utils.ts +52 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/uuid.utils.ts +4 -2
- package/tsconfig.base.json +4 -5
- package/tsconfig.json +5 -4
- package/dist/browser/index.cjs.js +0 -2420
- package/dist/browser/index.cjs.js.map +0 -7
- package/dist/browser/index.esm.js +0 -2395
- package/dist/browser/index.esm.js.map +0 -7
- package/dist/index.cjs.js +0 -2579
- package/dist/index.cjs.js.map +0 -7
- package/dist/index.esm.js +0 -2474
- package/dist/index.esm.js.map +0 -7
- package/src/adapter/adapter.browser.ts +0 -100
- package/src/adapter/adapter.constants.ts +0 -8
- package/src/adapter/adapter.server.ts +0 -93
- package/src/effect/index.ts +0 -2
- package/src/effect/request.effect.ts +0 -31
- package/src/effect/request.effect.types.ts +0 -44
- package/src/types/fetch.types.ts +0 -50
|
@@ -1,83 +1,101 @@
|
|
|
1
1
|
import {
|
|
2
2
|
NullableKeys,
|
|
3
|
-
|
|
3
|
+
EmptyTypes,
|
|
4
4
|
ExtractParamsType,
|
|
5
5
|
ExtractPayloadType,
|
|
6
|
-
ExtractRequestQueryParamsType,
|
|
7
6
|
ExtractAdapterType,
|
|
8
7
|
ExtractEndpointType,
|
|
9
|
-
|
|
8
|
+
ExtractHasPayloadType,
|
|
10
9
|
ExtractHasParamsType,
|
|
11
10
|
ExtractHasQueryParamsType,
|
|
12
11
|
ExtractErrorType,
|
|
13
12
|
ExtractResponseType,
|
|
14
13
|
HttpMethodsType,
|
|
15
|
-
|
|
16
|
-
import { Request } from "request";
|
|
17
|
-
import {
|
|
18
|
-
ResponseReturnType,
|
|
19
|
-
QueryParamsType,
|
|
20
|
-
ProgressType,
|
|
14
|
+
ExtractQueryParamsType,
|
|
21
15
|
ExtractAdapterOptionsType,
|
|
22
|
-
AdapterType,
|
|
23
16
|
ExtractAdapterMethodType,
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
TypeWithDefaults,
|
|
18
|
+
} from "types";
|
|
19
|
+
import { Request } from "request";
|
|
20
|
+
import { ResponseType, RequestResponseType } from "adapter";
|
|
21
|
+
import { RequestEventType, RequestProgressEventType, RequestResponseEventType } from "managers";
|
|
22
|
+
import { ClientInstance } from "client";
|
|
26
23
|
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
export type
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
24
|
+
// Instance
|
|
25
|
+
|
|
26
|
+
export type RequestInstanceProperties = {
|
|
27
|
+
response?: any;
|
|
28
|
+
payload?: any;
|
|
29
|
+
error?: any;
|
|
30
|
+
client?: ClientInstance;
|
|
31
|
+
queryParams?: any;
|
|
32
|
+
endpoint?: string;
|
|
33
|
+
hasParams?: boolean;
|
|
34
|
+
hasQueryParams?: boolean;
|
|
35
|
+
hasPayload?: boolean;
|
|
33
36
|
};
|
|
34
37
|
|
|
35
|
-
|
|
38
|
+
export type RequestInstance<
|
|
39
|
+
RequestProperties extends RequestInstanceProperties = {
|
|
40
|
+
response?: any;
|
|
41
|
+
payload?: any;
|
|
42
|
+
queryParams?: any;
|
|
43
|
+
error?: any;
|
|
44
|
+
client?: ClientInstance;
|
|
45
|
+
},
|
|
46
|
+
> = Request<
|
|
47
|
+
TypeWithDefaults<RequestProperties, "response", any>,
|
|
48
|
+
TypeWithDefaults<RequestProperties, "payload", any>,
|
|
49
|
+
TypeWithDefaults<RequestProperties, "queryParams", any>,
|
|
50
|
+
TypeWithDefaults<RequestProperties, "error", any>,
|
|
51
|
+
TypeWithDefaults<RequestProperties, "endpoint", any>,
|
|
52
|
+
TypeWithDefaults<RequestProperties, "client", ClientInstance>,
|
|
53
|
+
TypeWithDefaults<RequestProperties, "hasParams", any>,
|
|
54
|
+
TypeWithDefaults<RequestProperties, "hasQueryParams", any>,
|
|
55
|
+
TypeWithDefaults<RequestProperties, "hasPayload", any>
|
|
56
|
+
>;
|
|
57
|
+
|
|
58
|
+
// Progress
|
|
59
|
+
export type ProgressEventType = { total: number; loaded: number };
|
|
36
60
|
|
|
37
61
|
/**
|
|
38
62
|
* Dump of the request used to later recreate it
|
|
39
63
|
*/
|
|
40
|
-
export type RequestJSON<
|
|
41
|
-
Request extends RequestInstance,
|
|
42
|
-
// Bellow generics provided only to overcome the typescript bugs
|
|
43
|
-
AdapterOptions = unknown,
|
|
44
|
-
QueryParams = QueryParamsType,
|
|
45
|
-
Params = ExtractParamsType<Request>,
|
|
46
|
-
> = {
|
|
64
|
+
export type RequestJSON<Request extends RequestInstance> = {
|
|
47
65
|
requestOptions: RequestOptionsType<
|
|
48
|
-
|
|
49
|
-
|
|
66
|
+
ExtractEndpointType<Request>,
|
|
67
|
+
ExtractAdapterOptionsType<ExtractAdapterType<Request>>,
|
|
50
68
|
ExtractAdapterMethodType<ExtractAdapterType<Request>>
|
|
51
69
|
>;
|
|
52
|
-
endpoint:
|
|
70
|
+
endpoint: ExtractEndpointType<Request>;
|
|
53
71
|
method: ExtractAdapterMethodType<ExtractAdapterType<Request>>;
|
|
54
72
|
headers?: HeadersInit;
|
|
55
73
|
auth: boolean;
|
|
56
74
|
cancelable: boolean;
|
|
57
75
|
retry: number;
|
|
58
76
|
retryTime: number;
|
|
59
|
-
garbageCollection: number;
|
|
60
|
-
cache: boolean;
|
|
61
77
|
cacheTime: number;
|
|
78
|
+
cache: boolean;
|
|
79
|
+
staleTime: number;
|
|
62
80
|
queued: boolean;
|
|
63
81
|
offline: boolean;
|
|
64
82
|
disableResponseInterceptors: boolean | undefined;
|
|
65
83
|
disableRequestInterceptors: boolean | undefined;
|
|
66
|
-
options?:
|
|
67
|
-
|
|
68
|
-
params:
|
|
69
|
-
queryParams:
|
|
84
|
+
options?: ExtractAdapterOptionsType<ExtractAdapterType<Request>>;
|
|
85
|
+
payload: PayloadType<ExtractPayloadType<Request>>;
|
|
86
|
+
params: ExtractParamsType<Request> | EmptyTypes;
|
|
87
|
+
queryParams: ExtractQueryParamsType<Request> | EmptyTypes;
|
|
70
88
|
abortKey: string;
|
|
71
89
|
cacheKey: string;
|
|
72
|
-
|
|
73
|
-
effectKey: string;
|
|
90
|
+
queryKey: string;
|
|
74
91
|
used: boolean;
|
|
75
92
|
updatedAbortKey: boolean;
|
|
76
93
|
updatedCacheKey: boolean;
|
|
77
|
-
|
|
78
|
-
updatedEffectKey: boolean;
|
|
94
|
+
updatedQueryKey: boolean;
|
|
79
95
|
deduplicate: boolean;
|
|
80
|
-
deduplicateTime: number;
|
|
96
|
+
deduplicateTime: number | null;
|
|
97
|
+
isMockerEnabled: boolean;
|
|
98
|
+
hasMock: boolean;
|
|
81
99
|
};
|
|
82
100
|
|
|
83
101
|
// Request
|
|
@@ -85,11 +103,7 @@ export type RequestJSON<
|
|
|
85
103
|
/**
|
|
86
104
|
* Configuration options for request creation
|
|
87
105
|
*/
|
|
88
|
-
export type RequestOptionsType<
|
|
89
|
-
GenericEndpoint extends string,
|
|
90
|
-
AdapterOptions extends Record<string, any>,
|
|
91
|
-
RequestMethods = HttpMethodsType,
|
|
92
|
-
> = {
|
|
106
|
+
export type RequestOptionsType<GenericEndpoint, AdapterOptions, RequestMethods = HttpMethodsType> = {
|
|
93
107
|
/**
|
|
94
108
|
* Determine the endpoint for request request
|
|
95
109
|
*/
|
|
@@ -103,7 +117,8 @@ export type RequestOptionsType<
|
|
|
103
117
|
*/
|
|
104
118
|
auth?: boolean;
|
|
105
119
|
/**
|
|
106
|
-
* Request method
|
|
120
|
+
* Request method picked from method names handled by adapter
|
|
121
|
+
* With default adapter it is GET | POST | PATCH | PUT | DELETE
|
|
107
122
|
*/
|
|
108
123
|
method?: RequestMethods;
|
|
109
124
|
/**
|
|
@@ -121,7 +136,7 @@ export type RequestOptionsType<
|
|
|
121
136
|
/**
|
|
122
137
|
* Should we trigger garbage collection or leave data in memory
|
|
123
138
|
*/
|
|
124
|
-
|
|
139
|
+
cacheTime?: number;
|
|
125
140
|
/**
|
|
126
141
|
* Should we save the response to cache
|
|
127
142
|
*/
|
|
@@ -129,7 +144,7 @@ export type RequestOptionsType<
|
|
|
129
144
|
/**
|
|
130
145
|
* Time for which the cache is considered up-to-date
|
|
131
146
|
*/
|
|
132
|
-
|
|
147
|
+
staleTime?: number;
|
|
133
148
|
/**
|
|
134
149
|
* Should the requests from this request be send one-by-one
|
|
135
150
|
*/
|
|
@@ -161,11 +176,7 @@ export type RequestOptionsType<
|
|
|
161
176
|
/**
|
|
162
177
|
* Key which will be used to queue requests. Autogenerated by default.
|
|
163
178
|
*/
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Key which will be used to use effects. Autogenerated by default.
|
|
167
|
-
*/
|
|
168
|
-
effectKey?: string;
|
|
179
|
+
queryKey?: string;
|
|
169
180
|
/**
|
|
170
181
|
* Should we deduplicate two requests made at the same time into one
|
|
171
182
|
*/
|
|
@@ -176,25 +187,26 @@ export type RequestOptionsType<
|
|
|
176
187
|
deduplicateTime?: number;
|
|
177
188
|
};
|
|
178
189
|
|
|
179
|
-
export type PayloadMapperType<Payload> = <NewDataType>(
|
|
190
|
+
export type PayloadMapperType<Payload> = <NewDataType>(payload: Payload) => NewDataType;
|
|
180
191
|
|
|
181
|
-
export type PayloadType<Payload> = Payload |
|
|
192
|
+
export type PayloadType<Payload> = Payload | EmptyTypes;
|
|
182
193
|
|
|
183
|
-
export type
|
|
194
|
+
export type RequestConfigurationType<
|
|
184
195
|
Payload,
|
|
196
|
+
Params,
|
|
185
197
|
QueryParams,
|
|
186
198
|
GenericEndpoint extends string,
|
|
187
199
|
AdapterOptions,
|
|
188
|
-
MethodsType
|
|
200
|
+
MethodsType,
|
|
189
201
|
> = {
|
|
190
202
|
used?: boolean;
|
|
191
|
-
params?:
|
|
192
|
-
queryParams?: QueryParams |
|
|
193
|
-
|
|
203
|
+
params?: Params | EmptyTypes;
|
|
204
|
+
queryParams?: QueryParams | EmptyTypes;
|
|
205
|
+
payload?: PayloadType<Payload>;
|
|
194
206
|
headers?: HeadersInit;
|
|
195
207
|
updatedAbortKey?: boolean;
|
|
196
208
|
updatedCacheKey?: boolean;
|
|
197
|
-
|
|
209
|
+
updatedQueryKey?: boolean;
|
|
198
210
|
updatedEffectKey?: boolean;
|
|
199
211
|
} & Partial<NullableKeys<RequestOptionsType<GenericEndpoint, AdapterOptions, MethodsType>>>;
|
|
200
212
|
|
|
@@ -202,111 +214,90 @@ export type ParamType = string | number;
|
|
|
202
214
|
export type ParamsType = Record<string, ParamType>;
|
|
203
215
|
|
|
204
216
|
export type ExtractRouteParams<T extends string> = string extends T
|
|
205
|
-
?
|
|
217
|
+
? EmptyTypes
|
|
206
218
|
: T extends `${string}:${infer Param}/${infer Rest}`
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
export type FetchOptionsType<AdapterOptions> = Omit<
|
|
213
|
-
Partial<RequestOptionsType<string, AdapterOptions>>,
|
|
214
|
-
"endpoint" | "method"
|
|
215
|
-
>;
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* It will check if the query params are already set
|
|
219
|
-
*/
|
|
220
|
-
export type FetchQueryParamsType<QueryParams, HasQuery extends true | false = false> = HasQuery extends true
|
|
221
|
-
? { queryParams?: NegativeTypes }
|
|
222
|
-
: {
|
|
223
|
-
queryParams?: QueryParams;
|
|
224
|
-
};
|
|
219
|
+
? { [k in Param | keyof ExtractRouteParams<Rest>]: ParamType }
|
|
220
|
+
: T extends `${string}:${infer Param}`
|
|
221
|
+
? { [k in Param]: ParamType }
|
|
222
|
+
: EmptyTypes;
|
|
225
223
|
|
|
226
224
|
/**
|
|
227
225
|
* If the request endpoint parameters are not filled it will throw an error
|
|
228
226
|
*/
|
|
229
|
-
export type FetchParamsType<
|
|
230
|
-
|
|
231
|
-
HasParams extends true | false,
|
|
232
|
-
> = ExtractRouteParams<Endpoint> extends NegativeTypes
|
|
233
|
-
? { params?: NegativeTypes }
|
|
227
|
+
export type FetchParamsType<Params, HasParams extends true | false> = Params extends EmptyTypes | void | never
|
|
228
|
+
? { params?: EmptyTypes }
|
|
234
229
|
: HasParams extends true
|
|
235
|
-
|
|
236
|
-
|
|
230
|
+
? { params?: EmptyTypes }
|
|
231
|
+
: { params: Params };
|
|
237
232
|
|
|
238
233
|
/**
|
|
239
234
|
* If the request data is not filled it will throw an error
|
|
240
235
|
*/
|
|
241
|
-
export type FetchPayloadType<Payload,
|
|
242
|
-
? {
|
|
243
|
-
:
|
|
244
|
-
|
|
245
|
-
|
|
236
|
+
export type FetchPayloadType<Payload, HasPayload extends true | false> = Payload extends EmptyTypes | void | never
|
|
237
|
+
? { payload?: EmptyTypes }
|
|
238
|
+
: HasPayload extends true
|
|
239
|
+
? { payload?: EmptyTypes }
|
|
240
|
+
: { payload: Payload };
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* It will check if the query params are already set
|
|
244
|
+
*/
|
|
245
|
+
export type FetchQueryParamsType<QueryParams, HasQuery extends true | false = false> = HasQuery extends true
|
|
246
|
+
? { queryParams?: EmptyTypes | undefined }
|
|
247
|
+
: HasQuery extends true
|
|
248
|
+
? { queryParams?: EmptyTypes }
|
|
249
|
+
: QueryParams extends EmptyTypes | void | never
|
|
250
|
+
? { queryParams?: QueryParams }
|
|
251
|
+
: {
|
|
252
|
+
queryParams: QueryParams;
|
|
253
|
+
};
|
|
246
254
|
|
|
247
|
-
export type
|
|
255
|
+
export type RequestDynamicSendOptionsType<Request extends RequestInstance> = Omit<
|
|
256
|
+
Partial<RequestOptionsType<string, ExtractAdapterOptionsType<ExtractAdapterType<Request>>>>,
|
|
257
|
+
"params" | "data" | "endpoint" | "method"
|
|
258
|
+
> & {
|
|
248
259
|
dispatcherType?: "auto" | "fetch" | "submit";
|
|
249
260
|
};
|
|
250
261
|
|
|
251
262
|
// Request making
|
|
252
263
|
|
|
253
264
|
export type RequestSendOptionsType<Request extends RequestInstance> = FetchQueryParamsType<
|
|
254
|
-
|
|
265
|
+
ExtractQueryParamsType<Request>,
|
|
255
266
|
ExtractHasQueryParamsType<Request>
|
|
256
267
|
> &
|
|
257
|
-
FetchParamsType<
|
|
258
|
-
FetchPayloadType<ExtractPayloadType<Request>,
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
268
|
+
FetchParamsType<ExtractParamsType<Request>, ExtractHasParamsType<Request>> &
|
|
269
|
+
FetchPayloadType<ExtractPayloadType<Request>, ExtractHasPayloadType<Request>> &
|
|
270
|
+
FetchQueryParamsType<ExtractQueryParamsType<Request>, ExtractHasQueryParamsType<Request>> &
|
|
271
|
+
RequestSendActionsType<Request> &
|
|
272
|
+
RequestDynamicSendOptionsType<Request>;
|
|
262
273
|
|
|
263
|
-
export type
|
|
264
|
-
|
|
265
|
-
onRequestStart?: (
|
|
266
|
-
onResponseStart?: (
|
|
267
|
-
onUploadProgress?: (
|
|
268
|
-
onDownloadProgress?: (
|
|
269
|
-
onResponse?: (
|
|
270
|
-
|
|
271
|
-
details: ResponseDetailsType,
|
|
272
|
-
) => void;
|
|
273
|
-
onRemove?: (details: RequestEventType<Request>) => void;
|
|
274
|
+
export type RequestSendActionsType<Request extends RequestInstance> = {
|
|
275
|
+
onBeforeSent?: (eventData: RequestEventType<Request>) => void;
|
|
276
|
+
onRequestStart?: (eventData: RequestEventType<Request>) => void;
|
|
277
|
+
onResponseStart?: (eventData: RequestEventType<Request>) => void;
|
|
278
|
+
onUploadProgress?: (eventData: RequestProgressEventType<Request>) => void;
|
|
279
|
+
onDownloadProgress?: (eventData: RequestProgressEventType<Request>) => void;
|
|
280
|
+
onResponse?: (eventData: RequestResponseEventType<Request>) => void;
|
|
281
|
+
onRemove?: (eventData: RequestEventType<Request>) => void;
|
|
274
282
|
};
|
|
275
283
|
|
|
284
|
+
type IsNegativeType<T> = void extends T
|
|
285
|
+
? EmptyTypes
|
|
286
|
+
: undefined extends T
|
|
287
|
+
? EmptyTypes
|
|
288
|
+
: null extends T
|
|
289
|
+
? EmptyTypes
|
|
290
|
+
: T;
|
|
291
|
+
|
|
276
292
|
// If no data or params provided - options should be optional. If either data or params are provided - mandatory.
|
|
277
293
|
export type RequestSendType<Request extends RequestInstance> =
|
|
278
|
-
RequestSendOptionsType<Request>["
|
|
279
|
-
? RequestSendOptionsType<Request>["params"] extends
|
|
280
|
-
?
|
|
281
|
-
|
|
282
|
-
) => Promise<
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
: (
|
|
286
|
-
options: RequestSendOptionsType<Request>,
|
|
287
|
-
) => Promise<
|
|
288
|
-
ResponseReturnType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>>
|
|
289
|
-
>
|
|
290
|
-
: (
|
|
291
|
-
options: RequestSendOptionsType<Request>,
|
|
292
|
-
) => Promise<
|
|
293
|
-
ResponseReturnType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>>
|
|
294
|
-
>;
|
|
295
|
-
|
|
296
|
-
// Instance
|
|
297
|
-
|
|
298
|
-
export type RequestInstance = Request<
|
|
299
|
-
any,
|
|
300
|
-
any,
|
|
301
|
-
any,
|
|
302
|
-
any,
|
|
303
|
-
any,
|
|
304
|
-
any,
|
|
305
|
-
AdapterType<any, any, any, any, any>,
|
|
306
|
-
any,
|
|
307
|
-
any,
|
|
308
|
-
any
|
|
309
|
-
>;
|
|
294
|
+
IsNegativeType<RequestSendOptionsType<Request>["payload"]> extends EmptyTypes | void | never
|
|
295
|
+
? IsNegativeType<RequestSendOptionsType<Request>["params"]> extends EmptyTypes | void | never
|
|
296
|
+
? IsNegativeType<RequestSendOptionsType<Request>["queryParams"]> extends EmptyTypes | void | never
|
|
297
|
+
? (options?: RequestSendOptionsType<Request>) => Promise<RequestResponseType<Request>>
|
|
298
|
+
: (options: RequestSendOptionsType<Request>) => Promise<RequestResponseType<Request>>
|
|
299
|
+
: (options: RequestSendOptionsType<Request>) => Promise<RequestResponseType<Request>>
|
|
300
|
+
: (options: RequestSendOptionsType<Request>) => Promise<RequestResponseType<Request>>;
|
|
310
301
|
|
|
311
302
|
// Mappers
|
|
312
303
|
|
|
@@ -316,7 +307,7 @@ export type RequestMapper<Request extends RequestInstance, NewRequest extends Re
|
|
|
316
307
|
) => NewRequest | Promise<NewRequest>;
|
|
317
308
|
|
|
318
309
|
export type ResponseMapper<Request extends RequestInstance, NewResponse, NewError> = (
|
|
319
|
-
response:
|
|
310
|
+
response: ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>>,
|
|
320
311
|
) =>
|
|
321
|
-
|
|
|
322
|
-
| Promise<
|
|
312
|
+
| ResponseType<NewResponse, NewError, ExtractAdapterType<Request>>
|
|
313
|
+
| Promise<ResponseType<NewResponse, NewError, ExtractAdapterType<Request>>>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ProgressType,
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { ProgressType, ResponseType, getErrorMessage } from "adapter";
|
|
2
|
+
import { ProgressEventType, RequestInstance, RequestJSON, RequestSendOptionsType } from "request";
|
|
3
|
+
import { HttpMethods } from "constants/http.constants";
|
|
4
4
|
import { canRetryRequest, Dispatcher } from "dispatcher";
|
|
5
5
|
import { ExtractAdapterType, ExtractErrorType, ExtractResponseType } from "types";
|
|
6
6
|
|
|
@@ -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 }: ProgressEventType): 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 }: AdapterProgressEventType): n
|
|
|
24
24
|
export const getRequestEta = (
|
|
25
25
|
startDate: Date,
|
|
26
26
|
progressDate: Date,
|
|
27
|
-
{ total, loaded }:
|
|
27
|
+
{ total, loaded }: ProgressEventType,
|
|
28
28
|
): { sizeLeft: number; timeLeft: number | null } => {
|
|
29
29
|
const timeElapsed = +progressDate - +startDate || 1;
|
|
30
30
|
const uploadSpeed = loaded / timeElapsed;
|
|
@@ -39,7 +39,7 @@ export const getRequestEta = (
|
|
|
39
39
|
export const getProgressData = (
|
|
40
40
|
requestStartTime: Date,
|
|
41
41
|
progressDate: Date,
|
|
42
|
-
progressEvent:
|
|
42
|
+
progressEvent: ProgressEventType,
|
|
43
43
|
): ProgressType => {
|
|
44
44
|
const { total, loaded } = progressEvent;
|
|
45
45
|
if (Number.isNaN(total) || Number.isNaN(loaded)) {
|
|
@@ -94,16 +94,17 @@ export const getRequestKey = (
|
|
|
94
94
|
*/
|
|
95
95
|
const methodKey = stringifyKey(request.method);
|
|
96
96
|
const endpointKey = useInitialValues ? request.requestOptions.endpoint : stringifyKey(request.endpoint);
|
|
97
|
+
const queryParamsKey = useInitialValues ? "" : stringifyKey(request.queryParams);
|
|
97
98
|
|
|
98
|
-
return `${methodKey}_${endpointKey}`;
|
|
99
|
+
return `${methodKey}_${endpointKey}_${queryParamsKey}`;
|
|
99
100
|
};
|
|
100
101
|
|
|
101
102
|
export const getRequestDispatcher = <Request extends RequestInstance>(
|
|
102
103
|
request: Request,
|
|
103
104
|
dispatcherType: "auto" | "fetch" | "submit" = "auto",
|
|
104
|
-
): [Dispatcher
|
|
105
|
+
): [Dispatcher<ExtractAdapterType<Request>>, isFetchDispatcher: boolean] => {
|
|
105
106
|
const { fetchDispatcher, submitDispatcher } = request.client;
|
|
106
|
-
const isGet = request.method ===
|
|
107
|
+
const isGet = request.method === HttpMethods.GET;
|
|
107
108
|
const isFetchDispatcher = (dispatcherType === "auto" && isGet) || dispatcherType === "fetch";
|
|
108
109
|
const dispatcher = isFetchDispatcher ? fetchDispatcher : submitDispatcher;
|
|
109
110
|
|
|
@@ -114,53 +115,51 @@ export const sendRequest = <Request extends RequestInstance>(
|
|
|
114
115
|
request: Request,
|
|
115
116
|
options?: RequestSendOptionsType<Request>,
|
|
116
117
|
) => {
|
|
117
|
-
const {
|
|
118
|
+
const { client } = request;
|
|
119
|
+
const { requestManager } = client;
|
|
118
120
|
const [dispatcher] = getRequestDispatcher(request, options?.dispatcherType);
|
|
119
121
|
|
|
120
122
|
return new Promise<
|
|
121
|
-
|
|
123
|
+
ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>>
|
|
122
124
|
>((resolve) => {
|
|
123
125
|
let isResolved = false;
|
|
124
126
|
const requestId = dispatcher.add(request);
|
|
125
|
-
options?.
|
|
127
|
+
options?.onBeforeSent?.({ requestId, request });
|
|
126
128
|
|
|
127
|
-
const unmountRequestStart = requestManager.events.onRequestStartById<Request>(requestId, (
|
|
128
|
-
options?.onRequestStart?.(
|
|
129
|
+
const unmountRequestStart = requestManager.events.onRequestStartById<Request>(requestId, (data) =>
|
|
130
|
+
options?.onRequestStart?.(data),
|
|
129
131
|
);
|
|
130
132
|
|
|
131
|
-
const unmountResponseStart = requestManager.events.onResponseStartById<Request>(requestId, (
|
|
132
|
-
options?.onResponseStart?.(
|
|
133
|
+
const unmountResponseStart = requestManager.events.onResponseStartById<Request>(requestId, (data) =>
|
|
134
|
+
options?.onResponseStart?.(data),
|
|
133
135
|
);
|
|
134
136
|
|
|
135
|
-
const unmountUpload = requestManager.events.onUploadProgressById<Request>(requestId, (
|
|
136
|
-
options?.onUploadProgress?.(
|
|
137
|
+
const unmountUpload = requestManager.events.onUploadProgressById<Request>(requestId, (data) =>
|
|
138
|
+
options?.onUploadProgress?.(data),
|
|
137
139
|
);
|
|
138
140
|
|
|
139
|
-
const unmountDownload = requestManager.events.onDownloadProgressById<Request>(requestId, (
|
|
140
|
-
options?.onDownloadProgress?.(
|
|
141
|
+
const unmountDownload = requestManager.events.onDownloadProgressById<Request>(requestId, (data) =>
|
|
142
|
+
options?.onDownloadProgress?.(data),
|
|
141
143
|
);
|
|
142
144
|
|
|
143
145
|
// When resolved
|
|
144
|
-
const unmountResponse = requestManager.events.onResponseById<
|
|
145
|
-
|
|
146
|
-
ExtractErrorType<Request>,
|
|
147
|
-
ExtractAdapterType<Request>
|
|
148
|
-
>(requestId, (response, details) => {
|
|
146
|
+
const unmountResponse = requestManager.events.onResponseById<Request>(requestId, (values) => {
|
|
147
|
+
const { details, response } = values;
|
|
149
148
|
isResolved = true;
|
|
150
149
|
|
|
151
|
-
const mapping = request.
|
|
150
|
+
const mapping = request.unstable_responseMapper?.(response);
|
|
152
151
|
|
|
153
152
|
const isOfflineStatus = request.offline && details.isOffline;
|
|
154
153
|
const willRetry = canRetryRequest(details.retries, request.retry);
|
|
155
154
|
|
|
156
|
-
const handleResponse = (success: boolean, data: any) => {
|
|
155
|
+
const handleResponse = (success: boolean, data: ResponseType<any, any, ExtractAdapterType<Request>>) => {
|
|
157
156
|
// When going offline we can't handle the request as it will be postponed to later resolve
|
|
158
157
|
if (!success && isOfflineStatus) return;
|
|
159
158
|
|
|
160
159
|
// When request is in retry mode we need to listen for retries end
|
|
161
160
|
if (!success && willRetry) return;
|
|
162
161
|
|
|
163
|
-
options?.onResponse?.(
|
|
162
|
+
options?.onResponse?.(values);
|
|
164
163
|
resolve(data);
|
|
165
164
|
|
|
166
165
|
// Unmount Listeners
|
|
@@ -192,9 +191,11 @@ export const sendRequest = <Request extends RequestInstance>(
|
|
|
192
191
|
resolve({
|
|
193
192
|
data: null,
|
|
194
193
|
status: null,
|
|
195
|
-
success:
|
|
194
|
+
success: false,
|
|
196
195
|
error: getErrorMessage("deleted") as unknown as ExtractErrorType<Request>,
|
|
197
|
-
extra: request.client.defaultExtra,
|
|
196
|
+
extra: request.client.adapter.defaultExtra,
|
|
197
|
+
requestTimestamp: +new Date(),
|
|
198
|
+
responseTimestamp: +new Date(),
|
|
198
199
|
});
|
|
199
200
|
|
|
200
201
|
// Unmount Listeners
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Adapter, AdapterInstance } from "adapter";
|
|
2
|
+
|
|
3
|
+
export type ExtractAdapterOptionsType<T> =
|
|
4
|
+
T extends Adapter<infer O, any, any, any, any, any, any, any, any, any, any> ? O : never;
|
|
5
|
+
export type ExtractAdapterMethodType<T> =
|
|
6
|
+
T extends Adapter<any, infer M, any, any, any, any, any, any, any, any, any> ? M : never;
|
|
7
|
+
export type ExtractAdapterStatusType<T> =
|
|
8
|
+
T extends Adapter<any, any, infer S, any, any, any, any, any, any, any, any> ? S : never;
|
|
9
|
+
export type ExtractAdapterExtraType<T> =
|
|
10
|
+
T extends Adapter<any, any, any, infer A, any, any, any, any, any, any, any> ? A : never;
|
|
11
|
+
export type ExtractAdapterQueryParamsType<T> =
|
|
12
|
+
T extends Adapter<any, any, any, any, infer Q, any, any, any, any, any, any> ? Q : never;
|
|
13
|
+
export type ExtractAdapterDefaultQueryParamsType<T> =
|
|
14
|
+
T extends Adapter<any, any, any, any, any, infer Q, any, any, any, any, any> ? Q : never;
|
|
15
|
+
export type ExtractAdapterEndpointType<T> =
|
|
16
|
+
T extends Adapter<any, any, any, any, any, any, infer E, any, any, any, any> ? E : never;
|
|
17
|
+
export type ExtractAdapterEndpointMapperType<T> =
|
|
18
|
+
T extends Adapter<any, any, any, any, any, any, any, infer EM, any, any, any> ? EM : never;
|
|
19
|
+
export type ExtractAdapterQueryParamsMapperType<T> =
|
|
20
|
+
T extends Adapter<any, any, any, any, any, any, any, any, infer QPM, any, any> ? QPM : never;
|
|
21
|
+
export type ExtractAdapterHeaderMapperType<T> =
|
|
22
|
+
T extends Adapter<any, any, any, any, any, any, any, any, any, infer HM, any> ? HM : never;
|
|
23
|
+
export type ExtractAdapterPayloadMapperType<T> =
|
|
24
|
+
T extends Adapter<any, any, any, any, any, any, any, any, any, any, infer PM> ? PM : never;
|
|
25
|
+
|
|
26
|
+
// Special type only for selecting appropriate AdapterType union version (check FirebaseAdapterType).
|
|
27
|
+
export type ExtractUnionAdapter<
|
|
28
|
+
AdapterType extends AdapterInstance,
|
|
29
|
+
Values extends {
|
|
30
|
+
method?: any;
|
|
31
|
+
options?: any;
|
|
32
|
+
status?: any;
|
|
33
|
+
extra?: any;
|
|
34
|
+
queryParams?: any;
|
|
35
|
+
endpointType?: any;
|
|
36
|
+
defaultQueryParams?: any;
|
|
37
|
+
},
|
|
38
|
+
> =
|
|
39
|
+
Extract<
|
|
40
|
+
AdapterType,
|
|
41
|
+
Adapter<
|
|
42
|
+
Values["options"],
|
|
43
|
+
Values["method"],
|
|
44
|
+
Values["status"],
|
|
45
|
+
Values["extra"],
|
|
46
|
+
Values["queryParams"],
|
|
47
|
+
Values["defaultQueryParams"],
|
|
48
|
+
Values["endpointType"]
|
|
49
|
+
>
|
|
50
|
+
> extends AdapterInstance
|
|
51
|
+
? Extract<
|
|
52
|
+
AdapterType,
|
|
53
|
+
Adapter<
|
|
54
|
+
Values["options"],
|
|
55
|
+
Values["method"],
|
|
56
|
+
Values["status"],
|
|
57
|
+
Values["extra"],
|
|
58
|
+
Values["queryParams"],
|
|
59
|
+
Values["defaultQueryParams"],
|
|
60
|
+
Values["endpointType"]
|
|
61
|
+
>
|
|
62
|
+
>
|
|
63
|
+
: never;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Client } from "client";
|
|
1
|
+
import { Client, ClientInstance } from "client";
|
|
2
2
|
|
|
3
|
-
export type ExtractClientGlobalError<T> = T extends Client<infer
|
|
4
|
-
export type ExtractClientAdapterType<T> = T extends Client<any, infer
|
|
3
|
+
export type ExtractClientGlobalError<T extends ClientInstance> = T extends Client<infer P, any> ? P : never;
|
|
4
|
+
export type ExtractClientAdapterType<T extends ClientInstance> = T extends Client<any, infer P> ? P : never;
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type Prettify<T> = {
|
|
2
|
+
[K in keyof T]: T[K];
|
|
3
|
+
} & {};
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
// DO NOT ADD `void` to this type, it will break the type generation
|
|
6
|
+
export type EmptyTypes = null | undefined;
|
|
7
|
+
|
|
8
|
+
export type NullableType<T> = T | EmptyTypes;
|
|
4
9
|
|
|
5
10
|
export type NullableKeys<T> = {
|
|
6
11
|
[P in keyof T]-?: NullableType<T[P]>;
|
|
@@ -11,5 +16,13 @@ export type NonNullableKeys<T> = {
|
|
|
11
16
|
};
|
|
12
17
|
|
|
13
18
|
export type RequiredKeys<T> = {
|
|
14
|
-
[P in keyof T]-?: Exclude<T[P],
|
|
19
|
+
[P in keyof T]-?: Exclude<T[P], EmptyTypes>;
|
|
15
20
|
};
|
|
21
|
+
|
|
22
|
+
export type TypeWithDefaults<
|
|
23
|
+
Types extends Record<string, any>,
|
|
24
|
+
Key extends keyof Types,
|
|
25
|
+
Value,
|
|
26
|
+
> = Key extends keyof Types ? Exclude<Types[Key], EmptyTypes | void | never> : Value;
|
|
27
|
+
|
|
28
|
+
export type SyncOrAsync<T> = T | Promise<T>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AdapterInstance, ResponseType } from "adapter";
|
|
2
|
+
import { RequestCacheType } from "cache";
|
|
3
|
+
import { RequestInstance } from "request";
|
|
4
|
+
|
|
5
|
+
export type HydrationOptions = RequestCacheType<RequestInstance> & {
|
|
6
|
+
override: boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type HydrateDataType<Data = any, Error = any, Adapter extends AdapterInstance = any> = HydrationOptions & {
|
|
10
|
+
/** Hydration timestamp */
|
|
11
|
+
timestamp: number;
|
|
12
|
+
/** Hydrated response */
|
|
13
|
+
response: ResponseType<Data, Error, Adapter>;
|
|
14
|
+
/** Hydrated flag */
|
|
15
|
+
hydrated: true;
|
|
16
|
+
};
|