@robohall/react-query-factory 1.0.5 → 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 +18 -36
- package/dist/index.d.ts +18 -36
- package/dist/index.js +3 -3
- package/dist/index.mjs +3 -3
- 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
|
@@ -31,27 +31,21 @@ type QueryFactoryConfig<TParams = void, TData = unknown, TError = Error, TSelect
|
|
|
31
31
|
unknown
|
|
32
32
|
] extends [TPageParam] ? never : TPageParam>) => TData | Promise<TData>;
|
|
33
33
|
select?: (data: TData) => TSelected;
|
|
34
|
-
} & ({
|
|
35
34
|
/** TanStack v5 generic order: GetNextPageParamFunction<TPageParam, TData> */
|
|
36
|
-
getNextPageParam
|
|
37
|
-
/**
|
|
38
|
-
* ctx.pageParam
|
|
39
|
-
|
|
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;
|
|
40
40
|
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
41
41
|
/** Reduces crawled pages incrementally into the final query result.
|
|
42
42
|
* Called once per page; accumulator is undefined on the first call.
|
|
43
|
-
* When
|
|
43
|
+
* When omitted, the crawl result is the last fetched page (TSelected = TData). */
|
|
44
44
|
reduce?: (accumulator: TSelected | undefined, page: TData) => TSelected;
|
|
45
45
|
/** Called after each page to decide whether to keep crawling.
|
|
46
|
-
*
|
|
46
|
+
* Required (along with getNextPageParam) to activate crawling. */
|
|
47
47
|
shouldFetchNextPage?: (combined: TSelected | undefined, crawlOptions: TCrawlOptions) => boolean;
|
|
48
|
-
}
|
|
49
|
-
getNextPageParam?: never;
|
|
50
|
-
initialPageParam?: never;
|
|
51
|
-
getPreviousPageParam?: never;
|
|
52
|
-
shouldFetchNextPage?: never;
|
|
53
|
-
reduce?: never;
|
|
54
|
-
});
|
|
48
|
+
};
|
|
55
49
|
/**
|
|
56
50
|
* What `factory(params)` returns — pass directly to `useQuery()`.
|
|
57
51
|
*
|
|
@@ -97,7 +91,7 @@ type ResolvedInfiniteOptions<TData = unknown, TError = Error, TPageParam = unkno
|
|
|
97
91
|
* namespace key, which is useful for broad cache invalidation:
|
|
98
92
|
* `queryClient.invalidateQueries(factory())`
|
|
99
93
|
*/
|
|
100
|
-
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> {
|
|
101
95
|
(params?: TParams, crawlOptions?: TCrawlOptions): ResolvedQueryOptions<TData, TError, TSelected>;
|
|
102
96
|
infinite(params?: TParams, crawlOptions?: TCrawlOptions): ResolvedInfiniteOptions<TData, TError, TPageParam>;
|
|
103
97
|
}
|
|
@@ -112,12 +106,12 @@ declare function queryFactory<TParams = void, TData = unknown, TError = Error, T
|
|
|
112
106
|
unknown
|
|
113
107
|
] extends [TPageParam] ? never : TPageParam>) => TData | Promise<TData>;
|
|
114
108
|
select?: (data: TData) => TSelected;
|
|
115
|
-
getNextPageParam
|
|
116
|
-
initialPageParam
|
|
109
|
+
getNextPageParam?: GetNextPageParamFunction<TPageParam, TData>;
|
|
110
|
+
initialPageParam?: TPageParam;
|
|
117
111
|
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
118
112
|
reduce: (accumulator: TSelected | undefined, page: TData) => TSelected;
|
|
119
113
|
shouldFetchNextPage?: (combined: TSelected, crawlOptions: TCrawlOptions) => boolean;
|
|
120
|
-
}): QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions>;
|
|
114
|
+
}): QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions, true>;
|
|
121
115
|
/**
|
|
122
116
|
* Creates a standalone query factory from a config object.
|
|
123
117
|
*
|
|
@@ -128,7 +122,7 @@ declare function queryFactory<TParams = void, TData = unknown, TError = Error, T
|
|
|
128
122
|
* });
|
|
129
123
|
* // useQuery(usersFactory({ page: 1 }))
|
|
130
124
|
*/
|
|
131
|
-
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>;
|
|
132
126
|
/**
|
|
133
127
|
* Creates a child factory that inherits the query key and standard options from
|
|
134
128
|
* `parent` and introduces a new `queryFn`. The child's query key is appended to
|
|
@@ -136,22 +130,10 @@ declare function queryFactory<TParams = void, TData = unknown, TError = Error, T
|
|
|
136
130
|
*
|
|
137
131
|
* Use this overload when the child fetches different data than the parent.
|
|
138
132
|
*/
|
|
139
|
-
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'> & {
|
|
140
134
|
queryKey?: QueryKey;
|
|
141
135
|
queryFn: NonNullable<QueryFactoryConfig<TChildParams, TData, TError, TChildSelected, TPageParam>['queryFn']>;
|
|
142
|
-
}
|
|
143
|
-
getNextPageParam: GetNextPageParamFunction<TPageParam, TData>;
|
|
144
|
-
initialPageParam: TPageParam;
|
|
145
|
-
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
146
|
-
shouldFetchNextPage?: (combined: TChildSelected | undefined, crawlOptions: TCrawlOptions) => boolean;
|
|
147
|
-
reduce?: (accumulator: TChildSelected | undefined, page: TData) => TChildSelected;
|
|
148
|
-
} | {
|
|
149
|
-
getNextPageParam?: never;
|
|
150
|
-
initialPageParam?: never;
|
|
151
|
-
getPreviousPageParam?: never;
|
|
152
|
-
shouldFetchNextPage?: never;
|
|
153
|
-
reduce?: never;
|
|
154
|
-
})): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions>;
|
|
136
|
+
}): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions, boolean>;
|
|
155
137
|
/**
|
|
156
138
|
* Creates a child factory that reuses the parent's `queryFn` and pagination
|
|
157
139
|
* config. Useful for adding a `select` transform, narrowing params, or
|
|
@@ -159,7 +141,7 @@ declare function queryFactory<TChildParams extends TParentParams, TData = unknow
|
|
|
159
141
|
* without changing what data is fetched. Parent and child `select` functions
|
|
160
142
|
* are automatically composed: `child.select(parent.select(data))`.
|
|
161
143
|
*/
|
|
162
|
-
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> & {
|
|
163
145
|
queryKey?: QueryKey;
|
|
164
146
|
queryFn?: never;
|
|
165
147
|
select?: (data: TParentSelected) => TChildSelected;
|
|
@@ -167,7 +149,7 @@ declare function queryFactory<TChildParams extends TParentParams, TData = unknow
|
|
|
167
149
|
initialPageParam?: TPageParam;
|
|
168
150
|
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
169
151
|
reduce?: (accumulator: TChildSelected | undefined, page: TData) => TChildSelected;
|
|
170
|
-
shouldFetchNextPage?: (combined: TChildSelected | undefined, crawlOptions: TChildCrawlOptions) => boolean;
|
|
171
|
-
}): 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>;
|
|
172
154
|
|
|
173
155
|
export { type QueryFactory, type QueryFactoryConfig, type ResolvedInfiniteOptions, type ResolvedQueryOptions, type StandardQueryOptions, queryFactory };
|
package/dist/index.d.ts
CHANGED
|
@@ -31,27 +31,21 @@ type QueryFactoryConfig<TParams = void, TData = unknown, TError = Error, TSelect
|
|
|
31
31
|
unknown
|
|
32
32
|
] extends [TPageParam] ? never : TPageParam>) => TData | Promise<TData>;
|
|
33
33
|
select?: (data: TData) => TSelected;
|
|
34
|
-
} & ({
|
|
35
34
|
/** TanStack v5 generic order: GetNextPageParamFunction<TPageParam, TData> */
|
|
36
|
-
getNextPageParam
|
|
37
|
-
/**
|
|
38
|
-
* ctx.pageParam
|
|
39
|
-
|
|
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;
|
|
40
40
|
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
41
41
|
/** Reduces crawled pages incrementally into the final query result.
|
|
42
42
|
* Called once per page; accumulator is undefined on the first call.
|
|
43
|
-
* When
|
|
43
|
+
* When omitted, the crawl result is the last fetched page (TSelected = TData). */
|
|
44
44
|
reduce?: (accumulator: TSelected | undefined, page: TData) => TSelected;
|
|
45
45
|
/** Called after each page to decide whether to keep crawling.
|
|
46
|
-
*
|
|
46
|
+
* Required (along with getNextPageParam) to activate crawling. */
|
|
47
47
|
shouldFetchNextPage?: (combined: TSelected | undefined, crawlOptions: TCrawlOptions) => boolean;
|
|
48
|
-
}
|
|
49
|
-
getNextPageParam?: never;
|
|
50
|
-
initialPageParam?: never;
|
|
51
|
-
getPreviousPageParam?: never;
|
|
52
|
-
shouldFetchNextPage?: never;
|
|
53
|
-
reduce?: never;
|
|
54
|
-
});
|
|
48
|
+
};
|
|
55
49
|
/**
|
|
56
50
|
* What `factory(params)` returns — pass directly to `useQuery()`.
|
|
57
51
|
*
|
|
@@ -97,7 +91,7 @@ type ResolvedInfiniteOptions<TData = unknown, TError = Error, TPageParam = unkno
|
|
|
97
91
|
* namespace key, which is useful for broad cache invalidation:
|
|
98
92
|
* `queryClient.invalidateQueries(factory())`
|
|
99
93
|
*/
|
|
100
|
-
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> {
|
|
101
95
|
(params?: TParams, crawlOptions?: TCrawlOptions): ResolvedQueryOptions<TData, TError, TSelected>;
|
|
102
96
|
infinite(params?: TParams, crawlOptions?: TCrawlOptions): ResolvedInfiniteOptions<TData, TError, TPageParam>;
|
|
103
97
|
}
|
|
@@ -112,12 +106,12 @@ declare function queryFactory<TParams = void, TData = unknown, TError = Error, T
|
|
|
112
106
|
unknown
|
|
113
107
|
] extends [TPageParam] ? never : TPageParam>) => TData | Promise<TData>;
|
|
114
108
|
select?: (data: TData) => TSelected;
|
|
115
|
-
getNextPageParam
|
|
116
|
-
initialPageParam
|
|
109
|
+
getNextPageParam?: GetNextPageParamFunction<TPageParam, TData>;
|
|
110
|
+
initialPageParam?: TPageParam;
|
|
117
111
|
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
118
112
|
reduce: (accumulator: TSelected | undefined, page: TData) => TSelected;
|
|
119
113
|
shouldFetchNextPage?: (combined: TSelected, crawlOptions: TCrawlOptions) => boolean;
|
|
120
|
-
}): QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions>;
|
|
114
|
+
}): QueryFactory<TParams, TData, TError, TSelected, TPageParam, TCrawlOptions, true>;
|
|
121
115
|
/**
|
|
122
116
|
* Creates a standalone query factory from a config object.
|
|
123
117
|
*
|
|
@@ -128,7 +122,7 @@ declare function queryFactory<TParams = void, TData = unknown, TError = Error, T
|
|
|
128
122
|
* });
|
|
129
123
|
* // useQuery(usersFactory({ page: 1 }))
|
|
130
124
|
*/
|
|
131
|
-
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>;
|
|
132
126
|
/**
|
|
133
127
|
* Creates a child factory that inherits the query key and standard options from
|
|
134
128
|
* `parent` and introduces a new `queryFn`. The child's query key is appended to
|
|
@@ -136,22 +130,10 @@ declare function queryFactory<TParams = void, TData = unknown, TError = Error, T
|
|
|
136
130
|
*
|
|
137
131
|
* Use this overload when the child fetches different data than the parent.
|
|
138
132
|
*/
|
|
139
|
-
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'> & {
|
|
140
134
|
queryKey?: QueryKey;
|
|
141
135
|
queryFn: NonNullable<QueryFactoryConfig<TChildParams, TData, TError, TChildSelected, TPageParam>['queryFn']>;
|
|
142
|
-
}
|
|
143
|
-
getNextPageParam: GetNextPageParamFunction<TPageParam, TData>;
|
|
144
|
-
initialPageParam: TPageParam;
|
|
145
|
-
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
146
|
-
shouldFetchNextPage?: (combined: TChildSelected | undefined, crawlOptions: TCrawlOptions) => boolean;
|
|
147
|
-
reduce?: (accumulator: TChildSelected | undefined, page: TData) => TChildSelected;
|
|
148
|
-
} | {
|
|
149
|
-
getNextPageParam?: never;
|
|
150
|
-
initialPageParam?: never;
|
|
151
|
-
getPreviousPageParam?: never;
|
|
152
|
-
shouldFetchNextPage?: never;
|
|
153
|
-
reduce?: never;
|
|
154
|
-
})): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions>;
|
|
136
|
+
}): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions, boolean>;
|
|
155
137
|
/**
|
|
156
138
|
* Creates a child factory that reuses the parent's `queryFn` and pagination
|
|
157
139
|
* config. Useful for adding a `select` transform, narrowing params, or
|
|
@@ -159,7 +141,7 @@ declare function queryFactory<TChildParams extends TParentParams, TData = unknow
|
|
|
159
141
|
* without changing what data is fetched. Parent and child `select` functions
|
|
160
142
|
* are automatically composed: `child.select(parent.select(data))`.
|
|
161
143
|
*/
|
|
162
|
-
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> & {
|
|
163
145
|
queryKey?: QueryKey;
|
|
164
146
|
queryFn?: never;
|
|
165
147
|
select?: (data: TParentSelected) => TChildSelected;
|
|
@@ -167,7 +149,7 @@ declare function queryFactory<TChildParams extends TParentParams, TData = unknow
|
|
|
167
149
|
initialPageParam?: TPageParam;
|
|
168
150
|
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
169
151
|
reduce?: (accumulator: TChildSelected | undefined, page: TData) => TChildSelected;
|
|
170
|
-
shouldFetchNextPage?: (combined: TChildSelected | undefined, crawlOptions: TChildCrawlOptions) => boolean;
|
|
171
|
-
}): 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>;
|
|
172
154
|
|
|
173
155
|
export { type QueryFactory, type QueryFactoryConfig, type ResolvedInfiniteOptions, type ResolvedQueryOptions, type StandardQueryOptions, queryFactory };
|
package/dist/index.js
CHANGED
|
@@ -63,7 +63,7 @@ function buildCrawlingQueryFn(queryFn, getNextPageParam, initialPageParam, shoul
|
|
|
63
63
|
pageParams.push(currentParam);
|
|
64
64
|
if (reduce) acc = reduce(acc, page);
|
|
65
65
|
if ((_b = context.signal) == null ? void 0 : _b.aborted) break;
|
|
66
|
-
if (
|
|
66
|
+
if (!shouldFetchNextPage(acc, crawlOptions)) break;
|
|
67
67
|
const nextParam = getNextPageParam(page, pages, currentParam, pageParams);
|
|
68
68
|
if (nextParam == null) break;
|
|
69
69
|
currentParam = nextParam;
|
|
@@ -95,7 +95,7 @@ function buildInfiniteCrawlingQueryFn(queryFn, getNextPageParam, shouldFetchNext
|
|
|
95
95
|
const nextParam = getNextPageParam(page, pages, currentParam, pageParams);
|
|
96
96
|
nextBatchParam = nextParam != null ? nextParam : null;
|
|
97
97
|
if (nextParam == null) break;
|
|
98
|
-
if (
|
|
98
|
+
if (!shouldFetchNextPage(acc, crawlOptions)) break;
|
|
99
99
|
currentParam = nextParam;
|
|
100
100
|
}
|
|
101
101
|
if (acc === void 0) throw new DOMException("Aborted", "AbortError");
|
|
@@ -114,7 +114,7 @@ function buildFactory(cfg) {
|
|
|
114
114
|
reduce,
|
|
115
115
|
standardOptions
|
|
116
116
|
} = cfg;
|
|
117
|
-
const hasCrawling = rawQueryFn !== void 0 && getNextPageParam !== void 0;
|
|
117
|
+
const hasCrawling = rawQueryFn !== void 0 && getNextPageParam !== void 0 && shouldFetchNextPage !== void 0;
|
|
118
118
|
const hasInfiniteCrawling = hasCrawling && reduce !== void 0;
|
|
119
119
|
const crawlingFn = hasCrawling ? buildCrawlingQueryFn(
|
|
120
120
|
rawQueryFn,
|
package/dist/index.mjs
CHANGED
|
@@ -37,7 +37,7 @@ function buildCrawlingQueryFn(queryFn, getNextPageParam, initialPageParam, shoul
|
|
|
37
37
|
pageParams.push(currentParam);
|
|
38
38
|
if (reduce) acc = reduce(acc, page);
|
|
39
39
|
if ((_b = context.signal) == null ? void 0 : _b.aborted) break;
|
|
40
|
-
if (
|
|
40
|
+
if (!shouldFetchNextPage(acc, crawlOptions)) break;
|
|
41
41
|
const nextParam = getNextPageParam(page, pages, currentParam, pageParams);
|
|
42
42
|
if (nextParam == null) break;
|
|
43
43
|
currentParam = nextParam;
|
|
@@ -69,7 +69,7 @@ function buildInfiniteCrawlingQueryFn(queryFn, getNextPageParam, shouldFetchNext
|
|
|
69
69
|
const nextParam = getNextPageParam(page, pages, currentParam, pageParams);
|
|
70
70
|
nextBatchParam = nextParam != null ? nextParam : null;
|
|
71
71
|
if (nextParam == null) break;
|
|
72
|
-
if (
|
|
72
|
+
if (!shouldFetchNextPage(acc, crawlOptions)) break;
|
|
73
73
|
currentParam = nextParam;
|
|
74
74
|
}
|
|
75
75
|
if (acc === void 0) throw new DOMException("Aborted", "AbortError");
|
|
@@ -88,7 +88,7 @@ function buildFactory(cfg) {
|
|
|
88
88
|
reduce,
|
|
89
89
|
standardOptions
|
|
90
90
|
} = cfg;
|
|
91
|
-
const hasCrawling = rawQueryFn !== void 0 && getNextPageParam !== void 0;
|
|
91
|
+
const hasCrawling = rawQueryFn !== void 0 && getNextPageParam !== void 0 && shouldFetchNextPage !== void 0;
|
|
92
92
|
const hasInfiniteCrawling = hasCrawling && reduce !== void 0;
|
|
93
93
|
const crawlingFn = hasCrawling ? buildCrawlingQueryFn(
|
|
94
94
|
rawQueryFn,
|
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",
|