@orval/query 8.26.0 → 8.27.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
@@ -40,7 +40,8 @@ const normalizeMutator = (workspace, mutator) => {
40
40
  default: m.default ?? !m.name,
41
41
  alias: m.alias,
42
42
  external: m.external,
43
- extension: m.extension
43
+ extension: m.extension,
44
+ useHooks: m.useHooks
44
45
  };
45
46
  }
46
47
  if (isString(mutator)) return {
@@ -48,6 +49,10 @@ const normalizeMutator = (workspace, mutator) => {
48
49
  default: true
49
50
  };
50
51
  };
52
+ const shouldUseOptionsHook = ({ optionsMutator, queryKeyMutator, mutator }) => {
53
+ if (optionsMutator) return optionsMutator.useHooks ?? true;
54
+ return !!queryKeyMutator || !!mutator?.isHook;
55
+ };
51
56
  const getQueryTypeForFramework = (type) => {
52
57
  switch (type) {
53
58
  case "suspenseQuery": return "query";
@@ -377,10 +382,15 @@ const getQueryErrorType = (operationName, response, httpClient, mutator, forceSu
377
382
  if (forceSuccessResponse) return `globalThis.Error & { info?: ${errorsType}; status?: number }`;
378
383
  return errorsType;
379
384
  };
380
- const getHooksOptionImplementation = (isRequestOptions, httpClient, operationName, mutator, useRuntimeFetcher) => {
385
+ /**
386
+ * Options destructuring at the top of `get/useXxxMutationOptions`. The mutation
387
+ * key is read from the hoisted `getXxxMutationKey()` getter emitted by the
388
+ * mutation generator.
389
+ */
390
+ const getHooksOptionImplementation = (isRequestOptions, httpClient, mutationKeyFnName, mutator, useRuntimeFetcher) => {
381
391
  const fetcherOption = httpClient === OutputHttpClient.FETCH && useRuntimeFetcher ? ", fetcher: fetcherFn" : "";
382
392
  const options = httpClient === OutputHttpClient.AXIOS ? ", axios: axiosOptions" : `, fetch: fetchOptions${fetcherOption}`;
383
- return isRequestOptions ? `const mutationKey = ['${operationName}'];
393
+ return isRequestOptions ? `const mutationKey = ${mutationKeyFnName}();
384
394
  const {mutation: mutationOptions${mutator ? mutator.hasSecondArg ? ", request: requestOptions" : "" : options}} = options ?
385
395
  options.mutation && 'mutationKey' in options.mutation && options.mutation.mutationKey ?
386
396
  options
@@ -1929,8 +1939,13 @@ const generateMutationHook = async ({ verbOptions, options, isRequestOptions, ht
1929
1939
  hasInvalidation,
1930
1940
  useRuntimeFetcher
1931
1941
  });
1932
- const mutationOptionsFnName = camel(mutationOptionsMutator || mutator?.isHook ? `use-${operationName}-mutationOptions` : `get-${operationName}-mutationOptions`);
1933
- const hooksOptionImplementation = getHooksOptionImplementation(isRequestOptions, httpClient, camel(operationName), mutator, useRuntimeFetcher);
1942
+ const mutationOptionsFnName = camel(shouldUseOptionsHook({
1943
+ optionsMutator: mutationOptionsMutator,
1944
+ mutator
1945
+ }) ? `use-${operationName}-mutationOptions` : `get-${operationName}-mutationOptions`);
1946
+ const mutationKeyFnName = camel(`get-${operationName}-mutation-key`);
1947
+ const mutationKeyFn = query.shouldExportQueryKey || isRequestOptions ? `${query.shouldExportQueryKey ? "export " : ""}const ${mutationKeyFnName} = () => ['${camel(operationName)}'] as const;` : "";
1948
+ const hooksOptionImplementation = getHooksOptionImplementation(isRequestOptions, httpClient, mutationKeyFnName, mutator, useRuntimeFetcher);
1934
1949
  const mutationOptionsFn = `export const ${mutationOptionsFnName} = <TError = ${errorType},
1935
1950
  TContext = unknown>(${adapter.getHttpFirstParam(mutator)}${hasInvalidation ? "queryClient: QueryClient, " : ""}${mutationArgumentsForOptions}): ${mutationOptionFnReturnType} => {
1936
1951
 
@@ -1983,6 +1998,8 @@ ${hasInvalidation ? adapter.generateMutationOnSuccess({
1983
1998
  return {
1984
1999
  implementation: `
1985
2000
  ${!mutator?.isHook && operationReferenceName !== operationName ? `const ${operationReferenceName} = ${operationName};` : ""}
2001
+ ${mutationKeyFn}
2002
+
1986
2003
  ${mutationOptionsFn}
1987
2004
 
1988
2005
  export type ${pascal(typeName)}MutationResult = NonNullable<Awaited<ReturnType<${dataType}>>>
@@ -2333,7 +2350,11 @@ const generateQueryImplementation = ({ queryOption: { name, typeName: optionType
2333
2350
  type,
2334
2351
  adapter
2335
2352
  });
2336
- const queryOptionsFnName = camel(queryKeyMutator || queryOptionsMutator || mutator?.isHook ? `use-${name}-queryOptions` : `get-${name}-queryOptions`);
2353
+ const queryOptionsFnName = camel(shouldUseOptionsHook({
2354
+ optionsMutator: queryOptionsMutator,
2355
+ queryKeyMutator,
2356
+ mutator
2357
+ }) ? `use-${name}-queryOptions` : `get-${name}-queryOptions`);
2337
2358
  const queryOptionsVarName = isRequestOptions ? "queryOptions" : "options";
2338
2359
  const queryResultVarName = props.some((prop) => prop.name === "query") ? "_query" : "query";
2339
2360
  const infiniteParam = infiniteQueryParamType ? `, ${infiniteQueryParamType}` : "";