@hyper-fetch/core 1.7.0 → 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.
@@ -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.7.0",
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>",
package/release.config.js CHANGED
@@ -1,8 +1,6 @@
1
1
  // eslint-disable-next-line @typescript-eslint/no-var-requires
2
2
  const config = require("../../release.config");
3
3
 
4
- // We want to publish only single release notes
5
4
  const newConfig = { ...config, publish: undefined, success: undefined, fail: undefined };
6
- newConfig.release.plugins.push(["@semantic-release/git"]);
7
5
 
8
6
  module.exports = newConfig;
@@ -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";
@@ -85,7 +86,7 @@ export class Command<
85
86
  MappedData
86
87
  >
87
88
  | undefined,
88
- readonly dataMapper?: (data: RequestDataType) => MappedData,
89
+ readonly dataMapper?: CommandMapperType<RequestDataType, MappedData>,
89
90
  ) {
90
91
  const {
91
92
  endpoint,
@@ -147,13 +148,13 @@ export class Command<
147
148
  return this.clone({ auth });
148
149
  };
149
150
 
150
- public setParams = (params: ExtractRouteParams<EndpointType>) => {
151
- 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 });
152
153
  };
153
154
 
154
- public setData = (data: RequestDataType) => {
155
+ public setData = <D extends RequestDataType>(data: D) => {
155
156
  const modifiedData = this.dataMapper?.(data) || data;
156
- return this.clone<true, HasParams, HasQuery, MappedData>({
157
+ return this.clone<D extends null ? false : true, HasParams, HasQuery, MappedData>({
157
158
  data: modifiedData as CommandData<RequestDataType, MappedData>,
158
159
  });
159
160
  };
@@ -232,8 +233,8 @@ export class Command<
232
233
  return this.clone({ offline });
233
234
  };
234
235
 
235
- public setDataMapper = <DataMapper>(mapper: (data: RequestDataType) => DataMapper) => {
236
- return this.clone<HasData, HasParams, HasQuery, DataMapper>(undefined, mapper);
236
+ public setDataMapper = <DataMapper extends (data: RequestDataType) => any>(mapper: DataMapper) => {
237
+ return this.clone<HasData, HasParams, HasQuery>(undefined, mapper);
237
238
  };
238
239
 
239
240
  private paramsMapper = (
@@ -307,7 +308,7 @@ export class Command<
307
308
  D extends true | false = HasData,
308
309
  P extends true | false = HasParams,
309
310
  Q extends true | false = HasQuery,
310
- MapperData = MappedData,
311
+ NewMappedData = MappedData,
311
312
  >(
312
313
  options?: CommandCurrentType<
313
314
  ResponseType,
@@ -316,9 +317,9 @@ export class Command<
316
317
  GlobalErrorType | LocalErrorType,
317
318
  EndpointType,
318
319
  ClientOptions,
319
- MapperData
320
+ NewMappedData
320
321
  >,
321
- mapper?: (data: RequestDataType) => MapperData,
322
+ mapper?: CommandMapperType<RequestDataType, NewMappedData>,
322
323
  ): Command<
323
324
  ResponseType,
324
325
  RequestDataType,
@@ -330,7 +331,7 @@ export class Command<
330
331
  D,
331
332
  P,
332
333
  Q,
333
- MapperData
334
+ NewMappedData
334
335
  > {
335
336
  const dump = this.dump();
336
337
  const commandDump: CommandCurrentType<
@@ -340,7 +341,7 @@ export class Command<
340
341
  GlobalErrorType | LocalErrorType,
341
342
  EndpointType,
342
343
  ClientOptions,
343
- MapperData
344
+ NewMappedData
344
345
  > = {
345
346
  ...dump,
346
347
  ...options,
@@ -367,7 +368,7 @@ export class Command<
367
368
  D,
368
369
  P,
369
370
  Q,
370
- MapperData
371
+ NewMappedData
371
372
  >(this.builder, this.commandOptions, commandDump, mapper);
372
373
 
373
374
  return cloned;
@@ -490,7 +491,9 @@ export class Command<
490
491
  };
491
492
  }
492
493
 
493
- // Typescript test cases
494
+ // /**
495
+ // * Typescript test cases
496
+ // */
494
497
 
495
498
  // const builder = new Builder({
496
499
  // baseUrl: "http://localhost:3000",
@@ -516,6 +519,19 @@ export class Command<
516
519
  // endpoint: "/users/:id",
517
520
  // });
518
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
+
519
535
  // // OK
520
536
  // getUsers.send({ queryParams: "" });
521
537
  // getUsers.setQueryParams("").send();
@@ -524,6 +540,8 @@ export class Command<
524
540
  // getUsers.send({ params: "" });
525
541
  // getUsers.setQueryParams("").send({ queryParams: "" });
526
542
 
543
+ // // ================>
544
+
527
545
  // // OK
528
546
  // getUser.send({ params: { id: "" }, queryParams: "" });
529
547
  // getUser.setParams({ id: "" }).send({ queryParams: "" });
@@ -531,6 +549,10 @@ export class Command<
531
549
  // getUser.send({ queryParams: "" });
532
550
  // getUser.send();
533
551
  // getUser.setParams({ id: "" }).send({ params: { id: "" } });
552
+ // getUser.setParams(null).send();
553
+ // getUser.send({ params: { id: null } });
554
+
555
+ // // ================>
534
556
 
535
557
  // // OK
536
558
  // postUser.send({ data: { name: "" } });
@@ -538,15 +560,19 @@ export class Command<
538
560
  // // Fail
539
561
  // postUser.send({ queryParams: "" });
540
562
  // postUser.send({ data: null });
563
+ // postUser.setData(null).send();
541
564
  // postUser.send();
542
565
  // postUser.setData({ name: "" }).send({ data: { name: "" } });
543
566
 
567
+ // // ================>
568
+
544
569
  // // OK
545
570
  // patchUser.send({ params: { id: "" }, data: { name: "" } });
546
571
  // patchUser.setParams({ id: "" }).setData({ name: "" }).send();
547
572
  // // Fail
548
573
  // patchUser.send({ queryParams: "" });
549
574
  // patchUser.send({ data: null });
575
+ // patchUser.setData(null).send();
550
576
  // patchUser.send();
551
577
  // patchUser
552
578
  // .setParams({ id: "" })
@@ -556,3 +582,17 @@ export class Command<
556
582
  // .setParams({ id: "" })
557
583
  // .setData({ name: "" })
558
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: "" } });
@@ -161,8 +161,10 @@ export type CommandConfig<GenericEndpoint extends string, ClientOptions> = {
161
161
  deduplicateTime?: number;
162
162
  };
163
163
 
164
+ export type CommandMapperType<RequestDataType, MappedData> = (data: RequestDataType) => MappedData;
165
+
164
166
  export type CommandData<RequestDataType, MappedData> =
165
- | (MappedData extends undefined ? RequestDataType : MappedData)
167
+ | (MappedData extends never ? RequestDataType : MappedData)
166
168
  | NegativeTypes;
167
169
 
168
170
  export type CommandCurrentType<
@@ -205,7 +207,10 @@ export type FetchOptionsType<ClientOptions> = Omit<
205
207
  /**
206
208
  * It will check if the query params are already set
207
209
  */
208
- export type FetchQueryParamsType<QueryParamsType, HasQuery extends true | false = false> = HasQuery extends true
210
+ export type FetchQueryParamsType<
211
+ QueryParamsType extends ClientQueryParamsType | string,
212
+ HasQuery extends true | false = false,
213
+ > = HasQuery extends true
209
214
  ? { queryParams?: NegativeTypes }
210
215
  : {
211
216
  queryParams?: QueryParamsType | string;
@@ -216,24 +221,21 @@ export type FetchQueryParamsType<QueryParamsType, HasQuery extends true | false
216
221
  */
217
222
  export type FetchParamsType<
218
223
  EndpointType extends string,
219
- HasParams extends true | false = false,
224
+ HasParams extends true | false,
220
225
  > = ExtractRouteParams<EndpointType> extends NegativeTypes
221
226
  ? { params?: NegativeTypes }
222
227
  : true extends HasParams
223
228
  ? { params?: NegativeTypes }
224
- : { params: ExtractRouteParams<EndpointType> };
229
+ : { params: NonNullable<ExtractRouteParams<EndpointType>> };
225
230
 
226
231
  /**
227
232
  * If the command data is not filled it will throw an error
228
233
  */
229
- export type FetchRequestDataType<
230
- RequestDataType,
231
- HasData extends true | false = false,
232
- > = RequestDataType extends NegativeTypes
234
+ export type FetchRequestDataType<RequestDataType, HasData extends true | false> = RequestDataType extends NegativeTypes
233
235
  ? { data?: NegativeTypes }
234
236
  : HasData extends true
235
237
  ? { data?: NegativeTypes }
236
- : { data: RequestDataType };
238
+ : { data: NonNullable<RequestDataType> };
237
239
 
238
240
  export type CommandQueueOptions = {
239
241
  dispatcherType?: "auto" | "fetch" | "submit";
@@ -262,9 +264,7 @@ export type FetchSendActionsType<Command extends CommandInstance> = {
262
264
  onRemove?: (details: CommandEventDetails<Command>) => void;
263
265
  };
264
266
 
265
- export type FetchMethodType<Command extends CommandInstance> = FetchType<Command>["data"] extends any
266
- ? (options?: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>
267
- : FetchType<Command>["data"] extends NegativeTypes
267
+ export type FetchMethodType<Command extends CommandInstance> = FetchType<Command>["data"] extends NegativeTypes
268
268
  ? FetchType<Command>["params"] extends NegativeTypes
269
269
  ? (options?: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>
270
270
  : (options: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>
@@ -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> ? D : never;
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> ? D : never;
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> ? Q : never;
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> ? E : never;
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> ? E : never;
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> ? E : never;
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> ? D : never;
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> ? P : never;
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;