@orval/query 8.19.0 → 8.21.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
@@ -1,4 +1,4 @@
1
- import { GetterPropType, OutputClient, OutputHttpClient, Verbs, camel, compareVersions, generateFormDataAndUrlEncodedFunction, generateMutator, generateMutatorConfig, generateMutatorRequestOptions, generateOptions, generateVerbImports, getAngularFilteredParamsCallExpression, getAngularFilteredParamsHelperBody, getFullRoute, getRouteAsArray, getSuccessResponseType, isObject, isOperationInTagBucket, isString, isSyntheticDefaultImportsAllow, jsDoc, logWarning, makeRouteSafe, mergeDeep, pascal, stringify, toObjectString } from "@orval/core";
1
+ import { GetterPropType, OutputClient, OutputHttpClient, Verbs, camel, compareVersions, generateFormDataAndUrlEncodedFunction, generateMutator, generateMutatorConfig, generateMutatorRequestOptions, generateOptions, generateVerbImports, getAngularFilteredParamsCallExpression, getAngularFilteredParamsHelperBody, getFullRoute, getRoute, getRouteAsArray, getSuccessResponseType, isObject, isOperationInTagBucket, isString, isSyntheticDefaultImportsAllow, jsDoc, logWarning, makeRouteSafe, mergeDeep, pascal, stringify, toObjectString } from "@orval/core";
2
2
  import { generateFetchHeader, generateRequestFunction } from "@orval/fetch";
3
3
  import nodePath from "node:path";
4
4
  import { styleText } from "node:util";
@@ -48,24 +48,6 @@ const normalizeMutator = (workspace, mutator) => {
48
48
  default: true
49
49
  };
50
50
  };
51
- function vueWrapTypeWithMaybeRef(props) {
52
- return props.map((prop) => {
53
- const [paramName, paramType] = prop.implementation.split(":");
54
- if (!paramType) return prop;
55
- const name = prop.type === GetterPropType.NAMED_PATH_PARAMS ? prop.name : paramName;
56
- const [type, defaultValue] = paramType.split("=");
57
- return {
58
- ...prop,
59
- implementation: `${name}: MaybeRef<${type.trim()}>${defaultValue ? ` = ${defaultValue}` : ""}`
60
- };
61
- });
62
- }
63
- const vueUnRefParams = (props) => {
64
- return props.map((prop) => {
65
- if (prop.type === GetterPropType.NAMED_PATH_PARAMS) return `const ${prop.destructured} = unref(${prop.name});`;
66
- return `${prop.name} = unref(${prop.name});`;
67
- }).join("\n");
68
- };
69
51
  const getQueryTypeForFramework = (type) => {
70
52
  switch (type) {
71
53
  case "suspenseQuery": return "query";
@@ -688,15 +670,20 @@ const VUE_QUERY_DEPENDENCIES = [{
688
670
  dependency: "@tanstack/vue-query"
689
671
  }, {
690
672
  exports: [
673
+ {
674
+ name: "computed",
675
+ values: true
676
+ },
691
677
  {
692
678
  name: "unref",
693
679
  values: true
694
680
  },
695
- { name: "MaybeRef" },
696
681
  {
697
- name: "computed",
682
+ name: "toValue",
698
683
  values: true
699
- }
684
+ },
685
+ { name: "MaybeRef" },
686
+ { name: "MaybeRefOrGetter" }
700
687
  ],
701
688
  dependency: "vue"
702
689
  }];
@@ -1294,6 +1281,33 @@ const createSvelteAdapter = ({ hasSvelteQueryV4, hasSvelteQueryV6, hasQueryV5, h
1294
1281
  };
1295
1282
  //#endregion
1296
1283
  //#region src/frameworks/vue.ts
1284
+ const getVueReactivity = (hasQueryV5) => hasQueryV5 ? {
1285
+ wrapper: "MaybeRefOrGetter",
1286
+ resolve: "toValue"
1287
+ } : {
1288
+ wrapper: "MaybeRef",
1289
+ resolve: "unref"
1290
+ };
1291
+ function vueWrapTypeWithMaybeRef(props, hasQueryV5) {
1292
+ const { wrapper } = getVueReactivity(hasQueryV5);
1293
+ return props.map((prop) => {
1294
+ const [paramName, paramType] = prop.implementation.split(":");
1295
+ if (!paramType) return prop;
1296
+ const name = prop.type === GetterPropType.NAMED_PATH_PARAMS ? prop.name : paramName;
1297
+ const [type, defaultValue] = paramType.split("=");
1298
+ return {
1299
+ ...prop,
1300
+ implementation: `${name}: ${wrapper}<${type.trim()}>${defaultValue ? ` = ${defaultValue}` : ""}`
1301
+ };
1302
+ });
1303
+ }
1304
+ const vueUnRefParams = (props, hasQueryV5) => {
1305
+ const { resolve } = getVueReactivity(hasQueryV5);
1306
+ return props.map((prop) => {
1307
+ if (prop.type === GetterPropType.NAMED_PATH_PARAMS) return `const ${prop.destructured} = ${resolve}(${prop.name});`;
1308
+ return `${prop.name} = ${resolve}(${prop.name});`;
1309
+ }).join("\n");
1310
+ };
1297
1311
  const createVueAdapter = ({ hasVueQueryV4, hasQueryV5, hasQueryV5WithDataTagError, hasQueryV5WithInfiniteQueryOptionsError, hasQueryV5WithMutationContextOnSuccess, hasQueryV5WithRequiredContextOnSuccess }) => ({
1298
1312
  outputClient: OutputClient.VUE_QUERY,
1299
1313
  hookPrefix: "use",
@@ -1303,19 +1317,23 @@ const createVueAdapter = ({ hasVueQueryV4, hasQueryV5, hasQueryV5WithDataTagErro
1303
1317
  hasQueryV5WithMutationContextOnSuccess,
1304
1318
  hasQueryV5WithRequiredContextOnSuccess,
1305
1319
  transformProps(props) {
1306
- return vueWrapTypeWithMaybeRef(props);
1320
+ return vueWrapTypeWithMaybeRef(props, hasQueryV5);
1307
1321
  },
1308
1322
  shouldDestructureNamedPathParams() {
1309
1323
  return false;
1310
1324
  },
1311
1325
  getHttpFunctionQueryProps(queryProperties, httpClient) {
1312
- if (httpClient === OutputHttpClient.FETCH && queryProperties) return queryProperties.split(",").map((prop) => `unref(${prop})`).join(",");
1326
+ if (httpClient === OutputHttpClient.FETCH && queryProperties) {
1327
+ const { resolve } = getVueReactivity(hasQueryV5);
1328
+ return queryProperties.split(",").map((prop) => `${resolve}(${prop})`).join(",");
1329
+ }
1313
1330
  return queryProperties;
1314
1331
  },
1315
1332
  getInfiniteQueryHttpProps(props, queryParam, httpClient) {
1333
+ const { resolve } = getVueReactivity(hasQueryV5);
1316
1334
  return props.map((param) => {
1317
- if (param.name === "params") return `{...unref(params), '${queryParam}': pageParam ?? unref(params)?.['${queryParam}']}`;
1318
- return httpClient === OutputHttpClient.FETCH ? `unref(${param.name})` : param.name;
1335
+ if (param.name === "params") return `{...${resolve}(params), '${queryParam}': pageParam ?? ${resolve}(params)?.['${queryParam}']}`;
1336
+ return httpClient === OutputHttpClient.FETCH ? `${resolve}(${param.name})` : param.name;
1319
1337
  }).join(",");
1320
1338
  },
1321
1339
  getQueryReturnType({ type }) {
@@ -1343,17 +1361,20 @@ const createVueAdapter = ({ hasVueQueryV4, hasQueryV5, hasQueryV5WithDataTagErro
1343
1361
  return false;
1344
1362
  },
1345
1363
  getRequestUnrefStatements(props) {
1346
- return vueUnRefParams(props);
1364
+ return vueUnRefParams(props, hasQueryV5);
1347
1365
  },
1348
1366
  getQueryOptionsUnrefStatements(props) {
1349
- return vueUnRefParams(props.filter((prop) => prop.type === GetterPropType.NAMED_PATH_PARAMS));
1367
+ return vueUnRefParams(props.filter((prop) => prop.type === GetterPropType.NAMED_PATH_PARAMS), hasQueryV5);
1350
1368
  },
1351
1369
  wrapHookMutatorCallback(callback) {
1352
1370
  return callback;
1353
1371
  },
1354
1372
  generateEnabledOption(params, options) {
1355
1373
  if (params.length === 0) return "";
1356
- if (!isObject(options) || !Object.hasOwn(options, "enabled")) return `enabled: computed(() => ${params.map(({ name }) => `unref(${name}) !== null && unref(${name}) !== undefined`).join(" && ")}),`;
1374
+ if (!isObject(options) || !Object.hasOwn(options, "enabled")) {
1375
+ const { resolve } = getVueReactivity(hasQueryV5);
1376
+ return `enabled: computed(() => ${params.map(({ name }) => `${resolve}(${name}) !== null && ${resolve}(${name}) !== undefined`).join(" && ")}),`;
1377
+ }
1357
1378
  return "";
1358
1379
  },
1359
1380
  getQueryKeyPrefix() {
@@ -1723,7 +1744,7 @@ const createGenerateInvalidateCall = (spec, shouldSplitQueryKey, useOperationIdA
1723
1744
  if (info?.hasRequiredPathParams) {
1724
1745
  const prefix = getStaticRoutePrefix(info.route);
1725
1746
  if (prefix !== void 0) {
1726
- const prefixWithBase = getFullRoute(prefix, servers, baseUrl);
1747
+ const prefixWithBase = getFullRoute(getRoute(prefix), servers, baseUrl);
1727
1748
  const verbPrefix = getQueryKeyVerbPrefix({
1728
1749
  verb: info.method,
1729
1750
  useOperationIdAsQueryKey
@@ -1741,7 +1762,7 @@ const createGenerateInvalidateCall = (spec, shouldSplitQueryKey, useOperationIdA
1741
1762
  };
1742
1763
  };
1743
1764
  const generateMutationHook = async ({ verbOptions, options, isRequestOptions, httpClient, doc, adapter }) => {
1744
- const { operationName, body, props, mutator, response, operationId, route: pathRoute, override } = verbOptions;
1765
+ const { operationName, typeName, body, props, mutator, response, operationId, route: pathRoute, override } = verbOptions;
1745
1766
  const { route, context, output } = options;
1746
1767
  const query = override.query;
1747
1768
  const mutationOptionsMutator = query.mutationOptions ? await generateMutator({
@@ -1756,7 +1777,7 @@ const generateMutationHook = async ({ verbOptions, options, isRequestOptions, ht
1756
1777
  const properties = props.map(({ name, type }) => type === GetterPropType.BODY ? "data" : name).join(",");
1757
1778
  const operationLocalNames = new Set(MUTATION_OPERATION_LOCAL_NAMES);
1758
1779
  for (const { name, type } of props) operationLocalNames.add(type === GetterPropType.BODY ? "data" : name);
1759
- const errorType = getQueryErrorType(operationName, response, httpClient, mutator, override.fetch.forceSuccessResponse);
1780
+ const errorType = getQueryErrorType(typeName, response, httpClient, mutator, override.fetch.forceSuccessResponse);
1760
1781
  const operationReferenceName = getMutationOperationReferenceName(operationName, operationLocalNames);
1761
1782
  const dataType = mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationReferenceName}`;
1762
1783
  const operationTypeReferenceName = mutator?.isHook ? operationName : operationReferenceName;
@@ -1854,9 +1875,9 @@ ${hasInvalidation ? adapter.generateMutationOnSuccess({
1854
1875
  ${!mutator?.isHook && operationReferenceName !== operationName ? `const ${operationReferenceName} = ${operationName};` : ""}
1855
1876
  ${mutationOptionsFn}
1856
1877
 
1857
- export type ${pascal(operationName)}MutationResult = NonNullable<Awaited<ReturnType<${dataType}>>>
1858
- ${body.definition ? `export type ${pascal(operationName)}MutationBody = ${mutator?.bodyTypeName ? `${mutator.bodyTypeName}<${body.definition}>` : body.definition}${body.isOptional ? " | undefined" : ""}` : ""}
1859
- export type ${pascal(operationName)}MutationError = ${errorType}
1878
+ export type ${pascal(typeName)}MutationResult = NonNullable<Awaited<ReturnType<${dataType}>>>
1879
+ ${body.definition ? `export type ${pascal(typeName)}MutationBody = ${mutator?.bodyTypeName ? `${mutator.bodyTypeName}<${body.definition}>` : body.definition}${body.isOptional ? " | undefined" : ""}` : ""}
1880
+ export type ${pascal(typeName)}MutationError = ${errorType}
1860
1881
 
1861
1882
  ${doc}export const ${camel(`${operationPrefix}-${operationName}`)} = <TError = ${errorType},
1862
1883
  TContext = unknown>(${mutationArguments} ${optionalQueryClientArgument})${mutationReturnType} => {
@@ -2027,7 +2048,7 @@ const generatePrefetch = ({ usePrefetch, type, useQuery, useInfinite, operationN
2027
2048
  return queryClient;
2028
2049
  }\n`;
2029
2050
  };
2030
- const generateQueryImplementation = ({ queryOption: { name, queryParam, options, type, queryKeyFnName }, operationId, operationName, queryProperties, queryKeyProperties, queryParams, params, props, body, mutator, queryOptionsMutator, queryKeyMutator, isRequestOptions, response, httpClient, isExactOptionalPropertyTypes, hasSignal, useRuntimeFetcher, forceSuccessResponse, route, doc, usePrefetch, useQuery, useInfinite, useInvalidate, useSetQueryData, useGetQueryData, adapter }) => {
2051
+ const generateQueryImplementation = ({ queryOption: { name, typeName: optionTypeName, queryParam, options, type, queryKeyFnName }, operationId, operationName, typeName, queryProperties, queryKeyProperties, queryParams, params, props, body, mutator, queryOptionsMutator, queryKeyMutator, isRequestOptions, response, httpClient, isExactOptionalPropertyTypes, hasSignal, useRuntimeFetcher, forceSuccessResponse, route, doc, usePrefetch, useQuery, useInfinite, useInvalidate, useSetQueryData, useGetQueryData, adapter }) => {
2031
2052
  const { hasQueryV5, hasQueryV5WithDataTagError, hasQueryV5WithInfiniteQueryOptionsError } = adapter;
2032
2053
  const hasSignalParam = props.some((prop) => prop.name === "signal");
2033
2054
  const queryPropDefinitions = wrapPropsBodyWithMutatorBodyType({
@@ -2071,7 +2092,7 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
2071
2092
  hasQueryV5,
2072
2093
  hasQueryV5WithDataTagError
2073
2094
  });
2074
- const errorType = getQueryErrorType(operationName, response, httpClient, mutator, forceSuccessResponse);
2095
+ const errorType = getQueryErrorType(typeName, response, httpClient, mutator, forceSuccessResponse);
2075
2096
  const dataType = mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`;
2076
2097
  const definedInitialDataQueryArguments = adapter.generateQueryArguments({
2077
2098
  operationName,
@@ -2251,8 +2272,8 @@ export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${q
2251
2272
  return `
2252
2273
  ${queryOptionsFn}
2253
2274
 
2254
- export type ${pascal(name)}QueryResult = NonNullable<Awaited<ReturnType<${dataType}>>>
2255
- export type ${pascal(name)}QueryError = ${errorType}
2275
+ export type ${pascal(optionTypeName)}QueryResult = NonNullable<Awaited<ReturnType<${dataType}>>>
2276
+ export type ${pascal(optionTypeName)}QueryError = ${errorType}
2256
2277
 
2257
2278
  ${adapter.shouldGenerateOverrideTypes() ? overrideTypes : ""}
2258
2279
  ${doc}
@@ -2297,7 +2318,7 @@ ${shouldGenerateGetQueryData ? isReactQuery ? `${doc}export const ${getQueryData
2297
2318
  };
2298
2319
  const generateQueryHook = async (verbOptions, options, outputClient, adapter) => {
2299
2320
  if (!adapter) throw new Error("FrameworkAdapter is required for generateQueryHook");
2300
- const { queryParams, operationName, body, props: _props, verb, params, override, mutator, response, operationId, summary, deprecated } = verbOptions;
2321
+ const { queryParams, operationName, typeName, body, props: _props, verb, params, override, mutator, response, operationId, summary, deprecated } = verbOptions;
2301
2322
  const { route, override: { operations }, context, output } = options;
2302
2323
  const props = adapter.transformProps(_props);
2303
2324
  const query = override.query;
@@ -2353,6 +2374,7 @@ const generateQueryHook = async (verbOptions, options, outputClient, adapter) =>
2353
2374
  const queries = [
2354
2375
  ...effectiveUseInfinite ? [{
2355
2376
  name: camel(`${operationName}-infinite`),
2377
+ typeName: camel(`${typeName}-infinite`),
2356
2378
  options: query.options,
2357
2379
  type: QueryType.INFINITE,
2358
2380
  queryParam: query.useInfiniteQueryParam,
@@ -2360,18 +2382,21 @@ const generateQueryHook = async (verbOptions, options, outputClient, adapter) =>
2360
2382
  }] : [],
2361
2383
  ...effectiveUseQuery ? [{
2362
2384
  name: operationName,
2385
+ typeName,
2363
2386
  options: query.options,
2364
2387
  type: QueryType.QUERY,
2365
2388
  queryKeyFnName: camel(`get-${operationName}-query-key`)
2366
2389
  }] : [],
2367
2390
  ...effectiveUseSuspenseQuery ? [{
2368
2391
  name: camel(`${operationName}-suspense`),
2392
+ typeName: camel(`${typeName}-suspense`),
2369
2393
  options: query.options,
2370
2394
  type: QueryType.SUSPENSE_QUERY,
2371
2395
  queryKeyFnName: camel(`get-${operationName}-query-key`)
2372
2396
  }] : [],
2373
2397
  ...effectiveUseSuspenseInfiniteQuery ? [{
2374
2398
  name: camel(`${operationName}-suspense-infinite`),
2399
+ typeName: camel(`${typeName}-suspense-infinite`),
2375
2400
  options: query.options,
2376
2401
  type: QueryType.SUSPENSE_INFINITE,
2377
2402
  queryParam: query.useInfiniteQueryParam,
@@ -2415,6 +2440,7 @@ ${queryKeyFns}`;
2415
2440
  queryOption,
2416
2441
  operationId,
2417
2442
  operationName,
2443
+ typeName,
2418
2444
  queryProperties,
2419
2445
  queryKeyProperties,
2420
2446
  params,