@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.
- package/.eslintrc.json +7 -0
- package/README.md +83 -0
- package/dist/builder/builder.constants.d.ts +9 -0
- package/dist/builder/builder.d.ts +141 -0
- package/dist/builder/builder.types.d.ts +45 -0
- package/dist/builder/builder.utils.d.ts +10 -0
- package/dist/builder/index.d.ts +4 -0
- package/dist/cache/cache.d.ts +76 -0
- package/dist/cache/cache.events.d.ts +28 -0
- package/dist/cache/cache.types.d.ts +49 -0
- package/dist/cache/cache.utils.d.ts +6 -0
- package/dist/cache/index.d.ts +4 -0
- package/dist/client/fetch.client.bindings.d.ts +25 -0
- package/dist/client/fetch.client.constants.d.ts +1 -0
- package/dist/client/fetch.client.d.ts +2 -0
- package/dist/client/fetch.client.types.d.ts +57 -0
- package/dist/client/fetch.client.utils.d.ts +5 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/command/command.d.ts +87 -0
- package/dist/command/command.types.d.ts +196 -0
- package/dist/command/command.utils.d.ts +22 -0
- package/dist/command/index.d.ts +3 -0
- package/dist/constants/http.constants.d.ts +7 -0
- package/dist/constants/time.constants.d.ts +11 -0
- package/dist/dispatcher/dispatcher.constants.d.ts +6 -0
- package/dist/dispatcher/dispatcher.d.ts +155 -0
- package/dist/dispatcher/dispatcher.events.d.ts +11 -0
- package/dist/dispatcher/dispatcher.types.d.ts +31 -0
- package/dist/dispatcher/dispatcher.utils.d.ts +10 -0
- package/dist/dispatcher/index.d.ts +5 -0
- package/dist/effect/fetch.effect.d.ts +14 -0
- package/dist/effect/fetch.effect.types.d.ts +32 -0
- package/dist/effect/index.d.ts +2 -0
- package/dist/index.cjs.js +2 -0
- package/dist/index.cjs.js.map +7 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +7 -0
- package/dist/managers/app/app.manager.constants.d.ts +9 -0
- package/dist/managers/app/app.manager.d.ts +35 -0
- package/dist/managers/app/app.manager.events.d.ts +11 -0
- package/dist/managers/app/app.manager.types.d.ts +7 -0
- package/dist/managers/app/app.manager.utils.d.ts +4 -0
- package/dist/managers/app/index.d.ts +5 -0
- package/dist/managers/command/command.manager.d.ts +43 -0
- package/dist/managers/command/command.manager.events.d.ts +36 -0
- package/dist/managers/command/command.manager.types.d.ts +23 -0
- package/dist/managers/command/command.manager.utils.d.ts +16 -0
- package/dist/managers/command/index.d.ts +4 -0
- package/dist/managers/index.d.ts +3 -0
- package/dist/managers/logger/index.d.ts +4 -0
- package/dist/managers/logger/logger.manager.constants.d.ts +4 -0
- package/dist/managers/logger/logger.manager.d.ts +19 -0
- package/dist/managers/logger/logger.manager.types.d.ts +30 -0
- package/dist/managers/logger/logger.manager.utils.d.ts +3 -0
- package/dist/types/fetch.types.d.ts +15 -0
- package/dist/types/helpers.types.d.ts +11 -0
- package/dist/types/http.types.d.ts +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/uuid.utils.d.ts +1 -0
- package/package.json +60 -0
- package/src/builder/builder.constants.ts +9 -0
- package/src/builder/builder.ts +309 -0
- package/src/builder/builder.types.ts +57 -0
- package/src/builder/builder.utils.ts +178 -0
- package/src/builder/index.ts +4 -0
- package/src/cache/cache.events.ts +44 -0
- package/src/cache/cache.ts +221 -0
- package/src/cache/cache.types.ts +56 -0
- package/src/cache/cache.utils.ts +29 -0
- package/src/cache/index.ts +4 -0
- package/src/client/fetch.client.bindings.ts +290 -0
- package/src/client/fetch.client.constants.ts +3 -0
- package/src/client/fetch.client.ts +98 -0
- package/src/client/fetch.client.types.ts +83 -0
- package/src/client/fetch.client.utils.ts +28 -0
- package/src/client/index.ts +5 -0
- package/src/command/command.ts +539 -0
- package/src/command/command.types.ts +268 -0
- package/src/command/command.utils.ts +179 -0
- package/src/command/index.ts +3 -0
- package/src/constants/http.constants.ts +7 -0
- package/src/constants/time.constants.ts +11 -0
- package/src/dispatcher/dispatcher.constants.ts +6 -0
- package/src/dispatcher/dispatcher.events.ts +42 -0
- package/src/dispatcher/dispatcher.ts +596 -0
- package/src/dispatcher/dispatcher.types.ts +39 -0
- package/src/dispatcher/dispatcher.utils.ts +55 -0
- package/src/dispatcher/index.ts +5 -0
- package/src/effect/fetch.effect.ts +28 -0
- package/src/effect/fetch.effect.types.ts +35 -0
- package/src/effect/index.ts +2 -0
- package/src/index.ts +12 -0
- package/src/managers/app/app.manager.constants.ts +24 -0
- package/src/managers/app/app.manager.events.ts +34 -0
- package/src/managers/app/app.manager.ts +74 -0
- package/src/managers/app/app.manager.types.ts +8 -0
- package/src/managers/app/app.manager.utils.ts +34 -0
- package/src/managers/app/index.ts +5 -0
- package/src/managers/command/command.manager.events.ts +212 -0
- package/src/managers/command/command.manager.ts +68 -0
- package/src/managers/command/command.manager.types.ts +25 -0
- package/src/managers/command/command.manager.utils.ts +19 -0
- package/src/managers/command/index.ts +4 -0
- package/src/managers/index.ts +3 -0
- package/src/managers/logger/index.ts +4 -0
- package/src/managers/logger/logger.manager.constants.ts +24 -0
- package/src/managers/logger/logger.manager.ts +70 -0
- package/src/managers/logger/logger.manager.types.ts +35 -0
- package/src/managers/logger/logger.manager.utils.ts +27 -0
- package/src/types/fetch.types.ts +39 -0
- package/src/types/helpers.types.ts +15 -0
- package/src/types/http.types.ts +1 -0
- package/src/types/index.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/uuid.utils.ts +3 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FetchType,
|
|
3
|
+
ParamsType,
|
|
4
|
+
getSimpleKey,
|
|
5
|
+
getCommandKey,
|
|
6
|
+
FetchMethodType,
|
|
7
|
+
CommandData,
|
|
8
|
+
CommandDump,
|
|
9
|
+
CommandConfig,
|
|
10
|
+
ExtractRouteParams,
|
|
11
|
+
commandSendRequest,
|
|
12
|
+
CommandCurrentType,
|
|
13
|
+
} from "command";
|
|
14
|
+
import { Builder } from "builder";
|
|
15
|
+
import { getUniqueRequestId } from "utils";
|
|
16
|
+
import { ClientQueryParamsType } from "client";
|
|
17
|
+
import { HttpMethodsType, NegativeTypes } from "types";
|
|
18
|
+
import { DateInterval } from "constants/time.constants";
|
|
19
|
+
import { HttpMethodsEnum } from "constants/http.constants";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Fetch command it is designed to prepare the necessary setup to execute the request to the server.
|
|
23
|
+
* We can setup basic options for example endpoint, method, headers and advanced settings like cache, invalidation patterns, concurrency, retries and much, much more.
|
|
24
|
+
* :::info Usage
|
|
25
|
+
* We should not use this class directly in the standard development flow. We can initialize it using the `createCommand` method on the **Builder** class.
|
|
26
|
+
* :::
|
|
27
|
+
*
|
|
28
|
+
* @attention
|
|
29
|
+
* The most important thing about the command is that it keeps data in the format that can be dumped. This is necessary for the persistance and different dispatcher storage types.
|
|
30
|
+
* This class doesn't have any callback methods by design and communicate with dispatcher and cache by events.
|
|
31
|
+
*/
|
|
32
|
+
export class Command<
|
|
33
|
+
ResponseType,
|
|
34
|
+
RequestDataType,
|
|
35
|
+
QueryParamsType extends ClientQueryParamsType | string,
|
|
36
|
+
GlobalErrorType, // Global Error Type
|
|
37
|
+
LocalErrorType, // Additional Error for specific endpoint
|
|
38
|
+
EndpointType extends string,
|
|
39
|
+
ClientOptions,
|
|
40
|
+
HasData extends true | false = false,
|
|
41
|
+
HasParams extends true | false = false,
|
|
42
|
+
HasQuery extends true | false = false,
|
|
43
|
+
MappedData = undefined,
|
|
44
|
+
> {
|
|
45
|
+
endpoint: EndpointType;
|
|
46
|
+
headers?: HeadersInit;
|
|
47
|
+
auth: boolean;
|
|
48
|
+
method: HttpMethodsType;
|
|
49
|
+
params: ExtractRouteParams<EndpointType> | NegativeTypes;
|
|
50
|
+
data: CommandData<RequestDataType, MappedData>;
|
|
51
|
+
queryParams: QueryParamsType | NegativeTypes;
|
|
52
|
+
options?: ClientOptions | undefined;
|
|
53
|
+
cancelable: boolean;
|
|
54
|
+
retry: number;
|
|
55
|
+
retryTime: number;
|
|
56
|
+
cache: boolean;
|
|
57
|
+
cacheTime: number;
|
|
58
|
+
queued: boolean;
|
|
59
|
+
offline: boolean;
|
|
60
|
+
abortKey: string;
|
|
61
|
+
cacheKey: string;
|
|
62
|
+
queueKey: string;
|
|
63
|
+
effectKey: string;
|
|
64
|
+
used: boolean;
|
|
65
|
+
deduplicate: boolean;
|
|
66
|
+
deduplicateTime: number;
|
|
67
|
+
|
|
68
|
+
private updatedAbortKey: boolean;
|
|
69
|
+
private updatedCacheKey: boolean;
|
|
70
|
+
private updatedQueueKey: boolean;
|
|
71
|
+
private updatedEffectKey: boolean;
|
|
72
|
+
|
|
73
|
+
constructor(
|
|
74
|
+
readonly builder: Builder<GlobalErrorType, ClientOptions>,
|
|
75
|
+
readonly commandOptions: CommandConfig<EndpointType, ClientOptions>,
|
|
76
|
+
readonly commandDump?:
|
|
77
|
+
| CommandCurrentType<
|
|
78
|
+
ResponseType,
|
|
79
|
+
RequestDataType,
|
|
80
|
+
QueryParamsType,
|
|
81
|
+
GlobalErrorType | LocalErrorType,
|
|
82
|
+
EndpointType,
|
|
83
|
+
ClientOptions,
|
|
84
|
+
MappedData
|
|
85
|
+
>
|
|
86
|
+
| undefined,
|
|
87
|
+
readonly dataMapper?: (data: RequestDataType) => MappedData,
|
|
88
|
+
) {
|
|
89
|
+
const {
|
|
90
|
+
endpoint,
|
|
91
|
+
headers,
|
|
92
|
+
auth = true,
|
|
93
|
+
method = HttpMethodsEnum.get,
|
|
94
|
+
options,
|
|
95
|
+
cancelable = false,
|
|
96
|
+
retry = 0,
|
|
97
|
+
retryTime = 500,
|
|
98
|
+
cache = true,
|
|
99
|
+
cacheTime = DateInterval.minute * 5,
|
|
100
|
+
queued = false,
|
|
101
|
+
offline = true,
|
|
102
|
+
abortKey,
|
|
103
|
+
cacheKey,
|
|
104
|
+
queueKey,
|
|
105
|
+
effectKey,
|
|
106
|
+
deduplicate = false,
|
|
107
|
+
deduplicateTime = 10,
|
|
108
|
+
} = { ...this.builder.commandConfig?.(commandOptions), ...commandOptions };
|
|
109
|
+
|
|
110
|
+
this.endpoint = commandDump?.endpoint ?? endpoint;
|
|
111
|
+
this.headers = commandDump?.headers ?? headers;
|
|
112
|
+
this.auth = commandDump?.auth ?? auth;
|
|
113
|
+
this.method = method;
|
|
114
|
+
this.params = commandDump?.params;
|
|
115
|
+
this.data = commandDump?.data;
|
|
116
|
+
this.queryParams = commandDump?.queryParams;
|
|
117
|
+
this.options = commandDump?.options ?? options;
|
|
118
|
+
this.cancelable = commandDump?.cancelable ?? cancelable;
|
|
119
|
+
this.retry = commandDump?.retry ?? retry;
|
|
120
|
+
this.retryTime = commandDump?.retryTime ?? retryTime;
|
|
121
|
+
this.cache = commandDump?.cache ?? cache;
|
|
122
|
+
this.cacheTime = commandDump?.cacheTime ?? cacheTime;
|
|
123
|
+
this.queued = commandDump?.queued ?? queued;
|
|
124
|
+
this.offline = commandDump?.offline ?? offline;
|
|
125
|
+
this.abortKey = commandDump?.abortKey ?? abortKey ?? getSimpleKey(this);
|
|
126
|
+
this.cacheKey = commandDump?.cacheKey ?? cacheKey ?? getCommandKey(this);
|
|
127
|
+
this.queueKey = commandDump?.queueKey ?? queueKey ?? getSimpleKey(this);
|
|
128
|
+
this.effectKey = commandDump?.effectKey ?? effectKey ?? getSimpleKey(this);
|
|
129
|
+
this.used = commandDump?.used ?? false;
|
|
130
|
+
this.deduplicate = commandDump?.deduplicate ?? deduplicate;
|
|
131
|
+
this.deduplicateTime = commandDump?.deduplicateTime ?? deduplicateTime;
|
|
132
|
+
|
|
133
|
+
this.updatedAbortKey = commandDump?.updatedAbortKey ?? false;
|
|
134
|
+
this.updatedCacheKey = commandDump?.updatedCacheKey ?? false;
|
|
135
|
+
this.updatedQueueKey = commandDump?.updatedQueueKey ?? false;
|
|
136
|
+
this.updatedEffectKey = commandDump?.updatedEffectKey ?? false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public setHeaders = (headers: HeadersInit) => {
|
|
140
|
+
return this.clone({ headers });
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
public setAuth = (auth: boolean) => {
|
|
144
|
+
return this.clone({ auth });
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
public setParams = (params: ExtractRouteParams<EndpointType>) => {
|
|
148
|
+
return this.clone<HasData, true, HasQuery>({ params });
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
public setData = (data: RequestDataType) => {
|
|
152
|
+
const modifiedData = this.dataMapper?.(data) || data;
|
|
153
|
+
return this.clone<true, HasParams, HasQuery, MappedData>({
|
|
154
|
+
data: modifiedData as CommandData<RequestDataType, MappedData>,
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
public setQueryParams = (queryParams: QueryParamsType) => {
|
|
159
|
+
return this.clone<HasData, HasParams, true>({ queryParams });
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
public setOptions = (options: ClientOptions) => {
|
|
163
|
+
return this.clone<HasData, HasParams, true>({ options });
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
public setCancelable = (cancelable: boolean) => {
|
|
167
|
+
return this.clone({ cancelable });
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
public setRetry = (retry: CommandConfig<EndpointType, ClientOptions>["retry"]) => {
|
|
171
|
+
return this.clone({ retry });
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
public setRetryTime = (retryTime: CommandConfig<EndpointType, ClientOptions>["retryTime"]) => {
|
|
175
|
+
return this.clone({ retryTime });
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
public setCache = (cache: CommandConfig<EndpointType, ClientOptions>["cache"]) => {
|
|
179
|
+
return this.clone({ cache });
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
public setCacheTime = (cacheTime: CommandConfig<EndpointType, ClientOptions>["cacheTime"]) => {
|
|
183
|
+
return this.clone({ cacheTime });
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
public setQueued = (queued: boolean) => {
|
|
187
|
+
return this.clone({ queued });
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
public setAbortKey = (abortKey: string) => {
|
|
191
|
+
this.updatedAbortKey = true;
|
|
192
|
+
return this.clone({ abortKey });
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
public setCacheKey = (cacheKey: string) => {
|
|
196
|
+
this.updatedCacheKey = true;
|
|
197
|
+
return this.clone({ cacheKey });
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
public setQueueKey = (queueKey: string) => {
|
|
201
|
+
this.updatedQueueKey = true;
|
|
202
|
+
return this.clone({ queueKey });
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
public setEffectKey = (effectKey: string) => {
|
|
206
|
+
this.updatedEffectKey = true;
|
|
207
|
+
return this.clone({ effectKey });
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
public setDeduplicate = (deduplicate: boolean) => {
|
|
211
|
+
return this.clone({ deduplicate });
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
public setDeduplicateTime = (deduplicateTime: number) => {
|
|
215
|
+
return this.clone({ deduplicateTime });
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
public setUsed = (used: boolean) => {
|
|
219
|
+
return this.clone({ used });
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
public setOffline = (offline: boolean) => {
|
|
223
|
+
return this.clone({ offline });
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
public setDataMapper = <DataMapper>(mapper: (data: RequestDataType) => DataMapper) => {
|
|
227
|
+
return this.clone<HasData, HasParams, HasQuery, DataMapper>(undefined, mapper);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
private paramsMapper = (
|
|
231
|
+
params: ParamsType | null | undefined,
|
|
232
|
+
queryParams: QueryParamsType | NegativeTypes,
|
|
233
|
+
): string => {
|
|
234
|
+
let endpoint = this.commandOptions.endpoint as string;
|
|
235
|
+
if (params) {
|
|
236
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
237
|
+
endpoint = endpoint.replace(new RegExp(`:${key}`, "g"), String(value));
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
if (queryParams) {
|
|
241
|
+
endpoint += this.builder.stringifyQueryParams(queryParams);
|
|
242
|
+
}
|
|
243
|
+
return endpoint;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
public dump(): CommandDump<
|
|
247
|
+
Command<
|
|
248
|
+
ResponseType,
|
|
249
|
+
RequestDataType,
|
|
250
|
+
QueryParamsType,
|
|
251
|
+
GlobalErrorType,
|
|
252
|
+
LocalErrorType,
|
|
253
|
+
EndpointType,
|
|
254
|
+
ClientOptions,
|
|
255
|
+
HasData,
|
|
256
|
+
HasParams,
|
|
257
|
+
HasQuery,
|
|
258
|
+
MappedData
|
|
259
|
+
>,
|
|
260
|
+
ClientOptions,
|
|
261
|
+
QueryParamsType
|
|
262
|
+
> {
|
|
263
|
+
return {
|
|
264
|
+
commandOptions: this.commandOptions,
|
|
265
|
+
endpoint: this.endpoint,
|
|
266
|
+
headers: this.headers,
|
|
267
|
+
auth: this.auth,
|
|
268
|
+
method: this.method,
|
|
269
|
+
params: this.params,
|
|
270
|
+
data: this.data,
|
|
271
|
+
queryParams: this.queryParams,
|
|
272
|
+
options: this.options,
|
|
273
|
+
cancelable: this.cancelable,
|
|
274
|
+
retry: this.retry,
|
|
275
|
+
retryTime: this.retryTime,
|
|
276
|
+
cache: this.cache,
|
|
277
|
+
cacheTime: this.cacheTime,
|
|
278
|
+
queued: this.queued,
|
|
279
|
+
offline: this.offline,
|
|
280
|
+
abortKey: this.abortKey,
|
|
281
|
+
cacheKey: this.cacheKey,
|
|
282
|
+
queueKey: this.queueKey,
|
|
283
|
+
effectKey: this.effectKey,
|
|
284
|
+
used: this.used,
|
|
285
|
+
disableResponseInterceptors: this.commandOptions.disableResponseInterceptors,
|
|
286
|
+
disableRequestInterceptors: this.commandOptions.disableRequestInterceptors,
|
|
287
|
+
updatedAbortKey: this.updatedAbortKey,
|
|
288
|
+
updatedCacheKey: this.updatedCacheKey,
|
|
289
|
+
updatedQueueKey: this.updatedQueueKey,
|
|
290
|
+
updatedEffectKey: this.updatedEffectKey,
|
|
291
|
+
deduplicate: this.deduplicate,
|
|
292
|
+
deduplicateTime: this.deduplicateTime,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
public clone<
|
|
297
|
+
D extends true | false = HasData,
|
|
298
|
+
P extends true | false = HasParams,
|
|
299
|
+
Q extends true | false = HasQuery,
|
|
300
|
+
MapperData = MappedData,
|
|
301
|
+
>(
|
|
302
|
+
options?: CommandCurrentType<
|
|
303
|
+
ResponseType,
|
|
304
|
+
RequestDataType,
|
|
305
|
+
QueryParamsType,
|
|
306
|
+
GlobalErrorType | LocalErrorType,
|
|
307
|
+
EndpointType,
|
|
308
|
+
ClientOptions,
|
|
309
|
+
MapperData
|
|
310
|
+
>,
|
|
311
|
+
mapper?: (data: RequestDataType) => MapperData,
|
|
312
|
+
): Command<
|
|
313
|
+
ResponseType,
|
|
314
|
+
RequestDataType,
|
|
315
|
+
QueryParamsType,
|
|
316
|
+
GlobalErrorType,
|
|
317
|
+
LocalErrorType,
|
|
318
|
+
EndpointType,
|
|
319
|
+
ClientOptions,
|
|
320
|
+
D,
|
|
321
|
+
P,
|
|
322
|
+
Q,
|
|
323
|
+
MapperData
|
|
324
|
+
> {
|
|
325
|
+
const dump = this.dump();
|
|
326
|
+
const commandDump: CommandCurrentType<
|
|
327
|
+
ResponseType,
|
|
328
|
+
RequestDataType,
|
|
329
|
+
QueryParamsType,
|
|
330
|
+
GlobalErrorType | LocalErrorType,
|
|
331
|
+
EndpointType,
|
|
332
|
+
ClientOptions,
|
|
333
|
+
MapperData
|
|
334
|
+
> = {
|
|
335
|
+
...dump,
|
|
336
|
+
...options,
|
|
337
|
+
abortKey: this.updatedAbortKey ? options?.abortKey || this.abortKey : undefined,
|
|
338
|
+
cacheKey: this.updatedCacheKey ? options?.cacheKey || this.cacheKey : undefined,
|
|
339
|
+
queueKey: this.updatedQueueKey ? options?.queueKey || this.queueKey : undefined,
|
|
340
|
+
endpoint: this.paramsMapper(
|
|
341
|
+
options?.params || this.params,
|
|
342
|
+
options?.queryParams || this.queryParams,
|
|
343
|
+
) as EndpointType,
|
|
344
|
+
queryParams: options?.queryParams || this.queryParams,
|
|
345
|
+
// Typescript circular types issue - we have to leave any here
|
|
346
|
+
data: (options?.data || this.data) as any,
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const cloned = new Command<
|
|
350
|
+
ResponseType,
|
|
351
|
+
RequestDataType,
|
|
352
|
+
QueryParamsType,
|
|
353
|
+
GlobalErrorType,
|
|
354
|
+
LocalErrorType,
|
|
355
|
+
EndpointType,
|
|
356
|
+
ClientOptions,
|
|
357
|
+
D,
|
|
358
|
+
P,
|
|
359
|
+
Q,
|
|
360
|
+
MapperData
|
|
361
|
+
>(this.builder, this.commandOptions, commandDump, mapper);
|
|
362
|
+
|
|
363
|
+
return cloned;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
public abort = () => {
|
|
367
|
+
const { commandManager } = this.builder;
|
|
368
|
+
commandManager.abortByKey(this.abortKey);
|
|
369
|
+
|
|
370
|
+
return this.clone();
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Method to use the command WITHOUT adding it to cache and queues. This mean it will make simple request without queue side effects.
|
|
375
|
+
* @param options
|
|
376
|
+
* @returns
|
|
377
|
+
*/
|
|
378
|
+
public exec: FetchMethodType<
|
|
379
|
+
Command<
|
|
380
|
+
ResponseType,
|
|
381
|
+
RequestDataType,
|
|
382
|
+
QueryParamsType,
|
|
383
|
+
GlobalErrorType,
|
|
384
|
+
LocalErrorType,
|
|
385
|
+
EndpointType,
|
|
386
|
+
ClientOptions,
|
|
387
|
+
HasData,
|
|
388
|
+
HasParams,
|
|
389
|
+
HasQuery,
|
|
390
|
+
MappedData
|
|
391
|
+
>
|
|
392
|
+
> = async (
|
|
393
|
+
options?: FetchType<
|
|
394
|
+
Command<
|
|
395
|
+
ResponseType,
|
|
396
|
+
RequestDataType,
|
|
397
|
+
QueryParamsType,
|
|
398
|
+
GlobalErrorType,
|
|
399
|
+
LocalErrorType,
|
|
400
|
+
EndpointType,
|
|
401
|
+
ClientOptions,
|
|
402
|
+
HasData,
|
|
403
|
+
HasParams,
|
|
404
|
+
HasQuery,
|
|
405
|
+
MappedData
|
|
406
|
+
>
|
|
407
|
+
>,
|
|
408
|
+
) => {
|
|
409
|
+
const { client } = this.builder;
|
|
410
|
+
const command = this.clone(options as any);
|
|
411
|
+
|
|
412
|
+
const requestId = getUniqueRequestId(this.queueKey);
|
|
413
|
+
|
|
414
|
+
return client(command, requestId);
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Method used to perform requests with usage of cache and queues
|
|
419
|
+
* @param options
|
|
420
|
+
* @param requestCallback
|
|
421
|
+
*/
|
|
422
|
+
public send: FetchMethodType<
|
|
423
|
+
Command<
|
|
424
|
+
ResponseType,
|
|
425
|
+
RequestDataType,
|
|
426
|
+
QueryParamsType,
|
|
427
|
+
GlobalErrorType,
|
|
428
|
+
LocalErrorType,
|
|
429
|
+
EndpointType,
|
|
430
|
+
ClientOptions,
|
|
431
|
+
HasData,
|
|
432
|
+
HasParams,
|
|
433
|
+
HasQuery,
|
|
434
|
+
MappedData
|
|
435
|
+
>
|
|
436
|
+
> = async (
|
|
437
|
+
options?: FetchType<
|
|
438
|
+
Command<
|
|
439
|
+
ResponseType,
|
|
440
|
+
RequestDataType,
|
|
441
|
+
QueryParamsType,
|
|
442
|
+
GlobalErrorType,
|
|
443
|
+
LocalErrorType,
|
|
444
|
+
EndpointType,
|
|
445
|
+
ClientOptions,
|
|
446
|
+
HasData,
|
|
447
|
+
HasParams,
|
|
448
|
+
HasQuery,
|
|
449
|
+
MappedData
|
|
450
|
+
>
|
|
451
|
+
>,
|
|
452
|
+
) => {
|
|
453
|
+
const { dispatcherType, ...rest } = options || {};
|
|
454
|
+
|
|
455
|
+
const command = this.clone(rest as any);
|
|
456
|
+
return commandSendRequest<
|
|
457
|
+
Command<
|
|
458
|
+
ResponseType,
|
|
459
|
+
RequestDataType,
|
|
460
|
+
QueryParamsType,
|
|
461
|
+
GlobalErrorType,
|
|
462
|
+
LocalErrorType,
|
|
463
|
+
EndpointType,
|
|
464
|
+
ClientOptions,
|
|
465
|
+
HasData,
|
|
466
|
+
HasParams,
|
|
467
|
+
HasQuery,
|
|
468
|
+
MappedData
|
|
469
|
+
>
|
|
470
|
+
>(command, options);
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// Typescript test cases
|
|
475
|
+
|
|
476
|
+
// const builder = new Builder({
|
|
477
|
+
// baseUrl: "http://localhost:3000",
|
|
478
|
+
// });
|
|
479
|
+
|
|
480
|
+
// const getUsers = builder.createCommand<{ id: string }[]>()({
|
|
481
|
+
// method: "GET",
|
|
482
|
+
// endpoint: "/users",
|
|
483
|
+
// });
|
|
484
|
+
|
|
485
|
+
// const getUser = builder.createCommand<{ id: string }>()({
|
|
486
|
+
// method: "GET",
|
|
487
|
+
// endpoint: "/users/:id",
|
|
488
|
+
// });
|
|
489
|
+
|
|
490
|
+
// const postUser = builder.createCommand<{ id: string }, { name: string }>()({
|
|
491
|
+
// method: "POST",
|
|
492
|
+
// endpoint: "/users",
|
|
493
|
+
// });
|
|
494
|
+
|
|
495
|
+
// const patchUser = builder.createCommand<{ id: string }, { name: string }>()({
|
|
496
|
+
// method: "PATCH",
|
|
497
|
+
// endpoint: "/users/:id",
|
|
498
|
+
// });
|
|
499
|
+
|
|
500
|
+
// // OK
|
|
501
|
+
// getUsers.send({ queryParams: "" });
|
|
502
|
+
// getUsers.setQueryParams("").send();
|
|
503
|
+
// // Fail
|
|
504
|
+
// getUsers.send({ data: "" });
|
|
505
|
+
// getUsers.send({ params: "" });
|
|
506
|
+
// getUsers.setQueryParams("").send({ queryParams: "" });
|
|
507
|
+
|
|
508
|
+
// // OK
|
|
509
|
+
// getUser.send({ params: { id: "" }, queryParams: "" });
|
|
510
|
+
// getUser.setParams({ id: "" }).send({ queryParams: "" });
|
|
511
|
+
// // Fail
|
|
512
|
+
// getUser.send({ queryParams: "" });
|
|
513
|
+
// getUser.send();
|
|
514
|
+
// getUser.setParams({ id: "" }).send({ params: { id: "" } });
|
|
515
|
+
|
|
516
|
+
// // OK
|
|
517
|
+
// postUser.send({ data: { name: "" } });
|
|
518
|
+
// postUser.setData({ name: "" }).send();
|
|
519
|
+
// // Fail
|
|
520
|
+
// postUser.send({ queryParams: "" });
|
|
521
|
+
// postUser.send({ data: null });
|
|
522
|
+
// postUser.send();
|
|
523
|
+
// postUser.setData({ name: "" }).send({ data: { name: "" } });
|
|
524
|
+
|
|
525
|
+
// // OK
|
|
526
|
+
// patchUser.send({ params: { id: "" }, data: { name: "" } });
|
|
527
|
+
// patchUser.setParams({ id: "" }).setData({ name: "" }).send();
|
|
528
|
+
// // Fail
|
|
529
|
+
// patchUser.send({ queryParams: "" });
|
|
530
|
+
// patchUser.send({ data: null });
|
|
531
|
+
// patchUser.send();
|
|
532
|
+
// patchUser
|
|
533
|
+
// .setParams({ id: "" })
|
|
534
|
+
// .setData({ name: "" })
|
|
535
|
+
// .send({ data: { name: "" } });
|
|
536
|
+
// patchUser
|
|
537
|
+
// .setParams({ id: "" })
|
|
538
|
+
// .setData({ name: "" })
|
|
539
|
+
// .send({ params: { id: "" } });
|