@robohall/react-query-factory 1.0.4 → 1.1.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/README.md +13 -12
- package/dist/index.d.mts +24 -38
- package/dist/index.d.ts +24 -38
- package/dist/index.js +69 -41
- package/dist/index.mjs +69 -41
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,7 +38,6 @@ const describeInstances = queryFactory({
|
|
|
38
38
|
queryKey: ['ec2:DescribeInstances'],
|
|
39
39
|
queryFn: (params: DescribeInstancesCommandInput, ctx) =>
|
|
40
40
|
ec2.send(new DescribeInstancesCommand(params), { abortSignal: ctx.signal }),
|
|
41
|
-
staleTime: 30_000,
|
|
42
41
|
});
|
|
43
42
|
|
|
44
43
|
function InstanceList() {
|
|
@@ -46,7 +45,6 @@ function InstanceList() {
|
|
|
46
45
|
describeInstances({ Filters: [{ Name: 'instance-state-name', Values: ['running'] }] })
|
|
47
46
|
);
|
|
48
47
|
// query key: ['ec2:DescribeInstances', { Filters: [...] }]
|
|
49
|
-
// staleTime: 30 000 ms, no repetition required
|
|
50
48
|
}
|
|
51
49
|
```
|
|
52
50
|
|
|
@@ -58,7 +56,7 @@ function InstanceList() {
|
|
|
58
56
|
|
|
59
57
|
`DescribeInstances` is paginated. If you have more than 1000 instances, one call won't get them all. The standard approach — chaining `fetchNextPage` calls, accumulating results, checking `NextToken` — is correct but tedious to repeat everywhere.
|
|
60
58
|
|
|
61
|
-
Add `getNextPageParam
|
|
59
|
+
Add `getNextPageParam` and `shouldFetchNextPage` to activate crawling — those two are the only required pieces. `initialPageParam` types `ctx.pageParam` in your `queryFn` (without it, `ctx.pageParam` is `never`). `reduce` folds crawled pages into a single value; without it the result is the last fetched page. **`shouldFetchNextPage`** is called after each page — return `true` to keep fetching, `false` to stop. Use `() => true` to walk every page:
|
|
62
60
|
|
|
63
61
|
```typescript
|
|
64
62
|
import type { Instance, DescribeInstancesCommandInput } from '@aws-sdk/client-ec2';
|
|
@@ -72,11 +70,11 @@ const describeInstances = queryFactory({
|
|
|
72
70
|
),
|
|
73
71
|
getNextPageParam: response => response.NextToken,
|
|
74
72
|
initialPageParam: undefined as string | undefined,
|
|
73
|
+
shouldFetchNextPage: () => true,
|
|
75
74
|
reduce: (acc, page): Instance[] => [
|
|
76
75
|
...(acc ?? []),
|
|
77
76
|
...(page.Reservations?.flatMap(r => r.Instances ?? []) ?? []),
|
|
78
77
|
],
|
|
79
|
-
staleTime: 30_000,
|
|
80
78
|
});
|
|
81
79
|
|
|
82
80
|
function InstanceList() {
|
|
@@ -85,18 +83,21 @@ function InstanceList() {
|
|
|
85
83
|
}
|
|
86
84
|
```
|
|
87
85
|
|
|
88
|
-
|
|
86
|
+
`shouldFetchNextPage` also accepts a `crawlOptions` object passed at call time, letting each call site control the crawl independently:
|
|
89
87
|
|
|
90
88
|
```typescript
|
|
91
89
|
const describeInstances = queryFactory({
|
|
92
90
|
// ...
|
|
93
91
|
reduce: (acc, page): Instance[] => [...(acc ?? []), ...page.Reservations.flatMap(r => r.Instances)],
|
|
94
92
|
shouldFetchNextPage: (instances, opts: { minResults?: number }) =>
|
|
95
|
-
opts.minResults
|
|
93
|
+
opts.minResults == null || instances.length < opts.minResults,
|
|
96
94
|
});
|
|
97
95
|
|
|
96
|
+
// fetch all pages
|
|
97
|
+
const { data: all } = useQuery(describeInstances({ MaxResults: 1000 }));
|
|
98
|
+
|
|
98
99
|
// stop after accumulating at least 50 instances
|
|
99
|
-
const { data } = useQuery(
|
|
100
|
+
const { data: partial } = useQuery(
|
|
100
101
|
describeInstances({ MaxResults: 1000 }, { minResults: 50 })
|
|
101
102
|
);
|
|
102
103
|
```
|
|
@@ -131,7 +132,7 @@ const findInstance = queryFactory(describeInstances, {
|
|
|
131
132
|
// queryFn, getNextPageParam, initialPageParam, and reduce are all inherited
|
|
132
133
|
shouldFetchNextPage: (instances, opts: { instanceId?: string }) =>
|
|
133
134
|
opts.instanceId != null &&
|
|
134
|
-
!instances
|
|
135
|
+
!instances.some(i => i.InstanceId === opts.instanceId),
|
|
135
136
|
});
|
|
136
137
|
|
|
137
138
|
// query key: ['ec2:DescribeInstances', 'find', { MaxResults: 100 }, { instanceId: 'i-0abc123' }]
|
|
@@ -198,11 +199,11 @@ All fields except `reduce` and `shouldFetchNextPage` are the standard TanStack Q
|
|
|
198
199
|
| `queryKey` | `QueryKey` | Namespace segments. Params are appended at call time. |
|
|
199
200
|
| `queryFn` | `(params: TParams, ctx: QueryFunctionContext) => TData \| Promise<TData>` | Same as TanStack, with an extra leading `params` argument. |
|
|
200
201
|
| `select` | `(data: TData) => TSelected` | Exact TanStack API. Composed automatically on child factories. |
|
|
201
|
-
| `getNextPageParam` | `GetNextPageParamFunction<TPageParam, TData>` | Exact TanStack API.
|
|
202
|
-
| `initialPageParam` | `TPageParam` | Exact TanStack API.
|
|
202
|
+
| `getNextPageParam` | `GetNextPageParamFunction<TPageParam, TData>` | Exact TanStack API. Required (with `shouldFetchNextPage`) to activate crawling. Required (with `initialPageParam`) for `.infinite()`. |
|
|
203
|
+
| `initialPageParam` | `TPageParam` | Exact TanStack API. Drives `TPageParam` inference — without it `ctx.pageParam` is typed `never`. Required for `.infinite()` to work at runtime. |
|
|
203
204
|
| `getPreviousPageParam` | `GetPreviousPageParamFunction<TPageParam, TData>` | Exact TanStack API. Passed through on `.infinite()`. |
|
|
204
|
-
| `
|
|
205
|
-
| `
|
|
205
|
+
| `shouldFetchNextPage` | `(combined: TSelected \| undefined, crawlOptions: TCrawlOptions) => boolean` | Library addition. **Required (with `getNextPageParam`) to activate crawling.** Called after each page — return `true` to keep fetching, `false` to stop. |
|
|
206
|
+
| `reduce` | `(acc: TSelected \| undefined, page: TData) => TSelected` | Library addition. Optional. Folds crawled pages into a single `TSelected` value; when omitted the result is the last fetched page (`TSelected = TData`). |
|
|
206
207
|
| + all `StandardQueryOptions` fields | | All options accepted by TanStack's `useQuery` / `useInfiniteQuery` except `queryKey`, `queryFn`, and `select` (which the factory owns). Includes `staleTime`, `gcTime`, `retry`, `retryOnMount`, `enabled`, `refetchOnWindowFocus`, `refetchOnReconnect`, `refetchOnMount`, `refetchInterval`, `refetchIntervalInBackground`, `networkMode`, `notifyOnChangeProps`, `throwOnError`, `structuralSharing`, `initialData`, `initialDataUpdatedAt`, `placeholderData`, `queryKeyHashFn`, `persister`, `meta`, `maxPages`, `experimental_prefetchInRender`. Function-form callbacks (e.g. `enabled: (query) => boolean`) are supported wherever TanStack accepts them. |
|
|
207
208
|
|
|
208
209
|
### `QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions>`
|
package/dist/index.d.mts
CHANGED
|
@@ -27,29 +27,25 @@ type QueryFactoryConfig<TParams = void, TData = unknown, TError = Error, TSelect
|
|
|
27
27
|
/** Namespace segments. Params are appended as the final element at call time,
|
|
28
28
|
* giving a full key of [...namespace, 'infinite'?, params, crawlOptions?]. */
|
|
29
29
|
queryKey: QueryKey;
|
|
30
|
-
queryFn?: (params: TParams, context: QueryFunctionContext<QueryKey, [
|
|
30
|
+
queryFn?: (params: TParams, context: QueryFunctionContext<QueryKey, [
|
|
31
|
+
unknown
|
|
32
|
+
] extends [TPageParam] ? never : TPageParam>) => TData | Promise<TData>;
|
|
31
33
|
select?: (data: TData) => TSelected;
|
|
32
|
-
} & ({
|
|
33
34
|
/** TanStack v5 generic order: GetNextPageParamFunction<TPageParam, TData> */
|
|
34
|
-
getNextPageParam
|
|
35
|
-
/**
|
|
36
|
-
* ctx.pageParam
|
|
37
|
-
|
|
35
|
+
getNextPageParam?: GetNextPageParamFunction<TPageParam, TData>;
|
|
36
|
+
/** Drives TPageParam inference so ctx.pageParam in queryFn is typed as TPageParam.
|
|
37
|
+
* When omitted TPageParam stays unknown and ctx.pageParam is typed as never.
|
|
38
|
+
* Required for .infinite() to work correctly at runtime. */
|
|
39
|
+
initialPageParam?: TPageParam;
|
|
38
40
|
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
39
41
|
/** Reduces crawled pages incrementally into the final query result.
|
|
40
42
|
* Called once per page; accumulator is undefined on the first call.
|
|
41
|
-
* When
|
|
43
|
+
* When omitted, the crawl result is the last fetched page (TSelected = TData). */
|
|
42
44
|
reduce?: (accumulator: TSelected | undefined, page: TData) => TSelected;
|
|
43
45
|
/** Called after each page to decide whether to keep crawling.
|
|
44
|
-
*
|
|
46
|
+
* Required (along with getNextPageParam) to activate crawling. */
|
|
45
47
|
shouldFetchNextPage?: (combined: TSelected | undefined, crawlOptions: TCrawlOptions) => boolean;
|
|
46
|
-
}
|
|
47
|
-
getNextPageParam?: never;
|
|
48
|
-
initialPageParam?: never;
|
|
49
|
-
getPreviousPageParam?: never;
|
|
50
|
-
shouldFetchNextPage?: never;
|
|
51
|
-
reduce?: never;
|
|
52
|
-
});
|
|
48
|
+
};
|
|
53
49
|
/**
|
|
54
50
|
* What `factory(params)` returns — pass directly to `useQuery()`.
|
|
55
51
|
*
|
|
@@ -95,7 +91,7 @@ type ResolvedInfiniteOptions<TData = unknown, TError = Error, TPageParam = unkno
|
|
|
95
91
|
* namespace key, which is useful for broad cache invalidation:
|
|
96
92
|
* `queryClient.invalidateQueries(factory())`
|
|
97
93
|
*/
|
|
98
|
-
interface QueryFactory<TParams = void, TData = unknown, TError = Error, TSelected = TData, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown
|
|
94
|
+
interface QueryFactory<TParams = void, TData = unknown, TError = Error, TSelected = TData, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>, THasReduce extends boolean = boolean> {
|
|
99
95
|
(params?: TParams, crawlOptions?: TCrawlOptions): ResolvedQueryOptions<TData, TError, TSelected>;
|
|
100
96
|
infinite(params?: TParams, crawlOptions?: TCrawlOptions): ResolvedInfiniteOptions<TData, TError, TPageParam>;
|
|
101
97
|
}
|
|
@@ -106,14 +102,16 @@ interface QueryFactory<TParams = void, TData = unknown, TError = Error, TSelecte
|
|
|
106
102
|
*/
|
|
107
103
|
declare function queryFactory<TParams = void, TData = unknown, TError = Error, TSelected = TData, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>>(config: StandardQueryOptions<TError, TData> & {
|
|
108
104
|
queryKey: QueryKey;
|
|
109
|
-
queryFn?: (params: TParams, context: QueryFunctionContext<QueryKey, [
|
|
105
|
+
queryFn?: (params: TParams, context: QueryFunctionContext<QueryKey, [
|
|
106
|
+
unknown
|
|
107
|
+
] extends [TPageParam] ? never : TPageParam>) => TData | Promise<TData>;
|
|
110
108
|
select?: (data: TData) => TSelected;
|
|
111
|
-
getNextPageParam
|
|
112
|
-
initialPageParam
|
|
109
|
+
getNextPageParam?: GetNextPageParamFunction<TPageParam, TData>;
|
|
110
|
+
initialPageParam?: TPageParam;
|
|
113
111
|
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
114
112
|
reduce: (accumulator: TSelected | undefined, page: TData) => TSelected;
|
|
115
113
|
shouldFetchNextPage?: (combined: TSelected, crawlOptions: TCrawlOptions) => boolean;
|
|
116
|
-
}): QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions>;
|
|
114
|
+
}): QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions, true>;
|
|
117
115
|
/**
|
|
118
116
|
* Creates a standalone query factory from a config object.
|
|
119
117
|
*
|
|
@@ -124,7 +122,7 @@ declare function queryFactory<TParams = void, TData = unknown, TError = Error, T
|
|
|
124
122
|
* });
|
|
125
123
|
* // useQuery(usersFactory({ page: 1 }))
|
|
126
124
|
*/
|
|
127
|
-
declare function queryFactory<TParams = void, TData = unknown, TError = Error, TSelected = TData, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>>(config: QueryFactoryConfig<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions>): QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions>;
|
|
125
|
+
declare function queryFactory<TParams = void, TData = unknown, TError = Error, TSelected = TData, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>>(config: QueryFactoryConfig<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions>): QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions, false>;
|
|
128
126
|
/**
|
|
129
127
|
* Creates a child factory that inherits the query key and standard options from
|
|
130
128
|
* `parent` and introduces a new `queryFn`. The child's query key is appended to
|
|
@@ -132,22 +130,10 @@ declare function queryFactory<TParams = void, TData = unknown, TError = Error, T
|
|
|
132
130
|
*
|
|
133
131
|
* Use this overload when the child fetches different data than the parent.
|
|
134
132
|
*/
|
|
135
|
-
declare function queryFactory<TChildParams extends TParentParams, TData = unknown, TError = Error, TChildSelected = TData, TParentParams = TChildParams, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>>(parent: QueryFactory<TParentParams, any, any, any, any, any>, config: Omit<QueryFactoryConfig<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions>, 'queryKey' | '
|
|
133
|
+
declare function queryFactory<TChildParams extends TParentParams, TData = unknown, TError = Error, TChildSelected = TData, TParentParams = TChildParams, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>>(parent: QueryFactory<TParentParams, any, any, any, any, any, any>, config: Omit<QueryFactoryConfig<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions>, 'queryKey' | 'queryFn'> & {
|
|
136
134
|
queryKey?: QueryKey;
|
|
137
135
|
queryFn: NonNullable<QueryFactoryConfig<TChildParams, TData, TError, TChildSelected, TPageParam>['queryFn']>;
|
|
138
|
-
}
|
|
139
|
-
getNextPageParam: GetNextPageParamFunction<TPageParam, TData>;
|
|
140
|
-
initialPageParam: TPageParam;
|
|
141
|
-
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
142
|
-
shouldFetchNextPage?: (combined: TChildSelected | undefined, crawlOptions: TCrawlOptions) => boolean;
|
|
143
|
-
reduce?: (accumulator: TChildSelected | undefined, page: TData) => TChildSelected;
|
|
144
|
-
} | {
|
|
145
|
-
getNextPageParam?: never;
|
|
146
|
-
initialPageParam?: never;
|
|
147
|
-
getPreviousPageParam?: never;
|
|
148
|
-
shouldFetchNextPage?: never;
|
|
149
|
-
reduce?: never;
|
|
150
|
-
})): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions>;
|
|
136
|
+
}): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions, boolean>;
|
|
151
137
|
/**
|
|
152
138
|
* Creates a child factory that reuses the parent's `queryFn` and pagination
|
|
153
139
|
* config. Useful for adding a `select` transform, narrowing params, or
|
|
@@ -155,7 +141,7 @@ declare function queryFactory<TChildParams extends TParentParams, TData = unknow
|
|
|
155
141
|
* without changing what data is fetched. Parent and child `select` functions
|
|
156
142
|
* are automatically composed: `child.select(parent.select(data))`.
|
|
157
143
|
*/
|
|
158
|
-
declare function queryFactory<TChildParams extends TParentParams, TData = unknown, TError = Error, TParentSelected = TData, TChildSelected = TParentSelected, TParentParams = TChildParams, TPageParam = unknown, TParentCrawlOptions extends Record<string, unknown> = Record<string, unknown>, TChildCrawlOptions extends Record<string, unknown> = TParentCrawlOptions>(parent: QueryFactory<TParentParams, TData, any, TParentSelected, TPageParam, TParentCrawlOptions>, config: StandardQueryOptions<TError, TData> & {
|
|
144
|
+
declare function queryFactory<TChildParams extends TParentParams, TData = unknown, TError = Error, TParentSelected = TData, TChildSelected = TParentSelected, TParentParams = TChildParams, TPageParam = unknown, TParentCrawlOptions extends Record<string, unknown> = Record<string, unknown>, TChildCrawlOptions extends Record<string, unknown> = TParentCrawlOptions, TParentHasReduce extends boolean = boolean>(parent: QueryFactory<TParentParams, TData, any, TParentSelected, TPageParam, TParentCrawlOptions, TParentHasReduce>, config: StandardQueryOptions<TError, TData> & {
|
|
159
145
|
queryKey?: QueryKey;
|
|
160
146
|
queryFn?: never;
|
|
161
147
|
select?: (data: TParentSelected) => TChildSelected;
|
|
@@ -163,7 +149,7 @@ declare function queryFactory<TChildParams extends TParentParams, TData = unknow
|
|
|
163
149
|
initialPageParam?: TPageParam;
|
|
164
150
|
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
165
151
|
reduce?: (accumulator: TChildSelected | undefined, page: TData) => TChildSelected;
|
|
166
|
-
shouldFetchNextPage?: (combined: TChildSelected | undefined, crawlOptions: TChildCrawlOptions) => boolean;
|
|
167
|
-
}): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TChildCrawlOptions>;
|
|
152
|
+
shouldFetchNextPage?: (combined: TParentHasReduce extends true ? TChildSelected : TChildSelected | undefined, crawlOptions: TChildCrawlOptions) => boolean;
|
|
153
|
+
}): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TChildCrawlOptions, TParentHasReduce>;
|
|
168
154
|
|
|
169
155
|
export { type QueryFactory, type QueryFactoryConfig, type ResolvedInfiniteOptions, type ResolvedQueryOptions, type StandardQueryOptions, queryFactory };
|
package/dist/index.d.ts
CHANGED
|
@@ -27,29 +27,25 @@ type QueryFactoryConfig<TParams = void, TData = unknown, TError = Error, TSelect
|
|
|
27
27
|
/** Namespace segments. Params are appended as the final element at call time,
|
|
28
28
|
* giving a full key of [...namespace, 'infinite'?, params, crawlOptions?]. */
|
|
29
29
|
queryKey: QueryKey;
|
|
30
|
-
queryFn?: (params: TParams, context: QueryFunctionContext<QueryKey, [
|
|
30
|
+
queryFn?: (params: TParams, context: QueryFunctionContext<QueryKey, [
|
|
31
|
+
unknown
|
|
32
|
+
] extends [TPageParam] ? never : TPageParam>) => TData | Promise<TData>;
|
|
31
33
|
select?: (data: TData) => TSelected;
|
|
32
|
-
} & ({
|
|
33
34
|
/** TanStack v5 generic order: GetNextPageParamFunction<TPageParam, TData> */
|
|
34
|
-
getNextPageParam
|
|
35
|
-
/**
|
|
36
|
-
* ctx.pageParam
|
|
37
|
-
|
|
35
|
+
getNextPageParam?: GetNextPageParamFunction<TPageParam, TData>;
|
|
36
|
+
/** Drives TPageParam inference so ctx.pageParam in queryFn is typed as TPageParam.
|
|
37
|
+
* When omitted TPageParam stays unknown and ctx.pageParam is typed as never.
|
|
38
|
+
* Required for .infinite() to work correctly at runtime. */
|
|
39
|
+
initialPageParam?: TPageParam;
|
|
38
40
|
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
39
41
|
/** Reduces crawled pages incrementally into the final query result.
|
|
40
42
|
* Called once per page; accumulator is undefined on the first call.
|
|
41
|
-
* When
|
|
43
|
+
* When omitted, the crawl result is the last fetched page (TSelected = TData). */
|
|
42
44
|
reduce?: (accumulator: TSelected | undefined, page: TData) => TSelected;
|
|
43
45
|
/** Called after each page to decide whether to keep crawling.
|
|
44
|
-
*
|
|
46
|
+
* Required (along with getNextPageParam) to activate crawling. */
|
|
45
47
|
shouldFetchNextPage?: (combined: TSelected | undefined, crawlOptions: TCrawlOptions) => boolean;
|
|
46
|
-
}
|
|
47
|
-
getNextPageParam?: never;
|
|
48
|
-
initialPageParam?: never;
|
|
49
|
-
getPreviousPageParam?: never;
|
|
50
|
-
shouldFetchNextPage?: never;
|
|
51
|
-
reduce?: never;
|
|
52
|
-
});
|
|
48
|
+
};
|
|
53
49
|
/**
|
|
54
50
|
* What `factory(params)` returns — pass directly to `useQuery()`.
|
|
55
51
|
*
|
|
@@ -95,7 +91,7 @@ type ResolvedInfiniteOptions<TData = unknown, TError = Error, TPageParam = unkno
|
|
|
95
91
|
* namespace key, which is useful for broad cache invalidation:
|
|
96
92
|
* `queryClient.invalidateQueries(factory())`
|
|
97
93
|
*/
|
|
98
|
-
interface QueryFactory<TParams = void, TData = unknown, TError = Error, TSelected = TData, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown
|
|
94
|
+
interface QueryFactory<TParams = void, TData = unknown, TError = Error, TSelected = TData, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>, THasReduce extends boolean = boolean> {
|
|
99
95
|
(params?: TParams, crawlOptions?: TCrawlOptions): ResolvedQueryOptions<TData, TError, TSelected>;
|
|
100
96
|
infinite(params?: TParams, crawlOptions?: TCrawlOptions): ResolvedInfiniteOptions<TData, TError, TPageParam>;
|
|
101
97
|
}
|
|
@@ -106,14 +102,16 @@ interface QueryFactory<TParams = void, TData = unknown, TError = Error, TSelecte
|
|
|
106
102
|
*/
|
|
107
103
|
declare function queryFactory<TParams = void, TData = unknown, TError = Error, TSelected = TData, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>>(config: StandardQueryOptions<TError, TData> & {
|
|
108
104
|
queryKey: QueryKey;
|
|
109
|
-
queryFn?: (params: TParams, context: QueryFunctionContext<QueryKey, [
|
|
105
|
+
queryFn?: (params: TParams, context: QueryFunctionContext<QueryKey, [
|
|
106
|
+
unknown
|
|
107
|
+
] extends [TPageParam] ? never : TPageParam>) => TData | Promise<TData>;
|
|
110
108
|
select?: (data: TData) => TSelected;
|
|
111
|
-
getNextPageParam
|
|
112
|
-
initialPageParam
|
|
109
|
+
getNextPageParam?: GetNextPageParamFunction<TPageParam, TData>;
|
|
110
|
+
initialPageParam?: TPageParam;
|
|
113
111
|
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
114
112
|
reduce: (accumulator: TSelected | undefined, page: TData) => TSelected;
|
|
115
113
|
shouldFetchNextPage?: (combined: TSelected, crawlOptions: TCrawlOptions) => boolean;
|
|
116
|
-
}): QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions>;
|
|
114
|
+
}): QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions, true>;
|
|
117
115
|
/**
|
|
118
116
|
* Creates a standalone query factory from a config object.
|
|
119
117
|
*
|
|
@@ -124,7 +122,7 @@ declare function queryFactory<TParams = void, TData = unknown, TError = Error, T
|
|
|
124
122
|
* });
|
|
125
123
|
* // useQuery(usersFactory({ page: 1 }))
|
|
126
124
|
*/
|
|
127
|
-
declare function queryFactory<TParams = void, TData = unknown, TError = Error, TSelected = TData, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>>(config: QueryFactoryConfig<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions>): QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions>;
|
|
125
|
+
declare function queryFactory<TParams = void, TData = unknown, TError = Error, TSelected = TData, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>>(config: QueryFactoryConfig<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions>): QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions, false>;
|
|
128
126
|
/**
|
|
129
127
|
* Creates a child factory that inherits the query key and standard options from
|
|
130
128
|
* `parent` and introduces a new `queryFn`. The child's query key is appended to
|
|
@@ -132,22 +130,10 @@ declare function queryFactory<TParams = void, TData = unknown, TError = Error, T
|
|
|
132
130
|
*
|
|
133
131
|
* Use this overload when the child fetches different data than the parent.
|
|
134
132
|
*/
|
|
135
|
-
declare function queryFactory<TChildParams extends TParentParams, TData = unknown, TError = Error, TChildSelected = TData, TParentParams = TChildParams, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>>(parent: QueryFactory<TParentParams, any, any, any, any, any>, config: Omit<QueryFactoryConfig<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions>, 'queryKey' | '
|
|
133
|
+
declare function queryFactory<TChildParams extends TParentParams, TData = unknown, TError = Error, TChildSelected = TData, TParentParams = TChildParams, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>>(parent: QueryFactory<TParentParams, any, any, any, any, any, any>, config: Omit<QueryFactoryConfig<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions>, 'queryKey' | 'queryFn'> & {
|
|
136
134
|
queryKey?: QueryKey;
|
|
137
135
|
queryFn: NonNullable<QueryFactoryConfig<TChildParams, TData, TError, TChildSelected, TPageParam>['queryFn']>;
|
|
138
|
-
}
|
|
139
|
-
getNextPageParam: GetNextPageParamFunction<TPageParam, TData>;
|
|
140
|
-
initialPageParam: TPageParam;
|
|
141
|
-
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
142
|
-
shouldFetchNextPage?: (combined: TChildSelected | undefined, crawlOptions: TCrawlOptions) => boolean;
|
|
143
|
-
reduce?: (accumulator: TChildSelected | undefined, page: TData) => TChildSelected;
|
|
144
|
-
} | {
|
|
145
|
-
getNextPageParam?: never;
|
|
146
|
-
initialPageParam?: never;
|
|
147
|
-
getPreviousPageParam?: never;
|
|
148
|
-
shouldFetchNextPage?: never;
|
|
149
|
-
reduce?: never;
|
|
150
|
-
})): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions>;
|
|
136
|
+
}): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions, boolean>;
|
|
151
137
|
/**
|
|
152
138
|
* Creates a child factory that reuses the parent's `queryFn` and pagination
|
|
153
139
|
* config. Useful for adding a `select` transform, narrowing params, or
|
|
@@ -155,7 +141,7 @@ declare function queryFactory<TChildParams extends TParentParams, TData = unknow
|
|
|
155
141
|
* without changing what data is fetched. Parent and child `select` functions
|
|
156
142
|
* are automatically composed: `child.select(parent.select(data))`.
|
|
157
143
|
*/
|
|
158
|
-
declare function queryFactory<TChildParams extends TParentParams, TData = unknown, TError = Error, TParentSelected = TData, TChildSelected = TParentSelected, TParentParams = TChildParams, TPageParam = unknown, TParentCrawlOptions extends Record<string, unknown> = Record<string, unknown>, TChildCrawlOptions extends Record<string, unknown> = TParentCrawlOptions>(parent: QueryFactory<TParentParams, TData, any, TParentSelected, TPageParam, TParentCrawlOptions>, config: StandardQueryOptions<TError, TData> & {
|
|
144
|
+
declare function queryFactory<TChildParams extends TParentParams, TData = unknown, TError = Error, TParentSelected = TData, TChildSelected = TParentSelected, TParentParams = TChildParams, TPageParam = unknown, TParentCrawlOptions extends Record<string, unknown> = Record<string, unknown>, TChildCrawlOptions extends Record<string, unknown> = TParentCrawlOptions, TParentHasReduce extends boolean = boolean>(parent: QueryFactory<TParentParams, TData, any, TParentSelected, TPageParam, TParentCrawlOptions, TParentHasReduce>, config: StandardQueryOptions<TError, TData> & {
|
|
159
145
|
queryKey?: QueryKey;
|
|
160
146
|
queryFn?: never;
|
|
161
147
|
select?: (data: TParentSelected) => TChildSelected;
|
|
@@ -163,7 +149,7 @@ declare function queryFactory<TChildParams extends TParentParams, TData = unknow
|
|
|
163
149
|
initialPageParam?: TPageParam;
|
|
164
150
|
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
165
151
|
reduce?: (accumulator: TChildSelected | undefined, page: TData) => TChildSelected;
|
|
166
|
-
shouldFetchNextPage?: (combined: TChildSelected | undefined, crawlOptions: TChildCrawlOptions) => boolean;
|
|
167
|
-
}): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TChildCrawlOptions>;
|
|
152
|
+
shouldFetchNextPage?: (combined: TParentHasReduce extends true ? TChildSelected : TChildSelected | undefined, crawlOptions: TChildCrawlOptions) => boolean;
|
|
153
|
+
}): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TChildCrawlOptions, TParentHasReduce>;
|
|
168
154
|
|
|
169
155
|
export { type QueryFactory, type QueryFactoryConfig, type ResolvedInfiniteOptions, type ResolvedQueryOptions, type StandardQueryOptions, queryFactory };
|
package/dist/index.js
CHANGED
|
@@ -26,35 +26,44 @@ module.exports = __toCommonJS(index_exports);
|
|
|
26
26
|
|
|
27
27
|
// src/queryFactory.ts
|
|
28
28
|
var FACTORY_CONFIG = /* @__PURE__ */ Symbol("factoryConfig");
|
|
29
|
+
var getEnvelopeNextPageParam = (envelope) => envelope.nextPageParam;
|
|
30
|
+
var noNextPage = () => void 0;
|
|
29
31
|
function resolveKey(namespace, params, crawlOptions) {
|
|
30
32
|
const base = Array.isArray(namespace) ? namespace : [namespace];
|
|
31
33
|
const withParams = params === void 0 ? base : [...base, params];
|
|
32
34
|
if (!crawlOptions) return withParams;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
let defined;
|
|
36
|
+
for (const key in crawlOptions) {
|
|
37
|
+
if (crawlOptions[key] !== void 0) {
|
|
38
|
+
(defined != null ? defined : defined = {})[key] = crawlOptions[key];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return defined ? [...withParams, defined] : withParams;
|
|
35
42
|
}
|
|
36
|
-
function wrapGetNextPageParam(getNextPageParam, shouldFetchNextPage, crawlOptions
|
|
43
|
+
function wrapGetNextPageParam(getNextPageParam, shouldFetchNextPage, crawlOptions) {
|
|
37
44
|
return (lastPage, allPages, lastPageParam, allPageParams) => {
|
|
38
|
-
|
|
39
|
-
|
|
45
|
+
if (!shouldFetchNextPage(lastPage, crawlOptions))
|
|
46
|
+
return void 0;
|
|
40
47
|
return getNextPageParam(lastPage, allPages, lastPageParam, allPageParams);
|
|
41
48
|
};
|
|
42
49
|
}
|
|
43
|
-
function buildCrawlingQueryFn(queryFn, getNextPageParam, initialPageParam, shouldFetchNextPage, reduce
|
|
44
|
-
return async (context) => {
|
|
50
|
+
function buildCrawlingQueryFn(queryFn, getNextPageParam, initialPageParam, shouldFetchNextPage, reduce) {
|
|
51
|
+
return async (params, crawlOptions, context) => {
|
|
45
52
|
var _a, _b;
|
|
46
53
|
const pages = [];
|
|
47
54
|
const pageParams = [];
|
|
48
55
|
let currentParam = initialPageParam;
|
|
49
56
|
let acc = void 0;
|
|
57
|
+
const ctx = { ...context, pageParam: currentParam };
|
|
50
58
|
while (true) {
|
|
51
59
|
if ((_a = context.signal) == null ? void 0 : _a.aborted) break;
|
|
52
|
-
|
|
60
|
+
ctx.pageParam = currentParam;
|
|
61
|
+
const page = await queryFn(params, ctx);
|
|
53
62
|
pages.push(page);
|
|
54
63
|
pageParams.push(currentParam);
|
|
55
64
|
if (reduce) acc = reduce(acc, page);
|
|
56
65
|
if ((_b = context.signal) == null ? void 0 : _b.aborted) break;
|
|
57
|
-
if (
|
|
66
|
+
if (!shouldFetchNextPage(acc, crawlOptions)) break;
|
|
58
67
|
const nextParam = getNextPageParam(page, pages, currentParam, pageParams);
|
|
59
68
|
if (nextParam == null) break;
|
|
60
69
|
currentParam = nextParam;
|
|
@@ -66,17 +75,19 @@ function buildCrawlingQueryFn(queryFn, getNextPageParam, initialPageParam, shoul
|
|
|
66
75
|
return pages;
|
|
67
76
|
};
|
|
68
77
|
}
|
|
69
|
-
function buildInfiniteCrawlingQueryFn(queryFn, getNextPageParam, shouldFetchNextPage, reduce
|
|
70
|
-
return async (context) => {
|
|
78
|
+
function buildInfiniteCrawlingQueryFn(queryFn, getNextPageParam, shouldFetchNextPage, reduce) {
|
|
79
|
+
return async (params, crawlOptions, context) => {
|
|
71
80
|
var _a, _b;
|
|
72
81
|
const pages = [];
|
|
73
82
|
const pageParams = [];
|
|
74
83
|
let currentParam = context.pageParam;
|
|
75
84
|
let acc = void 0;
|
|
76
85
|
let nextBatchParam = null;
|
|
86
|
+
const ctx = { ...context, pageParam: currentParam };
|
|
77
87
|
while (true) {
|
|
78
88
|
if ((_a = context.signal) == null ? void 0 : _a.aborted) break;
|
|
79
|
-
|
|
89
|
+
ctx.pageParam = currentParam;
|
|
90
|
+
const page = await queryFn(params, ctx);
|
|
80
91
|
pages.push(page);
|
|
81
92
|
pageParams.push(currentParam);
|
|
82
93
|
acc = reduce(acc, page);
|
|
@@ -84,7 +95,7 @@ function buildInfiniteCrawlingQueryFn(queryFn, getNextPageParam, shouldFetchNext
|
|
|
84
95
|
const nextParam = getNextPageParam(page, pages, currentParam, pageParams);
|
|
85
96
|
nextBatchParam = nextParam != null ? nextParam : null;
|
|
86
97
|
if (nextParam == null) break;
|
|
87
|
-
if (
|
|
98
|
+
if (!shouldFetchNextPage(acc, crawlOptions)) break;
|
|
88
99
|
currentParam = nextParam;
|
|
89
100
|
}
|
|
90
101
|
if (acc === void 0) throw new DOMException("Aborted", "AbortError");
|
|
@@ -103,12 +114,33 @@ function buildFactory(cfg) {
|
|
|
103
114
|
reduce,
|
|
104
115
|
standardOptions
|
|
105
116
|
} = cfg;
|
|
106
|
-
const hasCrawling = rawQueryFn !== void 0 && getNextPageParam !== void 0;
|
|
117
|
+
const hasCrawling = rawQueryFn !== void 0 && getNextPageParam !== void 0 && shouldFetchNextPage !== void 0;
|
|
107
118
|
const hasInfiniteCrawling = hasCrawling && reduce !== void 0;
|
|
119
|
+
const crawlingFn = hasCrawling ? buildCrawlingQueryFn(
|
|
120
|
+
rawQueryFn,
|
|
121
|
+
getNextPageParam,
|
|
122
|
+
initialPageParam,
|
|
123
|
+
shouldFetchNextPage,
|
|
124
|
+
reduce
|
|
125
|
+
) : void 0;
|
|
126
|
+
const infiniteCrawlingFn = hasInfiniteCrawling ? buildInfiniteCrawlingQueryFn(
|
|
127
|
+
rawQueryFn,
|
|
128
|
+
getNextPageParam,
|
|
129
|
+
shouldFetchNextPage,
|
|
130
|
+
reduce
|
|
131
|
+
) : void 0;
|
|
132
|
+
const infiniteNamespace = [...resolveKey(namespace, void 0), "infinite"];
|
|
133
|
+
const envelopeSelect = infiniteCrawlingFn ? (data) => ({
|
|
134
|
+
...data,
|
|
135
|
+
pages: data.pages.map((e) => select ? select(e.data) : e.data)
|
|
136
|
+
}) : void 0;
|
|
137
|
+
const infiniteSelect = !infiniteCrawlingFn && select ? (data) => ({
|
|
138
|
+
...data,
|
|
139
|
+
pages: data.pages.map(select)
|
|
140
|
+
}) : void 0;
|
|
108
141
|
const factory = function(params, crawlOptions = {}) {
|
|
109
142
|
const queryKey = resolveKey(namespace, params, crawlOptions);
|
|
110
|
-
const
|
|
111
|
-
const resolvedQueryFn = hasCrawling ? buildCrawlingQueryFn(boundQueryFn, getNextPageParam, initialPageParam, shouldFetchNextPage, reduce, crawlOptions) : boundQueryFn;
|
|
143
|
+
const resolvedQueryFn = crawlingFn ? (ctx) => crawlingFn(params, crawlOptions, ctx) : rawQueryFn ? (ctx) => rawQueryFn(params, ctx) : void 0;
|
|
112
144
|
return {
|
|
113
145
|
...standardOptions,
|
|
114
146
|
queryKey,
|
|
@@ -118,37 +150,25 @@ function buildFactory(cfg) {
|
|
|
118
150
|
};
|
|
119
151
|
};
|
|
120
152
|
factory.infinite = function(params, crawlOptions = {}) {
|
|
121
|
-
const queryKey = resolveKey(
|
|
122
|
-
|
|
123
|
-
if (hasInfiniteCrawling) {
|
|
124
|
-
const crawlingQueryFn = buildInfiniteCrawlingQueryFn(
|
|
125
|
-
boundQueryFn,
|
|
126
|
-
getNextPageParam,
|
|
127
|
-
shouldFetchNextPage,
|
|
128
|
-
reduce,
|
|
129
|
-
crawlOptions
|
|
130
|
-
);
|
|
131
|
-
const envelopeGetNextPageParam = (envelope) => envelope.nextPageParam;
|
|
132
|
-
const envelopeSelect = (data) => ({
|
|
133
|
-
...data,
|
|
134
|
-
pages: data.pages.map((e) => select ? select(e.data) : e.data)
|
|
135
|
-
});
|
|
153
|
+
const queryKey = resolveKey(infiniteNamespace, params, crawlOptions);
|
|
154
|
+
if (infiniteCrawlingFn) {
|
|
136
155
|
return {
|
|
137
156
|
...standardOptions,
|
|
138
157
|
queryKey,
|
|
139
|
-
queryFn:
|
|
140
|
-
getNextPageParam:
|
|
158
|
+
queryFn: (ctx) => infiniteCrawlingFn(params, crawlOptions, ctx),
|
|
159
|
+
getNextPageParam: getEnvelopeNextPageParam,
|
|
141
160
|
initialPageParam,
|
|
142
161
|
select: envelopeSelect,
|
|
143
162
|
...getPreviousPageParam !== void 0 && { getPreviousPageParam },
|
|
144
163
|
[FACTORY_CONFIG]: cfg
|
|
145
164
|
};
|
|
146
165
|
}
|
|
147
|
-
const
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
166
|
+
const boundQueryFn = rawQueryFn ? (ctx) => rawQueryFn(params, ctx) : void 0;
|
|
167
|
+
const infiniteGetNextPageParam = getNextPageParam && shouldFetchNextPage ? wrapGetNextPageParam(
|
|
168
|
+
getNextPageParam,
|
|
169
|
+
shouldFetchNextPage,
|
|
170
|
+
crawlOptions
|
|
171
|
+
) : getNextPageParam != null ? getNextPageParam : noNextPage;
|
|
152
172
|
return {
|
|
153
173
|
...standardOptions,
|
|
154
174
|
queryKey,
|
|
@@ -168,11 +188,16 @@ function queryFactory(configOrParent, childConfig) {
|
|
|
168
188
|
const result = configOrParent();
|
|
169
189
|
const parentCfg = result == null ? void 0 : result[FACTORY_CONFIG];
|
|
170
190
|
if (!parentCfg) {
|
|
171
|
-
throw new Error(
|
|
191
|
+
throw new Error(
|
|
192
|
+
"queryFactory: first argument must be a factory created by queryFactory()"
|
|
193
|
+
);
|
|
172
194
|
}
|
|
173
195
|
const hasNewQueryFn = childConfig.queryFn !== void 0;
|
|
174
196
|
const childNamespace = childConfig.queryKey ? Array.isArray(childConfig.queryKey) ? childConfig.queryKey : [childConfig.queryKey] : [];
|
|
175
|
-
const composedNamespace = [
|
|
197
|
+
const composedNamespace = [
|
|
198
|
+
...parentCfg.queryKey,
|
|
199
|
+
...childNamespace
|
|
200
|
+
];
|
|
176
201
|
let resolvedSelect;
|
|
177
202
|
if (hasNewQueryFn) {
|
|
178
203
|
resolvedSelect = childConfig.select;
|
|
@@ -212,7 +237,10 @@ function queryFactory(configOrParent, childConfig) {
|
|
|
212
237
|
queryFn: hasNewQueryFn ? childConfig.queryFn : parentCfg.queryFn,
|
|
213
238
|
select: resolvedSelect,
|
|
214
239
|
...crawling,
|
|
215
|
-
standardOptions: {
|
|
240
|
+
standardOptions: {
|
|
241
|
+
...parentCfg.standardOptions,
|
|
242
|
+
...childStandardOptions
|
|
243
|
+
}
|
|
216
244
|
});
|
|
217
245
|
}
|
|
218
246
|
const {
|
package/dist/index.mjs
CHANGED
|
@@ -1,34 +1,43 @@
|
|
|
1
1
|
// src/queryFactory.ts
|
|
2
2
|
var FACTORY_CONFIG = /* @__PURE__ */ Symbol("factoryConfig");
|
|
3
|
+
var getEnvelopeNextPageParam = (envelope) => envelope.nextPageParam;
|
|
4
|
+
var noNextPage = () => void 0;
|
|
3
5
|
function resolveKey(namespace, params, crawlOptions) {
|
|
4
6
|
const base = Array.isArray(namespace) ? namespace : [namespace];
|
|
5
7
|
const withParams = params === void 0 ? base : [...base, params];
|
|
6
8
|
if (!crawlOptions) return withParams;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
let defined;
|
|
10
|
+
for (const key in crawlOptions) {
|
|
11
|
+
if (crawlOptions[key] !== void 0) {
|
|
12
|
+
(defined != null ? defined : defined = {})[key] = crawlOptions[key];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return defined ? [...withParams, defined] : withParams;
|
|
9
16
|
}
|
|
10
|
-
function wrapGetNextPageParam(getNextPageParam, shouldFetchNextPage, crawlOptions
|
|
17
|
+
function wrapGetNextPageParam(getNextPageParam, shouldFetchNextPage, crawlOptions) {
|
|
11
18
|
return (lastPage, allPages, lastPageParam, allPageParams) => {
|
|
12
|
-
|
|
13
|
-
|
|
19
|
+
if (!shouldFetchNextPage(lastPage, crawlOptions))
|
|
20
|
+
return void 0;
|
|
14
21
|
return getNextPageParam(lastPage, allPages, lastPageParam, allPageParams);
|
|
15
22
|
};
|
|
16
23
|
}
|
|
17
|
-
function buildCrawlingQueryFn(queryFn, getNextPageParam, initialPageParam, shouldFetchNextPage, reduce
|
|
18
|
-
return async (context) => {
|
|
24
|
+
function buildCrawlingQueryFn(queryFn, getNextPageParam, initialPageParam, shouldFetchNextPage, reduce) {
|
|
25
|
+
return async (params, crawlOptions, context) => {
|
|
19
26
|
var _a, _b;
|
|
20
27
|
const pages = [];
|
|
21
28
|
const pageParams = [];
|
|
22
29
|
let currentParam = initialPageParam;
|
|
23
30
|
let acc = void 0;
|
|
31
|
+
const ctx = { ...context, pageParam: currentParam };
|
|
24
32
|
while (true) {
|
|
25
33
|
if ((_a = context.signal) == null ? void 0 : _a.aborted) break;
|
|
26
|
-
|
|
34
|
+
ctx.pageParam = currentParam;
|
|
35
|
+
const page = await queryFn(params, ctx);
|
|
27
36
|
pages.push(page);
|
|
28
37
|
pageParams.push(currentParam);
|
|
29
38
|
if (reduce) acc = reduce(acc, page);
|
|
30
39
|
if ((_b = context.signal) == null ? void 0 : _b.aborted) break;
|
|
31
|
-
if (
|
|
40
|
+
if (!shouldFetchNextPage(acc, crawlOptions)) break;
|
|
32
41
|
const nextParam = getNextPageParam(page, pages, currentParam, pageParams);
|
|
33
42
|
if (nextParam == null) break;
|
|
34
43
|
currentParam = nextParam;
|
|
@@ -40,17 +49,19 @@ function buildCrawlingQueryFn(queryFn, getNextPageParam, initialPageParam, shoul
|
|
|
40
49
|
return pages;
|
|
41
50
|
};
|
|
42
51
|
}
|
|
43
|
-
function buildInfiniteCrawlingQueryFn(queryFn, getNextPageParam, shouldFetchNextPage, reduce
|
|
44
|
-
return async (context) => {
|
|
52
|
+
function buildInfiniteCrawlingQueryFn(queryFn, getNextPageParam, shouldFetchNextPage, reduce) {
|
|
53
|
+
return async (params, crawlOptions, context) => {
|
|
45
54
|
var _a, _b;
|
|
46
55
|
const pages = [];
|
|
47
56
|
const pageParams = [];
|
|
48
57
|
let currentParam = context.pageParam;
|
|
49
58
|
let acc = void 0;
|
|
50
59
|
let nextBatchParam = null;
|
|
60
|
+
const ctx = { ...context, pageParam: currentParam };
|
|
51
61
|
while (true) {
|
|
52
62
|
if ((_a = context.signal) == null ? void 0 : _a.aborted) break;
|
|
53
|
-
|
|
63
|
+
ctx.pageParam = currentParam;
|
|
64
|
+
const page = await queryFn(params, ctx);
|
|
54
65
|
pages.push(page);
|
|
55
66
|
pageParams.push(currentParam);
|
|
56
67
|
acc = reduce(acc, page);
|
|
@@ -58,7 +69,7 @@ function buildInfiniteCrawlingQueryFn(queryFn, getNextPageParam, shouldFetchNext
|
|
|
58
69
|
const nextParam = getNextPageParam(page, pages, currentParam, pageParams);
|
|
59
70
|
nextBatchParam = nextParam != null ? nextParam : null;
|
|
60
71
|
if (nextParam == null) break;
|
|
61
|
-
if (
|
|
72
|
+
if (!shouldFetchNextPage(acc, crawlOptions)) break;
|
|
62
73
|
currentParam = nextParam;
|
|
63
74
|
}
|
|
64
75
|
if (acc === void 0) throw new DOMException("Aborted", "AbortError");
|
|
@@ -77,12 +88,33 @@ function buildFactory(cfg) {
|
|
|
77
88
|
reduce,
|
|
78
89
|
standardOptions
|
|
79
90
|
} = cfg;
|
|
80
|
-
const hasCrawling = rawQueryFn !== void 0 && getNextPageParam !== void 0;
|
|
91
|
+
const hasCrawling = rawQueryFn !== void 0 && getNextPageParam !== void 0 && shouldFetchNextPage !== void 0;
|
|
81
92
|
const hasInfiniteCrawling = hasCrawling && reduce !== void 0;
|
|
93
|
+
const crawlingFn = hasCrawling ? buildCrawlingQueryFn(
|
|
94
|
+
rawQueryFn,
|
|
95
|
+
getNextPageParam,
|
|
96
|
+
initialPageParam,
|
|
97
|
+
shouldFetchNextPage,
|
|
98
|
+
reduce
|
|
99
|
+
) : void 0;
|
|
100
|
+
const infiniteCrawlingFn = hasInfiniteCrawling ? buildInfiniteCrawlingQueryFn(
|
|
101
|
+
rawQueryFn,
|
|
102
|
+
getNextPageParam,
|
|
103
|
+
shouldFetchNextPage,
|
|
104
|
+
reduce
|
|
105
|
+
) : void 0;
|
|
106
|
+
const infiniteNamespace = [...resolveKey(namespace, void 0), "infinite"];
|
|
107
|
+
const envelopeSelect = infiniteCrawlingFn ? (data) => ({
|
|
108
|
+
...data,
|
|
109
|
+
pages: data.pages.map((e) => select ? select(e.data) : e.data)
|
|
110
|
+
}) : void 0;
|
|
111
|
+
const infiniteSelect = !infiniteCrawlingFn && select ? (data) => ({
|
|
112
|
+
...data,
|
|
113
|
+
pages: data.pages.map(select)
|
|
114
|
+
}) : void 0;
|
|
82
115
|
const factory = function(params, crawlOptions = {}) {
|
|
83
116
|
const queryKey = resolveKey(namespace, params, crawlOptions);
|
|
84
|
-
const
|
|
85
|
-
const resolvedQueryFn = hasCrawling ? buildCrawlingQueryFn(boundQueryFn, getNextPageParam, initialPageParam, shouldFetchNextPage, reduce, crawlOptions) : boundQueryFn;
|
|
117
|
+
const resolvedQueryFn = crawlingFn ? (ctx) => crawlingFn(params, crawlOptions, ctx) : rawQueryFn ? (ctx) => rawQueryFn(params, ctx) : void 0;
|
|
86
118
|
return {
|
|
87
119
|
...standardOptions,
|
|
88
120
|
queryKey,
|
|
@@ -92,37 +124,25 @@ function buildFactory(cfg) {
|
|
|
92
124
|
};
|
|
93
125
|
};
|
|
94
126
|
factory.infinite = function(params, crawlOptions = {}) {
|
|
95
|
-
const queryKey = resolveKey(
|
|
96
|
-
|
|
97
|
-
if (hasInfiniteCrawling) {
|
|
98
|
-
const crawlingQueryFn = buildInfiniteCrawlingQueryFn(
|
|
99
|
-
boundQueryFn,
|
|
100
|
-
getNextPageParam,
|
|
101
|
-
shouldFetchNextPage,
|
|
102
|
-
reduce,
|
|
103
|
-
crawlOptions
|
|
104
|
-
);
|
|
105
|
-
const envelopeGetNextPageParam = (envelope) => envelope.nextPageParam;
|
|
106
|
-
const envelopeSelect = (data) => ({
|
|
107
|
-
...data,
|
|
108
|
-
pages: data.pages.map((e) => select ? select(e.data) : e.data)
|
|
109
|
-
});
|
|
127
|
+
const queryKey = resolveKey(infiniteNamespace, params, crawlOptions);
|
|
128
|
+
if (infiniteCrawlingFn) {
|
|
110
129
|
return {
|
|
111
130
|
...standardOptions,
|
|
112
131
|
queryKey,
|
|
113
|
-
queryFn:
|
|
114
|
-
getNextPageParam:
|
|
132
|
+
queryFn: (ctx) => infiniteCrawlingFn(params, crawlOptions, ctx),
|
|
133
|
+
getNextPageParam: getEnvelopeNextPageParam,
|
|
115
134
|
initialPageParam,
|
|
116
135
|
select: envelopeSelect,
|
|
117
136
|
...getPreviousPageParam !== void 0 && { getPreviousPageParam },
|
|
118
137
|
[FACTORY_CONFIG]: cfg
|
|
119
138
|
};
|
|
120
139
|
}
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
140
|
+
const boundQueryFn = rawQueryFn ? (ctx) => rawQueryFn(params, ctx) : void 0;
|
|
141
|
+
const infiniteGetNextPageParam = getNextPageParam && shouldFetchNextPage ? wrapGetNextPageParam(
|
|
142
|
+
getNextPageParam,
|
|
143
|
+
shouldFetchNextPage,
|
|
144
|
+
crawlOptions
|
|
145
|
+
) : getNextPageParam != null ? getNextPageParam : noNextPage;
|
|
126
146
|
return {
|
|
127
147
|
...standardOptions,
|
|
128
148
|
queryKey,
|
|
@@ -142,11 +162,16 @@ function queryFactory(configOrParent, childConfig) {
|
|
|
142
162
|
const result = configOrParent();
|
|
143
163
|
const parentCfg = result == null ? void 0 : result[FACTORY_CONFIG];
|
|
144
164
|
if (!parentCfg) {
|
|
145
|
-
throw new Error(
|
|
165
|
+
throw new Error(
|
|
166
|
+
"queryFactory: first argument must be a factory created by queryFactory()"
|
|
167
|
+
);
|
|
146
168
|
}
|
|
147
169
|
const hasNewQueryFn = childConfig.queryFn !== void 0;
|
|
148
170
|
const childNamespace = childConfig.queryKey ? Array.isArray(childConfig.queryKey) ? childConfig.queryKey : [childConfig.queryKey] : [];
|
|
149
|
-
const composedNamespace = [
|
|
171
|
+
const composedNamespace = [
|
|
172
|
+
...parentCfg.queryKey,
|
|
173
|
+
...childNamespace
|
|
174
|
+
];
|
|
150
175
|
let resolvedSelect;
|
|
151
176
|
if (hasNewQueryFn) {
|
|
152
177
|
resolvedSelect = childConfig.select;
|
|
@@ -186,7 +211,10 @@ function queryFactory(configOrParent, childConfig) {
|
|
|
186
211
|
queryFn: hasNewQueryFn ? childConfig.queryFn : parentCfg.queryFn,
|
|
187
212
|
select: resolvedSelect,
|
|
188
213
|
...crawling,
|
|
189
|
-
standardOptions: {
|
|
214
|
+
standardOptions: {
|
|
215
|
+
...parentCfg.standardOptions,
|
|
216
|
+
...childStandardOptions
|
|
217
|
+
}
|
|
190
218
|
});
|
|
191
219
|
}
|
|
192
220
|
const {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@robohall/react-query-factory",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A factory abstraction for TanStack Query (React Query) with composable keys, crawling support, and automatic infinite query generation",
|
|
5
5
|
"author": "Robert Hall",
|
|
6
6
|
"license": "MIT",
|