@hyper-fetch/core 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/.eslintrc.json +7 -0
  2. package/README.md +83 -0
  3. package/dist/builder/builder.constants.d.ts +9 -0
  4. package/dist/builder/builder.d.ts +141 -0
  5. package/dist/builder/builder.types.d.ts +45 -0
  6. package/dist/builder/builder.utils.d.ts +10 -0
  7. package/dist/builder/index.d.ts +4 -0
  8. package/dist/cache/cache.d.ts +76 -0
  9. package/dist/cache/cache.events.d.ts +28 -0
  10. package/dist/cache/cache.types.d.ts +49 -0
  11. package/dist/cache/cache.utils.d.ts +6 -0
  12. package/dist/cache/index.d.ts +4 -0
  13. package/dist/client/fetch.client.bindings.d.ts +25 -0
  14. package/dist/client/fetch.client.constants.d.ts +1 -0
  15. package/dist/client/fetch.client.d.ts +2 -0
  16. package/dist/client/fetch.client.types.d.ts +57 -0
  17. package/dist/client/fetch.client.utils.d.ts +5 -0
  18. package/dist/client/index.d.ts +5 -0
  19. package/dist/command/command.d.ts +87 -0
  20. package/dist/command/command.types.d.ts +196 -0
  21. package/dist/command/command.utils.d.ts +22 -0
  22. package/dist/command/index.d.ts +3 -0
  23. package/dist/constants/http.constants.d.ts +7 -0
  24. package/dist/constants/time.constants.d.ts +11 -0
  25. package/dist/dispatcher/dispatcher.constants.d.ts +6 -0
  26. package/dist/dispatcher/dispatcher.d.ts +155 -0
  27. package/dist/dispatcher/dispatcher.events.d.ts +11 -0
  28. package/dist/dispatcher/dispatcher.types.d.ts +31 -0
  29. package/dist/dispatcher/dispatcher.utils.d.ts +10 -0
  30. package/dist/dispatcher/index.d.ts +5 -0
  31. package/dist/effect/fetch.effect.d.ts +14 -0
  32. package/dist/effect/fetch.effect.types.d.ts +32 -0
  33. package/dist/effect/index.d.ts +2 -0
  34. package/dist/index.cjs.js +2 -0
  35. package/dist/index.cjs.js.map +7 -0
  36. package/dist/index.d.ts +10 -0
  37. package/dist/index.esm.js +2 -0
  38. package/dist/index.esm.js.map +7 -0
  39. package/dist/managers/app/app.manager.constants.d.ts +9 -0
  40. package/dist/managers/app/app.manager.d.ts +35 -0
  41. package/dist/managers/app/app.manager.events.d.ts +11 -0
  42. package/dist/managers/app/app.manager.types.d.ts +7 -0
  43. package/dist/managers/app/app.manager.utils.d.ts +4 -0
  44. package/dist/managers/app/index.d.ts +5 -0
  45. package/dist/managers/command/command.manager.d.ts +43 -0
  46. package/dist/managers/command/command.manager.events.d.ts +36 -0
  47. package/dist/managers/command/command.manager.types.d.ts +23 -0
  48. package/dist/managers/command/command.manager.utils.d.ts +16 -0
  49. package/dist/managers/command/index.d.ts +4 -0
  50. package/dist/managers/index.d.ts +3 -0
  51. package/dist/managers/logger/index.d.ts +4 -0
  52. package/dist/managers/logger/logger.manager.constants.d.ts +4 -0
  53. package/dist/managers/logger/logger.manager.d.ts +19 -0
  54. package/dist/managers/logger/logger.manager.types.d.ts +30 -0
  55. package/dist/managers/logger/logger.manager.utils.d.ts +3 -0
  56. package/dist/types/fetch.types.d.ts +15 -0
  57. package/dist/types/helpers.types.d.ts +11 -0
  58. package/dist/types/http.types.d.ts +1 -0
  59. package/dist/types/index.d.ts +3 -0
  60. package/dist/utils/index.d.ts +1 -0
  61. package/dist/utils/uuid.utils.d.ts +1 -0
  62. package/package.json +60 -0
  63. package/src/builder/builder.constants.ts +9 -0
  64. package/src/builder/builder.ts +309 -0
  65. package/src/builder/builder.types.ts +57 -0
  66. package/src/builder/builder.utils.ts +178 -0
  67. package/src/builder/index.ts +4 -0
  68. package/src/cache/cache.events.ts +44 -0
  69. package/src/cache/cache.ts +221 -0
  70. package/src/cache/cache.types.ts +56 -0
  71. package/src/cache/cache.utils.ts +29 -0
  72. package/src/cache/index.ts +4 -0
  73. package/src/client/fetch.client.bindings.ts +290 -0
  74. package/src/client/fetch.client.constants.ts +3 -0
  75. package/src/client/fetch.client.ts +98 -0
  76. package/src/client/fetch.client.types.ts +83 -0
  77. package/src/client/fetch.client.utils.ts +28 -0
  78. package/src/client/index.ts +5 -0
  79. package/src/command/command.ts +539 -0
  80. package/src/command/command.types.ts +268 -0
  81. package/src/command/command.utils.ts +179 -0
  82. package/src/command/index.ts +3 -0
  83. package/src/constants/http.constants.ts +7 -0
  84. package/src/constants/time.constants.ts +11 -0
  85. package/src/dispatcher/dispatcher.constants.ts +6 -0
  86. package/src/dispatcher/dispatcher.events.ts +42 -0
  87. package/src/dispatcher/dispatcher.ts +596 -0
  88. package/src/dispatcher/dispatcher.types.ts +39 -0
  89. package/src/dispatcher/dispatcher.utils.ts +55 -0
  90. package/src/dispatcher/index.ts +5 -0
  91. package/src/effect/fetch.effect.ts +28 -0
  92. package/src/effect/fetch.effect.types.ts +35 -0
  93. package/src/effect/index.ts +2 -0
  94. package/src/index.ts +12 -0
  95. package/src/managers/app/app.manager.constants.ts +24 -0
  96. package/src/managers/app/app.manager.events.ts +34 -0
  97. package/src/managers/app/app.manager.ts +74 -0
  98. package/src/managers/app/app.manager.types.ts +8 -0
  99. package/src/managers/app/app.manager.utils.ts +34 -0
  100. package/src/managers/app/index.ts +5 -0
  101. package/src/managers/command/command.manager.events.ts +212 -0
  102. package/src/managers/command/command.manager.ts +68 -0
  103. package/src/managers/command/command.manager.types.ts +25 -0
  104. package/src/managers/command/command.manager.utils.ts +19 -0
  105. package/src/managers/command/index.ts +4 -0
  106. package/src/managers/index.ts +3 -0
  107. package/src/managers/logger/index.ts +4 -0
  108. package/src/managers/logger/logger.manager.constants.ts +24 -0
  109. package/src/managers/logger/logger.manager.ts +70 -0
  110. package/src/managers/logger/logger.manager.types.ts +35 -0
  111. package/src/managers/logger/logger.manager.utils.ts +27 -0
  112. package/src/types/fetch.types.ts +39 -0
  113. package/src/types/helpers.types.ts +15 -0
  114. package/src/types/http.types.ts +1 -0
  115. package/src/types/index.ts +3 -0
  116. package/src/utils/index.ts +1 -0
  117. package/src/utils/uuid.utils.ts +3 -0
  118. package/tsconfig.json +8 -0
@@ -0,0 +1,268 @@
1
+ import {
2
+ NullableKeys,
3
+ NegativeTypes,
4
+ ExtractParams,
5
+ ExtractRequestData,
6
+ HttpMethodsType,
7
+ ExtractQueryParams,
8
+ ExtractClientOptions,
9
+ ExtractEndpoint,
10
+ ExtractHasData,
11
+ ExtractHasParams,
12
+ ExtractHasQueryParams,
13
+ ExtractError,
14
+ ExtractResponse,
15
+ } from "types";
16
+ import { Command } from "command";
17
+ import { ClientResponseType, ClientQueryParamsType, FetchProgressType } from "client";
18
+ import { CommandEventDetails, CommandResponseDetails } from "managers";
19
+
20
+ // Progress
21
+ export type ClientProgressEvent = { total: number; loaded: number };
22
+ export type ClientProgressResponse = {
23
+ progress: number;
24
+ timeLeft: number;
25
+ sizeLeft: number;
26
+ };
27
+
28
+ // Dump
29
+
30
+ /**
31
+ * Dump of the command used to later recreate it
32
+ */
33
+ export type CommandDump<
34
+ Command extends CommandInstance,
35
+ // Bellow generics provided only to overcome the typescript bugs
36
+ ClientOptions = unknown,
37
+ QueryParamsType = ClientQueryParamsType,
38
+ Params = ExtractParams<Command>,
39
+ > = {
40
+ commandOptions: CommandConfig<string, ClientOptions | ExtractClientOptions<Command>>;
41
+ endpoint: string;
42
+ method: HttpMethodsType;
43
+ headers?: HeadersInit;
44
+ auth: boolean;
45
+ cancelable: boolean;
46
+ retry: number;
47
+ retryTime: number;
48
+ cache: boolean;
49
+ cacheTime: number;
50
+ queued: boolean;
51
+ offline: boolean;
52
+ disableResponseInterceptors: boolean | undefined;
53
+ disableRequestInterceptors: boolean | undefined;
54
+ options?: ClientOptions | ExtractClientOptions<Command>;
55
+ data: CommandData<ExtractRequestData<Command>, unknown>;
56
+ params: Params | NegativeTypes;
57
+ queryParams: QueryParamsType | NegativeTypes;
58
+ abortKey: string;
59
+ cacheKey: string;
60
+ queueKey: string;
61
+ effectKey: string;
62
+ used: boolean;
63
+ updatedAbortKey: boolean;
64
+ updatedCacheKey: boolean;
65
+ updatedQueueKey: boolean;
66
+ updatedEffectKey: boolean;
67
+ deduplicate: boolean;
68
+ deduplicateTime: number;
69
+ };
70
+
71
+ // Command
72
+
73
+ /**
74
+ * Configuration options for command creation
75
+ */
76
+ export type CommandConfig<GenericEndpoint extends string, ClientOptions> = {
77
+ /**
78
+ * Determine the endpoint for command request
79
+ */
80
+ endpoint: GenericEndpoint;
81
+ /**
82
+ * Custom headers for command
83
+ */
84
+ headers?: HeadersInit;
85
+ /**
86
+ * Should the onAuth method get called on this command
87
+ */
88
+ auth?: boolean;
89
+ /**
90
+ * Request method GET | POST | PATCH | PUT | DELETE
91
+ */
92
+ method?: HttpMethodsType;
93
+ /**
94
+ * Should enable cancelable mode in the Dispatcher
95
+ */
96
+ cancelable?: boolean;
97
+ /**
98
+ * Retry count when request is failed
99
+ */
100
+ retry?: number;
101
+ /**
102
+ * Retry time delay between retries
103
+ */
104
+ retryTime?: number;
105
+ /**
106
+ * Should we save the response to cache
107
+ */
108
+ cache?: boolean;
109
+ /**
110
+ * Time for which the cache is considered up-to-date
111
+ */
112
+ cacheTime?: number;
113
+ /**
114
+ * Should the requests from this command be send one-by-one
115
+ */
116
+ queued?: boolean;
117
+ /**
118
+ * Do we want to store request made in offline mode for latter use when we go back online
119
+ */
120
+ offline?: boolean;
121
+ /**
122
+ * Disable post-request interceptors
123
+ */
124
+ disableResponseInterceptors?: boolean;
125
+ /**
126
+ * Disable pre-request interceptors
127
+ */
128
+ disableRequestInterceptors?: boolean;
129
+ /**
130
+ * Additional options for your client, by default XHR options
131
+ */
132
+ options?: ClientOptions;
133
+ /**
134
+ * Key which will be used to cancel requests. Autogenerated by default.
135
+ */
136
+ abortKey?: string;
137
+ /**
138
+ * Key which will be used to cache requests. Autogenerated by default.
139
+ */
140
+ cacheKey?: string;
141
+ /**
142
+ * Key which will be used to queue requests. Autogenerated by default.
143
+ */
144
+ queueKey?: string;
145
+ /**
146
+ * Key which will be used to use effects. Autogenerated by default.
147
+ */
148
+ effectKey?: string;
149
+ /**
150
+ * Should we deduplicate two requests made at the same time into one
151
+ */
152
+ deduplicate?: boolean;
153
+ /**
154
+ * Time of pooling for the deduplication to be active (default 10ms)
155
+ */
156
+ deduplicateTime?: number;
157
+ };
158
+
159
+ export type CommandData<RequestDataType, MappedData> =
160
+ | (MappedData extends undefined ? RequestDataType : MappedData)
161
+ | NegativeTypes;
162
+
163
+ export type CommandCurrentType<
164
+ ResponseType,
165
+ RequestDataType,
166
+ QueryParamsType,
167
+ ErrorType,
168
+ GenericEndpoint extends string,
169
+ ClientOptions,
170
+ MappedData,
171
+ > = {
172
+ used?: boolean;
173
+ params?: ExtractRouteParams<GenericEndpoint> | NegativeTypes;
174
+ queryParams?: QueryParamsType | NegativeTypes;
175
+ data?: CommandData<RequestDataType, MappedData>;
176
+ mockCallback?: ((data: RequestDataType) => ClientResponseType<ResponseType, ErrorType>) | undefined;
177
+ headers?: HeadersInit;
178
+ updatedAbortKey?: boolean;
179
+ updatedCacheKey?: boolean;
180
+ updatedQueueKey?: boolean;
181
+ updatedEffectKey?: boolean;
182
+ } & Partial<NullableKeys<CommandConfig<GenericEndpoint, ClientOptions>>>;
183
+
184
+ export type ParamType = string | number;
185
+ export type ParamsType = Record<string, ParamType>;
186
+
187
+ export type ExtractRouteParams<T extends string> = string extends T
188
+ ? NegativeTypes
189
+ : T extends `${string}:${infer Param}/${infer Rest}`
190
+ ? { [k in Param | keyof ExtractRouteParams<Rest>]: ParamType }
191
+ : T extends `${string}:${infer Param}`
192
+ ? { [k in Param]: ParamType }
193
+ : NegativeTypes;
194
+
195
+ export type FetchOptionsType<ClientOptions> = Omit<
196
+ Partial<CommandConfig<string, ClientOptions>>,
197
+ "endpoint" | "method"
198
+ >;
199
+
200
+ /**
201
+ * It will check if the query params are already set
202
+ */
203
+ export type FetchQueryParamsType<QueryParamsType, HasQuery extends true | false = false> = HasQuery extends true
204
+ ? { queryParams?: NegativeTypes }
205
+ : {
206
+ queryParams?: QueryParamsType | string;
207
+ };
208
+
209
+ /**
210
+ * If the command endpoint parameters are not filled it will throw an error
211
+ */
212
+ export type FetchParamsType<
213
+ EndpointType extends string,
214
+ HasParams extends true | false = false,
215
+ > = ExtractRouteParams<EndpointType> extends NegativeTypes
216
+ ? { params?: NegativeTypes }
217
+ : true extends HasParams
218
+ ? { params?: NegativeTypes }
219
+ : { params: ExtractRouteParams<EndpointType> };
220
+
221
+ /**
222
+ * If the command data is not filled it will throw an error
223
+ */
224
+ export type FetchRequestDataType<
225
+ RequestDataType,
226
+ HasData extends true | false = false,
227
+ > = RequestDataType extends NegativeTypes
228
+ ? { data?: NegativeTypes }
229
+ : HasData extends true
230
+ ? { data?: NegativeTypes }
231
+ : { data: RequestDataType };
232
+
233
+ export type CommandQueueOptions = {
234
+ dispatcherType?: "auto" | "fetch" | "submit";
235
+ };
236
+
237
+ export type FetchType<Command extends CommandInstance> = FetchQueryParamsType<
238
+ ExtractQueryParams<Command>,
239
+ ExtractHasQueryParams<Command>
240
+ > &
241
+ FetchParamsType<ExtractEndpoint<Command>, ExtractHasParams<Command>> &
242
+ FetchRequestDataType<ExtractRequestData<Command>, ExtractHasData<Command>> &
243
+ Omit<FetchOptionsType<ExtractClientOptions<Command>>, "params" | "data"> &
244
+ FetchSendActionsType<Command> &
245
+ CommandQueueOptions;
246
+
247
+ export type FetchSendActionsType<Command extends CommandInstance> = {
248
+ onSettle?: (requestId: string, command: Command) => void;
249
+ onRequestStart?: (details: CommandEventDetails<Command>) => void;
250
+ onResponseStart?: (details: CommandEventDetails<Command>) => void;
251
+ onUploadProgress?: (values: FetchProgressType, details: CommandEventDetails<Command>) => void;
252
+ onDownloadProgress?: (values: FetchProgressType, details: CommandEventDetails<Command>) => void;
253
+ onResponse?: (
254
+ response: ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>,
255
+ details: CommandResponseDetails,
256
+ ) => void;
257
+ onRemove?: (details: CommandEventDetails<Command>) => void;
258
+ };
259
+
260
+ export type FetchMethodType<Command extends CommandInstance> = FetchType<Command>["data"] extends any
261
+ ? (options?: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>
262
+ : FetchType<Command>["data"] extends NegativeTypes
263
+ ? FetchType<Command>["params"] extends NegativeTypes
264
+ ? (options?: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>
265
+ : (options: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>
266
+ : (options: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>;
267
+
268
+ export type CommandInstance = Command<any, any, any, any, any, any, any, any, any, any, any>;
@@ -0,0 +1,179 @@
1
+ import { FetchProgressType, ClientResponseType, getErrorMessage } from "client";
2
+ import { ClientProgressEvent, CommandInstance, CommandDump, FetchType } from "command";
3
+ import { HttpMethodsEnum } from "constants/http.constants";
4
+ import { canRetryRequest, Dispatcher, isFailedRequest } from "dispatcher";
5
+ import { ExtractError, ExtractResponse } from "types";
6
+
7
+ export const stringifyKey = (value: unknown): string => {
8
+ try {
9
+ if (typeof value === "string") return value;
10
+ if (value === undefined || value === null) return "";
11
+ const data = JSON.stringify(value);
12
+ if (typeof data !== "string") throw new Error();
13
+ return data;
14
+ } catch (_) {
15
+ return "";
16
+ }
17
+ };
18
+
19
+ export const getProgressValue = ({ loaded, total }: ClientProgressEvent): number => {
20
+ if (!loaded || !total) return 0;
21
+ return Number(((loaded * 100) / total).toFixed(0));
22
+ };
23
+
24
+ export const getRequestEta = (
25
+ startDate: Date,
26
+ progressDate: Date,
27
+ { total, loaded }: ClientProgressEvent,
28
+ ): { sizeLeft: number; timeLeft: number | null } => {
29
+ const timeElapsed = +progressDate - +startDate || 1;
30
+ const uploadSpeed = loaded / timeElapsed;
31
+ const totalValue = Math.max(total, loaded);
32
+ const sizeLeft = totalValue - loaded;
33
+ const estimatedTimeValue = uploadSpeed ? sizeLeft / uploadSpeed : null;
34
+ const timeLeft = totalValue === loaded ? 0 : estimatedTimeValue;
35
+
36
+ return { timeLeft, sizeLeft };
37
+ };
38
+
39
+ export const getProgressData = (
40
+ requestStartTime: Date,
41
+ progressDate: Date,
42
+ progressEvent: ClientProgressEvent,
43
+ ): FetchProgressType => {
44
+ const { total, loaded } = progressEvent;
45
+ if (Number.isNaN(total) || Number.isNaN(loaded)) {
46
+ return {
47
+ progress: 0,
48
+ timeLeft: 0,
49
+ sizeLeft: 0,
50
+ total: 0,
51
+ loaded: 0,
52
+ startTimestamp: +requestStartTime,
53
+ };
54
+ }
55
+
56
+ const { timeLeft, sizeLeft } = getRequestEta(requestStartTime, progressDate, progressEvent);
57
+
58
+ return {
59
+ progress: getProgressValue(progressEvent),
60
+ timeLeft,
61
+ sizeLeft,
62
+ total,
63
+ loaded,
64
+ startTimestamp: +requestStartTime,
65
+ };
66
+ };
67
+
68
+ // Keys
69
+ export const getSimpleKey = (command: CommandInstance | CommandDump<CommandInstance>): string => {
70
+ return `${command.method}_${command.commandOptions.endpoint}_${command.cancelable}`;
71
+ };
72
+
73
+ /**
74
+ * Cache instance for individual command that collects individual requests responses from
75
+ * the same endpoint (they may differ base on the custom key, endpoint params etc)
76
+ * @param command
77
+ * @param useInitialValues
78
+ * @returns
79
+ */
80
+ export const getCommandKey = (
81
+ command: CommandInstance | CommandDump<CommandInstance>,
82
+ useInitialValues?: boolean,
83
+ ): string => {
84
+ /**
85
+ * Below stringified values allow to match the response by method, endpoint and query params.
86
+ * That's because we have shared endpoint, but data with queryParams '?user=1' will not match regular request without queries.
87
+ * We want both results to be cached in separate places to not override each other.
88
+ *
89
+ * Values to be stringified:
90
+ *
91
+ * endpoint: string;
92
+ * queryParams: string;
93
+ * params: string;
94
+ */
95
+ const methodKey = stringifyKey(command.method);
96
+ const endpointKey = useInitialValues ? command.commandOptions.endpoint : stringifyKey(command.endpoint);
97
+
98
+ return `${methodKey}_${endpointKey}`;
99
+ };
100
+
101
+ export const getCommandDispatcher = <Command extends CommandInstance>(
102
+ command: Command,
103
+ dispatcherType: "auto" | "fetch" | "submit" = "auto",
104
+ ): [Dispatcher, boolean] => {
105
+ const { fetchDispatcher, submitDispatcher } = command.builder;
106
+ const isGet = command.method === HttpMethodsEnum.get;
107
+ const isFetchDispatcher = (dispatcherType === "auto" && isGet) || dispatcherType === "fetch";
108
+ const dispatcher = isFetchDispatcher ? fetchDispatcher : submitDispatcher;
109
+
110
+ return [dispatcher, isFetchDispatcher];
111
+ };
112
+
113
+ export const commandSendRequest = <Command extends CommandInstance>(command: Command, options?: FetchType<Command>) => {
114
+ const { commandManager } = command.builder;
115
+ const [dispatcher] = getCommandDispatcher(command, options?.dispatcherType);
116
+
117
+ return new Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>((resolve) => {
118
+ const requestId = dispatcher.add(command);
119
+ options?.onSettle?.(requestId, command);
120
+
121
+ const unmountRequestStart = commandManager.events.onRequestStartById<Command>(requestId, (...props) =>
122
+ options?.onRequestStart?.(...props),
123
+ );
124
+
125
+ const unmountResponseStart = commandManager.events.onResponseStartById<Command>(requestId, (...props) =>
126
+ options?.onResponseStart?.(...props),
127
+ );
128
+
129
+ const unmountUpload = commandManager.events.onUploadProgressById<Command>(requestId, (...props) =>
130
+ options?.onUploadProgress?.(...props),
131
+ );
132
+
133
+ const unmountDownload = commandManager.events.onDownloadProgressById<Command>(requestId, (...props) =>
134
+ options?.onDownloadProgress?.(...props),
135
+ );
136
+
137
+ // When resolved
138
+ const unmountResponse = commandManager.events.onResponseById<ExtractResponse<Command>, ExtractError<Command>>(
139
+ requestId,
140
+ (response, details) => {
141
+ const isFailed = isFailedRequest(response);
142
+ const isOfflineStatus = command.offline && details.isOffline;
143
+ const willRetry = canRetryRequest(details.retries, command.retry);
144
+
145
+ // When going offline we can't handle the request as it will be postponed to later resolve
146
+ if (isFailed && isOfflineStatus) return;
147
+
148
+ // When command is in retry mode we need to listen for retries end
149
+ if (isFailed && willRetry) return;
150
+
151
+ options?.onResponse?.(response, details);
152
+ resolve(response);
153
+
154
+ // Unmount Listeners
155
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
156
+ umountAll();
157
+ },
158
+ );
159
+
160
+ // When removed from queue storage we need to clean event listeners and return proper error
161
+ const unmountRemoveQueueElement = commandManager.events.onRemoveById<Command>(requestId, (...props) => {
162
+ options?.onRemove?.(...props);
163
+ resolve([null, getErrorMessage("deleted") as unknown as ExtractError<Command>, 0]);
164
+
165
+ // Unmount Listeners
166
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
167
+ umountAll();
168
+ });
169
+
170
+ function umountAll() {
171
+ unmountRequestStart();
172
+ unmountResponseStart();
173
+ unmountUpload();
174
+ unmountDownload();
175
+ unmountResponse();
176
+ unmountRemoveQueueElement();
177
+ }
178
+ });
179
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./command";
2
+ export * from "./command.types";
3
+ export * from "./command.utils";
@@ -0,0 +1,7 @@
1
+ export enum HttpMethodsEnum {
2
+ get = "GET",
3
+ post = "POST",
4
+ put = "PUT",
5
+ patch = "PATCH",
6
+ delete = "DELETE",
7
+ }
@@ -0,0 +1,11 @@
1
+ export enum DateInterval {
2
+ second = 1e3,
3
+ minute = 6e4,
4
+ hour = 3.6e6,
5
+ day = 8.64e7,
6
+ week = 6.048e8,
7
+ month30 = 2.592e9,
8
+ month31 = 2.6784e9,
9
+ year = 3.1536e10,
10
+ yearLeap = 3.16224e10,
11
+ }
@@ -0,0 +1,6 @@
1
+ export enum DispatcherRequestType {
2
+ oneByOne = "one-by-one",
3
+ allAtOnce = "all-at-once",
4
+ previousCanceled = "previous-canceled",
5
+ deduplicated = "deduplicated",
6
+ }
@@ -0,0 +1,42 @@
1
+ import EventEmitter from "events";
2
+
3
+ import {
4
+ DispatcherData,
5
+ getDispatcherChangeEventKey,
6
+ getDispatcherStatusEventKey,
7
+ getDispatcherDrainedEventKey,
8
+ } from "dispatcher";
9
+ import { CommandInstance } from "command";
10
+
11
+ export const getDispatcherEvents = (emitter: EventEmitter) => ({
12
+ setDrained: <Command extends CommandInstance>(queueKey: string, values: DispatcherData<Command>): void => {
13
+ emitter.emit(getDispatcherDrainedEventKey(queueKey), values);
14
+ },
15
+ setQueueStatus: <Command extends CommandInstance>(queueKey: string, values: DispatcherData<Command>): void => {
16
+ emitter.emit(getDispatcherStatusEventKey(queueKey), values);
17
+ },
18
+ setQueueChanged: <Command extends CommandInstance>(queueKey: string, values: DispatcherData<Command>): void => {
19
+ emitter.emit(getDispatcherChangeEventKey(queueKey), values);
20
+ },
21
+ onDrained: <Command extends CommandInstance>(
22
+ queueKey: string,
23
+ callback: (values: DispatcherData<Command>) => void,
24
+ ): VoidFunction => {
25
+ emitter.on(getDispatcherDrainedEventKey(queueKey), callback);
26
+ return () => emitter.removeListener(getDispatcherDrainedEventKey(queueKey), callback);
27
+ },
28
+ onQueueStatus: <Command extends CommandInstance>(
29
+ queueKey: string,
30
+ callback: (values: DispatcherData<Command>) => void,
31
+ ): VoidFunction => {
32
+ emitter.on(getDispatcherStatusEventKey(queueKey), callback);
33
+ return () => emitter.removeListener(getDispatcherStatusEventKey(queueKey), callback);
34
+ },
35
+ onQueueChange: <Command extends CommandInstance>(
36
+ queueKey: string,
37
+ callback: (values: DispatcherData<Command>) => void,
38
+ ): VoidFunction => {
39
+ emitter.on(getDispatcherChangeEventKey(queueKey), callback);
40
+ return () => emitter.removeListener(getDispatcherChangeEventKey(queueKey), callback);
41
+ },
42
+ });