@hyper-fetch/core 9.5.1 → 9.5.2
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/__tests__/features/dispatcher/dispatcher.retry.spec.ts +220 -0
- package/__tests__/features/request/request.response-mapper-failure.spec.ts +362 -0
- package/dist/dispatcher/dispatcher.d.ts +10 -0
- package/dist/dispatcher/dispatcher.d.ts.map +1 -1
- package/dist/index.mjs +90 -6
- package/dist/index.mjs.map +1 -1
- package/dist/request/request.utils.d.ts +12 -0
- package/dist/request/request.utils.d.ts.map +1 -1
- package/package.json +2 -3
- package/src/dispatcher/dispatcher.ts +49 -1
- package/src/request/request.utils.ts +34 -5
- package/release.config.js +0 -6
|
@@ -2,6 +2,7 @@ import { AdapterInstance, ProgressType, RequestResponseType, ResponseType } from
|
|
|
2
2
|
import { Dispatcher } from '../dispatcher';
|
|
3
3
|
import { ExtractAdapterType } from '../types';
|
|
4
4
|
import { ProgressEventType, RequestInstance, RequestJSON, RequestSendOptionsType } from './request.types';
|
|
5
|
+
type MappedResponse = ResponseType<any, any, AdapterInstance> | Promise<ResponseType<any, any, AdapterInstance>> | undefined;
|
|
5
6
|
export declare const scopeKey: (key: string, scope: string | null) => string;
|
|
6
7
|
export declare const stringifyKey: (value: unknown) => string;
|
|
7
8
|
export declare const getProgressValue: ({ loaded, total }: ProgressEventType) => number;
|
|
@@ -20,6 +21,17 @@ export declare const getSimpleKey: (request: RequestInstance | RequestJSON<Reque
|
|
|
20
21
|
*/
|
|
21
22
|
export declare const getRequestKey: (request: RequestInstance | RequestJSON<RequestInstance>, useInitialValues?: boolean) => string;
|
|
22
23
|
export declare const getRequestDispatcher: <Request extends RequestInstance>(request: Request, dispatcherType?: "auto" | "fetch" | "submit") => [Dispatcher<ExtractAdapterType<Request>>, isFetchDispatcher: boolean];
|
|
24
|
+
/**
|
|
25
|
+
* Runs the response mapper of the request. Mapper is user code running in the middle of the response handling,
|
|
26
|
+
* so its failure is logged and ends up as a failed response carrying the thrown value as the `error`.
|
|
27
|
+
* Failures are always delivered through a promise, so sync and async mappers fail the same way.
|
|
28
|
+
* @internal
|
|
29
|
+
* @param request request with the optional response mapper
|
|
30
|
+
* @param response response to be mapped
|
|
31
|
+
* @returns mapped response, promise of it for async and failed mappers, `undefined` when there is no mapper
|
|
32
|
+
*/
|
|
33
|
+
export declare const runResponseMapper: <Request extends RequestInstance>(request: Request, response: ResponseType<any, any, AdapterInstance>) => MappedResponse;
|
|
23
34
|
export declare const mapResponseForSend: <Request extends RequestInstance>(request: Request, response: ResponseType<any, any, AdapterInstance>) => Promise<RequestResponseType<Request>>;
|
|
24
35
|
export declare const sendRequest: <Request extends RequestInstance>(request: Request, options?: RequestSendOptionsType<Request>) => Promise<RequestResponseType<Request>>;
|
|
36
|
+
export {};
|
|
25
37
|
//# sourceMappingURL=request.utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.utils.d.ts","sourceRoot":"","sources":["../../src/request/request.utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEhG,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,OAAO,KAAK,EAAE,kBAAkB,EAAoB,MAAM,OAAO,CAAC;AAElE,OAAO,KAAK,EAGV,iBAAiB,EACjB,eAAe,EACf,WAAW,EACX,sBAAsB,EACvB,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"request.utils.d.ts","sourceRoot":"","sources":["../../src/request/request.utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEhG,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,OAAO,KAAK,EAAE,kBAAkB,EAAoB,MAAM,OAAO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AAM,iBAAiB,GAAI,OAAO,SAAS,eAAe,EAC/D,SAAS,OAAO,EAChB,UAAU,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,eAAe,CAAC,KAChD,cAkBF,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAU,OAAO,SAAS,eAAe,EACtE,SAAS,OAAO,EAChB,UAAU,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,eAAe,CAAC,KAChD,OAAO,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAQtC,CAAC;AAuCF,eAAO,MAAM,WAAW,GAAU,OAAO,SAAS,eAAe,EAC/D,SAAS,OAAO,EAChB,UAAU,sBAAsB,CAAC,OAAO,CAAC,KACxC,OAAO,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAkMtC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/core",
|
|
3
|
-
"version": "9.5.
|
|
3
|
+
"version": "9.5.2",
|
|
4
4
|
"description": "Cache, Queue and Persist your requests no matter if you are online or offline!",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
|
@@ -16,8 +16,7 @@
|
|
|
16
16
|
"lint": "oxlint .",
|
|
17
17
|
"format": "oxfmt --write .",
|
|
18
18
|
"typecheck": "tsgo --noEmit --emitDeclarationOnly false && tsgo --noEmit -p __tests__/tsconfig.json",
|
|
19
|
-
"tests": "pnpm lint-staged"
|
|
20
|
-
"release": "pnpm semantic-release --extends ./release.config.js -t 'hyper-fetch-v${version}'"
|
|
19
|
+
"tests": "pnpm lint-staged"
|
|
21
20
|
},
|
|
22
21
|
"publishConfig": {
|
|
23
22
|
"access": "public",
|
|
@@ -27,6 +27,7 @@ export class Dispatcher<Adapter extends AdapterInstance> {
|
|
|
27
27
|
|
|
28
28
|
private requestCount = new Map<string, number>();
|
|
29
29
|
private runningRequests = new Map<string, RunningRequestValueType[]>();
|
|
30
|
+
private retryTimers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
30
31
|
|
|
31
32
|
private logger: LoggerMethods;
|
|
32
33
|
private client: ClientInstance<{ adapter: Adapter }>;
|
|
@@ -203,6 +204,8 @@ export class Dispatcher<Adapter extends AdapterInstance> {
|
|
|
203
204
|
this.events.emitQueueChanged(newQueue);
|
|
204
205
|
this.events.emitDrained(newQueue);
|
|
205
206
|
|
|
207
|
+
this.removeWaitingRetries(queue);
|
|
208
|
+
|
|
206
209
|
return newQueue;
|
|
207
210
|
};
|
|
208
211
|
|
|
@@ -257,14 +260,41 @@ export class Dispatcher<Adapter extends AdapterInstance> {
|
|
|
257
260
|
*/
|
|
258
261
|
clear = () => {
|
|
259
262
|
const keys = this.getQueuesKeys();
|
|
263
|
+
const queues = keys.map((queryKey) => this.getQueue(queryKey));
|
|
260
264
|
keys.forEach((queryKey) => this.cancelRunningRequests(queryKey));
|
|
261
265
|
|
|
262
266
|
[...this.runningRequests.keys()].forEach((queryKey) => this.setRunningRequests(queryKey, []));
|
|
263
267
|
this.runningRequests.clear();
|
|
264
268
|
this.storage.clear();
|
|
269
|
+
queues.forEach((queue) => this.removeWaitingRetries(queue));
|
|
265
270
|
this.client.triggerPlugins("onDispatcherCleared", { dispatcher: this });
|
|
266
271
|
};
|
|
267
272
|
|
|
273
|
+
/**
|
|
274
|
+
* Drop the scheduled retry of the request
|
|
275
|
+
*/
|
|
276
|
+
private cancelRetry = (requestId: string) => {
|
|
277
|
+
const timer = this.retryTimers.get(requestId);
|
|
278
|
+
if (timer === undefined) {
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
clearTimeout(timer);
|
|
283
|
+
return this.retryTimers.delete(requestId);
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Clearing does not announce removed items. Requests waiting for a retry have no attempt left to end with,
|
|
288
|
+
* so without this event their listeners would never be notified.
|
|
289
|
+
*/
|
|
290
|
+
private removeWaitingRetries = (queue: ResolvedQueueDataType) => {
|
|
291
|
+
queue.requests.forEach(({ requestId, request, resolved }) => {
|
|
292
|
+
if (this.cancelRetry(requestId)) {
|
|
293
|
+
this.client.requestManager.events.emitRemove({ requestId, request, resolved });
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
};
|
|
297
|
+
|
|
268
298
|
// *********************************************************************
|
|
269
299
|
// *********************************************************************
|
|
270
300
|
// Requests
|
|
@@ -506,6 +536,8 @@ export class Dispatcher<Adapter extends AdapterInstance> {
|
|
|
506
536
|
|
|
507
537
|
if (!queueItem) {return;}
|
|
508
538
|
|
|
539
|
+
this.cancelRetry(requestId);
|
|
540
|
+
|
|
509
541
|
queue.requests = queue.requests.filter((req) => req.requestId !== requestId);
|
|
510
542
|
this.storage.set(queryKey, queue);
|
|
511
543
|
|
|
@@ -680,8 +712,23 @@ export class Dispatcher<Adapter extends AdapterInstance> {
|
|
|
680
712
|
// On retry
|
|
681
713
|
if (willRetry) {
|
|
682
714
|
this.logger.debug({ title: "Waiting for retry", type: "system", extra: { response, requestDetails, request } });
|
|
715
|
+
// Flushing the queue during the wait sends the request earlier - only the latest failure schedules the retry
|
|
716
|
+
this.cancelRetry(requestId);
|
|
683
717
|
// Perform retry once request is failed
|
|
684
|
-
setTimeout(() => {
|
|
718
|
+
const retryTimer = setTimeout(() => {
|
|
719
|
+
this.retryTimers.delete(requestId);
|
|
720
|
+
|
|
721
|
+
// Dispatcher methods cancel this timer, so the item was taken directly from the storage and nobody announced it
|
|
722
|
+
if (!this.getRequest(queryKey, requestId)) {
|
|
723
|
+
this.logger.debug({
|
|
724
|
+
title: "Request no longer in queue, skipping retry",
|
|
725
|
+
type: "request",
|
|
726
|
+
extra: { response, requestDetails, request, requestId },
|
|
727
|
+
});
|
|
728
|
+
requestManager.events.emitRemove({ requestId, request, resolved: storageItem.resolved });
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
|
|
685
732
|
this.logger.warning({
|
|
686
733
|
title: "Error response, performing retry",
|
|
687
734
|
type: "request",
|
|
@@ -692,6 +739,7 @@ export class Dispatcher<Adapter extends AdapterInstance> {
|
|
|
692
739
|
retries: storageItem.retries + 1,
|
|
693
740
|
});
|
|
694
741
|
}, retryTime || 0);
|
|
742
|
+
this.retryTimers.set(requestId, retryTimer);
|
|
695
743
|
} else {
|
|
696
744
|
if (request.retry) {
|
|
697
745
|
this.logger.error({
|
|
@@ -138,11 +138,43 @@ export const getRequestDispatcher = <Request extends RequestInstance>(
|
|
|
138
138
|
return [dispatcher, isFetchDispatcher];
|
|
139
139
|
};
|
|
140
140
|
|
|
141
|
+
/**
|
|
142
|
+
* Runs the response mapper of the request. Mapper is user code running in the middle of the response handling,
|
|
143
|
+
* so its failure is logged and ends up as a failed response carrying the thrown value as the `error`.
|
|
144
|
+
* Failures are always delivered through a promise, so sync and async mappers fail the same way.
|
|
145
|
+
* @internal
|
|
146
|
+
* @param request request with the optional response mapper
|
|
147
|
+
* @param response response to be mapped
|
|
148
|
+
* @returns mapped response, promise of it for async and failed mappers, `undefined` when there is no mapper
|
|
149
|
+
*/
|
|
150
|
+
export const runResponseMapper = <Request extends RequestInstance>(
|
|
151
|
+
request: Request,
|
|
152
|
+
response: ResponseType<any, any, AdapterInstance>,
|
|
153
|
+
): MappedResponse => {
|
|
154
|
+
const getFailedResponse = (error: unknown) => {
|
|
155
|
+
request.client.logger.error({
|
|
156
|
+
title: "Response mapper failed",
|
|
157
|
+
type: "system",
|
|
158
|
+
extra: { error, request, response },
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
return { ...response, data: null, error, success: false } as ResponseType<any, any, AdapterInstance>;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
const mapping = request.unstable_responseMapper?.(response as ResponseType<any, any, ExtractAdapterType<Request>>);
|
|
166
|
+
|
|
167
|
+
return mapping instanceof Promise ? mapping.catch(getFailedResponse) : mapping;
|
|
168
|
+
} catch (error) {
|
|
169
|
+
return Promise.resolve(getFailedResponse(error));
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
141
173
|
export const mapResponseForSend = async <Request extends RequestInstance>(
|
|
142
174
|
request: Request,
|
|
143
175
|
response: ResponseType<any, any, AdapterInstance>,
|
|
144
176
|
): Promise<RequestResponseType<Request>> => {
|
|
145
|
-
const mapping = request
|
|
177
|
+
const mapping = runResponseMapper(request, response);
|
|
146
178
|
|
|
147
179
|
if (mapping instanceof Promise) {
|
|
148
180
|
return (await mapping) as RequestResponseType<Request>;
|
|
@@ -240,10 +272,7 @@ export const sendRequest = async <Request extends RequestInstance>(
|
|
|
240
272
|
const mappings = new WeakMap<RequestResponseEventType<Request>, MappedResponse>();
|
|
241
273
|
const getMapping = (values: RequestResponseEventType<Request>) => {
|
|
242
274
|
if (!mappings.has(values)) {
|
|
243
|
-
mappings.set(
|
|
244
|
-
values,
|
|
245
|
-
request.unstable_responseMapper?.(values.response as ResponseType<any, any, ExtractAdapterType<Request>>),
|
|
246
|
-
);
|
|
275
|
+
mappings.set(values, runResponseMapper(request, values.response));
|
|
247
276
|
}
|
|
248
277
|
return mappings.get(values);
|
|
249
278
|
};
|
package/release.config.js
DELETED