@hyper-fetch/core 7.0.1 → 7.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyper-fetch/core",
3
- "version": "7.0.1",
3
+ "version": "7.0.3",
4
4
  "description": "Cache, Queue and Persist your requests no matter if you are online or offline!",
5
5
  "author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
6
6
  "homepage": "https://hyperfetch.bettertyped.com/",
@@ -32,10 +32,10 @@
32
32
  "scripts": {
33
33
  "clean": "npx rimraf dist",
34
34
  "test": "jest --watchAll --maxWorkers=3 --forceExit",
35
- "test-pipeline": "jest --watchAll=false --maxWorkers=1 --no-cache --forceExit",
35
+ "test:pipeline": "jest --watchAll=false --maxWorkers=1 --no-cache --forceExit",
36
36
  "start": "yarn build && npx run-when-changed --watch 'src/**/*.ts' --exec 'yarn build'",
37
37
  "build": "yarn clean && node ../../scripts/build.js",
38
- "postbuild": "rollup -c '../../rollup.config.js'",
38
+ "postbuild": "yarn rollup -c ../../rollup.config.js",
39
39
  "lint": "eslint . --fix",
40
40
  "format": "prettier --write .",
41
41
  "typescheck": "tsc --noEmit --emitDeclarationOnly false",
@@ -401,11 +401,7 @@ export class Client<
401
401
  *
402
402
  * @template Response Your response
403
403
  */
404
- createRequest = <
405
- RequestProperties extends RequestGenericType<ExtractAdapterQueryParamsType<Adapter>> = {
406
- error?: Error;
407
- },
408
- >(
404
+ createRequest = <RequestProperties extends RequestGenericType<ExtractAdapterQueryParamsType<Adapter>> = {}>(
409
405
  /**
410
406
  * `createRequest` must be initialized twice(currying).
411
407
  *
@@ -425,14 +421,13 @@ export class Client<
425
421
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
426
422
  _USE_DOUBLE_INITIALIZATION?: never,
427
423
  ) => {
424
+ type DefaultQueryParams = ExtractAdapterDefaultQueryParamsType<Adapter>;
425
+
428
426
  type Response = TypeWithDefaults<RequestProperties, "response", undefined>;
429
427
  type Payload = TypeWithDefaults<RequestProperties, "payload", undefined>;
430
428
  type LocalError = TypeWithDefaults<RequestProperties, "error", GlobalErrorType>;
431
- type QueryParams = TypeWithDefaults<
432
- RequestProperties,
433
- "queryParams",
434
- ExtractAdapterDefaultQueryParamsType<Adapter>
435
- >;
429
+ /** we pass never to prevent the type from being empty, but we allow it to be undefined (optional) */
430
+ type QueryParams = TypeWithDefaults<RequestProperties, "queryParams", DefaultQueryParams, never>;
436
431
 
437
432
  return <
438
433
  EndpointType extends ExtractAdapterEndpointType<Adapter>,
@@ -49,7 +49,7 @@ export const handleResponse = (
49
49
  };
50
50
 
51
51
  export const parseErrorResponse = <T extends RequestInstance>(response: unknown): ExtractErrorType<T> => {
52
- return response ? parseResponse(response) : getErrorMessage();
52
+ return response ? parseResponse(response) : (getErrorMessage() as ExtractErrorType<T>);
53
53
  };
54
54
 
55
55
  // Stringify
@@ -12,7 +12,7 @@ import {
12
12
  RequestInstance,
13
13
  RequestMapper,
14
14
  ResponseMapper,
15
- ExtractRouteParams,
15
+ ExtractUrlParams,
16
16
  } from "request";
17
17
  import { ClientInstance } from "client";
18
18
  import { ResponseErrorType, ResponseSuccessType, ResponseType } from "adapter";
@@ -66,7 +66,7 @@ export class Request<
66
66
  headers?: HeadersInit;
67
67
  auth: boolean;
68
68
  method: ExtractAdapterMethodType<ExtractClientAdapterType<Client>>;
69
- params: ExtractRouteParams<Endpoint> | EmptyTypes;
69
+ params: ExtractUrlParams<Endpoint> | EmptyTypes;
70
70
  payload: PayloadType<Payload>;
71
71
  queryParams: QueryParams | EmptyTypes;
72
72
  options?: ExtractAdapterOptionsType<ExtractClientAdapterType<Client>> | undefined;
@@ -101,6 +101,10 @@ export class Request<
101
101
  /** @internal */
102
102
  unstable_responseMapper?: ResponseMapper<this, ResponseSuccessType<any, any> | ResponseErrorType<any, any>>;
103
103
 
104
+ unstable_hasParams: HasParams = false as HasParams;
105
+ unstable_hasPayload: HasPayload = false as HasPayload;
106
+ unstable_hasQuery: HasQuery = false as HasQuery;
107
+
104
108
  private updatedAbortKey: boolean;
105
109
  private updatedCacheKey: boolean;
106
110
  private updatedQueryKey: boolean;
@@ -115,7 +119,7 @@ export class Request<
115
119
  readonly initialRequestConfiguration?:
116
120
  | RequestConfigurationType<
117
121
  Payload,
118
- Endpoint extends string ? ExtractRouteParams<Endpoint> : never,
122
+ Endpoint extends string ? ExtractUrlParams<Endpoint> : never,
119
123
  QueryParams,
120
124
  Endpoint,
121
125
  ExtractAdapterOptionsType<ExtractClientAdapterType<Client>>,
@@ -445,7 +449,7 @@ export class Request<
445
449
  const json = this.toJSON();
446
450
  const initialRequestConfiguration: RequestConfigurationType<
447
451
  Payload,
448
- Endpoint extends string ? ExtractRouteParams<Endpoint> : never,
452
+ Endpoint extends string ? ExtractUrlParams<Endpoint> : never,
449
453
  QueryParams,
450
454
  Endpoint,
451
455
  ExtractAdapterOptionsType<ExtractClientAdapterType<Client>>,
@@ -462,7 +466,7 @@ export class Request<
462
466
  payload: configuration?.payload || this.payload,
463
467
  params: (configuration?.params || this.params) as
464
468
  | EmptyTypes
465
- | (Endpoint extends string ? ExtractRouteParams<Endpoint> : never),
469
+ | (Endpoint extends string ? ExtractUrlParams<Endpoint> : never),
466
470
  };
467
471
 
468
472
  const cloned = new Request<
@@ -479,7 +483,7 @@ export class Request<
479
483
 
480
484
  // Inherit methods
481
485
  cloned.unstable_payloadMapper = this.unstable_payloadMapper;
482
- cloned.unstable_responseMapper = this.unstable_responseMapper;
486
+ cloned.unstable_responseMapper = this.unstable_responseMapper as typeof cloned.unstable_responseMapper;
483
487
  cloned.unstable_requestMapper = this.unstable_requestMapper;
484
488
 
485
489
  cloned.unstable_mock = this.unstable_mock;
@@ -213,11 +213,11 @@ export type RequestConfigurationType<
213
213
  export type ParamType = string | number;
214
214
  export type ParamsType = Record<string, ParamType>;
215
215
 
216
- export type ExtractRouteParams<T extends string> = string extends T
216
+ export type ExtractUrlParams<T extends string> = string extends T
217
217
  ? EmptyTypes
218
218
  : // eslint-disable-next-line @typescript-eslint/no-unused-vars
219
219
  T extends `${string}:${infer Param}/${infer Rest}`
220
- ? { [k in Param | keyof ExtractRouteParams<Rest>]: ParamType }
220
+ ? { [k in Param | keyof ExtractUrlParams<Rest>]: ParamType }
221
221
  : // eslint-disable-next-line @typescript-eslint/no-unused-vars
222
222
  T extends `${string}:${infer Param}`
223
223
  ? { [k in Param]: ParamType }
@@ -23,6 +23,11 @@ export type TypeWithDefaults<
23
23
  Types extends Record<string, any>,
24
24
  Key extends keyof Types,
25
25
  Value,
26
- > = Key extends keyof Types ? Exclude<Types[Key], EmptyTypes | void | never> : Value;
26
+ ExcludedTypes = void | never,
27
+ > = Key extends keyof Types
28
+ ? Exclude<Types[Key], ExcludedTypes> extends never
29
+ ? Value
30
+ : Exclude<Types[Key], ExcludedTypes>
31
+ : Value;
27
32
 
28
33
  export type SyncOrAsync<T> = T | Promise<T>;
@@ -1,5 +1,5 @@
1
1
  import { ResponseType } from "adapter";
2
- import { ExtractRouteParams, Request, RequestInstance } from "request";
2
+ import { ExtractUrlParams, Request, RequestInstance } from "request";
3
3
  import { ExtractClientAdapterType, ExtractClientGlobalError } from "./client.types";
4
4
  import { Client, ClientInstance } from "client";
5
5
  import { TypeWithDefaults } from "./helpers.types";
@@ -28,7 +28,7 @@ export type ExtractLocalErrorType<T> = T extends Request<any, any, any, infer E,
28
28
  export type ExtractParamsType<T> =
29
29
  T extends Request<any, any, any, any, infer E, any, any, any, any>
30
30
  ? E extends string
31
- ? ExtractRouteParams<E>
31
+ ? ExtractUrlParams<E>
32
32
  : never
33
33
  : never;
34
34