@orval/query 8.25.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
@@ -968,7 +978,7 @@ const resolveUserQueryOptionsConstraint = (adapter, type, options) => {
968
978
  * constraint reshapes the type without requiring anything, so it does not count.
969
979
  */
970
980
  const requiresUserSuppliedQueryOptions = (adapter, type, options) => !!type && !!resolveUserQueryOptionsConstraint(adapter, type, options)?.require?.length;
971
- const getQueryOptionsDefinition = ({ operationName, mutator, definitions, type, prefix, hasQueryV5, hasQueryV5WithInfiniteQueryOptionsError, queryParams, queryParam, isReturnType, initialData, adapter, options }) => {
981
+ const getQueryOptionsDefinition = ({ operationName, mutator, definitions, mutationVariablesType, type, prefix, hasQueryV5, hasQueryV5WithInfiniteQueryOptionsError, queryParams, queryParam, isReturnType, initialData, adapter, options }) => {
972
982
  const isMutatorHook = mutator?.isHook;
973
983
  const partialOptions = !isReturnType && hasQueryV5;
974
984
  if (type) {
@@ -1001,7 +1011,8 @@ const getQueryOptionsDefinition = ({ operationName, mutator, definitions, type,
1001
1011
  return `${partialOptions ? "Partial<" : ""}${optionType}${partialOptions ? ">" : ""}${optionTypeInitialDataPostfix}`;
1002
1012
  }
1003
1013
  const mutationOptionsTypeName = adapter?.getOptionsReturnTypeName ? adapter.getOptionsReturnTypeName("mutation") : void 0;
1004
- return mutationOptionsTypeName ? `${mutationOptionsTypeName}<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>, TError,${definitions ? `{${definitions}}` : "void"}, TContext>` : `${prefix}MutationOptions<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>, TError,${definitions ? `{${definitions}}` : "void"}, TContext>`;
1014
+ const variablesType = mutationVariablesType ?? (definitions ? `{${definitions}}` : "void");
1015
+ return mutationOptionsTypeName ? `${mutationOptionsTypeName}<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>, TError,${variablesType}, TContext>` : `${prefix}MutationOptions<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>, TError,${variablesType}, TContext>`;
1005
1016
  };
1006
1017
  //#endregion
1007
1018
  //#region src/frameworks/angular.ts
@@ -1082,11 +1093,12 @@ const createAngularAdapter = ({ hasQueryV5, hasQueryV5WithDataTagError, hasQuery
1082
1093
  getQueryOptionsDefinitionPrefix() {
1083
1094
  return prefix;
1084
1095
  },
1085
- generateQueryArguments({ operationName, definitions, mutator, isRequestOptions, type, queryParams, queryParam, initialData, httpClient, forQueryOptions = false, hasInvalidation, useRuntimeFetcher }) {
1096
+ generateQueryArguments({ operationName, definitions, mutationVariablesType, mutator, isRequestOptions, type, queryParams, queryParam, initialData, httpClient, forQueryOptions = false, hasInvalidation, useRuntimeFetcher }) {
1086
1097
  const definition = getQueryOptionsDefinition({
1087
1098
  operationName,
1088
1099
  mutator,
1089
1100
  definitions,
1101
+ mutationVariablesType,
1090
1102
  type,
1091
1103
  prefix,
1092
1104
  hasQueryV5,
@@ -1326,11 +1338,12 @@ const createSvelteAdapter = ({ hasSvelteQueryV4, hasSvelteQueryV6, hasQueryV5, h
1326
1338
  getQueryOptionsDefinitionPrefix() {
1327
1339
  return prefix;
1328
1340
  },
1329
- generateQueryArguments({ operationName, definitions, mutator, isRequestOptions, type, queryParams, queryParam, initialData, httpClient, forQueryOptions = false, hasInvalidation, useRuntimeFetcher }) {
1341
+ generateQueryArguments({ operationName, definitions, mutationVariablesType, mutator, isRequestOptions, type, queryParams, queryParam, initialData, httpClient, forQueryOptions = false, hasInvalidation, useRuntimeFetcher }) {
1330
1342
  const definition = getQueryOptionsDefinition({
1331
1343
  operationName,
1332
1344
  mutator,
1333
1345
  definitions,
1346
+ mutationVariablesType,
1334
1347
  type,
1335
1348
  prefix,
1336
1349
  hasQueryV5,
@@ -1470,27 +1483,28 @@ const createVueAdapter = ({ hasVueQueryV4, hasQueryV5, hasQueryV5WithDataTagErro
1470
1483
  supportsMutationInvalidation() {
1471
1484
  return hasQueryV5;
1472
1485
  },
1473
- generateMutationOnSuccess({ operationName, definitions, isRequestOptions, generateInvalidateCall, uniqueInvalidates }) {
1486
+ generateMutationOnSuccess({ operationName, definitions, mutationVariablesType, isRequestOptions, generateInvalidateCall, uniqueInvalidates }) {
1474
1487
  const invalidateCalls = uniqueInvalidates.map((t) => generateInvalidateCall(t)).join("\n");
1488
+ const variablesType = mutationVariablesType ?? (definitions ? `{${definitions}}` : "void");
1475
1489
  if (hasQueryV5WithMutationContextOnSuccess) {
1476
- if (isRequestOptions) return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${definitions ? `{${definitions}}` : "void"}, onMutateResult: TContext, context: MutationFunctionContext) => {
1490
+ if (isRequestOptions) return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${variablesType}, onMutateResult: TContext, context: MutationFunctionContext) => {
1477
1491
  if (!options?.skipInvalidation) {
1478
1492
  ${invalidateCalls}
1479
1493
  }
1480
1494
  unref(unref(typeof mutationOptions === 'function' ? mutationOptions() : mutationOptions)?.onSuccess)?.(data, variables, onMutateResult, context);
1481
1495
  };`;
1482
- return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${definitions ? `{${definitions}}` : "void"}, onMutateResult: TContext, context: MutationFunctionContext) => {
1496
+ return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${variablesType}, onMutateResult: TContext, context: MutationFunctionContext) => {
1483
1497
  ${invalidateCalls}
1484
1498
  unref(unref(typeof mutationOptions === 'function' ? mutationOptions() : mutationOptions)?.onSuccess)?.(data, variables, onMutateResult, context);
1485
1499
  };`;
1486
1500
  }
1487
- if (isRequestOptions) return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${definitions ? `{${definitions}}` : "void"}, context: TContext${hasQueryV5WithRequiredContextOnSuccess ? "" : " | undefined"}) => {
1501
+ if (isRequestOptions) return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${variablesType}, context: TContext${hasQueryV5WithRequiredContextOnSuccess ? "" : " | undefined"}) => {
1488
1502
  if (!options?.skipInvalidation) {
1489
1503
  ${invalidateCalls}
1490
1504
  }
1491
1505
  unref(unref(typeof mutationOptions === 'function' ? mutationOptions() : mutationOptions)?.onSuccess)?.(data, variables, context);
1492
1506
  };`;
1493
- return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${definitions ? `{${definitions}}` : "void"}, context: TContext${hasQueryV5WithRequiredContextOnSuccess ? "" : " | undefined"}) => {
1507
+ return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${variablesType}, context: TContext${hasQueryV5WithRequiredContextOnSuccess ? "" : " | undefined"}) => {
1494
1508
  ${invalidateCalls}
1495
1509
  unref(unref(typeof mutationOptions === 'function' ? mutationOptions() : mutationOptions)?.onSuccess)?.(data, variables, context);
1496
1510
  };`;
@@ -1556,12 +1570,13 @@ const withDefaults = (adapter) => {
1556
1570
  getOptionalQueryClientArgument() {
1557
1571
  return composed.hasQueryV5 ? ", queryClient?: QueryClient" : "";
1558
1572
  },
1559
- generateQueryArguments({ operationName, definitions, mutator, isRequestOptions, type, queryParams, queryParam, initialData, httpClient, hasInvalidation, useRuntimeFetcher, options }) {
1573
+ generateQueryArguments({ operationName, definitions, mutationVariablesType, mutator, isRequestOptions, type, queryParams, queryParam, initialData, httpClient, hasInvalidation, useRuntimeFetcher, options }) {
1560
1574
  const prefix = composed.getQueryOptionsDefinitionPrefix();
1561
1575
  const definition = getQueryOptionsDefinition({
1562
1576
  operationName,
1563
1577
  mutator,
1564
1578
  definitions,
1579
+ mutationVariablesType,
1565
1580
  type,
1566
1581
  prefix,
1567
1582
  hasQueryV5: composed.hasQueryV5,
@@ -1580,27 +1595,28 @@ const withDefaults = (adapter) => {
1580
1595
  const optionsType = `{ ${type ? "query" : "mutation"}${isQueryRequired ? "" : "?"}:${definition}, ${!type && hasInvalidation ? "skipInvalidation?: boolean, " : ""}${requestType}}`;
1581
1596
  return `options${isQueryRequired ? "" : "?"}: ${optionsType}\n`;
1582
1597
  },
1583
- generateMutationOnSuccess({ operationName, definitions, isRequestOptions, generateInvalidateCall, uniqueInvalidates }) {
1598
+ generateMutationOnSuccess({ operationName, definitions, mutationVariablesType, isRequestOptions, generateInvalidateCall, uniqueInvalidates }) {
1584
1599
  const invalidateCalls = uniqueInvalidates.map((t) => generateInvalidateCall(t)).join("\n");
1600
+ const variablesType = mutationVariablesType ?? (definitions ? `{${definitions}}` : "void");
1585
1601
  if (composed.hasQueryV5WithMutationContextOnSuccess) {
1586
- if (isRequestOptions) return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${definitions ? `{${definitions}}` : "void"}, onMutateResult: TContext, context: MutationFunctionContext) => {
1602
+ if (isRequestOptions) return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${variablesType}, onMutateResult: TContext, context: MutationFunctionContext) => {
1587
1603
  if (!options?.skipInvalidation) {
1588
1604
  ${invalidateCalls}
1589
1605
  }
1590
1606
  mutationOptions?.onSuccess?.(data, variables, onMutateResult, context);
1591
1607
  };`;
1592
- return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${definitions ? `{${definitions}}` : "void"}, onMutateResult: TContext, context: MutationFunctionContext) => {
1608
+ return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${variablesType}, onMutateResult: TContext, context: MutationFunctionContext) => {
1593
1609
  ${invalidateCalls}
1594
1610
  mutationOptions?.onSuccess?.(data, variables, onMutateResult, context);
1595
1611
  };`;
1596
1612
  } else {
1597
- if (isRequestOptions) return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${definitions ? `{${definitions}}` : "void"}, context: TContext${composed.hasQueryV5WithRequiredContextOnSuccess ? "" : " | undefined"}) => {
1613
+ if (isRequestOptions) return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${variablesType}, context: TContext${composed.hasQueryV5WithRequiredContextOnSuccess ? "" : " | undefined"}) => {
1598
1614
  if (!options?.skipInvalidation) {
1599
1615
  ${invalidateCalls}
1600
1616
  }
1601
1617
  mutationOptions?.onSuccess?.(data, variables, context);
1602
1618
  };`;
1603
- return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${definitions ? `{${definitions}}` : "void"}, context: TContext${composed.hasQueryV5WithRequiredContextOnSuccess ? "" : " | undefined"}) => {
1619
+ return ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${variablesType}, context: TContext${composed.hasQueryV5WithRequiredContextOnSuccess ? "" : " | undefined"}) => {
1604
1620
  ${invalidateCalls}
1605
1621
  mutationOptions?.onSuccess?.(data, variables, context);
1606
1622
  };`;
@@ -1872,6 +1888,8 @@ const generateMutationHook = async ({ verbOptions, options, isRequestOptions, ht
1872
1888
  }) : void 0;
1873
1889
  const bodyOptionalMark = body.isOptional ? "?" : "";
1874
1890
  const definitions = props.map(({ definition, type }) => type === GetterPropType.BODY ? mutator?.bodyTypeName ? `data${bodyOptionalMark}: ${mutator.bodyTypeName}<${body.definition}>` : `data${bodyOptionalMark}: ${body.definition}` : definition).join(";");
1891
+ const variablesTypeName = definitions ? `${pascal(typeName)}MutationVariables` : void 0;
1892
+ const mutationVariablesType = variablesTypeName ?? "void";
1875
1893
  const properties = props.map(({ name, type }) => type === GetterPropType.BODY ? "data" : name).join(",");
1876
1894
  const operationLocalNames = new Set(MUTATION_OPERATION_LOCAL_NAMES);
1877
1895
  for (const { name, type } of props) operationLocalNames.add(type === GetterPropType.BODY ? "data" : name);
@@ -1883,6 +1901,7 @@ const generateMutationHook = async ({ verbOptions, options, isRequestOptions, ht
1883
1901
  operationName: operationTypeReferenceName,
1884
1902
  mutator,
1885
1903
  definitions,
1904
+ mutationVariablesType,
1886
1905
  prefix: adapter.getQueryOptionsDefinitionPrefix(),
1887
1906
  hasQueryV5: adapter.hasQueryV5,
1888
1907
  hasQueryV5WithInfiniteQueryOptionsError: adapter.hasQueryV5WithInfiniteQueryOptionsError,
@@ -1902,6 +1921,7 @@ const generateMutationHook = async ({ verbOptions, options, isRequestOptions, ht
1902
1921
  const mutationArguments = adapter.generateQueryArguments({
1903
1922
  operationName: operationTypeReferenceName,
1904
1923
  definitions,
1924
+ mutationVariablesType,
1905
1925
  mutator,
1906
1926
  isRequestOptions,
1907
1927
  httpClient,
@@ -1911,6 +1931,7 @@ const generateMutationHook = async ({ verbOptions, options, isRequestOptions, ht
1911
1931
  const mutationArgumentsForOptions = adapter.generateQueryArguments({
1912
1932
  operationName: operationTypeReferenceName,
1913
1933
  definitions,
1934
+ mutationVariablesType,
1914
1935
  mutator,
1915
1936
  isRequestOptions,
1916
1937
  httpClient,
@@ -1918,8 +1939,13 @@ const generateMutationHook = async ({ verbOptions, options, isRequestOptions, ht
1918
1939
  hasInvalidation,
1919
1940
  useRuntimeFetcher
1920
1941
  });
1921
- const mutationOptionsFnName = camel(mutationOptionsMutator || mutator?.isHook ? `use-${operationName}-mutationOptions` : `get-${operationName}-mutationOptions`);
1922
- 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);
1923
1949
  const mutationOptionsFn = `export const ${mutationOptionsFnName} = <TError = ${errorType},
1924
1950
  TContext = unknown>(${adapter.getHttpFirstParam(mutator)}${hasInvalidation ? "queryClient: QueryClient, " : ""}${mutationArgumentsForOptions}): ${mutationOptionFnReturnType} => {
1925
1951
 
@@ -1928,7 +1954,7 @@ ${hooksOptionImplementation}
1928
1954
  ${mutator?.isHook ? `const ${operationReferenceName} = use${pascal(operationName)}Hook()` : ""}
1929
1955
 
1930
1956
 
1931
- const mutationFn: MutationFunction<Awaited<ReturnType<${dataType}>>, ${definitions ? `{${definitions}}` : "void"}> = (${properties ? "props" : ""}) => {
1957
+ const mutationFn: MutationFunction<Awaited<ReturnType<${dataType}>>, ${mutationVariablesType}> = (${properties ? "props" : ""}) => {
1932
1958
  ${properties ? `const {${properties}} = props ?? {};` : ""}
1933
1959
 
1934
1960
  return ${operationReferenceName}(${adapter.getMutationHttpPrefix(mutator)}${properties}${properties ? "," : ""}${getMutationRequestArgs(isRequestOptions, httpClient, mutator, useRuntimeFetcher)})
@@ -1937,6 +1963,7 @@ ${hooksOptionImplementation}
1937
1963
  ${hasInvalidation ? adapter.generateMutationOnSuccess({
1938
1964
  operationName,
1939
1965
  definitions,
1966
+ mutationVariablesType,
1940
1967
  isRequestOptions,
1941
1968
  generateInvalidateCall: createGenerateInvalidateCall(context.spec, !!query.shouldSplitQueryKey, !!query.useOperationIdAsQueryKey, context.output.baseUrl, context.spec.servers),
1942
1969
  uniqueInvalidates
@@ -1956,7 +1983,7 @@ ${hasInvalidation ? adapter.generateMutationOnSuccess({
1956
1983
  const mutationOptionsVarName = camel(`${operationName}-mutation-options`);
1957
1984
  const mutationReturnType = adapter.getMutationReturnType({
1958
1985
  dataType,
1959
- variableType: definitions ? `{${definitions}}` : "void"
1986
+ variableType: mutationVariablesType
1960
1987
  });
1961
1988
  const mutationHookBody = adapter.generateMutationHookBody({
1962
1989
  operationPrefix,
@@ -1971,11 +1998,14 @@ ${hasInvalidation ? adapter.generateMutationOnSuccess({
1971
1998
  return {
1972
1999
  implementation: `
1973
2000
  ${!mutator?.isHook && operationReferenceName !== operationName ? `const ${operationReferenceName} = ${operationName};` : ""}
2001
+ ${mutationKeyFn}
2002
+
1974
2003
  ${mutationOptionsFn}
1975
2004
 
1976
2005
  export type ${pascal(typeName)}MutationResult = NonNullable<Awaited<ReturnType<${dataType}>>>
1977
2006
  ${body.definition ? `export type ${pascal(typeName)}MutationBody = ${mutator?.bodyTypeName ? `${mutator.bodyTypeName}<${body.definition}>` : body.definition}${body.isOptional ? " | undefined" : ""}` : ""}
1978
2007
  export type ${pascal(typeName)}MutationError = ${errorType}
2008
+ ${variablesTypeName ? `export type ${variablesTypeName} = {${definitions}}` : ""}
1979
2009
 
1980
2010
  ${doc}export const ${camel(`${operationPrefix}-${operationName}`)} = <TError = ${errorType},
1981
2011
  TContext = unknown>(${mutationArguments} ${optionalQueryClientArgument})${mutationReturnType} => {
@@ -2320,7 +2350,11 @@ const generateQueryImplementation = ({ queryOption: { name, typeName: optionType
2320
2350
  type,
2321
2351
  adapter
2322
2352
  });
2323
- 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`);
2324
2358
  const queryOptionsVarName = isRequestOptions ? "queryOptions" : "options";
2325
2359
  const queryResultVarName = props.some((prop) => prop.name === "query") ? "_query" : "query";
2326
2360
  const infiniteParam = infiniteQueryParamType ? `, ${infiniteQueryParamType}` : "";