@hyper-fetch/core 1.6.1 → 1.7.1
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 +3 -7
- package/dist/builder/builder.d.ts +1 -0
- package/dist/cache/cache.types.d.ts +1 -0
- package/dist/command/command.d.ts +18 -7
- package/dist/command/command.types.d.ts +13 -7
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +2 -2
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +2 -2
- package/dist/types/fetch.types.d.ts +13 -12
- package/package.json +2 -2
- package/release.config.js +6 -0
- package/src/builder/builder.ts +1 -0
- package/src/cache/cache.ts +9 -6
- package/src/cache/cache.types.ts +1 -0
- package/src/command/command.ts +73 -14
- package/src/command/command.types.ts +17 -12
- package/src/types/fetch.types.ts +32 -12
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { ClientResponseType } from "../client";
|
|
2
2
|
import { ExtractRouteParams, Command, CommandInstance } from "../command";
|
|
3
3
|
export declare type ExtractClientReturnType<T extends CommandInstance> = ClientResponseType<ExtractResponse<T>, ExtractError<T>>;
|
|
4
|
-
export declare type ExtractResponse<T> = T extends Command<infer D, any, any, any, any, any, any, any, any, any> ? D : never;
|
|
5
|
-
export declare type ExtractRequestData<T> = T extends Command<any, infer D, any, any, any, any, any, any, any, any> ? D : never;
|
|
6
|
-
export declare type ExtractQueryParams<T> = T extends Command<any, any, infer Q, any, any, any, any, any, any, any> ? Q : never;
|
|
7
|
-
export declare type ExtractError<T> = T extends Command<any, any, any, infer G, infer L, any, any, any, any, any> ? G | L : never;
|
|
8
|
-
export declare type ExtractGlobalError<T> = T extends Command<any, any, any, infer E, any, any, any, any, any, any> ? E : never;
|
|
9
|
-
export declare type ExtractLocalError<T> = T extends Command<any, any, any, any, infer E, any, any, any, any, any> ? E : never;
|
|
10
|
-
export declare type ExtractParams<T> = T extends Command<any, any, any, any, any, infer P, any, any, any, any> ? ExtractRouteParams<P> : never;
|
|
11
|
-
export declare type ExtractEndpoint<T> = T extends Command<any, any, any, any, any, infer E, any, any, any, any> ? E : never;
|
|
12
|
-
export declare type ExtractClientOptions<T> = T extends Command<any, any, any, any, any, any, infer O, any, any, any> ? O : never;
|
|
13
|
-
export declare type ExtractHasData<T> = T extends Command<any, any, any, any, any, any, any, infer D, any, any> ? D : never;
|
|
14
|
-
export declare type ExtractHasParams<T> = T extends Command<any, any, any, any, any, any, any, any, infer P, any> ? P : never;
|
|
15
|
-
export declare type ExtractHasQueryParams<T> = T extends Command<any, any, any, any, any, any, any, any, any, infer Q> ? Q : never;
|
|
4
|
+
export declare type ExtractResponse<T> = T extends Command<infer D, any, any, any, any, any, any, any, any, any, any> ? D : never;
|
|
5
|
+
export declare type ExtractRequestData<T> = T extends Command<any, infer D, any, any, any, any, any, any, any, any, any> ? D : never;
|
|
6
|
+
export declare type ExtractQueryParams<T> = T extends Command<any, any, infer Q, any, any, any, any, any, any, any, any> ? Q : never;
|
|
7
|
+
export declare type ExtractError<T> = T extends Command<any, any, any, infer G, infer L, any, any, any, any, any, any> ? G | L : never;
|
|
8
|
+
export declare type ExtractGlobalError<T> = T extends Command<any, any, any, infer E, any, any, any, any, any, any, any> ? E : never;
|
|
9
|
+
export declare type ExtractLocalError<T> = T extends Command<any, any, any, any, infer E, any, any, any, any, any, any> ? E : never;
|
|
10
|
+
export declare type ExtractParams<T> = T extends Command<any, any, any, any, any, infer P, any, any, any, any, any> ? ExtractRouteParams<P> : never;
|
|
11
|
+
export declare type ExtractEndpoint<T> = T extends Command<any, any, any, any, any, infer E, any, any, any, any, any> ? E : never;
|
|
12
|
+
export declare type ExtractClientOptions<T> = T extends Command<any, any, any, any, any, any, infer O, any, any, any, any> ? O : never;
|
|
13
|
+
export declare type ExtractHasData<T> = T extends Command<any, any, any, any, any, any, any, infer D, any, any, any> ? D : never;
|
|
14
|
+
export declare type ExtractHasParams<T> = T extends Command<any, any, any, any, any, any, any, any, infer P, any, any> ? P : never;
|
|
15
|
+
export declare type ExtractHasQueryParams<T> = T extends Command<any, any, any, any, any, any, any, any, any, infer Q, any> ? Q : never;
|
|
16
|
+
export declare type ExtractHasMappedData<T> = T extends Command<any, any, any, any, any, any, any, any, any, any, infer M> ? M : never;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Cache, Queue and Persist your requests no matter if you are online or offline!",
|
|
6
6
|
"author": "Maciej Pyrc <m.pyrc@interia.pl>",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"lint": "eslint . --ext .js,.jsx,.tsx,.ts --fix",
|
|
22
22
|
"format": "prettier --write .",
|
|
23
23
|
"precommit": "yarn lint-staged",
|
|
24
|
-
"release": "yarn semantic-release --extends
|
|
24
|
+
"release": "yarn semantic-release --extends ./release.config.js -t 'hyper-fetch-v${version}'"
|
|
25
25
|
},
|
|
26
26
|
"publishConfig": {
|
|
27
27
|
"access": "public",
|
package/src/builder/builder.ts
CHANGED
|
@@ -30,6 +30,7 @@ import { interceptRequest, interceptResponse } from "./builder.utils";
|
|
|
30
30
|
* **Builder** is a class that allows you to configure the connection with the server and then use it to create
|
|
31
31
|
* commands which, when called using the appropriate method, will cause the server to be queried for the endpoint and
|
|
32
32
|
* method specified in the command.
|
|
33
|
+
* @position 1
|
|
33
34
|
*/
|
|
34
35
|
export class Builder<GlobalErrorType extends BuilderErrorType = Error, RequestConfigType = ClientDefaultOptionsType> {
|
|
35
36
|
readonly baseUrl: string;
|
package/src/cache/cache.ts
CHANGED
|
@@ -64,14 +64,14 @@ export class Cache {
|
|
|
64
64
|
details: CommandResponseDetails,
|
|
65
65
|
): void => {
|
|
66
66
|
this.logger.debug("Processing cache response", { command, response, details });
|
|
67
|
-
const { cacheKey, cache, cacheTime } = command;
|
|
67
|
+
const { cacheKey, cache, cacheTime, garbageCollection } = command;
|
|
68
68
|
const cachedData = this.storage.get<Response, Error>(cacheKey);
|
|
69
69
|
|
|
70
70
|
// Once refresh error occurs we don't want to override already valid data in our cache with the thrown error
|
|
71
71
|
// We need to check it against cache and return last valid data we have
|
|
72
72
|
const data = getCacheData(cachedData?.data, response);
|
|
73
73
|
|
|
74
|
-
const newCacheData: CacheValueType = { data, details, cacheTime, clearKey: this.clearKey };
|
|
74
|
+
const newCacheData: CacheValueType = { data, details, cacheTime, clearKey: this.clearKey, garbageCollection };
|
|
75
75
|
|
|
76
76
|
this.events.emitCacheData<Response, Error>(cacheKey, newCacheData);
|
|
77
77
|
this.logger.debug("Emitting cache response", { command, response, details });
|
|
@@ -193,20 +193,23 @@ export class Cache {
|
|
|
193
193
|
// We need to make sure that all of the values will be removed, also that we have the proper data
|
|
194
194
|
const cacheData = await this.getLazyResource(cacheKey);
|
|
195
195
|
|
|
196
|
-
|
|
196
|
+
// Clear running garbage collectors for given key
|
|
197
|
+
clearTimeout(this.garbageCollectors.get(cacheKey));
|
|
198
|
+
|
|
199
|
+
if (cacheData && cacheData.garbageCollection) {
|
|
197
200
|
const timeLeft = cacheData.cacheTime + cacheData.details.timestamp - +new Date();
|
|
198
201
|
if (timeLeft >= 0) {
|
|
199
|
-
clearTimeout(this.garbageCollectors.get(cacheKey));
|
|
200
202
|
this.garbageCollectors.set(
|
|
201
203
|
cacheKey,
|
|
202
204
|
setTimeout(() => {
|
|
203
|
-
if (this.builder.appManager.isOnline) {
|
|
205
|
+
if (this.builder.appManager.isOnline && cacheData.garbageCollection) {
|
|
204
206
|
this.logger.info("Garbage collecting cache element", { cacheKey });
|
|
205
207
|
this.delete(cacheKey);
|
|
206
208
|
}
|
|
207
209
|
}, timeLeft),
|
|
208
210
|
);
|
|
209
|
-
} else if (this.builder.appManager.isOnline) {
|
|
211
|
+
} else if (this.builder.appManager.isOnline && cacheData.garbageCollection) {
|
|
212
|
+
this.logger.info("Garbage collecting cache element", { cacheKey });
|
|
210
213
|
this.delete(cacheKey);
|
|
211
214
|
}
|
|
212
215
|
}
|
package/src/cache/cache.types.ts
CHANGED
package/src/command/command.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
ExtractRouteParams,
|
|
11
11
|
commandSendRequest,
|
|
12
12
|
CommandCurrentType,
|
|
13
|
+
CommandMapperType,
|
|
13
14
|
} from "command";
|
|
14
15
|
import { Builder } from "builder";
|
|
15
16
|
import { getUniqueRequestId } from "utils";
|
|
@@ -53,6 +54,7 @@ export class Command<
|
|
|
53
54
|
cancelable: boolean;
|
|
54
55
|
retry: number;
|
|
55
56
|
retryTime: number;
|
|
57
|
+
garbageCollection: boolean;
|
|
56
58
|
cache: boolean;
|
|
57
59
|
cacheTime: number;
|
|
58
60
|
queued: boolean;
|
|
@@ -84,7 +86,7 @@ export class Command<
|
|
|
84
86
|
MappedData
|
|
85
87
|
>
|
|
86
88
|
| undefined,
|
|
87
|
-
readonly dataMapper?:
|
|
89
|
+
readonly dataMapper?: CommandMapperType<RequestDataType, MappedData>,
|
|
88
90
|
) {
|
|
89
91
|
const {
|
|
90
92
|
endpoint,
|
|
@@ -95,6 +97,7 @@ export class Command<
|
|
|
95
97
|
cancelable = false,
|
|
96
98
|
retry = 0,
|
|
97
99
|
retryTime = 500,
|
|
100
|
+
garbageCollection = true,
|
|
98
101
|
cache = true,
|
|
99
102
|
cacheTime = DateInterval.minute * 5,
|
|
100
103
|
queued = false,
|
|
@@ -118,6 +121,7 @@ export class Command<
|
|
|
118
121
|
this.cancelable = commandDump?.cancelable ?? cancelable;
|
|
119
122
|
this.retry = commandDump?.retry ?? retry;
|
|
120
123
|
this.retryTime = commandDump?.retryTime ?? retryTime;
|
|
124
|
+
this.garbageCollection = commandDump?.garbageCollection ?? garbageCollection;
|
|
121
125
|
this.cache = commandDump?.cache ?? cache;
|
|
122
126
|
this.cacheTime = commandDump?.cacheTime ?? cacheTime;
|
|
123
127
|
this.queued = commandDump?.queued ?? queued;
|
|
@@ -144,13 +148,13 @@ export class Command<
|
|
|
144
148
|
return this.clone({ auth });
|
|
145
149
|
};
|
|
146
150
|
|
|
147
|
-
public setParams = (params:
|
|
148
|
-
return this.clone<HasData, true, HasQuery>({ params });
|
|
151
|
+
public setParams = <P extends ExtractRouteParams<EndpointType>>(params: P) => {
|
|
152
|
+
return this.clone<HasData, P extends null ? false : true, HasQuery>({ params });
|
|
149
153
|
};
|
|
150
154
|
|
|
151
|
-
public setData = (data:
|
|
155
|
+
public setData = <D extends RequestDataType>(data: D) => {
|
|
152
156
|
const modifiedData = this.dataMapper?.(data) || data;
|
|
153
|
-
return this.clone<true, HasParams, HasQuery, MappedData>({
|
|
157
|
+
return this.clone<D extends null ? false : true, HasParams, HasQuery, MappedData>({
|
|
154
158
|
data: modifiedData as CommandData<RequestDataType, MappedData>,
|
|
155
159
|
});
|
|
156
160
|
};
|
|
@@ -175,6 +179,12 @@ export class Command<
|
|
|
175
179
|
return this.clone({ retryTime });
|
|
176
180
|
};
|
|
177
181
|
|
|
182
|
+
public setGarbageCollection = (
|
|
183
|
+
garbageCollection: CommandConfig<EndpointType, ClientOptions>["garbageCollection"],
|
|
184
|
+
) => {
|
|
185
|
+
return this.clone({ garbageCollection });
|
|
186
|
+
};
|
|
187
|
+
|
|
178
188
|
public setCache = (cache: CommandConfig<EndpointType, ClientOptions>["cache"]) => {
|
|
179
189
|
return this.clone({ cache });
|
|
180
190
|
};
|
|
@@ -223,8 +233,8 @@ export class Command<
|
|
|
223
233
|
return this.clone({ offline });
|
|
224
234
|
};
|
|
225
235
|
|
|
226
|
-
public setDataMapper = <DataMapper
|
|
227
|
-
return this.clone<HasData, HasParams, HasQuery
|
|
236
|
+
public setDataMapper = <DataMapper extends (data: RequestDataType) => any>(mapper: DataMapper) => {
|
|
237
|
+
return this.clone<HasData, HasParams, HasQuery>(undefined, mapper);
|
|
228
238
|
};
|
|
229
239
|
|
|
230
240
|
private paramsMapper = (
|
|
@@ -273,6 +283,7 @@ export class Command<
|
|
|
273
283
|
cancelable: this.cancelable,
|
|
274
284
|
retry: this.retry,
|
|
275
285
|
retryTime: this.retryTime,
|
|
286
|
+
garbageCollection: this.garbageCollection,
|
|
276
287
|
cache: this.cache,
|
|
277
288
|
cacheTime: this.cacheTime,
|
|
278
289
|
queued: this.queued,
|
|
@@ -297,7 +308,7 @@ export class Command<
|
|
|
297
308
|
D extends true | false = HasData,
|
|
298
309
|
P extends true | false = HasParams,
|
|
299
310
|
Q extends true | false = HasQuery,
|
|
300
|
-
|
|
311
|
+
NewMappedData = MappedData,
|
|
301
312
|
>(
|
|
302
313
|
options?: CommandCurrentType<
|
|
303
314
|
ResponseType,
|
|
@@ -306,9 +317,9 @@ export class Command<
|
|
|
306
317
|
GlobalErrorType | LocalErrorType,
|
|
307
318
|
EndpointType,
|
|
308
319
|
ClientOptions,
|
|
309
|
-
|
|
320
|
+
NewMappedData
|
|
310
321
|
>,
|
|
311
|
-
mapper?:
|
|
322
|
+
mapper?: CommandMapperType<RequestDataType, NewMappedData>,
|
|
312
323
|
): Command<
|
|
313
324
|
ResponseType,
|
|
314
325
|
RequestDataType,
|
|
@@ -320,7 +331,7 @@ export class Command<
|
|
|
320
331
|
D,
|
|
321
332
|
P,
|
|
322
333
|
Q,
|
|
323
|
-
|
|
334
|
+
NewMappedData
|
|
324
335
|
> {
|
|
325
336
|
const dump = this.dump();
|
|
326
337
|
const commandDump: CommandCurrentType<
|
|
@@ -330,7 +341,7 @@ export class Command<
|
|
|
330
341
|
GlobalErrorType | LocalErrorType,
|
|
331
342
|
EndpointType,
|
|
332
343
|
ClientOptions,
|
|
333
|
-
|
|
344
|
+
NewMappedData
|
|
334
345
|
> = {
|
|
335
346
|
...dump,
|
|
336
347
|
...options,
|
|
@@ -357,7 +368,7 @@ export class Command<
|
|
|
357
368
|
D,
|
|
358
369
|
P,
|
|
359
370
|
Q,
|
|
360
|
-
|
|
371
|
+
NewMappedData
|
|
361
372
|
>(this.builder, this.commandOptions, commandDump, mapper);
|
|
362
373
|
|
|
363
374
|
return cloned;
|
|
@@ -373,7 +384,11 @@ export class Command<
|
|
|
373
384
|
/**
|
|
374
385
|
* Method to use the command WITHOUT adding it to cache and queues. This mean it will make simple request without queue side effects.
|
|
375
386
|
* @param options
|
|
387
|
+
* @disableReturns
|
|
376
388
|
* @returns
|
|
389
|
+
* ```tsx
|
|
390
|
+
* Promise<[Data | null, Error | null, HttpStatus]>
|
|
391
|
+
* ```
|
|
377
392
|
*/
|
|
378
393
|
public exec: FetchMethodType<
|
|
379
394
|
Command<
|
|
@@ -418,6 +433,11 @@ export class Command<
|
|
|
418
433
|
* Method used to perform requests with usage of cache and queues
|
|
419
434
|
* @param options
|
|
420
435
|
* @param requestCallback
|
|
436
|
+
* @disableReturns
|
|
437
|
+
* @returns
|
|
438
|
+
* ```tsx
|
|
439
|
+
* Promise<[Data | null, Error | null, HttpStatus]>
|
|
440
|
+
* ```
|
|
421
441
|
*/
|
|
422
442
|
public send: FetchMethodType<
|
|
423
443
|
Command<
|
|
@@ -471,7 +491,9 @@ export class Command<
|
|
|
471
491
|
};
|
|
472
492
|
}
|
|
473
493
|
|
|
474
|
-
//
|
|
494
|
+
// /**
|
|
495
|
+
// * Typescript test cases
|
|
496
|
+
// */
|
|
475
497
|
|
|
476
498
|
// const builder = new Builder({
|
|
477
499
|
// baseUrl: "http://localhost:3000",
|
|
@@ -497,6 +519,19 @@ export class Command<
|
|
|
497
519
|
// endpoint: "/users/:id",
|
|
498
520
|
// });
|
|
499
521
|
|
|
522
|
+
// const mappedReq = builder
|
|
523
|
+
// .createCommand<{ id: string }, { name: string }>()({
|
|
524
|
+
// method: "POST",
|
|
525
|
+
// endpoint: "/users",
|
|
526
|
+
// })
|
|
527
|
+
// .setDataMapper((data) => {
|
|
528
|
+
// const formData = new FormData();
|
|
529
|
+
// formData.append("key", data.name);
|
|
530
|
+
// return formData;
|
|
531
|
+
// });
|
|
532
|
+
|
|
533
|
+
// // ================>
|
|
534
|
+
|
|
500
535
|
// // OK
|
|
501
536
|
// getUsers.send({ queryParams: "" });
|
|
502
537
|
// getUsers.setQueryParams("").send();
|
|
@@ -505,6 +540,8 @@ export class Command<
|
|
|
505
540
|
// getUsers.send({ params: "" });
|
|
506
541
|
// getUsers.setQueryParams("").send({ queryParams: "" });
|
|
507
542
|
|
|
543
|
+
// // ================>
|
|
544
|
+
|
|
508
545
|
// // OK
|
|
509
546
|
// getUser.send({ params: { id: "" }, queryParams: "" });
|
|
510
547
|
// getUser.setParams({ id: "" }).send({ queryParams: "" });
|
|
@@ -512,6 +549,10 @@ export class Command<
|
|
|
512
549
|
// getUser.send({ queryParams: "" });
|
|
513
550
|
// getUser.send();
|
|
514
551
|
// getUser.setParams({ id: "" }).send({ params: { id: "" } });
|
|
552
|
+
// getUser.setParams(null).send();
|
|
553
|
+
// getUser.send({ params: { id: null } });
|
|
554
|
+
|
|
555
|
+
// // ================>
|
|
515
556
|
|
|
516
557
|
// // OK
|
|
517
558
|
// postUser.send({ data: { name: "" } });
|
|
@@ -519,15 +560,19 @@ export class Command<
|
|
|
519
560
|
// // Fail
|
|
520
561
|
// postUser.send({ queryParams: "" });
|
|
521
562
|
// postUser.send({ data: null });
|
|
563
|
+
// postUser.setData(null).send();
|
|
522
564
|
// postUser.send();
|
|
523
565
|
// postUser.setData({ name: "" }).send({ data: { name: "" } });
|
|
524
566
|
|
|
567
|
+
// // ================>
|
|
568
|
+
|
|
525
569
|
// // OK
|
|
526
570
|
// patchUser.send({ params: { id: "" }, data: { name: "" } });
|
|
527
571
|
// patchUser.setParams({ id: "" }).setData({ name: "" }).send();
|
|
528
572
|
// // Fail
|
|
529
573
|
// patchUser.send({ queryParams: "" });
|
|
530
574
|
// patchUser.send({ data: null });
|
|
575
|
+
// patchUser.setData(null).send();
|
|
531
576
|
// patchUser.send();
|
|
532
577
|
// patchUser
|
|
533
578
|
// .setParams({ id: "" })
|
|
@@ -537,3 +582,17 @@ export class Command<
|
|
|
537
582
|
// .setParams({ id: "" })
|
|
538
583
|
// .setData({ name: "" })
|
|
539
584
|
// .send({ params: { id: "" } });
|
|
585
|
+
|
|
586
|
+
// // ================>
|
|
587
|
+
|
|
588
|
+
// // OK
|
|
589
|
+
// mappedReq.send({ data: { name: "" } });
|
|
590
|
+
// mappedReq.setData({ name: "" }).send();
|
|
591
|
+
// // Fail
|
|
592
|
+
// mappedReq.send({ queryParams: "" });
|
|
593
|
+
// mappedReq.send({ data: undefined });
|
|
594
|
+
// mappedReq.setData(null).send();
|
|
595
|
+
// mappedReq.setData(null).send({ data: null, queryParams: () => null });
|
|
596
|
+
// mappedReq.send();
|
|
597
|
+
// mappedReq.send({ data: new FormData() });
|
|
598
|
+
// mappedReq.setData({ name: "" }).send({ data: { name: "" } });
|
|
@@ -45,6 +45,7 @@ export type CommandDump<
|
|
|
45
45
|
cancelable: boolean;
|
|
46
46
|
retry: number;
|
|
47
47
|
retryTime: number;
|
|
48
|
+
garbageCollection: boolean;
|
|
48
49
|
cache: boolean;
|
|
49
50
|
cacheTime: number;
|
|
50
51
|
queued: boolean;
|
|
@@ -102,6 +103,10 @@ export type CommandConfig<GenericEndpoint extends string, ClientOptions> = {
|
|
|
102
103
|
* Retry time delay between retries
|
|
103
104
|
*/
|
|
104
105
|
retryTime?: number;
|
|
106
|
+
/**
|
|
107
|
+
* Should we trigger garbage collection or leave data in memory
|
|
108
|
+
*/
|
|
109
|
+
garbageCollection?: boolean;
|
|
105
110
|
/**
|
|
106
111
|
* Should we save the response to cache
|
|
107
112
|
*/
|
|
@@ -156,8 +161,10 @@ export type CommandConfig<GenericEndpoint extends string, ClientOptions> = {
|
|
|
156
161
|
deduplicateTime?: number;
|
|
157
162
|
};
|
|
158
163
|
|
|
164
|
+
export type CommandMapperType<RequestDataType, MappedData> = (data: RequestDataType) => MappedData;
|
|
165
|
+
|
|
159
166
|
export type CommandData<RequestDataType, MappedData> =
|
|
160
|
-
| (MappedData extends
|
|
167
|
+
| (MappedData extends never ? RequestDataType : MappedData)
|
|
161
168
|
| NegativeTypes;
|
|
162
169
|
|
|
163
170
|
export type CommandCurrentType<
|
|
@@ -200,7 +207,10 @@ export type FetchOptionsType<ClientOptions> = Omit<
|
|
|
200
207
|
/**
|
|
201
208
|
* It will check if the query params are already set
|
|
202
209
|
*/
|
|
203
|
-
export type FetchQueryParamsType<
|
|
210
|
+
export type FetchQueryParamsType<
|
|
211
|
+
QueryParamsType extends ClientQueryParamsType | string,
|
|
212
|
+
HasQuery extends true | false = false,
|
|
213
|
+
> = HasQuery extends true
|
|
204
214
|
? { queryParams?: NegativeTypes }
|
|
205
215
|
: {
|
|
206
216
|
queryParams?: QueryParamsType | string;
|
|
@@ -211,24 +221,21 @@ export type FetchQueryParamsType<QueryParamsType, HasQuery extends true | false
|
|
|
211
221
|
*/
|
|
212
222
|
export type FetchParamsType<
|
|
213
223
|
EndpointType extends string,
|
|
214
|
-
HasParams extends true | false
|
|
224
|
+
HasParams extends true | false,
|
|
215
225
|
> = ExtractRouteParams<EndpointType> extends NegativeTypes
|
|
216
226
|
? { params?: NegativeTypes }
|
|
217
227
|
: true extends HasParams
|
|
218
228
|
? { params?: NegativeTypes }
|
|
219
|
-
: { params: ExtractRouteParams<EndpointType
|
|
229
|
+
: { params: NonNullable<ExtractRouteParams<EndpointType>> };
|
|
220
230
|
|
|
221
231
|
/**
|
|
222
232
|
* If the command data is not filled it will throw an error
|
|
223
233
|
*/
|
|
224
|
-
export type FetchRequestDataType<
|
|
225
|
-
RequestDataType,
|
|
226
|
-
HasData extends true | false = false,
|
|
227
|
-
> = RequestDataType extends NegativeTypes
|
|
234
|
+
export type FetchRequestDataType<RequestDataType, HasData extends true | false> = RequestDataType extends NegativeTypes
|
|
228
235
|
? { data?: NegativeTypes }
|
|
229
236
|
: HasData extends true
|
|
230
237
|
? { data?: NegativeTypes }
|
|
231
|
-
: { data: RequestDataType };
|
|
238
|
+
: { data: NonNullable<RequestDataType> };
|
|
232
239
|
|
|
233
240
|
export type CommandQueueOptions = {
|
|
234
241
|
dispatcherType?: "auto" | "fetch" | "submit";
|
|
@@ -257,9 +264,7 @@ export type FetchSendActionsType<Command extends CommandInstance> = {
|
|
|
257
264
|
onRemove?: (details: CommandEventDetails<Command>) => void;
|
|
258
265
|
};
|
|
259
266
|
|
|
260
|
-
export type FetchMethodType<Command extends CommandInstance> = FetchType<Command>["data"] extends
|
|
261
|
-
? (options?: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>
|
|
262
|
-
: FetchType<Command>["data"] extends NegativeTypes
|
|
267
|
+
export type FetchMethodType<Command extends CommandInstance> = FetchType<Command>["data"] extends NegativeTypes
|
|
263
268
|
? FetchType<Command>["params"] extends NegativeTypes
|
|
264
269
|
? (options?: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>
|
|
265
270
|
: (options: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>
|
package/src/types/fetch.types.ts
CHANGED
|
@@ -6,34 +6,54 @@ export type ExtractClientReturnType<T extends CommandInstance> = ClientResponseT
|
|
|
6
6
|
ExtractError<T>
|
|
7
7
|
>;
|
|
8
8
|
|
|
9
|
-
export type ExtractResponse<T> = T extends Command<infer D, any, any, any, any, any, any, any, any, any>
|
|
9
|
+
export type ExtractResponse<T> = T extends Command<infer D, any, any, any, any, any, any, any, any, any, any>
|
|
10
|
+
? D
|
|
11
|
+
: never;
|
|
10
12
|
|
|
11
|
-
export type ExtractRequestData<T> = T extends Command<any, infer D, any, any, any, any, any, any, any, any>
|
|
13
|
+
export type ExtractRequestData<T> = T extends Command<any, infer D, any, any, any, any, any, any, any, any, any>
|
|
14
|
+
? D
|
|
15
|
+
: never;
|
|
12
16
|
|
|
13
|
-
export type ExtractQueryParams<T> = T extends Command<any, any, infer Q, any, any, any, any, any, any, any>
|
|
17
|
+
export type ExtractQueryParams<T> = T extends Command<any, any, infer Q, any, any, any, any, any, any, any, any>
|
|
18
|
+
? Q
|
|
19
|
+
: never;
|
|
14
20
|
|
|
15
|
-
export type ExtractError<T> = T extends Command<any, any, any, infer G, infer L, any, any, any, any, any>
|
|
21
|
+
export type ExtractError<T> = T extends Command<any, any, any, infer G, infer L, any, any, any, any, any, any>
|
|
16
22
|
? G | L
|
|
17
23
|
: never;
|
|
18
24
|
|
|
19
|
-
export type ExtractGlobalError<T> = T extends Command<any, any, any, infer E, any, any, any, any, any, any>
|
|
25
|
+
export type ExtractGlobalError<T> = T extends Command<any, any, any, infer E, any, any, any, any, any, any, any>
|
|
26
|
+
? E
|
|
27
|
+
: never;
|
|
20
28
|
|
|
21
|
-
export type ExtractLocalError<T> = T extends Command<any, any, any, any, infer E, any, any, any, any, any>
|
|
29
|
+
export type ExtractLocalError<T> = T extends Command<any, any, any, any, infer E, any, any, any, any, any, any>
|
|
30
|
+
? E
|
|
31
|
+
: never;
|
|
22
32
|
|
|
23
|
-
export type ExtractParams<T> = T extends Command<any, any, any, any, any, infer P, any, any, any, any>
|
|
33
|
+
export type ExtractParams<T> = T extends Command<any, any, any, any, any, infer P, any, any, any, any, any>
|
|
24
34
|
? ExtractRouteParams<P>
|
|
25
35
|
: never;
|
|
26
36
|
|
|
27
|
-
export type ExtractEndpoint<T> = T extends Command<any, any, any, any, any, infer E, any, any, any, any>
|
|
37
|
+
export type ExtractEndpoint<T> = T extends Command<any, any, any, any, any, infer E, any, any, any, any, any>
|
|
38
|
+
? E
|
|
39
|
+
: never;
|
|
28
40
|
|
|
29
|
-
export type ExtractClientOptions<T> = T extends Command<any, any, any, any, any, any, infer O, any, any, any>
|
|
41
|
+
export type ExtractClientOptions<T> = T extends Command<any, any, any, any, any, any, infer O, any, any, any, any>
|
|
30
42
|
? O
|
|
31
43
|
: never;
|
|
32
44
|
|
|
33
|
-
export type ExtractHasData<T> = T extends Command<any, any, any, any, any, any, any, infer D, any, any>
|
|
45
|
+
export type ExtractHasData<T> = T extends Command<any, any, any, any, any, any, any, infer D, any, any, any>
|
|
46
|
+
? D
|
|
47
|
+
: never;
|
|
34
48
|
|
|
35
|
-
export type ExtractHasParams<T> = T extends Command<any, any, any, any, any, any, any, any, infer P, any>
|
|
49
|
+
export type ExtractHasParams<T> = T extends Command<any, any, any, any, any, any, any, any, infer P, any, any>
|
|
50
|
+
? P
|
|
51
|
+
: never;
|
|
36
52
|
|
|
37
|
-
export type ExtractHasQueryParams<T> = T extends Command<any, any, any, any, any, any, any, any, any, infer Q>
|
|
53
|
+
export type ExtractHasQueryParams<T> = T extends Command<any, any, any, any, any, any, any, any, any, infer Q, any>
|
|
38
54
|
? Q
|
|
39
55
|
: never;
|
|
56
|
+
|
|
57
|
+
export type ExtractHasMappedData<T> = T extends Command<any, any, any, any, any, any, any, any, any, any, infer M>
|
|
58
|
+
? M
|
|
59
|
+
: never;
|