@orval/query 8.23.0 → 8.24.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/dist/index.mjs CHANGED
@@ -1914,6 +1914,24 @@ const hasQueryParam = (queryParams, queryParam) => {
1914
1914
  if (!queryParam || !queryParams) return false;
1915
1915
  return queryParams.paramNames?.includes(queryParam) ?? true;
1916
1916
  };
1917
+ /**
1918
+ * When nothing is configured, infinite hooks stay allowed, just without a
1919
+ * page param. When candidates are configured but the operation declares none
1920
+ * of them, the infinite hook is suppressed so orval does not emit a useless
1921
+ * one for a non-paginated `GET`.
1922
+ */
1923
+ const resolveInfiniteQueryParam = (queryParams, useInfiniteQueryParam) => {
1924
+ const candidates = (Array.isArray(useInfiniteQueryParam) ? useInfiniteQueryParam : [useInfiniteQueryParam]).filter((name) => !!name);
1925
+ if (candidates.length === 0) return {
1926
+ queryParam: void 0,
1927
+ infiniteHookAllowed: true
1928
+ };
1929
+ const queryParam = candidates.find((name) => hasQueryParam(queryParams, name));
1930
+ return {
1931
+ queryParam,
1932
+ infiniteHookAllowed: queryParam !== void 0
1933
+ };
1934
+ };
1917
1935
  const escapeRegExpMetaChars = (value) => value.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`);
1918
1936
  /**
1919
1937
  * Wraps the body parameter's type in a property string with the mutator's
@@ -1983,7 +2001,8 @@ const renderSetQueryDataHelper = ({ doc, isReactQuery, fnName, propsSig, body })
1983
2001
  };
1984
2002
  /**
1985
2003
  * Renders the prop list shared by `getXxxQueryKey`, `setXxxQueryData` and
1986
- * `getXxxQueryData` helpers: headers are dropped, path params stay required,
2004
+ * `getXxxQueryData` helpers: headers are dropped (unless `includeHeaders`
2005
+ * is set — key-mutator factories need them), path params stay required,
1987
2006
  * non-path params (query params, body) are passed through `widenNonPath`
1988
2007
  * (defaults to identity — pass `makeOptionalParam` or `allowUndefinedParam`
1989
2008
  * to relax the signature).
@@ -1991,8 +2010,8 @@ const renderSetQueryDataHelper = ({ doc, isReactQuery, fnName, propsSig, body })
1991
2010
  * Centralising this prevents the three call sites from drifting apart on
1992
2011
  * how they treat the same props.
1993
2012
  */
1994
- const buildKeyShapedProps = ({ props, body, mutator, widenNonPath = (impl) => impl }) => wrapPropsBodyWithMutatorBodyType({
1995
- propsString: toObjectString(props.filter((prop) => prop.type !== GetterPropType.HEADER).map((prop) => ({
2013
+ const buildKeyShapedProps = ({ props, body, mutator, widenNonPath = (impl) => impl, includeHeaders = false }) => wrapPropsBodyWithMutatorBodyType({
2014
+ propsString: toObjectString(props.filter((prop) => includeHeaders || prop.type !== GetterPropType.HEADER).map((prop) => ({
1996
2015
  ...prop,
1997
2016
  implementation: prop.type === GetterPropType.PARAM || prop.type === GetterPropType.NAMED_PATH_PARAMS ? prop.implementation : widenNonPath(prop.implementation)
1998
2017
  })), "implementation"),
@@ -2336,9 +2355,9 @@ const generateQueryHook = async (verbOptions, options, outputClient, adapter) =>
2336
2355
  const effectiveUseMutation = operationQueryOptions?.useMutation ?? override.query.useMutation ?? verb !== Verbs.GET;
2337
2356
  const globalSuspenseOrInfiniteOnlyForGet = (flag) => flag === true && verb === Verbs.GET;
2338
2357
  const effectiveUseSuspenseQuery = operationQueryOptions?.useSuspenseQuery ?? globalSuspenseOrInfiniteOnlyForGet(override.query.useSuspenseQuery);
2339
- const hasConfiguredInfiniteQueryParam = !query.useInfiniteQueryParam || hasQueryParam(queryParams, query.useInfiniteQueryParam);
2340
- const effectiveUseInfinite = (operationQueryOptions?.useInfinite ?? globalSuspenseOrInfiniteOnlyForGet(override.query.useInfinite)) && hasConfiguredInfiniteQueryParam;
2341
- const effectiveUseSuspenseInfiniteQuery = (operationQueryOptions?.useSuspenseInfiniteQuery ?? globalSuspenseOrInfiniteOnlyForGet(override.query.useSuspenseInfiniteQuery)) && hasConfiguredInfiniteQueryParam;
2358
+ const { queryParam: infiniteQueryParam, infiniteHookAllowed } = resolveInfiniteQueryParam(queryParams, operationQueryOptions?.useInfiniteQueryParam ?? query.useInfiniteQueryParam);
2359
+ const effectiveUseInfinite = (operationQueryOptions?.useInfinite ?? globalSuspenseOrInfiniteOnlyForGet(override.query.useInfinite)) && infiniteHookAllowed;
2360
+ const effectiveUseSuspenseInfiniteQuery = (operationQueryOptions?.useSuspenseInfiniteQuery ?? globalSuspenseOrInfiniteOnlyForGet(override.query.useSuspenseInfiniteQuery)) && infiniteHookAllowed;
2342
2361
  let isQuery = effectiveUseQuery || effectiveUseSuspenseQuery || effectiveUseInfinite || effectiveUseSuspenseInfiniteQuery;
2343
2362
  let isMutation = effectiveUseMutation;
2344
2363
  if (verb !== Verbs.GET && isQuery) isMutation = false;
@@ -2377,7 +2396,7 @@ const generateQueryHook = async (verbOptions, options, outputClient, adapter) =>
2377
2396
  typeName: camel(`${typeName}-infinite`),
2378
2397
  options: query.options,
2379
2398
  type: QueryType.INFINITE,
2380
- queryParam: query.useInfiniteQueryParam,
2399
+ queryParam: infiniteQueryParam,
2381
2400
  queryKeyFnName: camel(`get-${operationName}-infinite-query-key`)
2382
2401
  }] : [],
2383
2402
  ...effectiveUseQuery ? [{
@@ -2399,7 +2418,7 @@ const generateQueryHook = async (verbOptions, options, outputClient, adapter) =>
2399
2418
  typeName: camel(`${typeName}-suspense-infinite`),
2400
2419
  options: query.options,
2401
2420
  type: QueryType.SUSPENSE_INFINITE,
2402
- queryParam: query.useInfiniteQueryParam,
2421
+ queryParam: infiniteQueryParam,
2403
2422
  queryKeyFnName: camel(`get-${operationName}-infinite-query-key`)
2404
2423
  }] : []
2405
2424
  ];
@@ -2431,6 +2450,19 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
2431
2450
  ].filter((x) => !!x).join(", ")}
2432
2451
  ]${override.query.shouldFilterQueryKey ? `.filter(${override.query.queryKeyFilter ?? "q => q !== undefined"})` : " as const"};
2433
2452
  }
2453
+ `;
2454
+ }
2455
+ else if (override.query.shouldExportQueryKey) for (const queryOption of uniqueQueryOptionsByKeys) {
2456
+ const queryKeyProps = buildKeyShapedProps({
2457
+ props,
2458
+ body,
2459
+ mutator,
2460
+ widenNonPath: makeOptionalParam,
2461
+ includeHeaders: true
2462
+ });
2463
+ queryKeyFns += `
2464
+ export const ${queryOption.queryKeyFnName} = (${queryKeyProps}) =>
2465
+ ${queryKeyMutator.name}({ ${queryProperties} }${queryKeyMutator.hasSecondArg ? `, { url: \`${route}\` }` : ""});
2434
2466
  `;
2435
2467
  }
2436
2468
  implementation += `