@orval/query 7.15.0 → 8.0.0-rc.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.js +122 -285
- package/dist/index.js.map +1 -1
- package/package.json +11 -9
package/dist/index.js
CHANGED
|
@@ -1,35 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
-
key = keys[i];
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
-
get: ((k) => from[k]).bind(null, key),
|
|
14
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
-
value: mod,
|
|
21
|
-
enumerable: true
|
|
22
|
-
}) : target, mod));
|
|
23
|
-
|
|
24
|
-
//#endregion
|
|
25
|
-
let __orval_core = require("@orval/core");
|
|
26
|
-
__orval_core = __toESM(__orval_core);
|
|
27
|
-
let lodash_omitby = require("lodash.omitby");
|
|
28
|
-
lodash_omitby = __toESM(lodash_omitby);
|
|
29
|
-
let __orval_fetch = require("@orval/fetch");
|
|
30
|
-
__orval_fetch = __toESM(__orval_fetch);
|
|
31
|
-
let chalk = require("chalk");
|
|
32
|
-
chalk = __toESM(chalk);
|
|
1
|
+
import { GetterPropType, OutputClient, OutputHttpClient, TEMPLATE_TAG_REGEX, Verbs, camel, compareVersions, generateFormDataAndUrlEncodedFunction, generateMutator, generateMutatorConfig, generateMutatorRequestOptions, generateOptions, generateVerbImports, getIsBodyVerb, getRouteAsArray, isObject, isString, isSyntheticDefaultImportsAllow, jsDoc, mergeDeep, pascal, stringify, toObjectString, upath } from "@orval/core";
|
|
2
|
+
import omitBy from "lodash.omitby";
|
|
3
|
+
import { generateFetchHeader, generateRequestFunction } from "@orval/fetch";
|
|
4
|
+
import chalk from "chalk";
|
|
33
5
|
|
|
34
6
|
//#region src/utils.ts
|
|
35
7
|
const normalizeQueryOptions = (queryOptions = {}, outputWorkspace) => {
|
|
@@ -52,16 +24,16 @@ const normalizeQueryOptions = (queryOptions = {}, outputWorkspace) => {
|
|
|
52
24
|
};
|
|
53
25
|
};
|
|
54
26
|
const normalizeMutator = (workspace, mutator) => {
|
|
55
|
-
if (
|
|
56
|
-
if (!mutator.path) throw new Error(chalk.
|
|
27
|
+
if (isObject(mutator)) {
|
|
28
|
+
if (!mutator.path) throw new Error(chalk.red(`Mutator need a path`));
|
|
57
29
|
return {
|
|
58
30
|
...mutator,
|
|
59
|
-
path:
|
|
31
|
+
path: upath.resolve(workspace, mutator.path),
|
|
60
32
|
default: (mutator.default || !mutator.name) ?? false
|
|
61
33
|
};
|
|
62
34
|
}
|
|
63
|
-
if (
|
|
64
|
-
path:
|
|
35
|
+
if (isString(mutator)) return {
|
|
36
|
+
path: upath.resolve(workspace, mutator),
|
|
65
37
|
default: true
|
|
66
38
|
};
|
|
67
39
|
return mutator;
|
|
@@ -70,7 +42,7 @@ function vueWrapTypeWithMaybeRef(props) {
|
|
|
70
42
|
return props.map((prop) => {
|
|
71
43
|
const [paramName, paramType] = prop.implementation.split(":");
|
|
72
44
|
if (!paramType) return prop;
|
|
73
|
-
const name = prop.type ===
|
|
45
|
+
const name = prop.type === GetterPropType.NAMED_PATH_PARAMS ? prop.name : paramName;
|
|
74
46
|
const [type, defaultValue] = paramType.split("=");
|
|
75
47
|
return {
|
|
76
48
|
...prop,
|
|
@@ -80,14 +52,14 @@ function vueWrapTypeWithMaybeRef(props) {
|
|
|
80
52
|
}
|
|
81
53
|
const vueUnRefParams = (props) => {
|
|
82
54
|
return props.map((prop) => {
|
|
83
|
-
if (prop.type ===
|
|
55
|
+
if (prop.type === GetterPropType.NAMED_PATH_PARAMS) return `const ${prop.destructured} = unref(${prop.name});`;
|
|
84
56
|
return `${prop.name} = unref(${prop.name});`;
|
|
85
57
|
}).join("\n");
|
|
86
58
|
};
|
|
87
|
-
const wrapRouteParameters = (route, prepend, append) => route.replaceAll(
|
|
59
|
+
const wrapRouteParameters = (route, prepend, append) => route.replaceAll(TEMPLATE_TAG_REGEX, `\${${prepend}$1${append}}`);
|
|
88
60
|
const makeRouteSafe = (route) => wrapRouteParameters(route, "encodeURIComponent(String(", "))");
|
|
89
|
-
const isVue = (client) =>
|
|
90
|
-
const getHasSignal = ({ overrideQuerySignal = false, verb }) => overrideQuerySignal && (!
|
|
61
|
+
const isVue = (client) => OutputClient.VUE_QUERY === client;
|
|
62
|
+
const getHasSignal = ({ overrideQuerySignal = false, verb }) => overrideQuerySignal && (!getIsBodyVerb(verb) || verb === Verbs.POST);
|
|
91
63
|
|
|
92
64
|
//#endregion
|
|
93
65
|
//#region src/client.ts
|
|
@@ -106,7 +78,7 @@ const AXIOS_DEPENDENCIES = [{
|
|
|
106
78
|
dependency: "axios"
|
|
107
79
|
}];
|
|
108
80
|
const generateQueryRequestFunction = (verbOptions, options, isVue$1) => {
|
|
109
|
-
return options.context.output.httpClient ===
|
|
81
|
+
return options.context.output.httpClient === OutputHttpClient.AXIOS ? generateAxiosRequestFunction(verbOptions, options, isVue$1) : generateRequestFunction(verbOptions, options);
|
|
110
82
|
};
|
|
111
83
|
const generateAxiosRequestFunction = ({ headers, queryParams, operationName, response, mutator, body, props: _props, verb, formData, formUrlEncoded, override, paramsSerializer }, { route: _route, context }, isVue$1) => {
|
|
112
84
|
let props = _props;
|
|
@@ -121,7 +93,7 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
121
93
|
verb
|
|
122
94
|
});
|
|
123
95
|
const isExactOptionalPropertyTypes = !!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
|
|
124
|
-
const bodyForm =
|
|
96
|
+
const bodyForm = generateFormDataAndUrlEncodedFunction({
|
|
125
97
|
formData,
|
|
126
98
|
formUrlEncoded,
|
|
127
99
|
body,
|
|
@@ -129,7 +101,7 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
129
101
|
isFormUrlEncoded
|
|
130
102
|
});
|
|
131
103
|
if (mutator) {
|
|
132
|
-
const mutatorConfig =
|
|
104
|
+
const mutatorConfig = generateMutatorConfig({
|
|
133
105
|
route,
|
|
134
106
|
body,
|
|
135
107
|
headers,
|
|
@@ -143,10 +115,10 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
143
115
|
isVue: isVue$1
|
|
144
116
|
});
|
|
145
117
|
const bodyDefinition = body.definition.replace("[]", String.raw`\[\]`);
|
|
146
|
-
const propsImplementation = mutator?.bodyTypeName && body.definition ?
|
|
147
|
-
const requestOptions = isRequestOptions ?
|
|
118
|
+
const propsImplementation = mutator?.bodyTypeName && body.definition ? toObjectString(props, "implementation").replace(/* @__PURE__ */ new RegExp(`(\\w*):\\s?${bodyDefinition}`), `$1: ${mutator.bodyTypeName}<${body.definition}>`) : toObjectString(props, "implementation");
|
|
119
|
+
const requestOptions = isRequestOptions ? generateMutatorRequestOptions(override.requestOptions, mutator.hasSecondArg) : "";
|
|
148
120
|
if (mutator.isHook) {
|
|
149
|
-
const ret = `${override.query.shouldExportMutatorHooks ? "export " : ""}const use${
|
|
121
|
+
const ret = `${override.query.shouldExportMutatorHooks ? "export " : ""}const use${pascal(operationName)}Hook = () => {
|
|
150
122
|
const ${operationName} = ${mutator.name}<${response.definition.success || "unknown"}>();
|
|
151
123
|
|
|
152
124
|
return useCallback((\n ${propsImplementation}\n ${isRequestOptions && mutator.hasSecondArg ? `options${context.output.optionsParamRequired ? "" : "?"}: SecondParameter<ReturnType<typeof ${mutator.name}>>,` : ""}${hasSignal ? "signal?: AbortSignal\n" : ""}) => {${bodyForm}
|
|
@@ -156,7 +128,7 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
156
128
|
}, [${operationName}])
|
|
157
129
|
}
|
|
158
130
|
`;
|
|
159
|
-
const vueRet = `${override.query.shouldExportMutatorHooks ? "export " : ""}const use${
|
|
131
|
+
const vueRet = `${override.query.shouldExportMutatorHooks ? "export " : ""}const use${pascal(operationName)}Hook = () => {
|
|
160
132
|
const ${operationName} = ${mutator.name}<${response.definition.success || "unknown"}>();
|
|
161
133
|
|
|
162
134
|
return (\n ${propsImplementation}\n ${isRequestOptions && mutator.hasSecondArg ? `options${context.output.optionsParamRequired ? "" : "?"}: SecondParameter<ReturnType<typeof ${mutator.name}>>,` : ""}${hasSignal ? "signal?: AbortSignal\n" : ""}) => {${bodyForm}
|
|
@@ -177,8 +149,8 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
177
149
|
}
|
|
178
150
|
`;
|
|
179
151
|
}
|
|
180
|
-
const isSyntheticDefaultImportsAllowed =
|
|
181
|
-
const options =
|
|
152
|
+
const isSyntheticDefaultImportsAllowed = isSyntheticDefaultImportsAllow(context.output.tsconfig);
|
|
153
|
+
const options = generateOptions({
|
|
182
154
|
route,
|
|
183
155
|
body,
|
|
184
156
|
headers,
|
|
@@ -198,7 +170,7 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
198
170
|
isRequestOptions,
|
|
199
171
|
hasSignal
|
|
200
172
|
});
|
|
201
|
-
const queryProps =
|
|
173
|
+
const queryProps = toObjectString(props, "implementation");
|
|
202
174
|
return `${override.query.shouldExportHttpClient ? "export " : ""}const ${operationName} = (\n ${queryProps} ${optionsArgs} ): Promise<AxiosResponse<${response.definition.success || "unknown"}>> => {
|
|
203
175
|
${isVue$1 ? vueUnRefParams(props) : ""}
|
|
204
176
|
${bodyForm}
|
|
@@ -211,20 +183,20 @@ const generateRequestOptionsArguments = ({ isRequestOptions, hasSignal }) => {
|
|
|
211
183
|
return hasSignal ? "signal?: AbortSignal\n" : "";
|
|
212
184
|
};
|
|
213
185
|
const getQueryArgumentsRequestType = (httpClient, mutator) => {
|
|
214
|
-
if (!mutator) return httpClient ===
|
|
186
|
+
if (!mutator) return httpClient === OutputHttpClient.AXIOS ? `axios?: AxiosRequestConfig` : "fetch?: RequestInit";
|
|
215
187
|
if (mutator.hasSecondArg && !mutator.isHook) return `request?: SecondParameter<typeof ${mutator.name}>`;
|
|
216
188
|
if (mutator.hasSecondArg && mutator.isHook) return `request?: SecondParameter<ReturnType<typeof ${mutator.name}>>`;
|
|
217
189
|
return "";
|
|
218
190
|
};
|
|
219
191
|
const getQueryOptions = ({ isRequestOptions, mutator, isExactOptionalPropertyTypes, hasSignal, httpClient }) => {
|
|
220
192
|
if (!mutator && isRequestOptions) {
|
|
221
|
-
const options = httpClient ===
|
|
193
|
+
const options = httpClient === OutputHttpClient.AXIOS ? "axiosOptions" : "fetchOptions";
|
|
222
194
|
if (!hasSignal) return options;
|
|
223
195
|
return `{ ${isExactOptionalPropertyTypes ? "...(signal ? { signal } : {})" : "signal"}, ...${options} }`;
|
|
224
196
|
}
|
|
225
197
|
if (mutator?.hasSecondArg && isRequestOptions) {
|
|
226
198
|
if (!hasSignal) return "requestOptions";
|
|
227
|
-
return httpClient ===
|
|
199
|
+
return httpClient === OutputHttpClient.AXIOS ? "requestOptions, signal" : "{ signal, ...requestOptions }";
|
|
228
200
|
}
|
|
229
201
|
if (hasSignal) return "signal";
|
|
230
202
|
return "";
|
|
@@ -233,7 +205,7 @@ const getHookOptions = ({ isRequestOptions, httpClient, mutator }) => {
|
|
|
233
205
|
if (!isRequestOptions) return "";
|
|
234
206
|
let value = "const {query: queryOptions";
|
|
235
207
|
if (!mutator) {
|
|
236
|
-
const options = httpClient ===
|
|
208
|
+
const options = httpClient === OutputHttpClient.AXIOS ? ", axios: axiosOptions" : ", fetch: fetchOptions";
|
|
237
209
|
value += options;
|
|
238
210
|
}
|
|
239
211
|
if (mutator?.hasSecondArg) value += ", request: requestOptions";
|
|
@@ -246,28 +218,28 @@ const dedupeUnionTypes = (types) => {
|
|
|
246
218
|
};
|
|
247
219
|
const getQueryErrorType = (operationName, response, httpClient, mutator) => {
|
|
248
220
|
const errorsType = dedupeUnionTypes(response.definition.errors || "unknown");
|
|
249
|
-
if (mutator) return mutator.hasErrorType ? `${mutator.default ?
|
|
250
|
-
else return httpClient ===
|
|
221
|
+
if (mutator) return mutator.hasErrorType ? `${mutator.default ? pascal(operationName) : ""}ErrorType<${errorsType}>` : errorsType;
|
|
222
|
+
else return httpClient === OutputHttpClient.AXIOS ? `AxiosError<${errorsType}>` : errorsType;
|
|
251
223
|
};
|
|
252
224
|
const getHooksOptionImplementation = (isRequestOptions, httpClient, operationName, mutator) => {
|
|
253
|
-
const options = httpClient ===
|
|
225
|
+
const options = httpClient === OutputHttpClient.AXIOS ? ", axios: axiosOptions" : ", fetch: fetchOptions";
|
|
254
226
|
return isRequestOptions ? `const mutationKey = ['${operationName}'];
|
|
255
227
|
const {mutation: mutationOptions${mutator ? mutator?.hasSecondArg ? ", request: requestOptions" : "" : options}} = options ?
|
|
256
228
|
options.mutation && 'mutationKey' in options.mutation && options.mutation.mutationKey ?
|
|
257
229
|
options
|
|
258
230
|
: {...options, mutation: {...options.mutation, mutationKey}}
|
|
259
|
-
: {mutation: { mutationKey, }${mutator?.hasSecondArg ? ", request: undefined" : ""}${mutator ? "" : httpClient ===
|
|
231
|
+
: {mutation: { mutationKey, }${mutator?.hasSecondArg ? ", request: undefined" : ""}${mutator ? "" : httpClient === OutputHttpClient.AXIOS ? ", axios: undefined" : ", fetch: undefined"}};` : "";
|
|
260
232
|
};
|
|
261
233
|
const getMutationRequestArgs = (isRequestOptions, httpClient, mutator) => {
|
|
262
|
-
const options = httpClient ===
|
|
234
|
+
const options = httpClient === OutputHttpClient.AXIOS ? "axiosOptions" : "fetchOptions";
|
|
263
235
|
return isRequestOptions ? mutator ? mutator?.hasSecondArg ? "requestOptions" : "" : options : "";
|
|
264
236
|
};
|
|
265
237
|
const getHttpFunctionQueryProps = (isVue$1, httpClient, queryProperties) => {
|
|
266
|
-
if (isVue$1 && httpClient ===
|
|
238
|
+
if (isVue$1 && httpClient === OutputHttpClient.FETCH && queryProperties) return queryProperties.split(",").map((prop) => `unref(${prop})`).join(",");
|
|
267
239
|
return queryProperties;
|
|
268
240
|
};
|
|
269
241
|
const getQueryHeader = (params) => {
|
|
270
|
-
return params.output.httpClient ===
|
|
242
|
+
return params.output.httpClient === OutputHttpClient.FETCH ? generateFetchHeader(params) : "";
|
|
271
243
|
};
|
|
272
244
|
|
|
273
245
|
//#endregion
|
|
@@ -288,33 +260,6 @@ const PARAMS_SERIALIZER_DEPENDENCIES = [{
|
|
|
288
260
|
}],
|
|
289
261
|
dependency: "qs"
|
|
290
262
|
}];
|
|
291
|
-
const SVELTE_QUERY_DEPENDENCIES_V3 = [{
|
|
292
|
-
exports: [
|
|
293
|
-
{
|
|
294
|
-
name: "useQuery",
|
|
295
|
-
values: true
|
|
296
|
-
},
|
|
297
|
-
{
|
|
298
|
-
name: "useInfiniteQuery",
|
|
299
|
-
values: true
|
|
300
|
-
},
|
|
301
|
-
{
|
|
302
|
-
name: "useMutation",
|
|
303
|
-
values: true
|
|
304
|
-
},
|
|
305
|
-
{ name: "UseQueryOptions" },
|
|
306
|
-
{ name: "UseInfiniteQueryOptions" },
|
|
307
|
-
{ name: "UseMutationOptions" },
|
|
308
|
-
{ name: "QueryFunction" },
|
|
309
|
-
{ name: "MutationFunction" },
|
|
310
|
-
{ name: "UseQueryStoreResult" },
|
|
311
|
-
{ name: "UseInfiniteQueryStoreResult" },
|
|
312
|
-
{ name: "QueryKey" },
|
|
313
|
-
{ name: "CreateMutationResult" },
|
|
314
|
-
{ name: "InvalidateOptions" }
|
|
315
|
-
],
|
|
316
|
-
dependency: "@sveltestack/svelte-query"
|
|
317
|
-
}];
|
|
318
263
|
const SVELTE_QUERY_DEPENDENCIES = [{
|
|
319
264
|
exports: [
|
|
320
265
|
{
|
|
@@ -345,54 +290,16 @@ const SVELTE_QUERY_DEPENDENCIES = [{
|
|
|
345
290
|
],
|
|
346
291
|
dependency: "@tanstack/svelte-query"
|
|
347
292
|
}];
|
|
348
|
-
const isSvelteQueryV3 = (packageJson) => {
|
|
349
|
-
const hasSvelteQuery = packageJson?.dependencies?.["@sveltestack/svelte-query"] ?? packageJson?.devDependencies?.["@sveltestack/svelte-query"] ?? packageJson?.peerDependencies?.["@sveltestack/svelte-query"];
|
|
350
|
-
const hasSvelteQueryV4 = packageJson?.dependencies?.["@tanstack/svelte-query"] ?? packageJson?.devDependencies?.["@tanstack/svelte-query"] ?? packageJson?.peerDependencies?.["@tanstack/svelte-query"];
|
|
351
|
-
return !!hasSvelteQuery && !hasSvelteQueryV4;
|
|
352
|
-
};
|
|
353
293
|
const isSvelteQueryV6 = (packageJson) => {
|
|
354
294
|
return isQueryV6(packageJson, "svelte-query");
|
|
355
295
|
};
|
|
356
296
|
const getSvelteQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient) => {
|
|
357
|
-
const hasSvelteQueryV3 = isSvelteQueryV3(packageJson);
|
|
358
297
|
return [
|
|
359
|
-
...!hasGlobalMutator && httpClient ===
|
|
298
|
+
...!hasGlobalMutator && httpClient === OutputHttpClient.AXIOS ? AXIOS_DEPENDENCIES : [],
|
|
360
299
|
...hasParamsSerializerOptions ? PARAMS_SERIALIZER_DEPENDENCIES : [],
|
|
361
|
-
...
|
|
300
|
+
...SVELTE_QUERY_DEPENDENCIES
|
|
362
301
|
];
|
|
363
302
|
};
|
|
364
|
-
const REACT_QUERY_DEPENDENCIES_V3 = [{
|
|
365
|
-
exports: [
|
|
366
|
-
{
|
|
367
|
-
name: "useQuery",
|
|
368
|
-
values: true
|
|
369
|
-
},
|
|
370
|
-
{
|
|
371
|
-
name: "useInfiniteQuery",
|
|
372
|
-
values: true
|
|
373
|
-
},
|
|
374
|
-
{
|
|
375
|
-
name: "useMutation",
|
|
376
|
-
values: true
|
|
377
|
-
},
|
|
378
|
-
{
|
|
379
|
-
name: "useQueryClient",
|
|
380
|
-
values: true
|
|
381
|
-
},
|
|
382
|
-
{ name: "UseQueryOptions" },
|
|
383
|
-
{ name: "UseInfiniteQueryOptions" },
|
|
384
|
-
{ name: "UseMutationOptions" },
|
|
385
|
-
{ name: "QueryFunction" },
|
|
386
|
-
{ name: "MutationFunction" },
|
|
387
|
-
{ name: "UseQueryResult" },
|
|
388
|
-
{ name: "UseInfiniteQueryResult" },
|
|
389
|
-
{ name: "QueryKey" },
|
|
390
|
-
{ name: "QueryClient" },
|
|
391
|
-
{ name: "UseMutationResult" },
|
|
392
|
-
{ name: "InvalidateOptions" }
|
|
393
|
-
],
|
|
394
|
-
dependency: "react-query"
|
|
395
|
-
}];
|
|
396
303
|
const REACT_QUERY_DEPENDENCIES = [{
|
|
397
304
|
exports: [
|
|
398
305
|
{
|
|
@@ -443,65 +350,14 @@ const REACT_QUERY_DEPENDENCIES = [{
|
|
|
443
350
|
],
|
|
444
351
|
dependency: "@tanstack/react-query"
|
|
445
352
|
}];
|
|
446
|
-
const getReactQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient, hasTagsMutator
|
|
447
|
-
const hasReactQuery = packageJson?.dependencies?.["react-query"] ?? packageJson?.devDependencies?.["react-query"] ?? packageJson?.peerDependencies?.["react-query"];
|
|
448
|
-
const hasReactQueryV4 = packageJson?.dependencies?.["@tanstack/react-query"] ?? packageJson?.devDependencies?.["@tanstack/react-query"] ?? packageJson?.peerDependencies?.["@tanstack/react-query"];
|
|
449
|
-
const useReactQueryV3 = override?.query.version === void 0 ? hasReactQuery && !hasReactQueryV4 : override?.query.version <= 3;
|
|
353
|
+
const getReactQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient, hasTagsMutator) => {
|
|
450
354
|
return [
|
|
451
355
|
...hasGlobalMutator || hasTagsMutator ? REACT_DEPENDENCIES : [],
|
|
452
|
-
...!hasGlobalMutator && httpClient ===
|
|
356
|
+
...!hasGlobalMutator && httpClient === OutputHttpClient.AXIOS ? AXIOS_DEPENDENCIES : [],
|
|
453
357
|
...hasParamsSerializerOptions ? PARAMS_SERIALIZER_DEPENDENCIES : [],
|
|
454
|
-
...
|
|
358
|
+
...REACT_QUERY_DEPENDENCIES
|
|
455
359
|
];
|
|
456
360
|
};
|
|
457
|
-
const VUE_QUERY_DEPENDENCIES_V3 = [
|
|
458
|
-
{
|
|
459
|
-
exports: [
|
|
460
|
-
{
|
|
461
|
-
name: "useQuery",
|
|
462
|
-
values: true
|
|
463
|
-
},
|
|
464
|
-
{
|
|
465
|
-
name: "useInfiniteQuery",
|
|
466
|
-
values: true
|
|
467
|
-
},
|
|
468
|
-
{
|
|
469
|
-
name: "useMutation",
|
|
470
|
-
values: true
|
|
471
|
-
}
|
|
472
|
-
],
|
|
473
|
-
dependency: "vue-query"
|
|
474
|
-
},
|
|
475
|
-
{
|
|
476
|
-
exports: [
|
|
477
|
-
{ name: "UseQueryOptions" },
|
|
478
|
-
{ name: "UseInfiniteQueryOptions" },
|
|
479
|
-
{ name: "UseMutationOptions" },
|
|
480
|
-
{ name: "QueryFunction" },
|
|
481
|
-
{ name: "MutationFunction" },
|
|
482
|
-
{ name: "UseQueryResult" },
|
|
483
|
-
{ name: "UseInfiniteQueryResult" },
|
|
484
|
-
{ name: "QueryKey" },
|
|
485
|
-
{ name: "UseMutationReturnType" },
|
|
486
|
-
{ name: "InvalidateOptions" }
|
|
487
|
-
],
|
|
488
|
-
dependency: "vue-query/types"
|
|
489
|
-
},
|
|
490
|
-
{
|
|
491
|
-
exports: [{
|
|
492
|
-
name: "unref",
|
|
493
|
-
values: true
|
|
494
|
-
}, {
|
|
495
|
-
name: "computed",
|
|
496
|
-
values: true
|
|
497
|
-
}],
|
|
498
|
-
dependency: "vue"
|
|
499
|
-
},
|
|
500
|
-
{
|
|
501
|
-
exports: [{ name: "UseQueryReturnType" }],
|
|
502
|
-
dependency: "vue-query/lib/vue/useBaseQuery"
|
|
503
|
-
}
|
|
504
|
-
];
|
|
505
361
|
const VUE_QUERY_DEPENDENCIES = [{
|
|
506
362
|
exports: [
|
|
507
363
|
{
|
|
@@ -545,42 +401,36 @@ const VUE_QUERY_DEPENDENCIES = [{
|
|
|
545
401
|
],
|
|
546
402
|
dependency: "vue"
|
|
547
403
|
}];
|
|
548
|
-
const isVueQueryV3 = (packageJson) => {
|
|
549
|
-
const hasVueQuery = packageJson?.dependencies?.["vue-query"] ?? packageJson?.devDependencies?.["vue-query"] ?? packageJson?.peerDependencies?.["vue-query"];
|
|
550
|
-
const hasVueQueryV4 = packageJson?.dependencies?.["@tanstack/vue-query"] ?? packageJson?.devDependencies?.["@tanstack/vue-query"] ?? packageJson?.peerDependencies?.["@tanstack/vue-query"];
|
|
551
|
-
return !!hasVueQuery && !hasVueQueryV4;
|
|
552
|
-
};
|
|
553
404
|
const getVueQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient) => {
|
|
554
|
-
const hasVueQueryV3 = isVueQueryV3(packageJson);
|
|
555
405
|
return [
|
|
556
|
-
...!hasGlobalMutator && httpClient ===
|
|
406
|
+
...!hasGlobalMutator && httpClient === OutputHttpClient.AXIOS ? AXIOS_DEPENDENCIES : [],
|
|
557
407
|
...hasParamsSerializerOptions ? PARAMS_SERIALIZER_DEPENDENCIES : [],
|
|
558
|
-
...
|
|
408
|
+
...VUE_QUERY_DEPENDENCIES
|
|
559
409
|
];
|
|
560
410
|
};
|
|
561
411
|
const isQueryV5 = (packageJson, queryClient) => {
|
|
562
412
|
const version = getPackageByQueryClient(packageJson, queryClient);
|
|
563
413
|
if (!version) return false;
|
|
564
414
|
const withoutRc = version.split("-")[0];
|
|
565
|
-
return
|
|
415
|
+
return compareVersions(withoutRc, "5.0.0");
|
|
566
416
|
};
|
|
567
417
|
const isQueryV6 = (packageJson, queryClient) => {
|
|
568
418
|
const version = getPackageByQueryClient(packageJson, queryClient);
|
|
569
419
|
if (!version) return false;
|
|
570
420
|
const withoutRc = version.split("-")[0];
|
|
571
|
-
return
|
|
421
|
+
return compareVersions(withoutRc, "6.0.0");
|
|
572
422
|
};
|
|
573
423
|
const isQueryV5WithDataTagError = (packageJson, queryClient) => {
|
|
574
424
|
const version = getPackageByQueryClient(packageJson, queryClient);
|
|
575
425
|
if (!version) return false;
|
|
576
426
|
const withoutRc = version.split("-")[0];
|
|
577
|
-
return
|
|
427
|
+
return compareVersions(withoutRc, "5.62.0");
|
|
578
428
|
};
|
|
579
429
|
const isQueryV5WithInfiniteQueryOptionsError = (packageJson, queryClient) => {
|
|
580
430
|
const version = getPackageByQueryClient(packageJson, queryClient);
|
|
581
431
|
if (!version) return false;
|
|
582
432
|
const withoutRc = version.split("-")[0];
|
|
583
|
-
return
|
|
433
|
+
return compareVersions(withoutRc, "5.80.0");
|
|
584
434
|
};
|
|
585
435
|
const getPackageByQueryClient = (packageJson, queryClient) => {
|
|
586
436
|
switch (queryClient) {
|
|
@@ -598,12 +448,12 @@ const QueryType = {
|
|
|
598
448
|
const INFINITE_QUERY_PROPERTIES = new Set(["getNextPageParam", "getPreviousPageParam"]);
|
|
599
449
|
const generateQueryOptions = ({ params, options, type, outputClient }) => {
|
|
600
450
|
if (options === false) return "";
|
|
601
|
-
const queryConfig =
|
|
451
|
+
const queryConfig = isObject(options) ? ` ${stringify(omitBy(options, (_, key) => (type !== QueryType.INFINITE || type !== QueryType.SUSPENSE_INFINITE) && INFINITE_QUERY_PROPERTIES.has(key)))?.slice(1, -1)}` : "";
|
|
602
452
|
if (params.length === 0 || isSuspenseQuery(type)) {
|
|
603
453
|
if (options) return `${queryConfig} ...queryOptions`;
|
|
604
454
|
return "...queryOptions";
|
|
605
455
|
}
|
|
606
|
-
return `${!
|
|
456
|
+
return `${!isObject(options) || !options.hasOwnProperty("enabled") ? isVue(outputClient) ? `enabled: computed(() => !!(${params.map(({ name }) => `unref(${name})`).join(" && ")})),` : `enabled: !!(${params.map(({ name }) => name).join(" && ")}),` : ""}${queryConfig} ...queryOptions`;
|
|
607
457
|
};
|
|
608
458
|
const isSuspenseQuery = (type) => {
|
|
609
459
|
return [QueryType.SUSPENSE_INFINITE, QueryType.SUSPENSE_QUERY].includes(type);
|
|
@@ -613,18 +463,18 @@ const getQueryOptionsDefinition = ({ operationName, mutator, definitions, type,
|
|
|
613
463
|
const prefix = hasSvelteQueryV4 ? "Create" : "Use";
|
|
614
464
|
const partialOptions = !isReturnType && hasQueryV5;
|
|
615
465
|
if (type) {
|
|
616
|
-
const funcReturnType = `Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${
|
|
466
|
+
const funcReturnType = `Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>`;
|
|
617
467
|
const optionTypeInitialDataPostfix = initialData && !isSuspenseQuery(type) ? ` & Pick<
|
|
618
|
-
${
|
|
468
|
+
${pascal(initialData)}InitialDataOptions<
|
|
619
469
|
${funcReturnType},
|
|
620
470
|
TError,
|
|
621
471
|
${funcReturnType}${hasQueryV5 && (type === QueryType.INFINITE || type === QueryType.SUSPENSE_INFINITE) && queryParam && queryParams ? `, QueryKey` : ""}
|
|
622
472
|
> , 'initialData'
|
|
623
473
|
>` : "";
|
|
624
|
-
const optionType = `${prefix}${
|
|
474
|
+
const optionType = `${prefix}${pascal(type)}Options<${funcReturnType}, TError, TData${hasQueryV5 && (type === QueryType.INFINITE || type === QueryType.SUSPENSE_INFINITE) && queryParam && queryParams ? hasQueryV5WithInfiniteQueryOptionsError ? `, QueryKey, ${queryParams?.schema.name}['${queryParam}']` : `, ${funcReturnType}, QueryKey, ${queryParams?.schema.name}['${queryParam}']` : ""}>`;
|
|
625
475
|
return `${partialOptions ? "Partial<" : ""}${optionType}${partialOptions ? ">" : ""}${optionTypeInitialDataPostfix}`;
|
|
626
476
|
}
|
|
627
|
-
return `${prefix}MutationOptions<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${
|
|
477
|
+
return `${prefix}MutationOptions<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>, TError,${definitions ? `{${definitions}}` : "void"}, TContext>`;
|
|
628
478
|
};
|
|
629
479
|
const generateQueryArguments = ({ operationName, definitions, mutator, isRequestOptions, type, hasSvelteQueryV4, hasQueryV5, hasQueryV5WithInfiniteQueryOptionsError, queryParams, queryParam, initialData, httpClient }) => {
|
|
630
480
|
const definition = getQueryOptionsDefinition({
|
|
@@ -647,31 +497,31 @@ const generateQueryArguments = ({ operationName, definitions, mutator, isRequest
|
|
|
647
497
|
};
|
|
648
498
|
const generateQueryReturnType = ({ outputClient, type, isMutatorHook, operationName, hasVueQueryV4, hasSvelteQueryV4, hasQueryV5, hasQueryV5WithDataTagError, isInitialDataDefined }) => {
|
|
649
499
|
switch (outputClient) {
|
|
650
|
-
case
|
|
651
|
-
if (!hasSvelteQueryV4) return `Use${
|
|
652
|
-
return `Create${
|
|
653
|
-
case
|
|
654
|
-
if (!hasVueQueryV4) return ` UseQueryReturnType<TData, TError, Use${
|
|
500
|
+
case OutputClient.SVELTE_QUERY:
|
|
501
|
+
if (!hasSvelteQueryV4) return `Use${pascal(type)}StoreResult<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>, TError, TData, QueryKey> & { queryKey: QueryKey} }`;
|
|
502
|
+
return `Create${pascal(type)}Result<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
503
|
+
case OutputClient.VUE_QUERY:
|
|
504
|
+
if (!hasVueQueryV4) return ` UseQueryReturnType<TData, TError, Use${pascal(type)}Result<TData, TError>> & { queryKey: QueryKey} }`;
|
|
655
505
|
if (type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE) return `UseQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
656
506
|
return `UseInfiniteQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
657
|
-
case
|
|
658
|
-
default: return ` ${isInitialDataDefined && !isSuspenseQuery(type) ? "Defined" : ""}Use${
|
|
507
|
+
case OutputClient.REACT_QUERY:
|
|
508
|
+
default: return ` ${isInitialDataDefined && !isSuspenseQuery(type) ? "Defined" : ""}Use${pascal(type)}Result<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
659
509
|
}
|
|
660
510
|
};
|
|
661
511
|
const generateMutatorReturnType = ({ outputClient, dataType, variableType }) => {
|
|
662
|
-
if (outputClient ===
|
|
512
|
+
if (outputClient === OutputClient.REACT_QUERY) return `: UseMutationResult<
|
|
663
513
|
Awaited<ReturnType<${dataType}>>,
|
|
664
514
|
TError,
|
|
665
515
|
${variableType},
|
|
666
516
|
TContext
|
|
667
517
|
>`;
|
|
668
|
-
if (outputClient ===
|
|
518
|
+
if (outputClient === OutputClient.SVELTE_QUERY) return `: CreateMutationResult<
|
|
669
519
|
Awaited<ReturnType<${dataType}>>,
|
|
670
520
|
TError,
|
|
671
521
|
${variableType},
|
|
672
522
|
TContext
|
|
673
523
|
>`;
|
|
674
|
-
if (outputClient ===
|
|
524
|
+
if (outputClient === OutputClient.VUE_QUERY) return `: UseMutationReturnType<
|
|
675
525
|
Awaited<ReturnType<${dataType}>>,
|
|
676
526
|
TError,
|
|
677
527
|
${variableType},
|
|
@@ -690,10 +540,8 @@ const getQueryFnArguments = ({ hasQueryParam, hasSignal }) => {
|
|
|
690
540
|
const generatePrefetch = ({ usePrefetch, type, useQuery, useInfinite, operationName, mutator, doc, queryProps, dataType, errorType, queryArguments, queryOptionsVarName, queryOptionsFnName, queryProperties, isRequestOptions, hasSvelteQueryV6 }) => {
|
|
691
541
|
if (!(usePrefetch && (type === QueryType.QUERY || type === QueryType.INFINITE || type === QueryType.SUSPENSE_QUERY && !useQuery || type === QueryType.SUSPENSE_INFINITE && !useInfinite))) return "";
|
|
692
542
|
const prefetchType = type === QueryType.QUERY || type === QueryType.SUSPENSE_QUERY ? "query" : "infinite-query";
|
|
693
|
-
const prefetchFnName =
|
|
694
|
-
if (mutator?.isHook) {
|
|
695
|
-
const prefetchVarName = (0, __orval_core.camel)(`use-prefetch-${operationName}-${prefetchType}`);
|
|
696
|
-
return `${doc}export const ${prefetchVarName} = <TData = Awaited<ReturnType<${dataType}>>, TError = ${errorType}>(${queryProps} ${queryArguments}) => {
|
|
543
|
+
const prefetchFnName = camel(`prefetch-${prefetchType}`);
|
|
544
|
+
if (mutator?.isHook) return `${doc}export const ${camel(`use-prefetch-${operationName}-${prefetchType}`)} = <TData = Awaited<ReturnType<${dataType}>>, TError = ${errorType}>(${queryProps} ${queryArguments}) => {
|
|
697
545
|
const queryClient = useQueryClient();
|
|
698
546
|
const ${queryOptionsVarName} = ${queryOptionsFnName}(${queryProperties}${queryProperties ? "," : ""}${isRequestOptions ? "options" : "queryOptions"})
|
|
699
547
|
return useCallback(async (): Promise<QueryClient> => {
|
|
@@ -701,9 +549,7 @@ const generatePrefetch = ({ usePrefetch, type, useQuery, useInfinite, operationN
|
|
|
701
549
|
return queryClient;
|
|
702
550
|
},[queryClient, ${queryOptionsVarName}]);
|
|
703
551
|
};\n`;
|
|
704
|
-
}
|
|
705
|
-
const prefetchVarName = (0, __orval_core.camel)(`prefetch-${operationName}-${prefetchType}`);
|
|
706
|
-
return `${doc}export const ${prefetchVarName} = async <TData = Awaited<ReturnType<${dataType}>>, TError = ${errorType}>(\n queryClient: QueryClient, ${queryProps} ${queryArguments}\n ): Promise<QueryClient> => {
|
|
552
|
+
else return `${doc}export const ${camel(`prefetch-${operationName}-${prefetchType}`)} = async <TData = Awaited<ReturnType<${dataType}>>, TError = ${errorType}>(\n queryClient: QueryClient, ${queryProps} ${queryArguments}\n ): Promise<QueryClient> => {
|
|
707
553
|
|
|
708
554
|
const ${queryOptionsVarName} = ${queryOptionsFnName}(${queryProperties}${queryProperties ? "," : ""}${isRequestOptions ? "options" : "queryOptions"})
|
|
709
555
|
|
|
@@ -711,11 +557,10 @@ const generatePrefetch = ({ usePrefetch, type, useQuery, useInfinite, operationN
|
|
|
711
557
|
|
|
712
558
|
return queryClient;
|
|
713
559
|
}\n`;
|
|
714
|
-
}
|
|
715
560
|
};
|
|
716
561
|
const generateQueryImplementation = ({ queryOption: { name, queryParam, options, type, queryKeyFnName }, operationName, queryProperties, queryKeyProperties, queryParams, params, props, mutator, queryOptionsMutator, queryKeyMutator, isRequestOptions, response, outputClient, httpClient, isExactOptionalPropertyTypes, hasSignal, route, hasVueQueryV4, hasSvelteQueryV4, hasSvelteQueryV6, hasQueryV5, hasQueryV5WithDataTagError, hasQueryV5WithInfiniteQueryOptionsError, doc, usePrefetch, useQuery, useInfinite, useInvalidate }) => {
|
|
717
|
-
const queryPropDefinitions =
|
|
718
|
-
const definedInitialDataQueryPropsDefinitions =
|
|
562
|
+
const queryPropDefinitions = toObjectString(props, "definition");
|
|
563
|
+
const definedInitialDataQueryPropsDefinitions = toObjectString(props.map((prop) => {
|
|
719
564
|
const regex = /* @__PURE__ */ new RegExp(`^${prop.name}\\s*\\?:`);
|
|
720
565
|
if (!regex.test(prop.definition)) return prop;
|
|
721
566
|
const definitionWithUndefined = prop.definition.replace(regex, `${prop.name}: undefined | `);
|
|
@@ -724,10 +569,10 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
724
569
|
definition: definitionWithUndefined
|
|
725
570
|
};
|
|
726
571
|
}), "definition");
|
|
727
|
-
const queryProps =
|
|
572
|
+
const queryProps = toObjectString(props, "implementation");
|
|
728
573
|
const hasInfiniteQueryParam = queryParam && queryParams?.schema.name;
|
|
729
574
|
const httpFunctionProps = queryParam ? props.map((param) => {
|
|
730
|
-
if (param.type ===
|
|
575
|
+
if (param.type === GetterPropType.NAMED_PATH_PARAMS && !isVue(outputClient)) return param.destructured;
|
|
731
576
|
return param.name === "params" ? `{...${isVue(outputClient) ? `unref(params)` : "params"}, '${queryParam}': pageParam || ${isVue(outputClient) ? `unref(params)?.['${queryParam}']` : `params?.['${queryParam}']`}}` : param.name;
|
|
732
577
|
}).join(",") : getHttpFunctionQueryProps(isVue(outputClient), httpClient, queryProperties);
|
|
733
578
|
const definedInitialDataReturnType = generateQueryReturnType({
|
|
@@ -752,7 +597,7 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
752
597
|
hasQueryV5WithDataTagError
|
|
753
598
|
});
|
|
754
599
|
const errorType = getQueryErrorType(operationName, response, httpClient, mutator);
|
|
755
|
-
const dataType = mutator?.isHook ? `ReturnType<typeof use${
|
|
600
|
+
const dataType = mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`;
|
|
756
601
|
const definedInitialDataQueryArguments = generateQueryArguments({
|
|
757
602
|
operationName,
|
|
758
603
|
mutator,
|
|
@@ -828,7 +673,7 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
828
673
|
type,
|
|
829
674
|
outputClient
|
|
830
675
|
});
|
|
831
|
-
const queryOptionsFnName =
|
|
676
|
+
const queryOptionsFnName = camel(queryKeyMutator || queryOptionsMutator || mutator?.isHook ? `use-${name}-queryOptions` : `get-${name}-queryOptions`);
|
|
832
677
|
const queryOptionsVarName = isRequestOptions ? "queryOptions" : "options";
|
|
833
678
|
const queryResultVarName = props.some((prop) => prop.name === "query") ? "_query" : "query";
|
|
834
679
|
const infiniteParam = queryParams && queryParam ? `, ${queryParams?.schema.name}['${queryParam}']` : "";
|
|
@@ -839,11 +684,11 @@ ${hookOptions}
|
|
|
839
684
|
|
|
840
685
|
const queryKey = ${queryKeyMutator ? `${queryKeyMutator.name}({ ${queryProperties} }${queryKeyMutator.hasSecondArg ? `, { url: \`${route}\`, queryOptions }` : ""});` : `${hasVueQueryV4 ? "" : "queryOptions?.queryKey ?? "}${queryKeyFnName}(${queryKeyProperties});`}
|
|
841
686
|
|
|
842
|
-
${mutator?.isHook ? `const ${operationName} = use${
|
|
687
|
+
${mutator?.isHook ? `const ${operationName} = use${pascal(operationName)}Hook();` : ""}
|
|
843
688
|
|
|
844
|
-
const queryFn: QueryFunction<Awaited<ReturnType<${mutator?.isHook ? `ReturnType<typeof use${
|
|
689
|
+
const queryFn: QueryFunction<Awaited<ReturnType<${mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>${hasQueryV5 && hasInfiniteQueryParam ? `, QueryKey, ${queryParams?.schema.name}['${queryParam}']` : ""}> = (${queryFnArguments}) => ${operationName}(${httpFunctionProps}${httpFunctionProps ? ", " : ""}${queryOptions});
|
|
845
690
|
|
|
846
|
-
${isVue(outputClient) ? vueUnRefParams(props.filter((prop) => prop.type ===
|
|
691
|
+
${isVue(outputClient) ? vueUnRefParams(props.filter((prop) => prop.type === GetterPropType.NAMED_PATH_PARAMS)) : ""}
|
|
847
692
|
|
|
848
693
|
${queryOptionsMutator ? `const customOptions = ${queryOptionsMutator.name}({...queryOptions, queryKey, queryFn}${queryOptionsMutator.hasSecondArg ? `, { ${queryProperties} }` : ""}${queryOptionsMutator.hasThirdArg ? `, { url: \`${route}\` }` : ""});` : ""}
|
|
849
694
|
|
|
@@ -851,7 +696,7 @@ ${hookOptions}
|
|
|
851
696
|
}`;
|
|
852
697
|
const operationPrefix = hasSvelteQueryV4 ? "create" : "use";
|
|
853
698
|
const optionalQueryClientArgument = hasQueryV5 ? ", queryClient?: QueryClient" : "";
|
|
854
|
-
const queryHookName =
|
|
699
|
+
const queryHookName = camel(`${operationPrefix}-${name}`);
|
|
855
700
|
const overrideTypes = `
|
|
856
701
|
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${definedInitialDataQueryPropsDefinitions} ${definedInitialDataQueryArguments} ${optionalQueryClientArgument}\n ): ${definedInitialDataReturnType}
|
|
857
702
|
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${queryPropDefinitions} ${undefinedInitialDataQueryArguments} ${optionalQueryClientArgument}\n ): ${returnType}
|
|
@@ -875,20 +720,20 @@ export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${q
|
|
|
875
720
|
doc
|
|
876
721
|
});
|
|
877
722
|
const shouldGenerateInvalidate = useInvalidate && (type === QueryType.QUERY || type === QueryType.INFINITE || type === QueryType.SUSPENSE_QUERY && !useQuery || type === QueryType.SUSPENSE_INFINITE && !useInfinite);
|
|
878
|
-
const invalidateFnName =
|
|
723
|
+
const invalidateFnName = camel(`invalidate-${operationName}`);
|
|
879
724
|
return `
|
|
880
725
|
${queryOptionsFn}
|
|
881
726
|
|
|
882
|
-
export type ${
|
|
883
|
-
export type ${
|
|
727
|
+
export type ${pascal(name)}QueryResult = NonNullable<Awaited<ReturnType<${dataType}>>>
|
|
728
|
+
export type ${pascal(name)}QueryError = ${errorType}
|
|
884
729
|
|
|
885
|
-
${hasQueryV5 &&
|
|
730
|
+
${hasQueryV5 && OutputClient.REACT_QUERY === outputClient ? overrideTypes : ""}
|
|
886
731
|
${doc}
|
|
887
732
|
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${queryProps} ${queryArguments} ${optionalQueryClientArgument} \n ): ${returnType} {
|
|
888
733
|
|
|
889
734
|
const ${queryOptionsVarName} = ${queryOptionsFnName}(${queryProperties}${queryProperties ? "," : ""}${isRequestOptions ? "options" : "queryOptions"})
|
|
890
735
|
|
|
891
|
-
const ${queryResultVarName} = ${
|
|
736
|
+
const ${queryResultVarName} = ${camel(`${operationPrefix}-${type}`)}(${hasSvelteQueryV6 ? `() => ({ ...${queryOptionsVarName}${optionalQueryClientArgument ? ", queryClient" : ""} })` : `${queryOptionsVarName}${optionalQueryClientArgument ? ", queryClient" : ""}`}) as ${returnType};
|
|
892
737
|
|
|
893
738
|
${queryResultVarName}.queryKey = ${isVue(outputClient) ? `unref(${queryOptionsVarName})` : queryOptionsVarName}.queryKey ${isVue(outputClient) ? `as ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"}` : ""};
|
|
894
739
|
|
|
@@ -911,34 +756,34 @@ const generateQueryHook = async ({ queryParams, operationName, body, props: _pro
|
|
|
911
756
|
const operationQueryOptions = operations[operationId]?.query;
|
|
912
757
|
const isExactOptionalPropertyTypes = !!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
|
|
913
758
|
const queryVersion = override.query.version ?? query?.version;
|
|
914
|
-
const hasVueQueryV4 =
|
|
915
|
-
const hasSvelteQueryV4 =
|
|
916
|
-
const hasSvelteQueryV6 =
|
|
759
|
+
const hasVueQueryV4 = OutputClient.VUE_QUERY === outputClient;
|
|
760
|
+
const hasSvelteQueryV4 = OutputClient.SVELTE_QUERY === outputClient;
|
|
761
|
+
const hasSvelteQueryV6 = OutputClient.SVELTE_QUERY === outputClient && isSvelteQueryV6(context.output.packageJson);
|
|
917
762
|
const hasQueryV5 = queryVersion === 5 || isQueryV5(context.output.packageJson, outputClient);
|
|
918
763
|
const hasQueryV5WithDataTagError = queryVersion === 5 || isQueryV5WithDataTagError(context.output.packageJson, outputClient);
|
|
919
764
|
const hasQueryV5WithInfiniteQueryOptionsError = queryVersion === 5 || isQueryV5WithInfiniteQueryOptionsError(context.output.packageJson, outputClient);
|
|
920
765
|
const httpClient = context.output.httpClient;
|
|
921
|
-
const doc =
|
|
766
|
+
const doc = jsDoc({
|
|
922
767
|
summary,
|
|
923
768
|
deprecated
|
|
924
769
|
});
|
|
925
770
|
let implementation = "";
|
|
926
771
|
let mutators;
|
|
927
772
|
const hasOperationQueryOption = !!(operationQueryOptions && (operationQueryOptions.useQuery || operationQueryOptions.useSuspenseQuery || operationQueryOptions.useInfinite || operationQueryOptions.useSuspenseInfiniteQuery));
|
|
928
|
-
let isQuery =
|
|
929
|
-
let isMutation = override.query.useMutation && verb !==
|
|
773
|
+
let isQuery = Verbs.GET === verb && (override.query.useQuery || override.query.useSuspenseQuery || override.query.useInfinite || override.query.useSuspenseInfiniteQuery) || hasOperationQueryOption;
|
|
774
|
+
let isMutation = override.query.useMutation && verb !== Verbs.GET;
|
|
930
775
|
if (operationQueryOptions?.useMutation !== void 0) isMutation = operationQueryOptions.useMutation;
|
|
931
|
-
if (verb !==
|
|
932
|
-
if (verb ===
|
|
776
|
+
if (verb !== Verbs.GET && isQuery) isMutation = false;
|
|
777
|
+
if (verb === Verbs.GET && isMutation) isQuery = false;
|
|
933
778
|
if (isQuery) {
|
|
934
|
-
const queryKeyMutator = query.queryKey ? await
|
|
779
|
+
const queryKeyMutator = query.queryKey ? await generateMutator({
|
|
935
780
|
output,
|
|
936
781
|
mutator: query.queryKey,
|
|
937
782
|
name: `${operationName}QueryKey`,
|
|
938
783
|
workspace: context.workspace,
|
|
939
784
|
tsconfig: context.output.tsconfig
|
|
940
785
|
}) : void 0;
|
|
941
|
-
const queryOptionsMutator = query.queryOptions ? await
|
|
786
|
+
const queryOptionsMutator = query.queryOptions ? await generateMutator({
|
|
942
787
|
output,
|
|
943
788
|
mutator: query.queryOptions,
|
|
944
789
|
name: `${operationName}QueryOptions`,
|
|
@@ -946,39 +791,39 @@ const generateQueryHook = async ({ queryParams, operationName, body, props: _pro
|
|
|
946
791
|
tsconfig: context.output.tsconfig
|
|
947
792
|
}) : void 0;
|
|
948
793
|
const queryProperties = props.map((param) => {
|
|
949
|
-
if (param.type ===
|
|
950
|
-
return param.type ===
|
|
794
|
+
if (param.type === GetterPropType.NAMED_PATH_PARAMS && !isVue(outputClient)) return param.destructured;
|
|
795
|
+
return param.type === GetterPropType.BODY ? body.implementation : param.name;
|
|
951
796
|
}).join(",");
|
|
952
|
-
const queryKeyProperties = props.filter((prop) => prop.type !==
|
|
953
|
-
if (param.type ===
|
|
954
|
-
return param.type ===
|
|
797
|
+
const queryKeyProperties = props.filter((prop) => prop.type !== GetterPropType.HEADER).map((param) => {
|
|
798
|
+
if (param.type === GetterPropType.NAMED_PATH_PARAMS && !isVue(outputClient)) return param.destructured;
|
|
799
|
+
return param.type === GetterPropType.BODY ? body.implementation : param.name;
|
|
955
800
|
}).join(",");
|
|
956
801
|
const queries = [
|
|
957
802
|
...query?.useInfinite || operationQueryOptions?.useInfinite ? [{
|
|
958
|
-
name:
|
|
803
|
+
name: camel(`${operationName}-infinite`),
|
|
959
804
|
options: query?.options,
|
|
960
805
|
type: QueryType.INFINITE,
|
|
961
806
|
queryParam: query?.useInfiniteQueryParam,
|
|
962
|
-
queryKeyFnName:
|
|
807
|
+
queryKeyFnName: camel(`get-${operationName}-infinite-query-key`)
|
|
963
808
|
}] : [],
|
|
964
809
|
...query?.useQuery || operationQueryOptions?.useQuery ? [{
|
|
965
810
|
name: operationName,
|
|
966
811
|
options: query?.options,
|
|
967
812
|
type: QueryType.QUERY,
|
|
968
|
-
queryKeyFnName:
|
|
813
|
+
queryKeyFnName: camel(`get-${operationName}-query-key`)
|
|
969
814
|
}] : [],
|
|
970
815
|
...query?.useSuspenseQuery || operationQueryOptions?.useSuspenseQuery ? [{
|
|
971
|
-
name:
|
|
816
|
+
name: camel(`${operationName}-suspense`),
|
|
972
817
|
options: query?.options,
|
|
973
818
|
type: QueryType.SUSPENSE_QUERY,
|
|
974
|
-
queryKeyFnName:
|
|
819
|
+
queryKeyFnName: camel(`get-${operationName}-query-key`)
|
|
975
820
|
}] : [],
|
|
976
821
|
...query?.useSuspenseInfiniteQuery || operationQueryOptions?.useSuspenseInfiniteQuery ? [{
|
|
977
|
-
name:
|
|
822
|
+
name: camel(`${operationName}-suspense-infinite`),
|
|
978
823
|
options: query?.options,
|
|
979
824
|
type: QueryType.SUSPENSE_INFINITE,
|
|
980
825
|
queryParam: query?.useInfiniteQueryParam,
|
|
981
|
-
queryKeyFnName:
|
|
826
|
+
queryKeyFnName: camel(`get-${operationName}-infinite-query-key`)
|
|
982
827
|
}] : []
|
|
983
828
|
];
|
|
984
829
|
const makeParamsOptional = (params$1) => {
|
|
@@ -990,11 +835,11 @@ const generateQueryHook = async ({ queryParams, operationName, body, props: _pro
|
|
|
990
835
|
};
|
|
991
836
|
const uniqueQueryOptionsByKeys = queries.filter((obj, index, self) => index === self.findIndex((t) => t.queryKeyFnName === obj.queryKeyFnName));
|
|
992
837
|
implementation += `
|
|
993
|
-
${
|
|
994
|
-
const queryKeyProps = makeParamsOptional(
|
|
995
|
-
const routeString = isVue(outputClient) || override.query.shouldSplitQueryKey ?
|
|
838
|
+
${queryKeyMutator ? "" : uniqueQueryOptionsByKeys.reduce((acc, queryOption) => {
|
|
839
|
+
const queryKeyProps = makeParamsOptional(toObjectString(props.filter((prop) => prop.type !== GetterPropType.HEADER), "implementation"));
|
|
840
|
+
const routeString = isVue(outputClient) || override.query.shouldSplitQueryKey ? getRouteAsArray(route) : `\`${route}\``;
|
|
996
841
|
const queryKeyIdentifier = override.query.useOperationIdAsQueryKey ? `"${operationName}"` : routeString;
|
|
997
|
-
|
|
842
|
+
return acc + `
|
|
998
843
|
${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.queryKeyFnName} = (${queryKeyProps}) => {
|
|
999
844
|
return [
|
|
1000
845
|
${[
|
|
@@ -1006,8 +851,7 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
|
|
|
1006
851
|
] as const;
|
|
1007
852
|
}
|
|
1008
853
|
`;
|
|
1009
|
-
|
|
1010
|
-
}, "") : ""}`;
|
|
854
|
+
}, "")}`;
|
|
1011
855
|
implementation += `
|
|
1012
856
|
${queries.reduce((acc, queryOption) => {
|
|
1013
857
|
return acc + generateQueryImplementation({
|
|
@@ -1048,17 +892,17 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
|
|
|
1048
892
|
mutators = queryOptionsMutator || queryKeyMutator ? [...queryOptionsMutator ? [queryOptionsMutator] : [], ...queryKeyMutator ? [queryKeyMutator] : []] : void 0;
|
|
1049
893
|
}
|
|
1050
894
|
if (isMutation) {
|
|
1051
|
-
const mutationOptionsMutator = query.mutationOptions ? await
|
|
895
|
+
const mutationOptionsMutator = query.mutationOptions ? await generateMutator({
|
|
1052
896
|
output,
|
|
1053
897
|
mutator: query.mutationOptions,
|
|
1054
898
|
name: `${operationName}MutationOptions`,
|
|
1055
899
|
workspace: context.workspace,
|
|
1056
900
|
tsconfig: context.output.tsconfig
|
|
1057
901
|
}) : void 0;
|
|
1058
|
-
const definitions = props.map(({ definition, type }) => type ===
|
|
1059
|
-
const properties = props.map(({ name, type }) => type ===
|
|
902
|
+
const definitions = props.map(({ definition, type }) => type === GetterPropType.BODY ? mutator?.bodyTypeName ? `data: ${mutator.bodyTypeName}<${body.definition}>` : `data: ${body.definition}` : definition).join(";");
|
|
903
|
+
const properties = props.map(({ name, type }) => type === GetterPropType.BODY ? "data" : name).join(",");
|
|
1060
904
|
const errorType = getQueryErrorType(operationName, response, httpClient, mutator);
|
|
1061
|
-
const dataType = mutator?.isHook ? `ReturnType<typeof use${
|
|
905
|
+
const dataType = mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`;
|
|
1062
906
|
const mutationOptionFnReturnType = getQueryOptionsDefinition({
|
|
1063
907
|
operationName,
|
|
1064
908
|
mutator,
|
|
@@ -1078,15 +922,14 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
|
|
|
1078
922
|
hasQueryV5WithInfiniteQueryOptionsError,
|
|
1079
923
|
httpClient
|
|
1080
924
|
});
|
|
1081
|
-
const mutationOptionsFnName =
|
|
925
|
+
const mutationOptionsFnName = camel(mutationOptionsMutator || mutator?.isHook ? `use-${operationName}-mutationOptions` : `get-${operationName}-mutationOptions`);
|
|
1082
926
|
const mutationOptionsVarName = isRequestOptions ? "mutationOptions" : "options";
|
|
1083
|
-
const hooksOptionImplementation = getHooksOptionImplementation(isRequestOptions, httpClient, (0, __orval_core.camel)(operationName), mutator);
|
|
1084
927
|
const mutationOptionsFn = `export const ${mutationOptionsFnName} = <TError = ${errorType},
|
|
1085
928
|
TContext = unknown>(${mutationArguments}): ${mutationOptionFnReturnType} => {
|
|
1086
929
|
|
|
1087
|
-
${
|
|
930
|
+
${getHooksOptionImplementation(isRequestOptions, httpClient, camel(operationName), mutator)}
|
|
1088
931
|
|
|
1089
|
-
${mutator?.isHook ? `const ${operationName} = use${
|
|
932
|
+
${mutator?.isHook ? `const ${operationName} = use${pascal(operationName)}Hook()` : ""}
|
|
1090
933
|
|
|
1091
934
|
|
|
1092
935
|
const mutationFn: MutationFunction<Awaited<ReturnType<${dataType}>>, ${definitions ? `{${definitions}}` : "void"}> = (${properties ? "props" : ""}) => {
|
|
@@ -1104,11 +947,11 @@ ${hooksOptionImplementation}
|
|
|
1104
947
|
implementation += `
|
|
1105
948
|
${mutationOptionsFn}
|
|
1106
949
|
|
|
1107
|
-
export type ${
|
|
1108
|
-
${body.definition ? `export type ${
|
|
1109
|
-
export type ${
|
|
950
|
+
export type ${pascal(operationName)}MutationResult = NonNullable<Awaited<ReturnType<${dataType}>>>
|
|
951
|
+
${body.definition ? `export type ${pascal(operationName)}MutationBody = ${mutator?.bodyTypeName ? `${mutator.bodyTypeName}<${body.definition}>` : body.definition}` : ""}
|
|
952
|
+
export type ${pascal(operationName)}MutationError = ${errorType}
|
|
1110
953
|
|
|
1111
|
-
${doc}export const ${
|
|
954
|
+
${doc}export const ${camel(`${operationPrefix}-${operationName}`)} = <TError = ${errorType},
|
|
1112
955
|
TContext = unknown>(${mutationArguments} ${optionalQueryClientArgument})${generateMutatorReturnType({
|
|
1113
956
|
outputClient,
|
|
1114
957
|
dataType,
|
|
@@ -1135,7 +978,7 @@ ${getQueryHeader(params)}
|
|
|
1135
978
|
`;
|
|
1136
979
|
};
|
|
1137
980
|
const generateQuery = async (verbOptions, options, outputClient) => {
|
|
1138
|
-
const imports =
|
|
981
|
+
const imports = generateVerbImports(verbOptions);
|
|
1139
982
|
const functionImplementation = generateQueryRequestFunction(verbOptions, options, isVue(outputClient));
|
|
1140
983
|
const { implementation: hookImplementation, mutators } = await generateQueryHook(verbOptions, options, outputClient);
|
|
1141
984
|
return {
|
|
@@ -1154,8 +997,8 @@ const builder = ({ type = "react-query", options: queryOptions, output } = {}) =
|
|
|
1154
997
|
if (options.override.useNamedParameters && (type === "vue-query" || outputClient === "vue-query")) throw new Error(`vue-query client does not support named parameters, and had broken reactivity previously, please set useNamedParameters to false; See for context: https://github.com/orval-labs/orval/pull/931#issuecomment-1752355686`);
|
|
1155
998
|
if (queryOptions) {
|
|
1156
999
|
const normalizedQueryOptions = normalizeQueryOptions(queryOptions, options.context.workspace);
|
|
1157
|
-
verbOptions.override.query =
|
|
1158
|
-
options.override.query =
|
|
1000
|
+
verbOptions.override.query = mergeDeep(normalizedQueryOptions, verbOptions.override.query);
|
|
1001
|
+
options.override.query = mergeDeep(normalizedQueryOptions, verbOptions.override.query);
|
|
1159
1002
|
}
|
|
1160
1003
|
return generateQuery(verbOptions, options, outputClient, output);
|
|
1161
1004
|
};
|
|
@@ -1168,11 +1011,5 @@ const builder = ({ type = "react-query", options: queryOptions, output } = {}) =
|
|
|
1168
1011
|
var src_default = builder;
|
|
1169
1012
|
|
|
1170
1013
|
//#endregion
|
|
1171
|
-
|
|
1172
|
-
exports.default = src_default;
|
|
1173
|
-
exports.generateQuery = generateQuery;
|
|
1174
|
-
exports.generateQueryHeader = generateQueryHeader;
|
|
1175
|
-
exports.getReactQueryDependencies = getReactQueryDependencies;
|
|
1176
|
-
exports.getSvelteQueryDependencies = getSvelteQueryDependencies;
|
|
1177
|
-
exports.getVueQueryDependencies = getVueQueryDependencies;
|
|
1014
|
+
export { builder, src_default as default, generateQuery, generateQueryHeader, getReactQueryDependencies, getSvelteQueryDependencies, getVueQueryDependencies };
|
|
1178
1015
|
//# sourceMappingURL=index.js.map
|