@hyper-fetch/core 9.5.1 → 9.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/__tests__/features/dispatcher/dispatcher.retry.spec.ts +220 -0
- package/__tests__/features/infinite/infinite.fetch.spec.ts +213 -0
- package/__tests__/features/infinite/infinite.utils.spec.ts +388 -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.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +354 -6
- package/dist/index.mjs.map +1 -1
- package/dist/infinite/index.d.ts +4 -0
- package/dist/infinite/index.d.ts.map +1 -0
- package/dist/infinite/infinite.d.ts +16 -0
- package/dist/infinite/infinite.d.ts.map +1 -0
- package/dist/infinite/infinite.types.d.ts +151 -0
- package/dist/infinite/infinite.types.d.ts.map +1 -0
- package/dist/infinite/infinite.utils.d.ts +53 -0
- package/dist/infinite/infinite.utils.d.ts.map +1 -0
- 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/index.ts +1 -0
- package/src/infinite/index.ts +3 -0
- package/src/infinite/infinite.ts +75 -0
- package/src/infinite/infinite.types.ts +180 -0
- package/src/infinite/infinite.utils.ts +306 -0
- package/src/request/request.utils.ts +34 -5
- package/release.config.js +0 -6
package/dist/index.mjs
CHANGED
|
@@ -1870,8 +1870,42 @@ var getRequestDispatcher = (request, dispatcherType = "auto") => {
|
|
|
1870
1870
|
const isFetchDispatcher = dispatcherType === "auto" && isSafeRead || dispatcherType === "fetch";
|
|
1871
1871
|
return [isFetchDispatcher ? fetchDispatcher : submitDispatcher, isFetchDispatcher];
|
|
1872
1872
|
};
|
|
1873
|
+
/**
|
|
1874
|
+
* Runs the response mapper of the request. Mapper is user code running in the middle of the response handling,
|
|
1875
|
+
* so its failure is logged and ends up as a failed response carrying the thrown value as the `error`.
|
|
1876
|
+
* Failures are always delivered through a promise, so sync and async mappers fail the same way.
|
|
1877
|
+
* @internal
|
|
1878
|
+
* @param request request with the optional response mapper
|
|
1879
|
+
* @param response response to be mapped
|
|
1880
|
+
* @returns mapped response, promise of it for async and failed mappers, `undefined` when there is no mapper
|
|
1881
|
+
*/
|
|
1882
|
+
var runResponseMapper = (request, response) => {
|
|
1883
|
+
const getFailedResponse = (error) => {
|
|
1884
|
+
request.client.logger.error({
|
|
1885
|
+
title: "Response mapper failed",
|
|
1886
|
+
type: "system",
|
|
1887
|
+
extra: {
|
|
1888
|
+
error,
|
|
1889
|
+
request,
|
|
1890
|
+
response
|
|
1891
|
+
}
|
|
1892
|
+
});
|
|
1893
|
+
return {
|
|
1894
|
+
...response,
|
|
1895
|
+
data: null,
|
|
1896
|
+
error,
|
|
1897
|
+
success: false
|
|
1898
|
+
};
|
|
1899
|
+
};
|
|
1900
|
+
try {
|
|
1901
|
+
const mapping = request.unstable_responseMapper?.(response);
|
|
1902
|
+
return mapping instanceof Promise ? mapping.catch(getFailedResponse) : mapping;
|
|
1903
|
+
} catch (error) {
|
|
1904
|
+
return Promise.resolve(getFailedResponse(error));
|
|
1905
|
+
}
|
|
1906
|
+
};
|
|
1873
1907
|
var mapResponseForSend = async (request, response) => {
|
|
1874
|
-
const mapping = request
|
|
1908
|
+
const mapping = runResponseMapper(request, response);
|
|
1875
1909
|
if (mapping instanceof Promise) return await mapping;
|
|
1876
1910
|
return mapping || response;
|
|
1877
1911
|
};
|
|
@@ -1947,7 +1981,7 @@ var sendRequest = async (request, options) => {
|
|
|
1947
1981
|
};
|
|
1948
1982
|
const mappings = /* @__PURE__ */ new WeakMap();
|
|
1949
1983
|
const getMapping = (values) => {
|
|
1950
|
-
if (!mappings.has(values)) mappings.set(values, request
|
|
1984
|
+
if (!mappings.has(values)) mappings.set(values, runResponseMapper(request, values.response));
|
|
1951
1985
|
return mappings.get(values);
|
|
1952
1986
|
};
|
|
1953
1987
|
const unmountSettlement = mutationContext ? requestManager.events.onResponseById(requestId, (values) => {
|
|
@@ -3021,6 +3055,7 @@ var Dispatcher = class {
|
|
|
3021
3055
|
storage = /* @__PURE__ */ new Map();
|
|
3022
3056
|
requestCount = /* @__PURE__ */ new Map();
|
|
3023
3057
|
runningRequests = /* @__PURE__ */ new Map();
|
|
3058
|
+
retryTimers = /* @__PURE__ */ new Map();
|
|
3024
3059
|
logger;
|
|
3025
3060
|
client;
|
|
3026
3061
|
constructor(options) {
|
|
@@ -3164,10 +3199,11 @@ var Dispatcher = class {
|
|
|
3164
3199
|
* Clear requests from queue cache
|
|
3165
3200
|
*/
|
|
3166
3201
|
clearQueue = (queryKey) => {
|
|
3202
|
+
const queue = this.getQueue(queryKey);
|
|
3167
3203
|
const newQueue = {
|
|
3168
3204
|
queryKey,
|
|
3169
3205
|
requests: [],
|
|
3170
|
-
stopped:
|
|
3206
|
+
stopped: queue.stopped
|
|
3171
3207
|
};
|
|
3172
3208
|
this.storage.set(queryKey, newQueue);
|
|
3173
3209
|
this.client.triggerPlugins("onDispatcherQueueCleared", {
|
|
@@ -3176,6 +3212,7 @@ var Dispatcher = class {
|
|
|
3176
3212
|
});
|
|
3177
3213
|
this.events.emitQueueChanged(newQueue);
|
|
3178
3214
|
this.events.emitDrained(newQueue);
|
|
3215
|
+
this.removeWaitingRetries(queue);
|
|
3179
3216
|
return newQueue;
|
|
3180
3217
|
};
|
|
3181
3218
|
/**
|
|
@@ -3218,13 +3255,38 @@ var Dispatcher = class {
|
|
|
3218
3255
|
* Clear all running requests and storage
|
|
3219
3256
|
*/
|
|
3220
3257
|
clear = () => {
|
|
3221
|
-
|
|
3258
|
+
const keys = this.getQueuesKeys();
|
|
3259
|
+
const queues = keys.map((queryKey) => this.getQueue(queryKey));
|
|
3260
|
+
keys.forEach((queryKey) => this.cancelRunningRequests(queryKey));
|
|
3222
3261
|
[...this.runningRequests.keys()].forEach((queryKey) => this.setRunningRequests(queryKey, []));
|
|
3223
3262
|
this.runningRequests.clear();
|
|
3224
3263
|
this.storage.clear();
|
|
3264
|
+
queues.forEach((queue) => this.removeWaitingRetries(queue));
|
|
3225
3265
|
this.client.triggerPlugins("onDispatcherCleared", { dispatcher: this });
|
|
3226
3266
|
};
|
|
3227
3267
|
/**
|
|
3268
|
+
* Drop the scheduled retry of the request
|
|
3269
|
+
*/
|
|
3270
|
+
cancelRetry = (requestId) => {
|
|
3271
|
+
const timer = this.retryTimers.get(requestId);
|
|
3272
|
+
if (timer === void 0) return false;
|
|
3273
|
+
clearTimeout(timer);
|
|
3274
|
+
return this.retryTimers.delete(requestId);
|
|
3275
|
+
};
|
|
3276
|
+
/**
|
|
3277
|
+
* Clearing does not announce removed items. Requests waiting for a retry have no attempt left to end with,
|
|
3278
|
+
* so without this event their listeners would never be notified.
|
|
3279
|
+
*/
|
|
3280
|
+
removeWaitingRetries = (queue) => {
|
|
3281
|
+
queue.requests.forEach(({ requestId, request, resolved }) => {
|
|
3282
|
+
if (this.cancelRetry(requestId)) this.client.requestManager.events.emitRemove({
|
|
3283
|
+
requestId,
|
|
3284
|
+
request,
|
|
3285
|
+
resolved
|
|
3286
|
+
});
|
|
3287
|
+
});
|
|
3288
|
+
};
|
|
3289
|
+
/**
|
|
3228
3290
|
* Start particular request
|
|
3229
3291
|
*/
|
|
3230
3292
|
startRequest = (queryKey, requestId) => {
|
|
@@ -3424,6 +3486,7 @@ var Dispatcher = class {
|
|
|
3424
3486
|
const queue = this.getQueue(queryKey);
|
|
3425
3487
|
const queueItem = queue.requests.find((req) => req.requestId === requestId);
|
|
3426
3488
|
if (!queueItem) return;
|
|
3489
|
+
this.cancelRetry(requestId);
|
|
3427
3490
|
queue.requests = queue.requests.filter((req) => req.requestId !== requestId);
|
|
3428
3491
|
this.storage.set(queryKey, queue);
|
|
3429
3492
|
if (this.hasRunningRequest(queryKey, requestId)) {
|
|
@@ -3607,7 +3670,27 @@ var Dispatcher = class {
|
|
|
3607
3670
|
request
|
|
3608
3671
|
}
|
|
3609
3672
|
});
|
|
3610
|
-
|
|
3673
|
+
this.cancelRetry(requestId);
|
|
3674
|
+
const retryTimer = setTimeout(() => {
|
|
3675
|
+
this.retryTimers.delete(requestId);
|
|
3676
|
+
if (!this.getRequest(queryKey, requestId)) {
|
|
3677
|
+
this.logger.debug({
|
|
3678
|
+
title: "Request no longer in queue, skipping retry",
|
|
3679
|
+
type: "request",
|
|
3680
|
+
extra: {
|
|
3681
|
+
response,
|
|
3682
|
+
requestDetails,
|
|
3683
|
+
request,
|
|
3684
|
+
requestId
|
|
3685
|
+
}
|
|
3686
|
+
});
|
|
3687
|
+
requestManager.events.emitRemove({
|
|
3688
|
+
requestId,
|
|
3689
|
+
request,
|
|
3690
|
+
resolved: storageItem.resolved
|
|
3691
|
+
});
|
|
3692
|
+
return;
|
|
3693
|
+
}
|
|
3611
3694
|
this.logger.warning({
|
|
3612
3695
|
title: "Error response, performing retry",
|
|
3613
3696
|
type: "request",
|
|
@@ -3623,6 +3706,7 @@ var Dispatcher = class {
|
|
|
3623
3706
|
retries: storageItem.retries + 1
|
|
3624
3707
|
});
|
|
3625
3708
|
}, retryTime || 0);
|
|
3709
|
+
this.retryTimers.set(requestId, retryTimer);
|
|
3626
3710
|
} else {
|
|
3627
3711
|
if (request.retry) this.logger.error({
|
|
3628
3712
|
title: "All retries have been used. Removing request from queue.",
|
|
@@ -4432,6 +4516,270 @@ var createConfiguration = () => {
|
|
|
4432
4516
|
return (defaults) => defaults;
|
|
4433
4517
|
};
|
|
4434
4518
|
//#endregion
|
|
4435
|
-
|
|
4519
|
+
//#region src/infinite/infinite.utils.ts
|
|
4520
|
+
var isEmptyCursor = (value) => value === null || value === void 0 || value === "";
|
|
4521
|
+
var toNumber = (value) => {
|
|
4522
|
+
if (value === null || value === void 0 || value === "") return;
|
|
4523
|
+
const number = Number(value);
|
|
4524
|
+
return Number.isFinite(number) ? number : void 0;
|
|
4525
|
+
};
|
|
4526
|
+
/**
|
|
4527
|
+
* Tells how the list is paged. Throws for a missing or incomplete pagination, because a list without it
|
|
4528
|
+
* would silently fetch the same page forever.
|
|
4529
|
+
*/
|
|
4530
|
+
var getPaginationMode = (pagination) => {
|
|
4531
|
+
if (pagination?.cursor) {
|
|
4532
|
+
if (!pagination.next) throw new Error("Cursor pagination requires the \"next\" option");
|
|
4533
|
+
return "cursor";
|
|
4534
|
+
}
|
|
4535
|
+
if (pagination?.offset) return "offset";
|
|
4536
|
+
if (pagination?.page) return "page";
|
|
4537
|
+
throw new Error("Infinite requests require the \"pagination\" option with a \"page\", \"offset\" or \"cursor\" field");
|
|
4538
|
+
};
|
|
4539
|
+
var getPagingField = (pagination) => {
|
|
4540
|
+
return pagination.cursor ?? pagination.offset ?? pagination.page;
|
|
4541
|
+
};
|
|
4542
|
+
var getSourceValues = (request, pagination) => {
|
|
4543
|
+
const values = request[pagination.in ?? "queryParams"];
|
|
4544
|
+
return values && typeof values === "object" ? values : {};
|
|
4545
|
+
};
|
|
4546
|
+
var getPageLimit = (request, pagination) => {
|
|
4547
|
+
if (!pagination.limit) return;
|
|
4548
|
+
const limit = toNumber(getSourceValues(request, pagination)[pagination.limit]);
|
|
4549
|
+
return limit && limit > 0 ? limit : void 0;
|
|
4550
|
+
};
|
|
4551
|
+
/**
|
|
4552
|
+
* The value the list starts from: what the request already holds, otherwise the first page.
|
|
4553
|
+
*/
|
|
4554
|
+
var getInitialPageParam = (request, pagination) => {
|
|
4555
|
+
const mode = getPaginationMode(pagination);
|
|
4556
|
+
const value = getSourceValues(request, pagination)[getPagingField(pagination)];
|
|
4557
|
+
if (mode === "cursor") return isEmptyCursor(value) ? void 0 : value;
|
|
4558
|
+
return toNumber(value) ?? (mode === "page" ? 1 : 0);
|
|
4559
|
+
};
|
|
4560
|
+
var getFirstPageNumber = (request, pagination) => {
|
|
4561
|
+
return getInitialPageParam(request, pagination) === 0 ? 0 : 1;
|
|
4562
|
+
};
|
|
4563
|
+
var setPagingValue = (request, pagination, value) => {
|
|
4564
|
+
const values = {
|
|
4565
|
+
...getSourceValues(request, pagination),
|
|
4566
|
+
[getPagingField(pagination)]: value
|
|
4567
|
+
};
|
|
4568
|
+
switch (pagination.in) {
|
|
4569
|
+
case "params": return request.setParams(values);
|
|
4570
|
+
case "payload": return request.setPayload(values);
|
|
4571
|
+
default: return request.setQueryParams(values);
|
|
4572
|
+
}
|
|
4573
|
+
};
|
|
4574
|
+
var keyDependencies = /* @__PURE__ */ new WeakMap();
|
|
4575
|
+
/**
|
|
4576
|
+
* Generated keys contain the query params, so usually every page has its own. A custom key, or paging inside
|
|
4577
|
+
* the payload, gives all the pages the same key - they would overwrite each other in one cache slot.
|
|
4578
|
+
*/
|
|
4579
|
+
var getKeyDependencies = (request, pagination) => {
|
|
4580
|
+
let dependencies = keyDependencies.get(request);
|
|
4581
|
+
if (!dependencies) {
|
|
4582
|
+
const first = setPagingValue(request, pagination, "__hyper_fetch_page_a__");
|
|
4583
|
+
const second = setPagingValue(request, pagination, "__hyper_fetch_page_b__");
|
|
4584
|
+
dependencies = {
|
|
4585
|
+
cacheKey: first.cacheKey !== second.cacheKey,
|
|
4586
|
+
queryKey: first.queryKey !== second.queryKey
|
|
4587
|
+
};
|
|
4588
|
+
keyDependencies.set(request, dependencies);
|
|
4589
|
+
}
|
|
4590
|
+
return dependencies;
|
|
4591
|
+
};
|
|
4592
|
+
/**
|
|
4593
|
+
* Creates the request of a single page. Generated keys are left untouched, so a page shares its cache
|
|
4594
|
+
* with any other request for the same data.
|
|
4595
|
+
*/
|
|
4596
|
+
var getPageRequest = (request, pagination, param) => {
|
|
4597
|
+
if (param === void 0) return request;
|
|
4598
|
+
const dependencies = getKeyDependencies(request, pagination);
|
|
4599
|
+
let page = setPagingValue(request, pagination, param);
|
|
4600
|
+
if (!dependencies.cacheKey) page = page.setCacheKey(`${page.cacheKey}_${stringifyKey(param)}`);
|
|
4601
|
+
if (!dependencies.queryKey) page = page.setQueryKey(`${page.queryKey}_${stringifyKey(param)}`);
|
|
4602
|
+
return page;
|
|
4603
|
+
};
|
|
4604
|
+
var getCallbackContext = ({ data, param, pages }) => ({
|
|
4605
|
+
data,
|
|
4606
|
+
param,
|
|
4607
|
+
pages
|
|
4608
|
+
});
|
|
4609
|
+
var readOption = (option, context) => {
|
|
4610
|
+
if (typeof option === "function") return option(getCallbackContext(context));
|
|
4611
|
+
return typeof option === "string" ? context.data?.[option] : void 0;
|
|
4612
|
+
};
|
|
4613
|
+
/**
|
|
4614
|
+
* Items of a single page
|
|
4615
|
+
*/
|
|
4616
|
+
var getPageItems = (context) => {
|
|
4617
|
+
const { pagination, data } = context;
|
|
4618
|
+
const items = pagination.items === void 0 ? data : readOption(pagination.items, context);
|
|
4619
|
+
return Array.isArray(items) ? items : [];
|
|
4620
|
+
};
|
|
4621
|
+
/**
|
|
4622
|
+
* Size of the whole list, when the pagination declares it
|
|
4623
|
+
*/
|
|
4624
|
+
var getPageTotal = (context) => {
|
|
4625
|
+
return toNumber(readOption(context.pagination.total, context)) ?? null;
|
|
4626
|
+
};
|
|
4627
|
+
/**
|
|
4628
|
+
* Paging value of the page after the given one
|
|
4629
|
+
*/
|
|
4630
|
+
var getNextPageParam = (context) => {
|
|
4631
|
+
const { request, pagination, param } = context;
|
|
4632
|
+
const mode = getPaginationMode(pagination);
|
|
4633
|
+
if (mode === "cursor") {
|
|
4634
|
+
const cursor = readOption(pagination.next, context);
|
|
4635
|
+
return isEmptyCursor(cursor) ? void 0 : cursor;
|
|
4636
|
+
}
|
|
4637
|
+
if (mode === "page") return Number(param) + 1;
|
|
4638
|
+
return Number(param) + (getPageLimit(request, pagination) ?? getPageItems(context).length);
|
|
4639
|
+
};
|
|
4640
|
+
/**
|
|
4641
|
+
* Paging value of the page before the given one
|
|
4642
|
+
*/
|
|
4643
|
+
var getPreviousPageParam = (context) => {
|
|
4644
|
+
const { request, pagination, param } = context;
|
|
4645
|
+
const mode = getPaginationMode(pagination);
|
|
4646
|
+
if (mode === "cursor") {
|
|
4647
|
+
const cursor = readOption(pagination.previous, context);
|
|
4648
|
+
return isEmptyCursor(cursor) ? void 0 : cursor;
|
|
4649
|
+
}
|
|
4650
|
+
if (mode === "page") {
|
|
4651
|
+
const previous = Number(param) - 1;
|
|
4652
|
+
return previous >= getFirstPageNumber(request, pagination) ? previous : void 0;
|
|
4653
|
+
}
|
|
4654
|
+
if (Number(param) <= 0) return;
|
|
4655
|
+
const size = getPageLimit(request, pagination) ?? getPageItems(context).length;
|
|
4656
|
+
return Math.max(0, Number(param) - size);
|
|
4657
|
+
};
|
|
4658
|
+
/**
|
|
4659
|
+
* Tells whether the list continues after the given page
|
|
4660
|
+
*/
|
|
4661
|
+
var getHasNextPage = (context) => {
|
|
4662
|
+
const { request, pagination, param } = context;
|
|
4663
|
+
const mode = getPaginationMode(pagination);
|
|
4664
|
+
if (getNextPageParam(context) === void 0) return false;
|
|
4665
|
+
if (pagination.hasNext) return !!pagination.hasNext(getCallbackContext(context));
|
|
4666
|
+
if (mode === "cursor") return true;
|
|
4667
|
+
const items = getPageItems(context);
|
|
4668
|
+
if (items.length === 0) return false;
|
|
4669
|
+
const limit = getPageLimit(request, pagination);
|
|
4670
|
+
const total = getPageTotal(context);
|
|
4671
|
+
if (total !== null) {
|
|
4672
|
+
if (mode === "offset") return Number(param) + items.length < total;
|
|
4673
|
+
return (Number(param) - getFirstPageNumber(request, pagination) + 1) * (limit ?? items.length) < total;
|
|
4674
|
+
}
|
|
4675
|
+
return limit === void 0 || items.length >= limit;
|
|
4676
|
+
};
|
|
4677
|
+
/**
|
|
4678
|
+
* Tells whether the list continues before the given page
|
|
4679
|
+
*/
|
|
4680
|
+
var getHasPreviousPage = (context) => {
|
|
4681
|
+
if (getPreviousPageParam(context) === void 0) return false;
|
|
4682
|
+
return context.pagination.hasPrevious ? !!context.pagination.hasPrevious(getCallbackContext(context)) : true;
|
|
4683
|
+
};
|
|
4684
|
+
/**
|
|
4685
|
+
* Rebuilds a list from already stored pages: from the starting page forwards, then backwards, until a page
|
|
4686
|
+
* is missing. Nothing has to remember which pages were loaded.
|
|
4687
|
+
*/
|
|
4688
|
+
var getCachedPages = ({ request, pagination, read, maxPages = Infinity }) => {
|
|
4689
|
+
const pages = [];
|
|
4690
|
+
const visited = /* @__PURE__ */ new Set();
|
|
4691
|
+
const readPage = (param) => {
|
|
4692
|
+
const key = stringifyKey(param);
|
|
4693
|
+
if (visited.has(key) || pages.length >= maxPages) return null;
|
|
4694
|
+
visited.add(key);
|
|
4695
|
+
const pageRequest = getPageRequest(request, pagination, param);
|
|
4696
|
+
const value = read(pageRequest);
|
|
4697
|
+
return value ? {
|
|
4698
|
+
param,
|
|
4699
|
+
request: pageRequest,
|
|
4700
|
+
value
|
|
4701
|
+
} : null;
|
|
4702
|
+
};
|
|
4703
|
+
const getContext = (page) => ({
|
|
4704
|
+
request,
|
|
4705
|
+
pagination,
|
|
4706
|
+
data: page.value.data,
|
|
4707
|
+
param: page.param,
|
|
4708
|
+
pages: pages.map((item) => item.value.data)
|
|
4709
|
+
});
|
|
4710
|
+
const first = readPage(getInitialPageParam(request, pagination));
|
|
4711
|
+
if (!first) return pages;
|
|
4712
|
+
pages.push(first);
|
|
4713
|
+
while (true) {
|
|
4714
|
+
const last = pages.at(-1);
|
|
4715
|
+
const param = getHasNextPage(getContext(last)) ? getNextPageParam(getContext(last)) : void 0;
|
|
4716
|
+
const page = param === void 0 ? null : readPage(param);
|
|
4717
|
+
if (!page) break;
|
|
4718
|
+
pages.push(page);
|
|
4719
|
+
}
|
|
4720
|
+
while (true) {
|
|
4721
|
+
const param = getHasPreviousPage(getContext(pages[0])) ? getPreviousPageParam(getContext(pages[0])) : void 0;
|
|
4722
|
+
const page = param === void 0 ? null : readPage(param);
|
|
4723
|
+
if (!page) break;
|
|
4724
|
+
pages.unshift(page);
|
|
4725
|
+
}
|
|
4726
|
+
return pages;
|
|
4727
|
+
};
|
|
4728
|
+
//#endregion
|
|
4729
|
+
//#region src/infinite/infinite.ts
|
|
4730
|
+
/**
|
|
4731
|
+
* Fetches the first pages of a list one after another, without touching the cache or the queues.
|
|
4732
|
+
* Made for server rendering: pass `dehydrated` to the `Hydrate` component to render them on the client.
|
|
4733
|
+
*
|
|
4734
|
+
* @example
|
|
4735
|
+
* ```ts
|
|
4736
|
+
* const { dehydrated } = await fetchInfinite(getFeed, {
|
|
4737
|
+
* pagination: { cursor: "cursor", next: "nextCursor", items: "data" },
|
|
4738
|
+
* pages: 2,
|
|
4739
|
+
* });
|
|
4740
|
+
* ```
|
|
4741
|
+
*/
|
|
4742
|
+
var fetchInfinite = async (request, options) => {
|
|
4743
|
+
const pagination = options.pagination;
|
|
4744
|
+
const pageCount = Math.max(1, options.pages ?? 1);
|
|
4745
|
+
const result = {
|
|
4746
|
+
items: [],
|
|
4747
|
+
pages: [],
|
|
4748
|
+
pageParams: [],
|
|
4749
|
+
total: null,
|
|
4750
|
+
hasNextPage: false,
|
|
4751
|
+
error: null,
|
|
4752
|
+
responses: [],
|
|
4753
|
+
dehydrated: []
|
|
4754
|
+
};
|
|
4755
|
+
let param = getInitialPageParam(request, pagination);
|
|
4756
|
+
for (let index = 0; index < pageCount; index += 1) {
|
|
4757
|
+
const pageRequest = getPageRequest(request, pagination, param);
|
|
4758
|
+
const response = await pageRequest.exec({});
|
|
4759
|
+
result.responses.push(response);
|
|
4760
|
+
if (!response.success) {
|
|
4761
|
+
result.error = response.error;
|
|
4762
|
+
break;
|
|
4763
|
+
}
|
|
4764
|
+
const context = {
|
|
4765
|
+
request,
|
|
4766
|
+
pagination,
|
|
4767
|
+
data: response.data,
|
|
4768
|
+
param,
|
|
4769
|
+
pages: result.pages
|
|
4770
|
+
};
|
|
4771
|
+
result.pages.push(response.data);
|
|
4772
|
+
result.pageParams.push(param);
|
|
4773
|
+
result.items.push(...getPageItems(context));
|
|
4774
|
+
result.total = getPageTotal(context) ?? result.total;
|
|
4775
|
+
result.hasNextPage = getHasNextPage(context);
|
|
4776
|
+
result.dehydrated.push(pageRequest.dehydrate({ response }));
|
|
4777
|
+
if (!result.hasNextPage) break;
|
|
4778
|
+
param = getNextPageParam(context);
|
|
4779
|
+
}
|
|
4780
|
+
return result;
|
|
4781
|
+
};
|
|
4782
|
+
//#endregion
|
|
4783
|
+
export { AbortError, Adapter, AppEvents, AppManager, Cache, Client, DeletedError, Dispatcher, DispatcherMode, EventEmitter, HttpAdapter, HttpMethods, LoggerManager, Plugin, Request, RequestManager, RequestProcessingError, SAFE_HTTP_METHODS, Time, TimeoutError, UnexpectedError, appManagerInitialOptions, canRetryRequest, createClient, createConfiguration, createRequestHooks, createSdk, defaultMapper, defaultTimeout, fetchInfinite, getAbortByAbortKey, getAbortByIdKey, getAbortKey, getAdapter, getAdapterBindings, getAdapterHeaders, getAdapterOnError, getAdapterPayload, getAppManagerEvents, getCacheByKey, getCacheData, getCacheEvents, getCacheKey, getCachedPages, getDeleteByKey, getDeleteKey, getDispatcherChangeByKey, getDispatcherChangeKey, getDispatcherDrainedByKey, getDispatcherDrainedKey, getDispatcherEvents, getDispatcherRunningByKey, getDispatcherRunningKey, getDispatcherStatusByKey, getDispatcherStatusKey, getDownloadProgressByIdKey, getDownloadProgressByQueryKey, getDownloadProgressKey, getErrorMessage, getHasNextPage, getHasPreviousPage, getInitialPageParam, getInvalidateByKey, getInvalidateKey, getIsEqualTimestamp, getLoadingByCacheKey, getLoadingByIdKey, getLoadingByQueryKey, getLoadingKey, getNextPageParam, getPageItems, getPageRequest, getPageTotal, getPaginationMode, getPreviousPageParam, getProgressData, getProgressValue, getRemoveByIdKey, getRemoveByQueryKey, getRemoveKey, getRequestDeduplicatedByCacheKey, getRequestDeduplicatedByIdKey, getRequestDeduplicatedByQueryKey, getRequestDeduplicatedKey, getRequestDispatcher, getRequestEta, getRequestKey, getRequestManagerEvents, getRequestStarByQueryKey, getRequestStartByIdKey, getRequestStartKey, getRequestType, getResponseByCacheKey, getResponseByIdKey, getResponseHeaders, getResponseKey, getResponseStartByIdKey, getResponseStartByQueryKey, getResponseStartKey, getSimpleKey, getTime, getUniqueRequestId, getUploadProgressByIdKey, getUploadProgressByQueryKey, getUploadProgressKey, handleResponse, hasDocument, hasWindow, interceptRequest, interceptResponse, isNavigatorOnline, isPlainObject, logLevelOrder, logger, loggerColors, loggerStyles, mapResponseForSend, mocker, onDocumentEvent, onWindowEvent, parseErrorResponse, parseResponse, replaceEqualDeep, resolveClientMode, runResponseMapper, scopeKey, sendRequest, stringifyDefaultOptions, stringifyKey, stringifyQueryParams, stringifyValue, xhrExtra };
|
|
4436
4784
|
|
|
4437
4785
|
//# sourceMappingURL=index.mjs.map
|