@hyper-fetch/core 2.1.0 → 3.0.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.
Files changed (109) hide show
  1. package/README.md +1 -1
  2. package/dist/adapter/adapter.bindings.d.ts +25 -0
  3. package/dist/adapter/adapter.browser.d.ts +2 -0
  4. package/dist/{client/fetch.client.constants.d.ts → adapter/adapter.constants.d.ts} +0 -0
  5. package/dist/adapter/adapter.d.ts +2 -0
  6. package/dist/adapter/adapter.server.d.ts +2 -0
  7. package/dist/adapter/adapter.types.d.ts +57 -0
  8. package/dist/{client/fetch.client.utils.d.ts → adapter/adapter.utils.d.ts} +3 -3
  9. package/dist/adapter/index.d.ts +7 -0
  10. package/dist/cache/cache.d.ts +9 -9
  11. package/dist/cache/cache.types.d.ts +4 -4
  12. package/dist/cache/cache.utils.d.ts +3 -3
  13. package/dist/{builder/builder.constants.d.ts → client/client.constants.d.ts} +0 -0
  14. package/dist/client/client.d.ts +142 -0
  15. package/dist/client/client.types.d.ts +41 -0
  16. package/dist/client/client.utils.d.ts +10 -0
  17. package/dist/client/index.d.ts +4 -6
  18. package/dist/dispatcher/dispatcher.d.ts +23 -23
  19. package/dist/dispatcher/dispatcher.events.d.ts +8 -8
  20. package/dist/dispatcher/dispatcher.types.d.ts +10 -10
  21. package/dist/dispatcher/dispatcher.utils.d.ts +5 -5
  22. package/dist/effect/index.d.ts +2 -2
  23. package/dist/effect/request.effect.d.ts +14 -0
  24. package/dist/effect/request.effect.types.d.ts +32 -0
  25. package/dist/index.cjs.js +1 -1
  26. package/dist/index.cjs.js.map +4 -4
  27. package/dist/index.d.ts +3 -3
  28. package/dist/index.esm.js +1 -1
  29. package/dist/index.esm.js.map +4 -4
  30. package/dist/managers/index.d.ts +1 -1
  31. package/dist/managers/logger/logger.manager.d.ts +5 -5
  32. package/dist/managers/logger/logger.manager.types.d.ts +7 -7
  33. package/dist/managers/request/index.d.ts +4 -0
  34. package/dist/managers/request/request.manager.d.ts +43 -0
  35. package/dist/managers/request/request.manager.events.d.ts +36 -0
  36. package/dist/managers/{command/command.manager.types.d.ts → request/request.manager.types.d.ts} +6 -6
  37. package/dist/managers/{command/command.manager.utils.d.ts → request/request.manager.utils.d.ts} +0 -0
  38. package/dist/request/index.d.ts +3 -0
  39. package/dist/request/request.d.ts +98 -0
  40. package/dist/request/request.types.d.ts +202 -0
  41. package/dist/request/request.utils.d.ts +22 -0
  42. package/dist/types/fetch.types.d.ts +16 -16
  43. package/package.json +1 -1
  44. package/src/{client/fetch.client.bindings.ts → adapter/adapter.bindings.ts} +58 -58
  45. package/src/{client/fetch.client.browser.ts → adapter/adapter.browser.ts} +6 -6
  46. package/src/{client/fetch.client.constants.ts → adapter/adapter.constants.ts} +0 -0
  47. package/src/{client/fetch.client.server.ts → adapter/adapter.server.ts} +22 -18
  48. package/src/adapter/adapter.ts +8 -0
  49. package/src/adapter/adapter.types.ts +79 -0
  50. package/src/{client/fetch.client.utils.ts → adapter/adapter.utils.ts} +3 -3
  51. package/src/adapter/index.ts +7 -0
  52. package/src/cache/cache.ts +19 -19
  53. package/src/cache/cache.types.ts +4 -4
  54. package/src/cache/cache.utils.ts +6 -6
  55. package/src/{builder/builder.constants.ts → client/client.constants.ts} +0 -0
  56. package/src/client/client.ts +310 -0
  57. package/src/client/client.types.ts +52 -0
  58. package/src/{builder/builder.utils.ts → client/client.utils.ts} +34 -31
  59. package/src/client/index.ts +4 -6
  60. package/src/dispatcher/dispatcher.events.ts +11 -11
  61. package/src/dispatcher/dispatcher.ts +68 -68
  62. package/src/dispatcher/dispatcher.types.ts +10 -10
  63. package/src/dispatcher/dispatcher.utils.ts +7 -7
  64. package/src/effect/index.ts +2 -2
  65. package/src/effect/request.effect.ts +28 -0
  66. package/src/effect/request.effect.types.ts +35 -0
  67. package/src/index.ts +4 -4
  68. package/src/managers/index.ts +1 -1
  69. package/src/managers/logger/logger.manager.ts +8 -8
  70. package/src/managers/logger/logger.manager.types.ts +7 -7
  71. package/src/managers/request/index.ts +4 -0
  72. package/src/managers/{command/command.manager.events.ts → request/request.manager.events.ts} +47 -51
  73. package/src/managers/{command/command.manager.ts → request/request.manager.ts} +5 -5
  74. package/src/managers/request/request.manager.types.ts +25 -0
  75. package/src/managers/{command/command.manager.utils.ts → request/request.manager.utils.ts} +0 -0
  76. package/src/request/index.ts +3 -0
  77. package/src/{command/command.ts → request/request.ts} +188 -194
  78. package/src/request/request.types.ts +282 -0
  79. package/src/{command/command.utils.ts → request/request.utils.ts} +56 -53
  80. package/src/types/fetch.types.ts +18 -18
  81. package/dist/builder/builder.d.ts +0 -141
  82. package/dist/builder/builder.types.d.ts +0 -41
  83. package/dist/builder/builder.utils.d.ts +0 -10
  84. package/dist/builder/index.d.ts +0 -4
  85. package/dist/client/fetch.client.bindings.d.ts +0 -25
  86. package/dist/client/fetch.client.browser.d.ts +0 -2
  87. package/dist/client/fetch.client.d.ts +0 -2
  88. package/dist/client/fetch.client.server.d.ts +0 -2
  89. package/dist/client/fetch.client.types.d.ts +0 -57
  90. package/dist/command/command.d.ts +0 -98
  91. package/dist/command/command.types.d.ts +0 -202
  92. package/dist/command/command.utils.d.ts +0 -22
  93. package/dist/command/index.d.ts +0 -3
  94. package/dist/effect/fetch.effect.d.ts +0 -14
  95. package/dist/effect/fetch.effect.types.d.ts +0 -32
  96. package/dist/managers/command/command.manager.d.ts +0 -43
  97. package/dist/managers/command/command.manager.events.d.ts +0 -36
  98. package/dist/managers/command/index.d.ts +0 -4
  99. package/src/builder/builder.ts +0 -308
  100. package/src/builder/builder.types.ts +0 -53
  101. package/src/builder/index.ts +0 -4
  102. package/src/client/fetch.client.ts +0 -9
  103. package/src/client/fetch.client.types.ts +0 -83
  104. package/src/command/command.types.ts +0 -273
  105. package/src/command/index.ts +0 -3
  106. package/src/effect/fetch.effect.ts +0 -28
  107. package/src/effect/fetch.effect.types.ts +0 -35
  108. package/src/managers/command/command.manager.types.ts +0 -25
  109. package/src/managers/command/index.ts +0 -4
@@ -1,3 +1,3 @@
1
1
  export * from "./app";
2
- export * from "./command";
2
+ export * from "./request";
3
3
  export * from "./logger";
@@ -1,19 +1,19 @@
1
1
  /// <reference types="node" />
2
2
  import EventEmitter from "events";
3
- import { BuilderInstance } from "builder";
3
+ import { ClientInstance } from "client";
4
4
  import { LoggerType, SeverityType, LoggerOptionsType, LoggerFunctionType } from "managers";
5
5
  /**
6
- * This class is used across the Hyper Fetch library to provide unified logging system with necessary setup per each builder.
6
+ * This class is used across the Hyper Fetch library to provide unified logging system with necessary setup per each client.
7
7
  * We can set up the logging level based on available values. This manager enable to initialize the logging instance per individual module
8
- * like Builder, Command etc. Which can give you better feedback on the logging itself.
8
+ * like Client, Request etc. Which can give you better feedback on the logging itself.
9
9
  */
10
10
  export declare class LoggerManager {
11
- private builder;
11
+ private client;
12
12
  private options?;
13
13
  logger: LoggerFunctionType;
14
14
  severity: SeverityType;
15
15
  emitter: EventEmitter;
16
- constructor(builder: Pick<BuilderInstance, "debug">, options?: LoggerOptionsType);
16
+ constructor(client: Pick<ClientInstance, "debug">, options?: LoggerOptionsType);
17
17
  setSeverity: (severity: SeverityType) => void;
18
18
  init: (module: string) => LoggerType;
19
19
  }
@@ -1,6 +1,6 @@
1
- import { ClientResponseType } from "client";
2
- import { CommandInstance } from "command";
3
- import { CommandResponseDetails } from "managers/command";
1
+ import { ResponseType } from "adapter";
2
+ import { RequestInstance } from "request";
3
+ import { ResponseDetailsType } from "managers/request";
4
4
  export type SeverityType = 0 | 1 | 2 | 3;
5
5
  export type LoggerType = Record<LoggerLevelType, (message: LoggerMessageType, ...additionalData: LoggerMessageType[]) => void>;
6
6
  export type LoggerFunctionType = (log: LogType) => void;
@@ -20,11 +20,11 @@ export type LoggerLevelType = "error" | "warning" | "info" | "debug";
20
20
  export type LoggerMessageType = string | Record<string, unknown> | Array<unknown>;
21
21
  export type LoggerRequestEventData = {
22
22
  requestId: string;
23
- command: CommandInstance;
23
+ request: RequestInstance;
24
24
  };
25
25
  export type LoggerResponseEventData = {
26
26
  requestId: string;
27
- command: CommandInstance;
28
- response: ClientResponseType<unknown, unknown>;
29
- details: CommandResponseDetails;
27
+ request: RequestInstance;
28
+ response: ResponseType<unknown, unknown>;
29
+ details: ResponseDetailsType;
30
30
  };
@@ -0,0 +1,4 @@
1
+ export * from "./request.manager";
2
+ export * from "./request.manager.utils";
3
+ export * from "./request.manager.events";
4
+ export * from "./request.manager.types";
@@ -0,0 +1,43 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from "events";
3
+ /**
4
+ * **Request Manager** is used to emit `request lifecycle events` like - request start, request end, upload and download progress.
5
+ * It is also the place of `request aborting` system, here we store all the keys and controllers that are isolated for each client instance.
6
+ */
7
+ export declare class RequestManager {
8
+ emitter: EventEmitter;
9
+ events: {
10
+ emitLoading: (queueKey: string, requestId: string, values: import("managers").RequestLoadingEventType) => void;
11
+ emitRequestStart: (queueKey: string, requestId: string, details: import("managers").RequestEventType<import("../..").RequestInstance>) => void;
12
+ emitResponseStart: (queueKey: string, requestId: string, details: import("managers").RequestEventType<import("../..").RequestInstance>) => void;
13
+ emitUploadProgress: (queueKey: string, requestId: string, values: import("../..").ProgressType, details: import("managers").RequestEventType<import("../..").RequestInstance>) => void;
14
+ emitDownloadProgress: (queueKey: string, requestId: string, values: import("../..").ProgressType, details: import("managers").RequestEventType<import("../..").RequestInstance>) => void;
15
+ emitResponse: (cacheKey: string, requestId: string, response: import("../..").ResponseType<unknown, unknown>, details: import("managers").ResponseDetailsType) => void;
16
+ emitAbort: (abortKey: string, requestId: string, request: import("../..").RequestInstance) => void;
17
+ emitRemove: <T extends import("../..").RequestInstance>(queueKey: string, requestId: string, details: import("managers").RequestEventType<T>) => void;
18
+ onLoading: (queueKey: string, callback: (values: import("managers").RequestLoadingEventType) => void) => VoidFunction;
19
+ onLoadingById: (requestId: string, callback: (values: import("managers").RequestLoadingEventType) => void) => VoidFunction;
20
+ onRequestStart: <T_1 extends import("../..").RequestInstance>(queueKey: string, callback: (details: import("managers").RequestEventType<T_1>) => void) => VoidFunction;
21
+ onRequestStartById: <T_2 extends import("../..").RequestInstance>(requestId: string, callback: (details: import("managers").RequestEventType<T_2>) => void) => VoidFunction;
22
+ onResponseStart: <T_3 extends import("../..").RequestInstance>(queueKey: string, callback: (details: import("managers").RequestEventType<T_3>) => void) => VoidFunction;
23
+ onResponseStartById: <T_4 extends import("../..").RequestInstance>(requestId: string, callback: (details: import("managers").RequestEventType<T_4>) => void) => VoidFunction;
24
+ onUploadProgress: <T_5 extends import("../..").RequestInstance = import("../..").RequestInstance>(queueKey: string, callback: (values: import("../..").ProgressType, details: import("managers").RequestEventType<T_5>) => void) => VoidFunction;
25
+ onUploadProgressById: <T_6 extends import("../..").RequestInstance = import("../..").RequestInstance>(requestId: string, callback: (values: import("../..").ProgressType, details: import("managers").RequestEventType<T_6>) => void) => VoidFunction;
26
+ onDownloadProgress: <T_7 extends import("../..").RequestInstance = import("../..").RequestInstance>(queueKey: string, callback: (values: import("../..").ProgressType, details: import("managers").RequestEventType<T_7>) => void) => VoidFunction;
27
+ onDownloadProgressById: <T_8 extends import("../..").RequestInstance = import("../..").RequestInstance>(requestId: string, callback: (values: import("../..").ProgressType, details: import("managers").RequestEventType<T_8>) => void) => VoidFunction;
28
+ onResponse: <Response_1, ErrorType>(cacheKey: string, callback: (response: import("../..").ResponseType<Response_1, ErrorType>, details: import("managers").ResponseDetailsType) => void) => VoidFunction;
29
+ onResponseById: <Response_2, ErrorType_1>(requestId: string, callback: (response: import("../..").ResponseType<Response_2, ErrorType_1>, details: import("managers").ResponseDetailsType) => void) => VoidFunction;
30
+ onAbort: (abortKey: string, callback: (request: import("../..").RequestInstance) => void) => VoidFunction;
31
+ onAbortById: (requestId: string, callback: (request: import("../..").RequestInstance) => void) => VoidFunction;
32
+ onRemove: <T_9 extends import("../..").RequestInstance = import("../..").RequestInstance>(queueKey: string, callback: (details: import("managers").RequestEventType<T_9>) => void) => VoidFunction;
33
+ onRemoveById: <T_10 extends import("../..").RequestInstance = import("../..").RequestInstance>(requestId: string, callback: (details: import("managers").RequestEventType<T_10>) => void) => VoidFunction;
34
+ };
35
+ abortControllers: Map<string, Map<string, AbortController>>;
36
+ addAbortController: (abortKey: string, requestId: string) => void;
37
+ getAbortController: (abortKey: string, requestId: string) => AbortController;
38
+ removeAbortController: (abortKey: string, requestId: string) => void;
39
+ useAbortController: (abortKey: string, requestId: string) => void;
40
+ abortByKey: (abortKey: string) => void;
41
+ abortByRequestId: (abortKey: string, requestId: string) => void;
42
+ abortAll: () => void;
43
+ }
@@ -0,0 +1,36 @@
1
+ import EventEmitter from "events";
2
+ import { RequestEventType, ResponseDetailsType, RequestLoadingEventType } from "managers";
3
+ import { ProgressType, ResponseType } from "adapter";
4
+ import { RequestInstance } from "request";
5
+ export declare const getRequestManagerEvents: (emitter: EventEmitter) => {
6
+ /**
7
+ * Emiter
8
+ */
9
+ emitLoading: (queueKey: string, requestId: string, values: RequestLoadingEventType) => void;
10
+ emitRequestStart: (queueKey: string, requestId: string, details: RequestEventType<RequestInstance>) => void;
11
+ emitResponseStart: (queueKey: string, requestId: string, details: RequestEventType<RequestInstance>) => void;
12
+ emitUploadProgress: (queueKey: string, requestId: string, values: ProgressType, details: RequestEventType<RequestInstance>) => void;
13
+ emitDownloadProgress: (queueKey: string, requestId: string, values: ProgressType, details: RequestEventType<RequestInstance>) => void;
14
+ emitResponse: (cacheKey: string, requestId: string, response: ResponseType<unknown, unknown>, details: ResponseDetailsType) => void;
15
+ emitAbort: (abortKey: string, requestId: string, request: RequestInstance) => void;
16
+ emitRemove: <T extends RequestInstance>(queueKey: string, requestId: string, details: RequestEventType<T>) => void;
17
+ /**
18
+ * Listeners
19
+ */
20
+ onLoading: (queueKey: string, callback: (values: RequestLoadingEventType) => void) => VoidFunction;
21
+ onLoadingById: (requestId: string, callback: (values: RequestLoadingEventType) => void) => VoidFunction;
22
+ onRequestStart: <T_1 extends RequestInstance>(queueKey: string, callback: (details: RequestEventType<T_1>) => void) => VoidFunction;
23
+ onRequestStartById: <T_2 extends RequestInstance>(requestId: string, callback: (details: RequestEventType<T_2>) => void) => VoidFunction;
24
+ onResponseStart: <T_3 extends RequestInstance>(queueKey: string, callback: (details: RequestEventType<T_3>) => void) => VoidFunction;
25
+ onResponseStartById: <T_4 extends RequestInstance>(requestId: string, callback: (details: RequestEventType<T_4>) => void) => VoidFunction;
26
+ onUploadProgress: <T_5 extends RequestInstance = RequestInstance>(queueKey: string, callback: (values: ProgressType, details: RequestEventType<T_5>) => void) => VoidFunction;
27
+ onUploadProgressById: <T_6 extends RequestInstance = RequestInstance>(requestId: string, callback: (values: ProgressType, details: RequestEventType<T_6>) => void) => VoidFunction;
28
+ onDownloadProgress: <T_7 extends RequestInstance = RequestInstance>(queueKey: string, callback: (values: ProgressType, details: RequestEventType<T_7>) => void) => VoidFunction;
29
+ onDownloadProgressById: <T_8 extends RequestInstance = RequestInstance>(requestId: string, callback: (values: ProgressType, details: RequestEventType<T_8>) => void) => VoidFunction;
30
+ onResponse: <Response_1, ErrorType>(cacheKey: string, callback: (response: ResponseType<Response_1, ErrorType>, details: ResponseDetailsType) => void) => VoidFunction;
31
+ onResponseById: <Response_2, ErrorType_1>(requestId: string, callback: (response: ResponseType<Response_2, ErrorType_1>, details: ResponseDetailsType) => void) => VoidFunction;
32
+ onAbort: (abortKey: string, callback: (request: RequestInstance) => void) => VoidFunction;
33
+ onAbortById: (requestId: string, callback: (request: RequestInstance) => void) => VoidFunction;
34
+ onRemove: <T_9 extends RequestInstance = RequestInstance>(queueKey: string, callback: (details: RequestEventType<T_9>) => void) => VoidFunction;
35
+ onRemoveById: <T_10 extends RequestInstance = RequestInstance>(requestId: string, callback: (details: RequestEventType<T_10>) => void) => VoidFunction;
36
+ };
@@ -1,23 +1,23 @@
1
- import { CommandInstance } from "command";
2
- export type CommandLoadingEventType = {
1
+ import { RequestInstance } from "request";
2
+ export type RequestLoadingEventType = {
3
3
  queueKey: string;
4
4
  requestId: string;
5
5
  loading: boolean;
6
6
  isRetry: boolean;
7
7
  isOffline: boolean;
8
8
  };
9
- export type CommandEventDetails<T extends CommandInstance> = {
9
+ export type RequestEventType<T extends RequestInstance> = {
10
10
  requestId: string;
11
- command: T;
11
+ request: T;
12
12
  };
13
- export type CommandResponseDetails = {
13
+ export type ResponseDetailsType = {
14
14
  retries: number;
15
15
  timestamp: number;
16
16
  isFailed: boolean;
17
17
  isCanceled: boolean;
18
18
  isOffline: boolean;
19
19
  };
20
- export type CommandRemoveDetails = {
20
+ export type RequestRemoveDataType = {
21
21
  queueKey: string;
22
22
  requestId: string;
23
23
  };
@@ -0,0 +1,3 @@
1
+ export * from "./request";
2
+ export * from "./request.types";
3
+ export * from "./request.utils";
@@ -0,0 +1,98 @@
1
+ import { RequestSendType, PayloadType, RequestDump, RequestOptionsType, ExtractRouteParams, RequestCurrentType, PayloadMapperType } from "request";
2
+ import { Client } from "client";
3
+ import { QueryParamsType } from "adapter";
4
+ import { HttpMethodsType, NegativeTypes } from "types";
5
+ /**
6
+ * Fetch request it is designed to prepare the necessary setup to execute the request to the server.
7
+ * We can setup basic options for example endpoint, method, headers and advanced settings like cache, invalidation patterns, concurrency, retries and much, much more.
8
+ * :::info Usage
9
+ * We should not use this class directly in the standard development flow. We can initialize it using the `createRequest` method on the **Client** class.
10
+ * :::
11
+ *
12
+ * @attention
13
+ * The most important thing about the request is that it keeps data in the format that can be dumped. This is necessary for the persistance and different dispatcher storage types.
14
+ * This class doesn't have any callback methods by design and communicate with dispatcher and cache by events.
15
+ */
16
+ export declare class Request<Response, Payload, QueryParams extends QueryParamsType | string, GlobalError, // Global Error Type
17
+ LocalError, // Additional Error for specific endpoint
18
+ Endpoint extends string, AdapterOptions, HasData extends true | false = false, HasParams extends true | false = false, HasQuery extends true | false = false, MappedData = undefined> {
19
+ readonly client: Client<GlobalError, AdapterOptions>;
20
+ readonly requestOptions: RequestOptionsType<Endpoint, AdapterOptions>;
21
+ readonly requestDump?: RequestCurrentType<Response, Payload, QueryParams, GlobalError | LocalError, Endpoint, AdapterOptions, MappedData> | undefined;
22
+ readonly dataMapper?: PayloadMapperType<Payload, MappedData>;
23
+ endpoint: Endpoint;
24
+ headers?: HeadersInit;
25
+ auth: boolean;
26
+ method: HttpMethodsType;
27
+ params: ExtractRouteParams<Endpoint> | NegativeTypes;
28
+ data: PayloadType<Payload, MappedData>;
29
+ queryParams: QueryParams | NegativeTypes;
30
+ options?: AdapterOptions | undefined;
31
+ cancelable: boolean;
32
+ retry: number;
33
+ retryTime: number;
34
+ garbageCollection: number;
35
+ cache: boolean;
36
+ cacheTime: number;
37
+ queued: boolean;
38
+ offline: boolean;
39
+ abortKey: string;
40
+ cacheKey: string;
41
+ queueKey: string;
42
+ effectKey: string;
43
+ used: boolean;
44
+ deduplicate: boolean;
45
+ deduplicateTime: number;
46
+ private updatedAbortKey;
47
+ private updatedCacheKey;
48
+ private updatedQueueKey;
49
+ private updatedEffectKey;
50
+ constructor(client: Client<GlobalError, AdapterOptions>, requestOptions: RequestOptionsType<Endpoint, AdapterOptions>, requestDump?: RequestCurrentType<Response, Payload, QueryParams, GlobalError | LocalError, Endpoint, AdapterOptions, MappedData> | undefined, dataMapper?: PayloadMapperType<Payload, MappedData>);
51
+ setHeaders: (headers: HeadersInit) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
52
+ setAuth: (auth: boolean) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
53
+ setParams: <P extends ExtractRouteParams<Endpoint>>(params: P) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, P extends null ? false : true, HasQuery, MappedData>;
54
+ setData: <D extends Payload>(data: D) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, D extends null ? false : true, HasParams, HasQuery, MappedData>;
55
+ setQueryParams: (queryParams: QueryParams) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, true, MappedData>;
56
+ setOptions: (options: AdapterOptions) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, true, MappedData>;
57
+ setCancelable: (cancelable: boolean) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
58
+ setRetry: (retry: RequestOptionsType<Endpoint, AdapterOptions>["retry"]) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
59
+ setRetryTime: (retryTime: RequestOptionsType<Endpoint, AdapterOptions>["retryTime"]) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
60
+ setGarbageCollection: (garbageCollection: RequestOptionsType<Endpoint, AdapterOptions>["garbageCollection"]) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
61
+ setCache: (cache: RequestOptionsType<Endpoint, AdapterOptions>["cache"]) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
62
+ setCacheTime: (cacheTime: RequestOptionsType<Endpoint, AdapterOptions>["cacheTime"]) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
63
+ setQueued: (queued: boolean) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
64
+ setAbortKey: (abortKey: string) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
65
+ setCacheKey: (cacheKey: string) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
66
+ setQueueKey: (queueKey: string) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
67
+ setEffectKey: (effectKey: string) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
68
+ setDeduplicate: (deduplicate: boolean) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
69
+ setDeduplicateTime: (deduplicateTime: number) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
70
+ setUsed: (used: boolean) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
71
+ setOffline: (offline: boolean) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
72
+ setDataMapper: <DataMapper extends (data: Payload) => any>(mapper: DataMapper) => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
73
+ private paramsMapper;
74
+ dump(): RequestDump<Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>, AdapterOptions, QueryParams>;
75
+ clone<D extends true | false = HasData, P extends true | false = HasParams, Q extends true | false = HasQuery, NewMappedData = MappedData>(options?: RequestCurrentType<Response, Payload, QueryParams, GlobalError | LocalError, Endpoint, AdapterOptions, NewMappedData>, mapper?: PayloadMapperType<Payload, NewMappedData>): Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, D, P, Q, NewMappedData>;
76
+ abort: () => Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>;
77
+ /**
78
+ * Method to use the request WITHOUT adding it to cache and queues. This mean it will make simple request without queue side effects.
79
+ * @param options
80
+ * @disableReturns
81
+ * @returns
82
+ * ```tsx
83
+ * Promise<[Data | null, Error | null, HttpStatus]>
84
+ * ```
85
+ */
86
+ exec: RequestSendType<Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>>;
87
+ /**
88
+ * Method used to perform requests with usage of cache and queues
89
+ * @param options
90
+ * @param requestCallback
91
+ * @disableReturns
92
+ * @returns
93
+ * ```tsx
94
+ * Promise<[Data | null, Error | null, HttpStatus]>
95
+ * ```
96
+ */
97
+ send: RequestSendType<Request<Response, Payload, QueryParams, GlobalError, LocalError, Endpoint, AdapterOptions, HasData, HasParams, HasQuery, MappedData>>;
98
+ }
@@ -0,0 +1,202 @@
1
+ import { NullableKeys, NegativeTypes, ExtractParamsType, ExtractPayloadType, HttpMethodsType, ExtractQueryParamsType, ExtractAdapterOptionsType, ExtractEndpointType, ExtractHasDataType, ExtractHasParamsType, ExtractHasQueryParamsType, ExtractErrorType, ExtractResponseType } from "types";
2
+ import { Request } from "request";
3
+ import { ResponseType, QueryParamsType, ProgressType } from "adapter";
4
+ import { RequestEventType, ResponseDetailsType } from "managers";
5
+ export type AdapterProgressEventType = {
6
+ total: number;
7
+ loaded: number;
8
+ };
9
+ export type AdapterProgressType = {
10
+ progress: number;
11
+ timeLeft: number;
12
+ sizeLeft: number;
13
+ };
14
+ /**
15
+ * Dump of the request used to later recreate it
16
+ */
17
+ export type RequestDump<Request extends RequestInstance, AdapterOptions = unknown, QueryParams = QueryParamsType, Params = ExtractParamsType<Request>> = {
18
+ requestOptions: RequestOptionsType<string, AdapterOptions | ExtractAdapterOptionsType<Request>>;
19
+ endpoint: string;
20
+ method: HttpMethodsType;
21
+ headers?: HeadersInit;
22
+ auth: boolean;
23
+ cancelable: boolean;
24
+ retry: number;
25
+ retryTime: number;
26
+ garbageCollection: number;
27
+ cache: boolean;
28
+ cacheTime: number;
29
+ queued: boolean;
30
+ offline: boolean;
31
+ disableResponseInterceptors: boolean | undefined;
32
+ disableRequestInterceptors: boolean | undefined;
33
+ options?: AdapterOptions | ExtractAdapterOptionsType<Request>;
34
+ data: PayloadType<ExtractPayloadType<Request>, unknown>;
35
+ params: Params | NegativeTypes;
36
+ queryParams: QueryParams | NegativeTypes;
37
+ abortKey: string;
38
+ cacheKey: string;
39
+ queueKey: string;
40
+ effectKey: string;
41
+ used: boolean;
42
+ updatedAbortKey: boolean;
43
+ updatedCacheKey: boolean;
44
+ updatedQueueKey: boolean;
45
+ updatedEffectKey: boolean;
46
+ deduplicate: boolean;
47
+ deduplicateTime: number;
48
+ };
49
+ /**
50
+ * Configuration options for request creation
51
+ */
52
+ export type RequestOptionsType<GenericEndpoint extends string, AdapterOptions> = {
53
+ /**
54
+ * Determine the endpoint for request request
55
+ */
56
+ endpoint: GenericEndpoint;
57
+ /**
58
+ * Custom headers for request
59
+ */
60
+ headers?: HeadersInit;
61
+ /**
62
+ * Should the onAuth method get called on this request
63
+ */
64
+ auth?: boolean;
65
+ /**
66
+ * Request method GET | POST | PATCH | PUT | DELETE
67
+ */
68
+ method?: HttpMethodsType;
69
+ /**
70
+ * Should enable cancelable mode in the Dispatcher
71
+ */
72
+ cancelable?: boolean;
73
+ /**
74
+ * Retry count when request is failed
75
+ */
76
+ retry?: number;
77
+ /**
78
+ * Retry time delay between retries
79
+ */
80
+ retryTime?: number;
81
+ /**
82
+ * Should we trigger garbage collection or leave data in memory
83
+ */
84
+ garbageCollection?: number;
85
+ /**
86
+ * Should we save the response to cache
87
+ */
88
+ cache?: boolean;
89
+ /**
90
+ * Time for which the cache is considered up-to-date
91
+ */
92
+ cacheTime?: number;
93
+ /**
94
+ * Should the requests from this request be send one-by-one
95
+ */
96
+ queued?: boolean;
97
+ /**
98
+ * Do we want to store request made in offline mode for latter use when we go back online
99
+ */
100
+ offline?: boolean;
101
+ /**
102
+ * Disable post-request interceptors
103
+ */
104
+ disableResponseInterceptors?: boolean;
105
+ /**
106
+ * Disable pre-request interceptors
107
+ */
108
+ disableRequestInterceptors?: boolean;
109
+ /**
110
+ * Additional options for your adapter, by default XHR options
111
+ */
112
+ options?: AdapterOptions;
113
+ /**
114
+ * Key which will be used to cancel requests. Autogenerated by default.
115
+ */
116
+ abortKey?: string;
117
+ /**
118
+ * Key which will be used to cache requests. Autogenerated by default.
119
+ */
120
+ cacheKey?: string;
121
+ /**
122
+ * Key which will be used to queue requests. Autogenerated by default.
123
+ */
124
+ queueKey?: string;
125
+ /**
126
+ * Key which will be used to use effects. Autogenerated by default.
127
+ */
128
+ effectKey?: string;
129
+ /**
130
+ * Should we deduplicate two requests made at the same time into one
131
+ */
132
+ deduplicate?: boolean;
133
+ /**
134
+ * Time of pooling for the deduplication to be active (default 10ms)
135
+ */
136
+ deduplicateTime?: number;
137
+ };
138
+ export type PayloadMapperType<PayloadType, MappedData> = (data: PayloadType) => MappedData;
139
+ export type PayloadType<Payload, MappedData> = (MappedData extends never ? Payload : MappedData) | NegativeTypes;
140
+ export type RequestCurrentType<Response, Payload, QueryParams, ErrorType, GenericEndpoint extends string, AdapterOptions, MappedData> = {
141
+ used?: boolean;
142
+ params?: ExtractRouteParams<GenericEndpoint> | NegativeTypes;
143
+ queryParams?: QueryParams | NegativeTypes;
144
+ data?: PayloadType<Payload, MappedData>;
145
+ mockCallback?: ((data: Payload) => ResponseType<Response, ErrorType>) | undefined;
146
+ headers?: HeadersInit;
147
+ updatedAbortKey?: boolean;
148
+ updatedCacheKey?: boolean;
149
+ updatedQueueKey?: boolean;
150
+ updatedEffectKey?: boolean;
151
+ } & Partial<NullableKeys<RequestOptionsType<GenericEndpoint, AdapterOptions>>>;
152
+ export type ParamType = string | number;
153
+ export type ParamsType = Record<string, ParamType>;
154
+ export type ExtractRouteParams<T extends string> = string extends T ? NegativeTypes : T extends `${string}:${infer Param}/${infer Rest}` ? {
155
+ [k in Param | keyof ExtractRouteParams<Rest>]: ParamType;
156
+ } : T extends `${string}:${infer Param}` ? {
157
+ [k in Param]: ParamType;
158
+ } : NegativeTypes;
159
+ export type FetchOptionsType<AdapterOptions> = Omit<Partial<RequestOptionsType<string, AdapterOptions>>, "endpoint" | "method">;
160
+ /**
161
+ * It will check if the query params are already set
162
+ */
163
+ export type FetchQueryParamsType<QueryParams extends QueryParamsType | string, HasQuery extends true | false = false> = HasQuery extends true ? {
164
+ queryParams?: NegativeTypes;
165
+ } : {
166
+ queryParams?: QueryParams | string;
167
+ };
168
+ /**
169
+ * If the request endpoint parameters are not filled it will throw an error
170
+ */
171
+ export type FetchParamsType<Endpoint extends string, HasParams extends true | false> = ExtractRouteParams<Endpoint> extends NegativeTypes ? {
172
+ params?: NegativeTypes;
173
+ } : true extends HasParams ? {
174
+ params?: NegativeTypes;
175
+ } : {
176
+ params: NonNullable<ExtractRouteParams<Endpoint>>;
177
+ };
178
+ /**
179
+ * If the request data is not filled it will throw an error
180
+ */
181
+ export type FetchPayloadType<Payload, HasData extends true | false> = Payload extends NegativeTypes ? {
182
+ data?: NegativeTypes;
183
+ } : HasData extends true ? {
184
+ data?: NegativeTypes;
185
+ } : {
186
+ data: NonNullable<Payload>;
187
+ };
188
+ export type RequestQueueOptions = {
189
+ dispatcherType?: "auto" | "fetch" | "submit";
190
+ };
191
+ export type RequestSendOptionsType<Request extends RequestInstance> = FetchQueryParamsType<ExtractQueryParamsType<Request>, ExtractHasQueryParamsType<Request>> & FetchParamsType<ExtractEndpointType<Request>, ExtractHasParamsType<Request>> & FetchPayloadType<ExtractPayloadType<Request>, ExtractHasDataType<Request>> & Omit<FetchOptionsType<ExtractAdapterOptionsType<Request>>, "params" | "data"> & FetchSendActionsType<Request> & RequestQueueOptions;
192
+ export type FetchSendActionsType<Request extends RequestInstance> = {
193
+ onSettle?: (requestId: string, request: Request) => void;
194
+ onRequestStart?: (details: RequestEventType<Request>) => void;
195
+ onResponseStart?: (details: RequestEventType<Request>) => void;
196
+ onUploadProgress?: (values: ProgressType, details: RequestEventType<Request>) => void;
197
+ onDownloadProgress?: (values: ProgressType, details: RequestEventType<Request>) => void;
198
+ onResponse?: (response: ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>>, details: ResponseDetailsType) => void;
199
+ onRemove?: (details: RequestEventType<Request>) => void;
200
+ };
201
+ export type RequestSendType<Request extends RequestInstance> = RequestSendOptionsType<Request>["data"] extends NegativeTypes ? RequestSendOptionsType<Request>["params"] extends NegativeTypes ? (options?: RequestSendOptionsType<Request>) => Promise<ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>>> : (options: RequestSendOptionsType<Request>) => Promise<ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>>> : (options: RequestSendOptionsType<Request>) => Promise<ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>>>;
202
+ export type RequestInstance = Request<any, any, any, any, any, any, any, any, any, any, any>;
@@ -0,0 +1,22 @@
1
+ import { ProgressType, ResponseType } from "adapter";
2
+ import { AdapterProgressEventType, RequestInstance, RequestDump, RequestSendOptionsType } from "request";
3
+ import { Dispatcher } from "dispatcher";
4
+ import { ExtractErrorType, ExtractResponseType } from "types";
5
+ export declare const stringifyKey: (value: unknown) => string;
6
+ export declare const getProgressValue: ({ loaded, total }: AdapterProgressEventType) => number;
7
+ export declare const getRequestEta: (startDate: Date, progressDate: Date, { total, loaded }: AdapterProgressEventType) => {
8
+ sizeLeft: number;
9
+ timeLeft: number | null;
10
+ };
11
+ export declare const getProgressData: (requestStartTime: Date, progressDate: Date, progressEvent: AdapterProgressEventType) => ProgressType;
12
+ export declare const getSimpleKey: (request: RequestInstance | RequestDump<RequestInstance>) => string;
13
+ /**
14
+ * Cache instance for individual request that collects individual requests responses from
15
+ * the same endpoint (they may differ base on the custom key, endpoint params etc)
16
+ * @param request
17
+ * @param useInitialValues
18
+ * @returns
19
+ */
20
+ export declare const getRequestKey: (request: RequestInstance | RequestDump<RequestInstance>, useInitialValues?: boolean) => string;
21
+ export declare const getRequestDispatcher: <Request_1 extends RequestInstance>(request: Request_1, dispatcherType?: "auto" | "fetch" | "submit") => [Dispatcher, boolean];
22
+ export declare const requestSendRequest: <Request_1 extends RequestInstance>(request: Request_1, options?: RequestSendOptionsType<Request_1>) => Promise<ResponseType<ExtractResponseType<Request_1>, ExtractErrorType<Request_1>>>;
@@ -1,16 +1,16 @@
1
- import { ClientResponseType } from "client";
2
- import { ExtractRouteParams, Command, CommandInstance } from "command";
3
- export type ExtractClientReturnType<T extends CommandInstance> = ClientResponseType<ExtractResponse<T>, ExtractError<T>>;
4
- export type ExtractResponse<T> = T extends Command<infer D, any, any, any, any, any, any, any, any, any, any> ? D : never;
5
- export type ExtractRequestData<T> = T extends Command<any, infer D, any, any, any, any, any, any, any, any, any> ? D : never;
6
- export type ExtractQueryParams<T> = T extends Command<any, any, infer Q, any, any, any, any, any, any, any, any> ? Q : never;
7
- export type ExtractError<T> = T extends Command<any, any, any, infer G, infer L, any, any, any, any, any, any> ? G | L : never;
8
- export type ExtractGlobalError<T> = T extends Command<any, any, any, infer E, any, any, any, any, any, any, any> ? E : never;
9
- export type ExtractLocalError<T> = T extends Command<any, any, any, any, infer E, any, any, any, any, any, any> ? E : never;
10
- export type ExtractParams<T> = T extends Command<any, any, any, any, any, infer P, any, any, any, any, any> ? ExtractRouteParams<P> : never;
11
- export type ExtractEndpoint<T> = T extends Command<any, any, any, any, any, infer E, any, any, any, any, any> ? E : never;
12
- export type ExtractClientOptions<T> = T extends Command<any, any, any, any, any, any, infer O, any, any, any, any> ? O : never;
13
- export type ExtractHasData<T> = T extends Command<any, any, any, any, any, any, any, infer D, any, any, any> ? D : never;
14
- export type ExtractHasParams<T> = T extends Command<any, any, any, any, any, any, any, any, infer P, any, any> ? P : never;
15
- export type ExtractHasQueryParams<T> = T extends Command<any, any, any, any, any, any, any, any, any, infer Q, any> ? Q : never;
16
- export type ExtractHasMappedData<T> = T extends Command<any, any, any, any, any, any, any, any, any, any, infer M> ? M : never;
1
+ import { ResponseType } from "adapter";
2
+ import { ExtractRouteParams, Request, RequestInstance } from "request";
3
+ export type ExtractAdapterReturnType<T extends RequestInstance> = ResponseType<ExtractResponseType<T>, ExtractErrorType<T>>;
4
+ export type ExtractResponseType<T> = T extends Request<infer D, any, any, any, any, any, any, any, any, any, any> ? D : never;
5
+ export type ExtractPayloadType<T> = T extends Request<any, infer D, any, any, any, any, any, any, any, any, any> ? D : never;
6
+ export type ExtractQueryParamsType<T> = T extends Request<any, any, infer Q, any, any, any, any, any, any, any, any> ? Q : never;
7
+ export type ExtractErrorType<T> = T extends Request<any, any, any, infer G, infer L, any, any, any, any, any, any> ? G | L : never;
8
+ export type ExtractGlobalErrorType<T> = T extends Request<any, any, any, infer E, any, any, any, any, any, any, any> ? E : never;
9
+ export type ExtractLocalErrorType<T> = T extends Request<any, any, any, any, infer E, any, any, any, any, any, any> ? E : never;
10
+ export type ExtractParamsType<T> = T extends Request<any, any, any, any, any, infer P, any, any, any, any, any> ? ExtractRouteParams<P> : never;
11
+ export type ExtractEndpointType<T> = T extends Request<any, any, any, any, any, infer E, any, any, any, any, any> ? E : never;
12
+ export type ExtractAdapterOptionsType<T> = T extends Request<any, any, any, any, any, any, infer O, any, any, any, any> ? O : never;
13
+ export type ExtractHasDataType<T> = T extends Request<any, any, any, any, any, any, any, infer D, any, any, any> ? D : never;
14
+ export type ExtractHasParamsType<T> = T extends Request<any, any, any, any, any, any, any, any, infer P, any, any> ? P : never;
15
+ export type ExtractHasQueryParamsType<T> = T extends Request<any, any, any, any, any, any, any, any, any, infer Q, any> ? Q : never;
16
+ export type ExtractHasMappedDataType<T> = T extends Request<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": "2.1.0",
3
+ "version": "3.0.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>",