@hyper-fetch/core 10.4.1 → 10.5.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.
- package/__tests__/features/request/request.exec.spec.ts +129 -0
- package/__tests__/features/request/request.signal.spec.ts +243 -0
- package/__tests__/features/request/request.throw-on-error.spec.ts +219 -0
- package/__tests__/features/request/request.typescript.spec.ts +46 -0
- package/dist/index.mjs +1529 -1394
- package/dist/index.mjs.map +1 -1
- package/dist/infinite/infinite.d.ts.map +1 -1
- package/dist/request/request.d.ts +10 -2
- package/dist/request/request.d.ts.map +1 -1
- package/dist/request/request.types.d.ts +15 -0
- package/dist/request/request.types.d.ts.map +1 -1
- package/dist/request/request.utils.d.ts +19 -1
- package/dist/request/request.utils.d.ts.map +1 -1
- package/dist/sdk/sdk.d.ts +1 -0
- package/dist/sdk/sdk.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/infinite/infinite.ts +2 -1
- package/src/request/request.ts +95 -26
- package/src/request/request.types.ts +15 -0
- package/src/request/request.utils.ts +148 -46
- package/src/sdk/sdk.ts +4 -0
|
@@ -14,6 +14,7 @@ import type {
|
|
|
14
14
|
ProgressEventType,
|
|
15
15
|
RequestInstance,
|
|
16
16
|
RequestJSON,
|
|
17
|
+
RequestSendActionsType,
|
|
17
18
|
RequestSendOptionsType,
|
|
18
19
|
RetryTimeFunctionType,
|
|
19
20
|
} from "./request.types";
|
|
@@ -202,6 +203,87 @@ export const mapResponseForSend = async <Request extends RequestInstance>(
|
|
|
202
203
|
return (mapping || response) as RequestResponseType<Request>;
|
|
203
204
|
};
|
|
204
205
|
|
|
206
|
+
/**
|
|
207
|
+
* Passes the lifecycle events of one request id to the callbacks given to `send()` / `exec()` and to the request
|
|
208
|
+
* `$hooks`. Returns the function that removes the listeners.
|
|
209
|
+
*/
|
|
210
|
+
export const subscribeRequestCallbacks = <Request extends RequestInstance>({
|
|
211
|
+
request,
|
|
212
|
+
requestId,
|
|
213
|
+
options,
|
|
214
|
+
mutationContext,
|
|
215
|
+
}: {
|
|
216
|
+
request: Request;
|
|
217
|
+
requestId: string;
|
|
218
|
+
options: RequestSendActionsType<Request> | undefined;
|
|
219
|
+
mutationContext?: OptimisticCallbackResult<any>;
|
|
220
|
+
}) => {
|
|
221
|
+
const { requestManager } = request.client;
|
|
222
|
+
const { $hooks } = request;
|
|
223
|
+
|
|
224
|
+
const unmounts = [
|
|
225
|
+
requestManager.events.onRequestStartById<Request>(requestId, (data) => {
|
|
226
|
+
const enriched = { ...data, mutationContext };
|
|
227
|
+
runUserCode(() => options?.onRequestStart?.(enriched));
|
|
228
|
+
runUserCode(() => $hooks.__emit("onRequestStart", enriched));
|
|
229
|
+
}),
|
|
230
|
+
requestManager.events.onResponseStartById<Request>(requestId, (data) => {
|
|
231
|
+
const enriched = { ...data, mutationContext };
|
|
232
|
+
runUserCode(() => options?.onResponseStart?.(enriched));
|
|
233
|
+
runUserCode(() => $hooks.__emit("onResponseStart", enriched));
|
|
234
|
+
}),
|
|
235
|
+
requestManager.events.onUploadProgressById<Request>(requestId, (data) => {
|
|
236
|
+
const enriched = { ...data, mutationContext };
|
|
237
|
+
runUserCode(() => options?.onUploadProgress?.(enriched));
|
|
238
|
+
runUserCode(() => $hooks.__emit("onUploadProgress", enriched));
|
|
239
|
+
}),
|
|
240
|
+
requestManager.events.onDownloadProgressById<Request>(requestId, (data) => {
|
|
241
|
+
const enriched = { ...data, mutationContext };
|
|
242
|
+
runUserCode(() => options?.onDownloadProgress?.(enriched));
|
|
243
|
+
runUserCode(() => $hooks.__emit("onDownloadProgress", enriched));
|
|
244
|
+
}),
|
|
245
|
+
// Failed attempts which are retried do not reach `onResponse` - this is how they are announced
|
|
246
|
+
requestManager.events.onRetryById<Request>(requestId, (data) => {
|
|
247
|
+
const enriched = { ...data, mutationContext };
|
|
248
|
+
runUserCode(() => options?.onRetry?.(enriched));
|
|
249
|
+
runUserCode(() => $hooks.__emit("onRetry", enriched));
|
|
250
|
+
}),
|
|
251
|
+
];
|
|
252
|
+
|
|
253
|
+
return () => unmounts.forEach((unmount) => unmount());
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Calls `onAbort` once when the signal of the caller aborts - right away when it is aborted already. The signal only
|
|
258
|
+
* triggers the abort controller HyperFetch keeps for the request, so it ends like `request.abort()` does. Returns the
|
|
259
|
+
* function that stops listening.
|
|
260
|
+
*/
|
|
261
|
+
export const linkAbortSignal = (signal: AbortSignal | undefined, onAbort: () => void) => {
|
|
262
|
+
if (!signal) {
|
|
263
|
+
return () => {};
|
|
264
|
+
}
|
|
265
|
+
if (signal.aborted) {
|
|
266
|
+
onAbort();
|
|
267
|
+
return () => {};
|
|
268
|
+
}
|
|
269
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
270
|
+
return () => signal.removeEventListener("abort", onAbort);
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/** Response of a call that was aborted before the adapter could answer it - the same as the adapter gives an abort */
|
|
274
|
+
export const getAbortedResponse = <Request extends RequestInstance>(request: Request): RequestResponseType<Request> => {
|
|
275
|
+
const { adapter } = request.client;
|
|
276
|
+
return {
|
|
277
|
+
data: null,
|
|
278
|
+
error: adapter.unstable_internalErrorMapping(getErrorMessage("abort")) as ExtractErrorType<Request>,
|
|
279
|
+
status: adapter.systemErrorStatus,
|
|
280
|
+
success: false,
|
|
281
|
+
extra: adapter.systemErrorExtra,
|
|
282
|
+
requestTimestamp: Date.now(),
|
|
283
|
+
responseTimestamp: Date.now(),
|
|
284
|
+
};
|
|
285
|
+
};
|
|
286
|
+
|
|
205
287
|
export const sendRequest = async <Request extends RequestInstance>(
|
|
206
288
|
request: Request,
|
|
207
289
|
options?: RequestSendOptionsType<Request>,
|
|
@@ -209,6 +291,12 @@ export const sendRequest = async <Request extends RequestInstance>(
|
|
|
209
291
|
const { client } = request;
|
|
210
292
|
const { requestManager } = client;
|
|
211
293
|
const [dispatcher] = getRequestDispatcher(request, options?.dispatcherType);
|
|
294
|
+
const signal = options?.signal;
|
|
295
|
+
|
|
296
|
+
// Nothing is queued, sent or optimistically changed for a call that was aborted before it started
|
|
297
|
+
if (signal?.aborted) {
|
|
298
|
+
return getAbortedResponse(request);
|
|
299
|
+
}
|
|
212
300
|
|
|
213
301
|
let mutationContext: OptimisticCallbackResult<any> | undefined;
|
|
214
302
|
let pendingOptimistic: OptimisticPendingType | undefined;
|
|
@@ -252,8 +340,14 @@ export const sendRequest = async <Request extends RequestInstance>(
|
|
|
252
340
|
let isResolved = false;
|
|
253
341
|
let isDelivered = false;
|
|
254
342
|
let isSettled = false;
|
|
255
|
-
const requestId = dispatcher.add(request);
|
|
256
343
|
const queryKey = scopeKey(request.queryKey, request.scope);
|
|
344
|
+
// A deduplicated call gets the id of the request it joined, which was in the queue before
|
|
345
|
+
const queuedIds = signal ? new Set(dispatcher.getQueue(queryKey).requests.map((item) => item.requestId)) : null;
|
|
346
|
+
const requestId = dispatcher.add(request);
|
|
347
|
+
const isJoined = !!queuedIds?.has(requestId);
|
|
348
|
+
// Set when the signal of the caller took the request out of the queue before it was sent
|
|
349
|
+
let isRemovedBySignal = false;
|
|
350
|
+
let unlinkSignal = () => {};
|
|
257
351
|
const { $hooks } = request;
|
|
258
352
|
|
|
259
353
|
// Failed attempts stay in the dispatcher queue when they are going to be sent again
|
|
@@ -311,36 +405,7 @@ export const sendRequest = async <Request extends RequestInstance>(
|
|
|
311
405
|
runUserCode(() => options?.onBeforeSent?.(beforeSentData));
|
|
312
406
|
runUserCode(() => $hooks.__emit("onBeforeSent", beforeSentData));
|
|
313
407
|
|
|
314
|
-
const
|
|
315
|
-
const enriched = { ...data, mutationContext };
|
|
316
|
-
runUserCode(() => options?.onRequestStart?.(enriched));
|
|
317
|
-
runUserCode(() => $hooks.__emit("onRequestStart", enriched));
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
const unmountResponseStart = requestManager.events.onResponseStartById<Request>(requestId, (data) => {
|
|
321
|
-
const enriched = { ...data, mutationContext };
|
|
322
|
-
runUserCode(() => options?.onResponseStart?.(enriched));
|
|
323
|
-
runUserCode(() => $hooks.__emit("onResponseStart", enriched));
|
|
324
|
-
});
|
|
325
|
-
|
|
326
|
-
const unmountUpload = requestManager.events.onUploadProgressById<Request>(requestId, (data) => {
|
|
327
|
-
const enriched = { ...data, mutationContext };
|
|
328
|
-
runUserCode(() => options?.onUploadProgress?.(enriched));
|
|
329
|
-
runUserCode(() => $hooks.__emit("onUploadProgress", enriched));
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
const unmountDownload = requestManager.events.onDownloadProgressById<Request>(requestId, (data) => {
|
|
333
|
-
const enriched = { ...data, mutationContext };
|
|
334
|
-
runUserCode(() => options?.onDownloadProgress?.(enriched));
|
|
335
|
-
runUserCode(() => $hooks.__emit("onDownloadProgress", enriched));
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
// Failed attempts which are retried do not reach `onResponse` - this is how they are announced
|
|
339
|
-
const unmountRetry = requestManager.events.onRetryById<Request>(requestId, (data) => {
|
|
340
|
-
const enriched = { ...data, mutationContext };
|
|
341
|
-
runUserCode(() => options?.onRetry?.(enriched));
|
|
342
|
-
runUserCode(() => $hooks.__emit("onRetry", enriched));
|
|
343
|
-
});
|
|
408
|
+
const unmountCallbacks = subscribeRequestCallbacks({ request, requestId, options, mutationContext });
|
|
344
409
|
|
|
345
410
|
// When resolved
|
|
346
411
|
const unmountResponse = requestManager.events.onResponseById<Request>(requestId, (values) => {
|
|
@@ -390,23 +455,33 @@ export const sendRequest = async <Request extends RequestInstance>(
|
|
|
390
455
|
isResolved = true;
|
|
391
456
|
isDelivered = true;
|
|
392
457
|
|
|
458
|
+
// Taken out of the queue by the signal of the caller, it ends the same way an aborted request does
|
|
459
|
+
const removedResponse: RequestResponseType<Request> = isRemovedBySignal
|
|
460
|
+
? getAbortedResponse(request)
|
|
461
|
+
: {
|
|
462
|
+
data: null,
|
|
463
|
+
status: null,
|
|
464
|
+
success: false,
|
|
465
|
+
error: getErrorMessage("deleted") as unknown as ExtractErrorType<Request>,
|
|
466
|
+
extra: request.client.adapter.defaultExtra,
|
|
467
|
+
requestTimestamp: Date.now(),
|
|
468
|
+
responseTimestamp: Date.now(),
|
|
469
|
+
};
|
|
470
|
+
|
|
393
471
|
if (pendingOptimistic && !isSettled) {
|
|
394
472
|
isSettled = true;
|
|
395
|
-
settleOptimistic(
|
|
473
|
+
settleOptimistic(
|
|
474
|
+
pendingOptimistic,
|
|
475
|
+
isRemovedBySignal
|
|
476
|
+
? { type: "rollback", reason: "abort", response: removedResponse, request }
|
|
477
|
+
: { type: "rollback", reason: "removed", request },
|
|
478
|
+
);
|
|
396
479
|
}
|
|
397
480
|
|
|
398
481
|
const enriched = { ...data, mutationContext };
|
|
399
482
|
runUserCode(() => options?.onRemove?.(enriched));
|
|
400
483
|
runUserCode(() => $hooks.__emit("onRemove", enriched));
|
|
401
|
-
resolve(
|
|
402
|
-
data: null,
|
|
403
|
-
status: null,
|
|
404
|
-
success: false,
|
|
405
|
-
error: getErrorMessage("deleted") as unknown as ExtractErrorType<Request>,
|
|
406
|
-
extra: request.client.adapter.defaultExtra,
|
|
407
|
-
requestTimestamp: Date.now(),
|
|
408
|
-
responseTimestamp: Date.now(),
|
|
409
|
-
});
|
|
484
|
+
resolve(removedResponse);
|
|
410
485
|
|
|
411
486
|
// Unmount Listeners
|
|
412
487
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
@@ -414,15 +489,42 @@ export const sendRequest = async <Request extends RequestInstance>(
|
|
|
414
489
|
}
|
|
415
490
|
});
|
|
416
491
|
|
|
492
|
+
// Linked last - an abort can remove the request synchronously and the listeners above have to observe it
|
|
493
|
+
unlinkSignal = linkAbortSignal(signal, () => {
|
|
494
|
+
if (isDelivered) {
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
// The joined request belongs to the other callers as well - only this call stops waiting for it
|
|
498
|
+
if (isJoined) {
|
|
499
|
+
isResolved = true;
|
|
500
|
+
isDelivered = true;
|
|
501
|
+
const aborted = getAbortedResponse(request);
|
|
502
|
+
if (pendingOptimistic && !isSettled) {
|
|
503
|
+
isSettled = true;
|
|
504
|
+
settleOptimistic(pendingOptimistic, { type: "rollback", reason: "abort", response: aborted, request });
|
|
505
|
+
}
|
|
506
|
+
resolve(aborted);
|
|
507
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
508
|
+
umountAll();
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
const abortKey = scopeKey(request.abortKey, request.scope);
|
|
512
|
+
// A running request is aborted like `request.abort()` does, only this one by its id
|
|
513
|
+
if (dispatcher.hasRunningRequest(queryKey, requestId)) {
|
|
514
|
+
requestManager.abortByRequestId(abortKey, requestId);
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
// A queued one, or one waiting for its retry or for the connection, is not sent at all
|
|
518
|
+
isRemovedBySignal = true;
|
|
519
|
+
dispatcher.delete(queryKey, requestId, abortKey);
|
|
520
|
+
});
|
|
521
|
+
|
|
417
522
|
function umountAll() {
|
|
418
523
|
unmountSettlement?.();
|
|
419
|
-
|
|
420
|
-
unmountResponseStart();
|
|
421
|
-
unmountUpload();
|
|
422
|
-
unmountDownload();
|
|
423
|
-
unmountRetry();
|
|
524
|
+
unmountCallbacks();
|
|
424
525
|
unmountResponse();
|
|
425
526
|
unmountRemoveQueueElement();
|
|
527
|
+
unlinkSignal();
|
|
426
528
|
}
|
|
427
529
|
});
|
|
428
530
|
};
|
package/src/sdk/sdk.ts
CHANGED
|
@@ -87,6 +87,7 @@ export type SdkRequestDefaults = {
|
|
|
87
87
|
deduplicate?: boolean;
|
|
88
88
|
deduplicateTime?: number | null;
|
|
89
89
|
structuralSharing?: boolean;
|
|
90
|
+
throwOnError?: boolean;
|
|
90
91
|
};
|
|
91
92
|
|
|
92
93
|
/**
|
|
@@ -207,6 +208,9 @@ const applyObjectDefaults = (request: RequestInstance, config: SdkRequestDefault
|
|
|
207
208
|
if (config.structuralSharing !== undefined) {
|
|
208
209
|
result = result.setStructuralSharing(config.structuralSharing);
|
|
209
210
|
}
|
|
211
|
+
if (config.throwOnError !== undefined) {
|
|
212
|
+
result = result.setThrowOnError(config.throwOnError);
|
|
213
|
+
}
|
|
210
214
|
return result;
|
|
211
215
|
};
|
|
212
216
|
|