@hyper-fetch/core 9.5.2 → 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.
@@ -0,0 +1,4 @@
1
+ export * from './infinite';
2
+ export * from './infinite.types';
3
+ export * from './infinite.utils';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/infinite/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { RequestInstance } from '../request';
2
+ import { FetchInfiniteOptions, FetchInfiniteResult, Pagination } from './infinite.types';
3
+ /**
4
+ * Fetches the first pages of a list one after another, without touching the cache or the queues.
5
+ * Made for server rendering: pass `dehydrated` to the `Hydrate` component to render them on the client.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const { dehydrated } = await fetchInfinite(getFeed, {
10
+ * pagination: { cursor: "cursor", next: "nextCursor", items: "data" },
11
+ * pages: 2,
12
+ * });
13
+ * ```
14
+ */
15
+ export declare const fetchInfinite: <R extends RequestInstance, const P extends Pagination<R>>(request: R, options: FetchInfiniteOptions<P>) => Promise<FetchInfiniteResult<R, P>>;
16
+ //# sourceMappingURL=infinite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"infinite.d.ts","sourceRoot":"","sources":["../../src/infinite/infinite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAG/C,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,UAAU,EAAoB,MAAM,kBAAkB,CAAC;AAUhH;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,aAAa,GAAU,CAAC,SAAS,eAAe,EAAE,KAAK,CAAC,CAAC,SAAS,UAAU,CAAC,CAAC,CAAC,EAC1F,SAAS,CAAC,EACV,SAAS,oBAAoB,CAAC,CAAC,CAAC,KAC/B,OAAO,CAAC,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,CA8CnC,CAAC"}
@@ -0,0 +1,151 @@
1
+ import { ResponseType } from '../adapter';
2
+ import { RequestInstance } from '../request';
3
+ import { ExtractAdapterType, ExtractErrorType, ExtractParamsType, ExtractPayloadType, ExtractQueryParamsType, ExtractResponseType, HydrateDataType } from '../types';
4
+ /** Part of the request that holds the paging field */
5
+ export type PaginationSource = "queryParams" | "params" | "payload";
6
+ export type PaginationMode = "page" | "offset" | "cursor";
7
+ type PaginationFields<R, In extends PaginationSource> = NonNullable<In extends "params" ? ExtractParamsType<R> : In extends "payload" ? ExtractPayloadType<R> : ExtractQueryParamsType<R>>;
8
+ type KeyOf<T> = Extract<keyof T, string>;
9
+ type NumericFieldKey<T> = {
10
+ [K in KeyOf<T>]-?: number extends NonNullable<T[K]> ? K : never;
11
+ }[KeyOf<T>];
12
+ type ArrayFieldKey<T> = {
13
+ [K in KeyOf<T>]-?: NonNullable<T[K]> extends readonly unknown[] ? K : never;
14
+ }[KeyOf<T>];
15
+ type NumberFieldKey<T> = {
16
+ [K in KeyOf<T>]-?: NonNullable<T[K]> extends number ? K : never;
17
+ }[KeyOf<T>];
18
+ type CursorFieldKey<T> = {
19
+ [K in KeyOf<T>]-?: NonNullable<T[K]> extends string | number ? K : never;
20
+ }[KeyOf<T>];
21
+ /**
22
+ * Passed to every pagination callback
23
+ */
24
+ export type PaginationContext<R> = {
25
+ /** Response data of the page */
26
+ data: ExtractResponseType<R>;
27
+ /** Value of the paging field used to fetch this page */
28
+ param: unknown;
29
+ /** Response data of the pages loaded so far */
30
+ pages: ExtractResponseType<R>[];
31
+ };
32
+ type PaginationItems<R> = (context: PaginationContext<R>) => readonly unknown[];
33
+ type PaginationCursor<R> = (context: PaginationContext<R>) => string | number | null | undefined;
34
+ type PaginationItemsOption<R> = ExtractResponseType<R> extends readonly unknown[] ? {
35
+ /** Where the items are. Not needed when the response is an array. */
36
+ items?: PaginationItems<R>;
37
+ } : {
38
+ /** Array field of the response holding the items, or a function returning them */
39
+ items: ArrayFieldKey<NonNullable<ExtractResponseType<R>>> | PaginationItems<R>;
40
+ };
41
+ type PaginationShared<R> = PaginationItemsOption<R> & {
42
+ /** Number field of the response with the size of the whole list, or a function returning it */
43
+ total?: NumberFieldKey<NonNullable<ExtractResponseType<R>>> | ((context: PaginationContext<R>) => number | null | undefined);
44
+ /** Overrides detecting the end of the list */
45
+ hasNext?: (context: PaginationContext<R>) => boolean;
46
+ /** Overrides detecting the start of the list */
47
+ hasPrevious?: (context: PaginationContext<R>) => boolean;
48
+ };
49
+ type PaginationModes<R, In extends PaginationSource> = {
50
+ /** Numeric field holding the page number */
51
+ page: NumericFieldKey<PaginationFields<R, In>>;
52
+ /** Numeric field holding the page size */
53
+ limit?: NumericFieldKey<PaginationFields<R, In>>;
54
+ offset?: never;
55
+ cursor?: never;
56
+ next?: never;
57
+ previous?: never;
58
+ } | {
59
+ /** Numeric field holding the number of skipped items */
60
+ offset: NumericFieldKey<PaginationFields<R, In>>;
61
+ /** Numeric field holding the page size */
62
+ limit: NumericFieldKey<PaginationFields<R, In>>;
63
+ page?: never;
64
+ cursor?: never;
65
+ next?: never;
66
+ previous?: never;
67
+ } | {
68
+ /** Field holding the cursor */
69
+ cursor: KeyOf<PaginationFields<R, In>>;
70
+ /** Response field with the cursor of the next page, or a function returning it */
71
+ next: CursorFieldKey<NonNullable<ExtractResponseType<R>>> | PaginationCursor<R>;
72
+ /** Response field with the cursor of the previous page, or a function returning it */
73
+ previous?: CursorFieldKey<NonNullable<ExtractResponseType<R>>> | PaginationCursor<R>;
74
+ page?: never;
75
+ offset?: never;
76
+ limit?: never;
77
+ };
78
+ /**
79
+ * Describes which fields of a request page through a list. Every key is checked against the request types.
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * const pagination = { cursor: "cursor", next: "nextCursor", items: "data" } satisfies Pagination<typeof getFeed>;
84
+ * ```
85
+ */
86
+ export type Pagination<R> = PaginationShared<R> & ((PaginationModes<R, "queryParams"> & {
87
+ in?: "queryParams";
88
+ }) | (PaginationModes<R, "params"> & {
89
+ in: "params";
90
+ }) | (PaginationModes<R, "payload"> & {
91
+ in: "payload";
92
+ }));
93
+ type ArrayItem<T> = NonNullable<T> extends readonly (infer Item)[] ? Item : never;
94
+ /**
95
+ * Type of a single list item for the given request and pagination
96
+ */
97
+ export type PaginationItem<R, P> = P extends {
98
+ items: infer Items;
99
+ } ? Items extends (...args: any[]) => infer Output ? ArrayItem<Output> : Items extends keyof NonNullable<ExtractResponseType<R>> ? ArrayItem<NonNullable<ExtractResponseType<R>>[Items]> : never : ArrayItem<ExtractResponseType<R>>;
100
+ /**
101
+ * Untyped form of the pagination, used where the request type is not known
102
+ */
103
+ export type PaginationConfig = {
104
+ page?: string;
105
+ offset?: string;
106
+ limit?: string;
107
+ cursor?: string;
108
+ next?: string | ((context: any) => unknown);
109
+ previous?: string | ((context: any) => unknown);
110
+ items?: string | ((context: any) => readonly unknown[]);
111
+ total?: string | ((context: any) => number | null | undefined);
112
+ hasNext?: (context: any) => boolean;
113
+ hasPrevious?: (context: any) => boolean;
114
+ in?: PaginationSource;
115
+ };
116
+ export type PaginationPageContext = {
117
+ request: RequestInstance;
118
+ pagination: PaginationConfig;
119
+ data: any;
120
+ param: unknown;
121
+ pages: any[];
122
+ };
123
+ export type CachedInfinitePage<T = any> = {
124
+ param: unknown;
125
+ request: RequestInstance;
126
+ value: T;
127
+ };
128
+ export type FetchInfiniteOptions<P> = {
129
+ pagination: P;
130
+ /** How many pages to fetch, one after another. Defaults to 1. */
131
+ pages?: number;
132
+ };
133
+ export type FetchInfiniteResult<R extends RequestInstance, P> = {
134
+ /** Items of all the fetched pages */
135
+ items: PaginationItem<R, P>[];
136
+ /** Response data of the fetched pages */
137
+ pages: ExtractResponseType<R>[];
138
+ /** Paging values of the fetched pages */
139
+ pageParams: unknown[];
140
+ /** Size of the whole list, when the pagination declares `total` */
141
+ total: number | null;
142
+ hasNextPage: boolean;
143
+ /** Error of the page that stopped fetching */
144
+ error: ExtractErrorType<R> | null;
145
+ /** Full responses of the fetched pages */
146
+ responses: ResponseType<ExtractResponseType<R>, ExtractErrorType<R>, ExtractAdapterType<R>>[];
147
+ /** Pass it to `client.hydrate()` or the `Hydrate` component */
148
+ dehydrated: HydrateDataType[];
149
+ };
150
+ export {};
151
+ //# sourceMappingURL=infinite.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"infinite.types.d.ts","sourceRoot":"","sources":["../../src/infinite/infinite.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,KAAK,EACV,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,mBAAmB,EACnB,eAAe,EAChB,MAAM,OAAO,CAAC;AAEf,sDAAsD;AACtD,MAAM,MAAM,gBAAgB,GAAG,aAAa,GAAG,QAAQ,GAAG,SAAS,CAAC;AAEpE,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAE1D,KAAK,gBAAgB,CAAC,CAAC,EAAE,EAAE,SAAS,gBAAgB,IAAI,WAAW,CACjE,EAAE,SAAS,QAAQ,GAAG,iBAAiB,CAAC,CAAC,CAAC,GAAG,EAAE,SAAS,SAAS,GAAG,kBAAkB,CAAC,CAAC,CAAC,GAAG,sBAAsB,CAAC,CAAC,CAAC,CACtH,CAAC;AAEF,KAAK,KAAK,CAAC,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;AAEzC,KAAK,eAAe,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,SAAS,WAAW,CAAC,CAAC,C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,CAAC,CAAC,EAAE,CAAC;IAC9F,+DAA+D;IAC/D,UAAU,EAAE,eAAe,EAAE,CAAC;CAC/B,CAAC"}
@@ -0,0 +1,53 @@
1
+ import { RequestInstance } from '../request';
2
+ import { CachedInfinitePage, PaginationConfig, PaginationMode, PaginationPageContext } from './infinite.types';
3
+ /**
4
+ * Tells how the list is paged. Throws for a missing or incomplete pagination, because a list without it
5
+ * would silently fetch the same page forever.
6
+ */
7
+ export declare const getPaginationMode: (pagination: PaginationConfig) => PaginationMode;
8
+ /**
9
+ * The value the list starts from: what the request already holds, otherwise the first page.
10
+ */
11
+ export declare const getInitialPageParam: (request: RequestInstance, pagination: PaginationConfig) => unknown;
12
+ /**
13
+ * Creates the request of a single page. Generated keys are left untouched, so a page shares its cache
14
+ * with any other request for the same data.
15
+ */
16
+ export declare const getPageRequest: <R extends RequestInstance>(request: R, pagination: PaginationConfig, param: unknown) => R;
17
+ /**
18
+ * Items of a single page
19
+ */
20
+ export declare const getPageItems: (context: PaginationPageContext) => unknown[];
21
+ /**
22
+ * Size of the whole list, when the pagination declares it
23
+ */
24
+ export declare const getPageTotal: (context: PaginationPageContext) => number | null;
25
+ /**
26
+ * Paging value of the page after the given one
27
+ */
28
+ export declare const getNextPageParam: (context: PaginationPageContext) => unknown;
29
+ /**
30
+ * Paging value of the page before the given one
31
+ */
32
+ export declare const getPreviousPageParam: (context: PaginationPageContext) => unknown;
33
+ /**
34
+ * Tells whether the list continues after the given page
35
+ */
36
+ export declare const getHasNextPage: (context: PaginationPageContext) => boolean;
37
+ /**
38
+ * Tells whether the list continues before the given page
39
+ */
40
+ export declare const getHasPreviousPage: (context: PaginationPageContext) => boolean;
41
+ /**
42
+ * Rebuilds a list from already stored pages: from the starting page forwards, then backwards, until a page
43
+ * is missing. Nothing has to remember which pages were loaded.
44
+ */
45
+ export declare const getCachedPages: <T extends {
46
+ data: unknown;
47
+ }>({ request, pagination, read, maxPages, }: {
48
+ request: RequestInstance;
49
+ pagination: PaginationConfig;
50
+ read: (pageRequest: RequestInstance) => T | null | undefined;
51
+ maxPages?: number;
52
+ }) => CachedInfinitePage<T>[];
53
+ //# sourceMappingURL=infinite.utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"infinite.utils.d.ts","sourceRoot":"","sources":["../../src/infinite/infinite.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAG/C,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAYpH;;;GAGG;AACH,eAAO,MAAM,iBAAiB,GAAI,YAAY,gBAAgB,KAAG,cAchE,CAAC;AAmBF;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,SAAS,eAAe,EAAE,YAAY,gBAAgB,KAAG,OAQ5F,CAAC;AA2CF;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,eAAe,EACtD,SAAS,CAAC,EACV,YAAY,gBAAgB,EAC5B,OAAO,OAAO,KACb,CAeF,CAAC;AAWF;;GAEG;AACH,eAAO,MAAM,YAAY,GAAI,SAAS,qBAAqB,KAAG,OAAO,EAIpE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,YAAY,GAAI,SAAS,qBAAqB,KAAG,MAAM,GAAG,IAEtE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,gBAAgB,GAAI,SAAS,qBAAqB,KAAG,OAYjE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,GAAI,SAAS,qBAAqB,KAAG,OAiBrE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,SAAS,qBAAqB,KAAG,OA8B/D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,GAAI,SAAS,qBAAqB,KAAG,OAKnE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,EAAE,0CAKzD;IACD,OAAO,EAAE,eAAe,CAAC;IACzB,UAAU,EAAE,gBAAgB,CAAC;IAC7B,IAAI,EAAE,CAAC,WAAW,EAAE,eAAe,KAAK,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC;IAC7D,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,KAAG,kBAAkB,CAAC,CAAC,CAAC,EAmDxB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyper-fetch/core",
3
- "version": "9.5.2",
3
+ "version": "9.6.0",
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/",
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ export * from "./adapter";
7
7
  export * from "./sdk";
8
8
  export * from "./http-adapter";
9
9
  export * from "./request";
10
+ export * from "./infinite";
10
11
  export * from "./constants/time.constants";
11
12
  export * from "./constants/http.constants";
12
13
  export * from "./managers";
@@ -0,0 +1,3 @@
1
+ export * from "./infinite";
2
+ export * from "./infinite.types";
3
+ export * from "./infinite.utils";
@@ -0,0 +1,75 @@
1
+ import type { RequestInstance } from "request";
2
+ import type { HydrateDataType } from "types";
3
+
4
+ import type { FetchInfiniteOptions, FetchInfiniteResult, Pagination, PaginationConfig } from "./infinite.types";
5
+ import {
6
+ getHasNextPage,
7
+ getInitialPageParam,
8
+ getNextPageParam,
9
+ getPageItems,
10
+ getPageRequest,
11
+ getPageTotal,
12
+ } from "./infinite.utils";
13
+
14
+ /**
15
+ * Fetches the first pages of a list one after another, without touching the cache or the queues.
16
+ * Made for server rendering: pass `dehydrated` to the `Hydrate` component to render them on the client.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const { dehydrated } = await fetchInfinite(getFeed, {
21
+ * pagination: { cursor: "cursor", next: "nextCursor", items: "data" },
22
+ * pages: 2,
23
+ * });
24
+ * ```
25
+ */
26
+ export const fetchInfinite = async <R extends RequestInstance, const P extends Pagination<R>>(
27
+ request: R,
28
+ options: FetchInfiniteOptions<P>,
29
+ ): Promise<FetchInfiniteResult<R, P>> => {
30
+ const pagination = options.pagination as PaginationConfig;
31
+ const pageCount = Math.max(1, options.pages ?? 1);
32
+
33
+ const result: FetchInfiniteResult<any, any> = {
34
+ items: [],
35
+ pages: [],
36
+ pageParams: [],
37
+ total: null,
38
+ hasNextPage: false,
39
+ error: null,
40
+ responses: [],
41
+ dehydrated: [],
42
+ };
43
+
44
+ let param = getInitialPageParam(request, pagination);
45
+
46
+ for (let index = 0; index < pageCount; index += 1) {
47
+ const pageRequest = getPageRequest(request, pagination, param);
48
+ // Pages depend on each other, so they cannot be requested at the same time
49
+ // eslint-disable-next-line no-await-in-loop
50
+ const response = await (pageRequest as RequestInstance).exec({});
51
+
52
+ result.responses.push(response);
53
+
54
+ if (!response.success) {
55
+ result.error = response.error;
56
+ break;
57
+ }
58
+
59
+ const context = { request, pagination, data: response.data, param, pages: result.pages };
60
+
61
+ result.pages.push(response.data);
62
+ result.pageParams.push(param);
63
+ (result.items as unknown[]).push(...getPageItems(context));
64
+ result.total = getPageTotal(context) ?? result.total;
65
+ result.hasNextPage = getHasNextPage(context);
66
+ result.dehydrated.push(pageRequest.dehydrate({ response }) as HydrateDataType);
67
+
68
+ if (!result.hasNextPage) {
69
+ break;
70
+ }
71
+ param = getNextPageParam(context);
72
+ }
73
+
74
+ return result as FetchInfiniteResult<R, P>;
75
+ };
@@ -0,0 +1,180 @@
1
+ import type { ResponseType } from "adapter";
2
+ import type { RequestInstance } from "request";
3
+ import type {
4
+ ExtractAdapterType,
5
+ ExtractErrorType,
6
+ ExtractParamsType,
7
+ ExtractPayloadType,
8
+ ExtractQueryParamsType,
9
+ ExtractResponseType,
10
+ HydrateDataType,
11
+ } from "types";
12
+
13
+ /** Part of the request that holds the paging field */
14
+ export type PaginationSource = "queryParams" | "params" | "payload";
15
+
16
+ export type PaginationMode = "page" | "offset" | "cursor";
17
+
18
+ type PaginationFields<R, In extends PaginationSource> = NonNullable<
19
+ In extends "params" ? ExtractParamsType<R> : In extends "payload" ? ExtractPayloadType<R> : ExtractQueryParamsType<R>
20
+ >;
21
+
22
+ type KeyOf<T> = Extract<keyof T, string>;
23
+
24
+ type NumericFieldKey<T> = { [K in KeyOf<T>]-?: number extends NonNullable<T[K]> ? K : never }[KeyOf<T>];
25
+ type ArrayFieldKey<T> = { [K in KeyOf<T>]-?: NonNullable<T[K]> extends readonly unknown[] ? K : never }[KeyOf<T>];
26
+ type NumberFieldKey<T> = { [K in KeyOf<T>]-?: NonNullable<T[K]> extends number ? K : never }[KeyOf<T>];
27
+ type CursorFieldKey<T> = { [K in KeyOf<T>]-?: NonNullable<T[K]> extends string | number ? K : never }[KeyOf<T>];
28
+
29
+ /**
30
+ * Passed to every pagination callback
31
+ */
32
+ export type PaginationContext<R> = {
33
+ /** Response data of the page */
34
+ data: ExtractResponseType<R>;
35
+ /** Value of the paging field used to fetch this page */
36
+ param: unknown;
37
+ /** Response data of the pages loaded so far */
38
+ pages: ExtractResponseType<R>[];
39
+ };
40
+
41
+ type PaginationItems<R> = (context: PaginationContext<R>) => readonly unknown[];
42
+ type PaginationCursor<R> = (context: PaginationContext<R>) => string | number | null | undefined;
43
+
44
+ type PaginationItemsOption<R> =
45
+ ExtractResponseType<R> extends readonly unknown[]
46
+ ? {
47
+ /** Where the items are. Not needed when the response is an array. */
48
+ items?: PaginationItems<R>;
49
+ }
50
+ : {
51
+ /** Array field of the response holding the items, or a function returning them */
52
+ items: ArrayFieldKey<NonNullable<ExtractResponseType<R>>> | PaginationItems<R>;
53
+ };
54
+
55
+ type PaginationShared<R> = PaginationItemsOption<R> & {
56
+ /** Number field of the response with the size of the whole list, or a function returning it */
57
+ total?:
58
+ | NumberFieldKey<NonNullable<ExtractResponseType<R>>>
59
+ | ((context: PaginationContext<R>) => number | null | undefined);
60
+ /** Overrides detecting the end of the list */
61
+ hasNext?: (context: PaginationContext<R>) => boolean;
62
+ /** Overrides detecting the start of the list */
63
+ hasPrevious?: (context: PaginationContext<R>) => boolean;
64
+ };
65
+
66
+ type PaginationModes<R, In extends PaginationSource> =
67
+ | {
68
+ /** Numeric field holding the page number */
69
+ page: NumericFieldKey<PaginationFields<R, In>>;
70
+ /** Numeric field holding the page size */
71
+ limit?: NumericFieldKey<PaginationFields<R, In>>;
72
+ offset?: never;
73
+ cursor?: never;
74
+ next?: never;
75
+ previous?: never;
76
+ }
77
+ | {
78
+ /** Numeric field holding the number of skipped items */
79
+ offset: NumericFieldKey<PaginationFields<R, In>>;
80
+ /** Numeric field holding the page size */
81
+ limit: NumericFieldKey<PaginationFields<R, In>>;
82
+ page?: never;
83
+ cursor?: never;
84
+ next?: never;
85
+ previous?: never;
86
+ }
87
+ | {
88
+ /** Field holding the cursor */
89
+ cursor: KeyOf<PaginationFields<R, In>>;
90
+ /** Response field with the cursor of the next page, or a function returning it */
91
+ next: CursorFieldKey<NonNullable<ExtractResponseType<R>>> | PaginationCursor<R>;
92
+ /** Response field with the cursor of the previous page, or a function returning it */
93
+ previous?: CursorFieldKey<NonNullable<ExtractResponseType<R>>> | PaginationCursor<R>;
94
+ page?: never;
95
+ offset?: never;
96
+ limit?: never;
97
+ };
98
+
99
+ /**
100
+ * Describes which fields of a request page through a list. Every key is checked against the request types.
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * const pagination = { cursor: "cursor", next: "nextCursor", items: "data" } satisfies Pagination<typeof getFeed>;
105
+ * ```
106
+ */
107
+ export type Pagination<R> = PaginationShared<R> &
108
+ (
109
+ | (PaginationModes<R, "queryParams"> & { in?: "queryParams" })
110
+ | (PaginationModes<R, "params"> & { in: "params" })
111
+ | (PaginationModes<R, "payload"> & { in: "payload" })
112
+ );
113
+
114
+ type ArrayItem<T> = NonNullable<T> extends readonly (infer Item)[] ? Item : never;
115
+
116
+ /**
117
+ * Type of a single list item for the given request and pagination
118
+ */
119
+ export type PaginationItem<R, P> = P extends { items: infer Items }
120
+ ? Items extends (...args: any[]) => infer Output
121
+ ? ArrayItem<Output>
122
+ : Items extends keyof NonNullable<ExtractResponseType<R>>
123
+ ? ArrayItem<NonNullable<ExtractResponseType<R>>[Items]>
124
+ : never
125
+ : ArrayItem<ExtractResponseType<R>>;
126
+
127
+ /**
128
+ * Untyped form of the pagination, used where the request type is not known
129
+ */
130
+ export type PaginationConfig = {
131
+ page?: string;
132
+ offset?: string;
133
+ limit?: string;
134
+ cursor?: string;
135
+ next?: string | ((context: any) => unknown);
136
+ previous?: string | ((context: any) => unknown);
137
+ items?: string | ((context: any) => readonly unknown[]);
138
+ total?: string | ((context: any) => number | null | undefined);
139
+ hasNext?: (context: any) => boolean;
140
+ hasPrevious?: (context: any) => boolean;
141
+ in?: PaginationSource;
142
+ };
143
+
144
+ export type PaginationPageContext = {
145
+ request: RequestInstance;
146
+ pagination: PaginationConfig;
147
+ data: any;
148
+ param: unknown;
149
+ pages: any[];
150
+ };
151
+
152
+ export type CachedInfinitePage<T = any> = {
153
+ param: unknown;
154
+ request: RequestInstance;
155
+ value: T;
156
+ };
157
+
158
+ export type FetchInfiniteOptions<P> = {
159
+ pagination: P;
160
+ /** How many pages to fetch, one after another. Defaults to 1. */
161
+ pages?: number;
162
+ };
163
+
164
+ export type FetchInfiniteResult<R extends RequestInstance, P> = {
165
+ /** Items of all the fetched pages */
166
+ items: PaginationItem<R, P>[];
167
+ /** Response data of the fetched pages */
168
+ pages: ExtractResponseType<R>[];
169
+ /** Paging values of the fetched pages */
170
+ pageParams: unknown[];
171
+ /** Size of the whole list, when the pagination declares `total` */
172
+ total: number | null;
173
+ hasNextPage: boolean;
174
+ /** Error of the page that stopped fetching */
175
+ error: ExtractErrorType<R> | null;
176
+ /** Full responses of the fetched pages */
177
+ responses: ResponseType<ExtractResponseType<R>, ExtractErrorType<R>, ExtractAdapterType<R>>[];
178
+ /** Pass it to `client.hydrate()` or the `Hydrate` component */
179
+ dehydrated: HydrateDataType[];
180
+ };