@robohall/react-query-factory 2.1.2 → 2.1.4

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 CHANGED
@@ -258,8 +258,12 @@ import { paginateDescribeInstances } from '@aws-sdk/client-ec2';
258
258
 
259
259
  const describeInstances = queryFactory({
260
260
  queryKey: ['ec2:DescribeInstances'],
261
- queryFn: (params: DescribeInstancesCommandInput) =>
262
- paginateDescribeInstances({ client: ec2 }, params),
261
+ queryFn: (params: DescribeInstancesCommandInput, ctx) =>
262
+ paginateDescribeInstances(
263
+ { client: ec2, startingToken: ctx.pageParam ?? params.NextToken },
264
+ params,
265
+ ),
266
+ initialPageParam: undefined as string | undefined,
263
267
  shouldFetchNextPage: (instances, opts: { minResults?: number }) =>
264
268
  opts.minResults != null && instances.length < opts.minResults,
265
269
  reduce: (acc, page: DescribeInstancesResponse): Instance[] => [
@@ -278,8 +282,8 @@ const describeInstances = queryFactory({
278
282
  queryKey: ['ec2:DescribeInstances'],
279
283
  queryFn: (params: DescribeInstancesCommandInput, ctx) =>
280
284
  paginateDescribeInstances(
281
- { client: ec2 },
282
- { ...params, StartingToken: ctx.pageParam },
285
+ { client: ec2, startingToken: ctx.pageParam ?? params.NextToken },
286
+ params,
283
287
  ),
284
288
  getNextPageParam: page => page.NextToken,
285
289
  initialPageParam: undefined as string | undefined,
package/dist/index.d.mts CHANGED
@@ -128,6 +128,23 @@ declare function queryFactory<TParams = void, TData = unknown, TError = Error, T
128
128
  * // useQuery(usersFactory({ page: 1 }))
129
129
  */
130
130
  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>;
131
+ /**
132
+ * Creates a child factory whose queryFn returns an AsyncIterable (e.g. an AWS SDK v3
133
+ * paginator). Inherits the parent's key namespace. Use this overload when switching
134
+ * to a paginator-based fetch while keeping the same result shape and crawl options.
135
+ */
136
+ declare function queryFactory<TChildParams extends TParentParams, TData = unknown, TError = Error, TParentSelected = TData, TChildSelected = TParentSelected, TParentParams = TChildParams, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>>(parent: QueryFactory<TParentParams, any, any, TParentSelected, any, any, any>, config: StandardQueryOptions<TError, TData> & {
137
+ queryKey?: QueryKey;
138
+ queryFn: (params: TChildParams, context: QueryFunctionContext<QueryKey, [
139
+ unknown
140
+ ] extends [TPageParam] ? never : TPageParam>) => AsyncIterable<TData>;
141
+ select?: (data: TData) => TChildSelected;
142
+ reduce?: (accumulator: TChildSelected | undefined, page: TData) => TChildSelected;
143
+ shouldFetchNextPage?: (combined: TChildSelected | undefined, crawlOptions: TCrawlOptions) => boolean;
144
+ getNextPageParam?: GetNextPageParamFunction<TPageParam, TData>;
145
+ getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
146
+ initialPageParam?: TPageParam;
147
+ }): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions, boolean>;
131
148
  /**
132
149
  * Creates a child factory that inherits the query key and standard options from
133
150
  * `parent` and introduces a new `queryFn`. The child's query key is appended to
package/dist/index.d.ts CHANGED
@@ -128,6 +128,23 @@ declare function queryFactory<TParams = void, TData = unknown, TError = Error, T
128
128
  * // useQuery(usersFactory({ page: 1 }))
129
129
  */
130
130
  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>;
131
+ /**
132
+ * Creates a child factory whose queryFn returns an AsyncIterable (e.g. an AWS SDK v3
133
+ * paginator). Inherits the parent's key namespace. Use this overload when switching
134
+ * to a paginator-based fetch while keeping the same result shape and crawl options.
135
+ */
136
+ declare function queryFactory<TChildParams extends TParentParams, TData = unknown, TError = Error, TParentSelected = TData, TChildSelected = TParentSelected, TParentParams = TChildParams, TPageParam = unknown, TCrawlOptions extends Record<string, unknown> = Record<string, unknown>>(parent: QueryFactory<TParentParams, any, any, TParentSelected, any, any, any>, config: StandardQueryOptions<TError, TData> & {
137
+ queryKey?: QueryKey;
138
+ queryFn: (params: TChildParams, context: QueryFunctionContext<QueryKey, [
139
+ unknown
140
+ ] extends [TPageParam] ? never : TPageParam>) => AsyncIterable<TData>;
141
+ select?: (data: TData) => TChildSelected;
142
+ reduce?: (accumulator: TChildSelected | undefined, page: TData) => TChildSelected;
143
+ shouldFetchNextPage?: (combined: TChildSelected | undefined, crawlOptions: TCrawlOptions) => boolean;
144
+ getNextPageParam?: GetNextPageParamFunction<TPageParam, TData>;
145
+ getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
146
+ initialPageParam?: TPageParam;
147
+ }): QueryFactory<TChildParams, TData, TError, TChildSelected, TPageParam, TCrawlOptions, boolean>;
131
148
  /**
132
149
  * Creates a child factory that inherits the query key and standard options from
133
150
  * `parent` and introduces a new `queryFn`. The child's query key is appended to
package/dist/index.js CHANGED
@@ -248,7 +248,7 @@ function buildFactory(cfg) {
248
248
  return factory;
249
249
  }
250
250
  function queryFactory(configOrParent, childConfig) {
251
- var _a, _b, _c, _d, _e;
251
+ var _a, _b, _c, _d, _e, _f, _g;
252
252
  if (childConfig !== void 0 && typeof configOrParent === "function") {
253
253
  const result = configOrParent();
254
254
  const parentCfg = result == null ? void 0 : result[FACTORY_CONFIG];
@@ -277,20 +277,23 @@ function queryFactory(configOrParent, childConfig) {
277
277
  getNextPageParam: childConfig.getNextPageParam,
278
278
  getPreviousPageParam: childConfig.getPreviousPageParam,
279
279
  initialPageParam: childConfig.initialPageParam,
280
- shouldFetchNextPage: childConfig.shouldFetchNextPage,
281
- reduce: childConfig.reduce
280
+ // Fall back to parent's crawling logic when child doesn't override —
281
+ // useful when the child only changes how pages are fetched (e.g. switching
282
+ // to a paginator queryFn) but accumulates the same result shape.
283
+ shouldFetchNextPage: (_b = childConfig.shouldFetchNextPage) != null ? _b : parentCfg.shouldFetchNextPage,
284
+ reduce: (_c = childConfig.reduce) != null ? _c : parentCfg.reduce
282
285
  } : {
283
- getNextPageParam: (_b = childConfig.getNextPageParam) != null ? _b : parentCfg.getNextPageParam,
284
- getPreviousPageParam: (_c = childConfig.getPreviousPageParam) != null ? _c : parentCfg.getPreviousPageParam,
286
+ getNextPageParam: (_d = childConfig.getNextPageParam) != null ? _d : parentCfg.getNextPageParam,
287
+ getPreviousPageParam: (_e = childConfig.getPreviousPageParam) != null ? _e : parentCfg.getPreviousPageParam,
285
288
  initialPageParam: "initialPageParam" in childConfig ? childConfig.initialPageParam : parentCfg.initialPageParam,
286
- shouldFetchNextPage: (_d = childConfig.shouldFetchNextPage) != null ? _d : parentCfg.shouldFetchNextPage,
287
- reduce: (_e = childConfig.reduce) != null ? _e : parentCfg.reduce
289
+ shouldFetchNextPage: (_f = childConfig.shouldFetchNextPage) != null ? _f : parentCfg.shouldFetchNextPage,
290
+ reduce: (_g = childConfig.reduce) != null ? _g : parentCfg.reduce
288
291
  };
289
292
  const {
290
293
  queryKey: _k,
291
- queryFn: _f,
294
+ queryFn: _f2,
292
295
  select: _s,
293
- getNextPageParam: _g,
296
+ getNextPageParam: _g2,
294
297
  getPreviousPageParam: _gp,
295
298
  initialPageParam: _ip,
296
299
  shouldFetchNextPage: _sfnp,
package/dist/index.mjs CHANGED
@@ -222,7 +222,7 @@ function buildFactory(cfg) {
222
222
  return factory;
223
223
  }
224
224
  function queryFactory(configOrParent, childConfig) {
225
- var _a, _b, _c, _d, _e;
225
+ var _a, _b, _c, _d, _e, _f, _g;
226
226
  if (childConfig !== void 0 && typeof configOrParent === "function") {
227
227
  const result = configOrParent();
228
228
  const parentCfg = result == null ? void 0 : result[FACTORY_CONFIG];
@@ -251,20 +251,23 @@ function queryFactory(configOrParent, childConfig) {
251
251
  getNextPageParam: childConfig.getNextPageParam,
252
252
  getPreviousPageParam: childConfig.getPreviousPageParam,
253
253
  initialPageParam: childConfig.initialPageParam,
254
- shouldFetchNextPage: childConfig.shouldFetchNextPage,
255
- reduce: childConfig.reduce
254
+ // Fall back to parent's crawling logic when child doesn't override —
255
+ // useful when the child only changes how pages are fetched (e.g. switching
256
+ // to a paginator queryFn) but accumulates the same result shape.
257
+ shouldFetchNextPage: (_b = childConfig.shouldFetchNextPage) != null ? _b : parentCfg.shouldFetchNextPage,
258
+ reduce: (_c = childConfig.reduce) != null ? _c : parentCfg.reduce
256
259
  } : {
257
- getNextPageParam: (_b = childConfig.getNextPageParam) != null ? _b : parentCfg.getNextPageParam,
258
- getPreviousPageParam: (_c = childConfig.getPreviousPageParam) != null ? _c : parentCfg.getPreviousPageParam,
260
+ getNextPageParam: (_d = childConfig.getNextPageParam) != null ? _d : parentCfg.getNextPageParam,
261
+ getPreviousPageParam: (_e = childConfig.getPreviousPageParam) != null ? _e : parentCfg.getPreviousPageParam,
259
262
  initialPageParam: "initialPageParam" in childConfig ? childConfig.initialPageParam : parentCfg.initialPageParam,
260
- shouldFetchNextPage: (_d = childConfig.shouldFetchNextPage) != null ? _d : parentCfg.shouldFetchNextPage,
261
- reduce: (_e = childConfig.reduce) != null ? _e : parentCfg.reduce
263
+ shouldFetchNextPage: (_f = childConfig.shouldFetchNextPage) != null ? _f : parentCfg.shouldFetchNextPage,
264
+ reduce: (_g = childConfig.reduce) != null ? _g : parentCfg.reduce
262
265
  };
263
266
  const {
264
267
  queryKey: _k,
265
- queryFn: _f,
268
+ queryFn: _f2,
266
269
  select: _s,
267
- getNextPageParam: _g,
270
+ getNextPageParam: _g2,
268
271
  getPreviousPageParam: _gp,
269
272
  initialPageParam: _ip,
270
273
  shouldFetchNextPage: _sfnp,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@robohall/react-query-factory",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
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",
@@ -33,6 +33,7 @@
33
33
  "build": "tsup src/index.ts --format cjs,esm --dts --clean",
34
34
  "typecheck": "tsc --noEmit",
35
35
  "test": "vitest run",
36
+ "test:coverage": "vitest run --coverage",
36
37
  "test:watch": "vitest",
37
38
  "prepublishOnly": "npm run build && npm test",
38
39
  "sandbox": "cd sandbox && npm run dev",
@@ -43,6 +44,7 @@
43
44
  },
44
45
  "devDependencies": {
45
46
  "@tanstack/react-query": "^5.0.0",
47
+ "@vitest/coverage-v8": "^2.1.9",
46
48
  "prettier": "^3.8.3",
47
49
  "tsup": "^8.0.0",
48
50
  "typescript": "^5.0.0",