@orval/query 7.14.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 +181 -282
- package/dist/index.js.map +1 -1
- package/package.json +12 -10
package/dist/index.js
CHANGED
|
@@ -1,40 +1,13 @@
|
|
|
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) => {
|
|
36
8
|
return {
|
|
37
9
|
...queryOptions.usePrefetch ? { usePrefetch: true } : {},
|
|
10
|
+
...queryOptions.useInvalidate ? { useInvalidate: true } : {},
|
|
38
11
|
...queryOptions.useQuery ? { useQuery: true } : {},
|
|
39
12
|
...queryOptions.useInfinite ? { useInfinite: true } : {},
|
|
40
13
|
...queryOptions.useInfiniteQueryParam ? { useInfiniteQueryParam: queryOptions.useInfiniteQueryParam } : {},
|
|
@@ -51,16 +24,16 @@ const normalizeQueryOptions = (queryOptions = {}, outputWorkspace) => {
|
|
|
51
24
|
};
|
|
52
25
|
};
|
|
53
26
|
const normalizeMutator = (workspace, mutator) => {
|
|
54
|
-
if (
|
|
55
|
-
if (!mutator.path) throw new Error(chalk.
|
|
27
|
+
if (isObject(mutator)) {
|
|
28
|
+
if (!mutator.path) throw new Error(chalk.red(`Mutator need a path`));
|
|
56
29
|
return {
|
|
57
30
|
...mutator,
|
|
58
|
-
path:
|
|
31
|
+
path: upath.resolve(workspace, mutator.path),
|
|
59
32
|
default: (mutator.default || !mutator.name) ?? false
|
|
60
33
|
};
|
|
61
34
|
}
|
|
62
|
-
if (
|
|
63
|
-
path:
|
|
35
|
+
if (isString(mutator)) return {
|
|
36
|
+
path: upath.resolve(workspace, mutator),
|
|
64
37
|
default: true
|
|
65
38
|
};
|
|
66
39
|
return mutator;
|
|
@@ -69,7 +42,7 @@ function vueWrapTypeWithMaybeRef(props) {
|
|
|
69
42
|
return props.map((prop) => {
|
|
70
43
|
const [paramName, paramType] = prop.implementation.split(":");
|
|
71
44
|
if (!paramType) return prop;
|
|
72
|
-
const name = prop.type ===
|
|
45
|
+
const name = prop.type === GetterPropType.NAMED_PATH_PARAMS ? prop.name : paramName;
|
|
73
46
|
const [type, defaultValue] = paramType.split("=");
|
|
74
47
|
return {
|
|
75
48
|
...prop,
|
|
@@ -79,14 +52,14 @@ function vueWrapTypeWithMaybeRef(props) {
|
|
|
79
52
|
}
|
|
80
53
|
const vueUnRefParams = (props) => {
|
|
81
54
|
return props.map((prop) => {
|
|
82
|
-
if (prop.type ===
|
|
55
|
+
if (prop.type === GetterPropType.NAMED_PATH_PARAMS) return `const ${prop.destructured} = unref(${prop.name});`;
|
|
83
56
|
return `${prop.name} = unref(${prop.name});`;
|
|
84
57
|
}).join("\n");
|
|
85
58
|
};
|
|
86
|
-
const wrapRouteParameters = (route, prepend, append) => route.replaceAll(
|
|
59
|
+
const wrapRouteParameters = (route, prepend, append) => route.replaceAll(TEMPLATE_TAG_REGEX, `\${${prepend}$1${append}}`);
|
|
87
60
|
const makeRouteSafe = (route) => wrapRouteParameters(route, "encodeURIComponent(String(", "))");
|
|
88
|
-
const isVue = (client) =>
|
|
89
|
-
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);
|
|
90
63
|
|
|
91
64
|
//#endregion
|
|
92
65
|
//#region src/client.ts
|
|
@@ -105,7 +78,7 @@ const AXIOS_DEPENDENCIES = [{
|
|
|
105
78
|
dependency: "axios"
|
|
106
79
|
}];
|
|
107
80
|
const generateQueryRequestFunction = (verbOptions, options, isVue$1) => {
|
|
108
|
-
return options.context.output.httpClient ===
|
|
81
|
+
return options.context.output.httpClient === OutputHttpClient.AXIOS ? generateAxiosRequestFunction(verbOptions, options, isVue$1) : generateRequestFunction(verbOptions, options);
|
|
109
82
|
};
|
|
110
83
|
const generateAxiosRequestFunction = ({ headers, queryParams, operationName, response, mutator, body, props: _props, verb, formData, formUrlEncoded, override, paramsSerializer }, { route: _route, context }, isVue$1) => {
|
|
111
84
|
let props = _props;
|
|
@@ -120,7 +93,7 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
120
93
|
verb
|
|
121
94
|
});
|
|
122
95
|
const isExactOptionalPropertyTypes = !!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
|
|
123
|
-
const bodyForm =
|
|
96
|
+
const bodyForm = generateFormDataAndUrlEncodedFunction({
|
|
124
97
|
formData,
|
|
125
98
|
formUrlEncoded,
|
|
126
99
|
body,
|
|
@@ -128,7 +101,7 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
128
101
|
isFormUrlEncoded
|
|
129
102
|
});
|
|
130
103
|
if (mutator) {
|
|
131
|
-
const mutatorConfig =
|
|
104
|
+
const mutatorConfig = generateMutatorConfig({
|
|
132
105
|
route,
|
|
133
106
|
body,
|
|
134
107
|
headers,
|
|
@@ -142,10 +115,10 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
142
115
|
isVue: isVue$1
|
|
143
116
|
});
|
|
144
117
|
const bodyDefinition = body.definition.replace("[]", String.raw`\[\]`);
|
|
145
|
-
const propsImplementation = mutator?.bodyTypeName && body.definition ?
|
|
146
|
-
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) : "";
|
|
147
120
|
if (mutator.isHook) {
|
|
148
|
-
const ret = `${override.query.shouldExportMutatorHooks ? "export " : ""}const use${
|
|
121
|
+
const ret = `${override.query.shouldExportMutatorHooks ? "export " : ""}const use${pascal(operationName)}Hook = () => {
|
|
149
122
|
const ${operationName} = ${mutator.name}<${response.definition.success || "unknown"}>();
|
|
150
123
|
|
|
151
124
|
return useCallback((\n ${propsImplementation}\n ${isRequestOptions && mutator.hasSecondArg ? `options${context.output.optionsParamRequired ? "" : "?"}: SecondParameter<ReturnType<typeof ${mutator.name}>>,` : ""}${hasSignal ? "signal?: AbortSignal\n" : ""}) => {${bodyForm}
|
|
@@ -155,7 +128,7 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
155
128
|
}, [${operationName}])
|
|
156
129
|
}
|
|
157
130
|
`;
|
|
158
|
-
const vueRet = `${override.query.shouldExportMutatorHooks ? "export " : ""}const use${
|
|
131
|
+
const vueRet = `${override.query.shouldExportMutatorHooks ? "export " : ""}const use${pascal(operationName)}Hook = () => {
|
|
159
132
|
const ${operationName} = ${mutator.name}<${response.definition.success || "unknown"}>();
|
|
160
133
|
|
|
161
134
|
return (\n ${propsImplementation}\n ${isRequestOptions && mutator.hasSecondArg ? `options${context.output.optionsParamRequired ? "" : "?"}: SecondParameter<ReturnType<typeof ${mutator.name}>>,` : ""}${hasSignal ? "signal?: AbortSignal\n" : ""}) => {${bodyForm}
|
|
@@ -176,8 +149,8 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
176
149
|
}
|
|
177
150
|
`;
|
|
178
151
|
}
|
|
179
|
-
const isSyntheticDefaultImportsAllowed =
|
|
180
|
-
const options =
|
|
152
|
+
const isSyntheticDefaultImportsAllowed = isSyntheticDefaultImportsAllow(context.output.tsconfig);
|
|
153
|
+
const options = generateOptions({
|
|
181
154
|
route,
|
|
182
155
|
body,
|
|
183
156
|
headers,
|
|
@@ -197,7 +170,7 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
197
170
|
isRequestOptions,
|
|
198
171
|
hasSignal
|
|
199
172
|
});
|
|
200
|
-
const queryProps =
|
|
173
|
+
const queryProps = toObjectString(props, "implementation");
|
|
201
174
|
return `${override.query.shouldExportHttpClient ? "export " : ""}const ${operationName} = (\n ${queryProps} ${optionsArgs} ): Promise<AxiosResponse<${response.definition.success || "unknown"}>> => {
|
|
202
175
|
${isVue$1 ? vueUnRefParams(props) : ""}
|
|
203
176
|
${bodyForm}
|
|
@@ -210,20 +183,20 @@ const generateRequestOptionsArguments = ({ isRequestOptions, hasSignal }) => {
|
|
|
210
183
|
return hasSignal ? "signal?: AbortSignal\n" : "";
|
|
211
184
|
};
|
|
212
185
|
const getQueryArgumentsRequestType = (httpClient, mutator) => {
|
|
213
|
-
if (!mutator) return httpClient ===
|
|
186
|
+
if (!mutator) return httpClient === OutputHttpClient.AXIOS ? `axios?: AxiosRequestConfig` : "fetch?: RequestInit";
|
|
214
187
|
if (mutator.hasSecondArg && !mutator.isHook) return `request?: SecondParameter<typeof ${mutator.name}>`;
|
|
215
188
|
if (mutator.hasSecondArg && mutator.isHook) return `request?: SecondParameter<ReturnType<typeof ${mutator.name}>>`;
|
|
216
189
|
return "";
|
|
217
190
|
};
|
|
218
191
|
const getQueryOptions = ({ isRequestOptions, mutator, isExactOptionalPropertyTypes, hasSignal, httpClient }) => {
|
|
219
192
|
if (!mutator && isRequestOptions) {
|
|
220
|
-
const options = httpClient ===
|
|
193
|
+
const options = httpClient === OutputHttpClient.AXIOS ? "axiosOptions" : "fetchOptions";
|
|
221
194
|
if (!hasSignal) return options;
|
|
222
195
|
return `{ ${isExactOptionalPropertyTypes ? "...(signal ? { signal } : {})" : "signal"}, ...${options} }`;
|
|
223
196
|
}
|
|
224
197
|
if (mutator?.hasSecondArg && isRequestOptions) {
|
|
225
198
|
if (!hasSignal) return "requestOptions";
|
|
226
|
-
return httpClient ===
|
|
199
|
+
return httpClient === OutputHttpClient.AXIOS ? "requestOptions, signal" : "{ signal, ...requestOptions }";
|
|
227
200
|
}
|
|
228
201
|
if (hasSignal) return "signal";
|
|
229
202
|
return "";
|
|
@@ -232,36 +205,41 @@ const getHookOptions = ({ isRequestOptions, httpClient, mutator }) => {
|
|
|
232
205
|
if (!isRequestOptions) return "";
|
|
233
206
|
let value = "const {query: queryOptions";
|
|
234
207
|
if (!mutator) {
|
|
235
|
-
const options = httpClient ===
|
|
208
|
+
const options = httpClient === OutputHttpClient.AXIOS ? ", axios: axiosOptions" : ", fetch: fetchOptions";
|
|
236
209
|
value += options;
|
|
237
210
|
}
|
|
238
211
|
if (mutator?.hasSecondArg) value += ", request: requestOptions";
|
|
239
212
|
value += "} = options ?? {};";
|
|
240
213
|
return value;
|
|
241
214
|
};
|
|
215
|
+
const dedupeUnionTypes = (types) => {
|
|
216
|
+
if (!types) return types;
|
|
217
|
+
return [...new Set(types.split("|").map((t) => t.trim()).filter(Boolean))].join(" | ");
|
|
218
|
+
};
|
|
242
219
|
const getQueryErrorType = (operationName, response, httpClient, mutator) => {
|
|
243
|
-
|
|
244
|
-
|
|
220
|
+
const errorsType = dedupeUnionTypes(response.definition.errors || "unknown");
|
|
221
|
+
if (mutator) return mutator.hasErrorType ? `${mutator.default ? pascal(operationName) : ""}ErrorType<${errorsType}>` : errorsType;
|
|
222
|
+
else return httpClient === OutputHttpClient.AXIOS ? `AxiosError<${errorsType}>` : errorsType;
|
|
245
223
|
};
|
|
246
224
|
const getHooksOptionImplementation = (isRequestOptions, httpClient, operationName, mutator) => {
|
|
247
|
-
const options = httpClient ===
|
|
225
|
+
const options = httpClient === OutputHttpClient.AXIOS ? ", axios: axiosOptions" : ", fetch: fetchOptions";
|
|
248
226
|
return isRequestOptions ? `const mutationKey = ['${operationName}'];
|
|
249
227
|
const {mutation: mutationOptions${mutator ? mutator?.hasSecondArg ? ", request: requestOptions" : "" : options}} = options ?
|
|
250
228
|
options.mutation && 'mutationKey' in options.mutation && options.mutation.mutationKey ?
|
|
251
229
|
options
|
|
252
230
|
: {...options, mutation: {...options.mutation, mutationKey}}
|
|
253
|
-
: {mutation: { mutationKey, }${mutator?.hasSecondArg ? ", request: undefined" : ""}${mutator ? "" : httpClient ===
|
|
231
|
+
: {mutation: { mutationKey, }${mutator?.hasSecondArg ? ", request: undefined" : ""}${mutator ? "" : httpClient === OutputHttpClient.AXIOS ? ", axios: undefined" : ", fetch: undefined"}};` : "";
|
|
254
232
|
};
|
|
255
233
|
const getMutationRequestArgs = (isRequestOptions, httpClient, mutator) => {
|
|
256
|
-
const options = httpClient ===
|
|
234
|
+
const options = httpClient === OutputHttpClient.AXIOS ? "axiosOptions" : "fetchOptions";
|
|
257
235
|
return isRequestOptions ? mutator ? mutator?.hasSecondArg ? "requestOptions" : "" : options : "";
|
|
258
236
|
};
|
|
259
237
|
const getHttpFunctionQueryProps = (isVue$1, httpClient, queryProperties) => {
|
|
260
|
-
if (isVue$1 && httpClient ===
|
|
238
|
+
if (isVue$1 && httpClient === OutputHttpClient.FETCH && queryProperties) return queryProperties.split(",").map((prop) => `unref(${prop})`).join(",");
|
|
261
239
|
return queryProperties;
|
|
262
240
|
};
|
|
263
241
|
const getQueryHeader = (params) => {
|
|
264
|
-
return params.output.httpClient ===
|
|
242
|
+
return params.output.httpClient === OutputHttpClient.FETCH ? generateFetchHeader(params) : "";
|
|
265
243
|
};
|
|
266
244
|
|
|
267
245
|
//#endregion
|
|
@@ -282,32 +260,6 @@ const PARAMS_SERIALIZER_DEPENDENCIES = [{
|
|
|
282
260
|
}],
|
|
283
261
|
dependency: "qs"
|
|
284
262
|
}];
|
|
285
|
-
const SVELTE_QUERY_DEPENDENCIES_V3 = [{
|
|
286
|
-
exports: [
|
|
287
|
-
{
|
|
288
|
-
name: "useQuery",
|
|
289
|
-
values: true
|
|
290
|
-
},
|
|
291
|
-
{
|
|
292
|
-
name: "useInfiniteQuery",
|
|
293
|
-
values: true
|
|
294
|
-
},
|
|
295
|
-
{
|
|
296
|
-
name: "useMutation",
|
|
297
|
-
values: true
|
|
298
|
-
},
|
|
299
|
-
{ name: "UseQueryOptions" },
|
|
300
|
-
{ name: "UseInfiniteQueryOptions" },
|
|
301
|
-
{ name: "UseMutationOptions" },
|
|
302
|
-
{ name: "QueryFunction" },
|
|
303
|
-
{ name: "MutationFunction" },
|
|
304
|
-
{ name: "UseQueryStoreResult" },
|
|
305
|
-
{ name: "UseInfiniteQueryStoreResult" },
|
|
306
|
-
{ name: "QueryKey" },
|
|
307
|
-
{ name: "CreateMutationResult" }
|
|
308
|
-
],
|
|
309
|
-
dependency: "@sveltestack/svelte-query"
|
|
310
|
-
}];
|
|
311
263
|
const SVELTE_QUERY_DEPENDENCIES = [{
|
|
312
264
|
exports: [
|
|
313
265
|
{
|
|
@@ -333,53 +285,21 @@ const SVELTE_QUERY_DEPENDENCIES = [{
|
|
|
333
285
|
{ name: "InfiniteData" },
|
|
334
286
|
{ name: "CreateMutationResult" },
|
|
335
287
|
{ name: "DataTag" },
|
|
336
|
-
{ name: "QueryClient" }
|
|
288
|
+
{ name: "QueryClient" },
|
|
289
|
+
{ name: "InvalidateOptions" }
|
|
337
290
|
],
|
|
338
291
|
dependency: "@tanstack/svelte-query"
|
|
339
292
|
}];
|
|
340
|
-
const isSvelteQueryV3 = (packageJson) => {
|
|
341
|
-
const hasSvelteQuery = packageJson?.dependencies?.["@sveltestack/svelte-query"] ?? packageJson?.devDependencies?.["@sveltestack/svelte-query"] ?? packageJson?.peerDependencies?.["@sveltestack/svelte-query"];
|
|
342
|
-
const hasSvelteQueryV4 = packageJson?.dependencies?.["@tanstack/svelte-query"] ?? packageJson?.devDependencies?.["@tanstack/svelte-query"] ?? packageJson?.peerDependencies?.["@tanstack/svelte-query"];
|
|
343
|
-
return !!hasSvelteQuery && !hasSvelteQueryV4;
|
|
344
|
-
};
|
|
345
293
|
const isSvelteQueryV6 = (packageJson) => {
|
|
346
294
|
return isQueryV6(packageJson, "svelte-query");
|
|
347
295
|
};
|
|
348
296
|
const getSvelteQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient) => {
|
|
349
|
-
const hasSvelteQueryV3 = isSvelteQueryV3(packageJson);
|
|
350
297
|
return [
|
|
351
|
-
...!hasGlobalMutator && httpClient ===
|
|
298
|
+
...!hasGlobalMutator && httpClient === OutputHttpClient.AXIOS ? AXIOS_DEPENDENCIES : [],
|
|
352
299
|
...hasParamsSerializerOptions ? PARAMS_SERIALIZER_DEPENDENCIES : [],
|
|
353
|
-
...
|
|
300
|
+
...SVELTE_QUERY_DEPENDENCIES
|
|
354
301
|
];
|
|
355
302
|
};
|
|
356
|
-
const REACT_QUERY_DEPENDENCIES_V3 = [{
|
|
357
|
-
exports: [
|
|
358
|
-
{
|
|
359
|
-
name: "useQuery",
|
|
360
|
-
values: true
|
|
361
|
-
},
|
|
362
|
-
{
|
|
363
|
-
name: "useInfiniteQuery",
|
|
364
|
-
values: true
|
|
365
|
-
},
|
|
366
|
-
{
|
|
367
|
-
name: "useMutation",
|
|
368
|
-
values: true
|
|
369
|
-
},
|
|
370
|
-
{ name: "UseQueryOptions" },
|
|
371
|
-
{ name: "UseInfiniteQueryOptions" },
|
|
372
|
-
{ name: "UseMutationOptions" },
|
|
373
|
-
{ name: "QueryFunction" },
|
|
374
|
-
{ name: "MutationFunction" },
|
|
375
|
-
{ name: "UseQueryResult" },
|
|
376
|
-
{ name: "UseInfiniteQueryResult" },
|
|
377
|
-
{ name: "QueryKey" },
|
|
378
|
-
{ name: "QueryClient" },
|
|
379
|
-
{ name: "UseMutationResult" }
|
|
380
|
-
],
|
|
381
|
-
dependency: "react-query"
|
|
382
|
-
}];
|
|
383
303
|
const REACT_QUERY_DEPENDENCIES = [{
|
|
384
304
|
exports: [
|
|
385
305
|
{
|
|
@@ -402,6 +322,10 @@ const REACT_QUERY_DEPENDENCIES = [{
|
|
|
402
322
|
name: "useMutation",
|
|
403
323
|
values: true
|
|
404
324
|
},
|
|
325
|
+
{
|
|
326
|
+
name: "useQueryClient",
|
|
327
|
+
values: true
|
|
328
|
+
},
|
|
405
329
|
{ name: "UseQueryOptions" },
|
|
406
330
|
{ name: "DefinedInitialDataOptions" },
|
|
407
331
|
{ name: "UndefinedInitialDataOptions" },
|
|
@@ -421,68 +345,19 @@ const REACT_QUERY_DEPENDENCIES = [{
|
|
|
421
345
|
{ name: "QueryClient" },
|
|
422
346
|
{ name: "InfiniteData" },
|
|
423
347
|
{ name: "UseMutationResult" },
|
|
424
|
-
{ name: "DataTag" }
|
|
348
|
+
{ name: "DataTag" },
|
|
349
|
+
{ name: "InvalidateOptions" }
|
|
425
350
|
],
|
|
426
351
|
dependency: "@tanstack/react-query"
|
|
427
352
|
}];
|
|
428
|
-
const getReactQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient, hasTagsMutator
|
|
429
|
-
const hasReactQuery = packageJson?.dependencies?.["react-query"] ?? packageJson?.devDependencies?.["react-query"] ?? packageJson?.peerDependencies?.["react-query"];
|
|
430
|
-
const hasReactQueryV4 = packageJson?.dependencies?.["@tanstack/react-query"] ?? packageJson?.devDependencies?.["@tanstack/react-query"] ?? packageJson?.peerDependencies?.["@tanstack/react-query"];
|
|
431
|
-
const useReactQueryV3 = override?.query.version === void 0 ? hasReactQuery && !hasReactQueryV4 : override?.query.version <= 3;
|
|
353
|
+
const getReactQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient, hasTagsMutator) => {
|
|
432
354
|
return [
|
|
433
355
|
...hasGlobalMutator || hasTagsMutator ? REACT_DEPENDENCIES : [],
|
|
434
|
-
...!hasGlobalMutator && httpClient ===
|
|
356
|
+
...!hasGlobalMutator && httpClient === OutputHttpClient.AXIOS ? AXIOS_DEPENDENCIES : [],
|
|
435
357
|
...hasParamsSerializerOptions ? PARAMS_SERIALIZER_DEPENDENCIES : [],
|
|
436
|
-
...
|
|
358
|
+
...REACT_QUERY_DEPENDENCIES
|
|
437
359
|
];
|
|
438
360
|
};
|
|
439
|
-
const VUE_QUERY_DEPENDENCIES_V3 = [
|
|
440
|
-
{
|
|
441
|
-
exports: [
|
|
442
|
-
{
|
|
443
|
-
name: "useQuery",
|
|
444
|
-
values: true
|
|
445
|
-
},
|
|
446
|
-
{
|
|
447
|
-
name: "useInfiniteQuery",
|
|
448
|
-
values: true
|
|
449
|
-
},
|
|
450
|
-
{
|
|
451
|
-
name: "useMutation",
|
|
452
|
-
values: true
|
|
453
|
-
}
|
|
454
|
-
],
|
|
455
|
-
dependency: "vue-query"
|
|
456
|
-
},
|
|
457
|
-
{
|
|
458
|
-
exports: [
|
|
459
|
-
{ name: "UseQueryOptions" },
|
|
460
|
-
{ name: "UseInfiniteQueryOptions" },
|
|
461
|
-
{ name: "UseMutationOptions" },
|
|
462
|
-
{ name: "QueryFunction" },
|
|
463
|
-
{ name: "MutationFunction" },
|
|
464
|
-
{ name: "UseQueryResult" },
|
|
465
|
-
{ name: "UseInfiniteQueryResult" },
|
|
466
|
-
{ name: "QueryKey" },
|
|
467
|
-
{ name: "UseMutationReturnType" }
|
|
468
|
-
],
|
|
469
|
-
dependency: "vue-query/types"
|
|
470
|
-
},
|
|
471
|
-
{
|
|
472
|
-
exports: [{
|
|
473
|
-
name: "unref",
|
|
474
|
-
values: true
|
|
475
|
-
}, {
|
|
476
|
-
name: "computed",
|
|
477
|
-
values: true
|
|
478
|
-
}],
|
|
479
|
-
dependency: "vue"
|
|
480
|
-
},
|
|
481
|
-
{
|
|
482
|
-
exports: [{ name: "UseQueryReturnType" }],
|
|
483
|
-
dependency: "vue-query/lib/vue/useBaseQuery"
|
|
484
|
-
}
|
|
485
|
-
];
|
|
486
361
|
const VUE_QUERY_DEPENDENCIES = [{
|
|
487
362
|
exports: [
|
|
488
363
|
{
|
|
@@ -508,7 +383,8 @@ const VUE_QUERY_DEPENDENCIES = [{
|
|
|
508
383
|
{ name: "InfiniteData" },
|
|
509
384
|
{ name: "UseMutationReturnType" },
|
|
510
385
|
{ name: "DataTag" },
|
|
511
|
-
{ name: "QueryClient" }
|
|
386
|
+
{ name: "QueryClient" },
|
|
387
|
+
{ name: "InvalidateOptions" }
|
|
512
388
|
],
|
|
513
389
|
dependency: "@tanstack/vue-query"
|
|
514
390
|
}, {
|
|
@@ -525,42 +401,36 @@ const VUE_QUERY_DEPENDENCIES = [{
|
|
|
525
401
|
],
|
|
526
402
|
dependency: "vue"
|
|
527
403
|
}];
|
|
528
|
-
const isVueQueryV3 = (packageJson) => {
|
|
529
|
-
const hasVueQuery = packageJson?.dependencies?.["vue-query"] ?? packageJson?.devDependencies?.["vue-query"] ?? packageJson?.peerDependencies?.["vue-query"];
|
|
530
|
-
const hasVueQueryV4 = packageJson?.dependencies?.["@tanstack/vue-query"] ?? packageJson?.devDependencies?.["@tanstack/vue-query"] ?? packageJson?.peerDependencies?.["@tanstack/vue-query"];
|
|
531
|
-
return !!hasVueQuery && !hasVueQueryV4;
|
|
532
|
-
};
|
|
533
404
|
const getVueQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient) => {
|
|
534
|
-
const hasVueQueryV3 = isVueQueryV3(packageJson);
|
|
535
405
|
return [
|
|
536
|
-
...!hasGlobalMutator && httpClient ===
|
|
406
|
+
...!hasGlobalMutator && httpClient === OutputHttpClient.AXIOS ? AXIOS_DEPENDENCIES : [],
|
|
537
407
|
...hasParamsSerializerOptions ? PARAMS_SERIALIZER_DEPENDENCIES : [],
|
|
538
|
-
...
|
|
408
|
+
...VUE_QUERY_DEPENDENCIES
|
|
539
409
|
];
|
|
540
410
|
};
|
|
541
411
|
const isQueryV5 = (packageJson, queryClient) => {
|
|
542
412
|
const version = getPackageByQueryClient(packageJson, queryClient);
|
|
543
413
|
if (!version) return false;
|
|
544
414
|
const withoutRc = version.split("-")[0];
|
|
545
|
-
return
|
|
415
|
+
return compareVersions(withoutRc, "5.0.0");
|
|
546
416
|
};
|
|
547
417
|
const isQueryV6 = (packageJson, queryClient) => {
|
|
548
418
|
const version = getPackageByQueryClient(packageJson, queryClient);
|
|
549
419
|
if (!version) return false;
|
|
550
420
|
const withoutRc = version.split("-")[0];
|
|
551
|
-
return
|
|
421
|
+
return compareVersions(withoutRc, "6.0.0");
|
|
552
422
|
};
|
|
553
423
|
const isQueryV5WithDataTagError = (packageJson, queryClient) => {
|
|
554
424
|
const version = getPackageByQueryClient(packageJson, queryClient);
|
|
555
425
|
if (!version) return false;
|
|
556
426
|
const withoutRc = version.split("-")[0];
|
|
557
|
-
return
|
|
427
|
+
return compareVersions(withoutRc, "5.62.0");
|
|
558
428
|
};
|
|
559
429
|
const isQueryV5WithInfiniteQueryOptionsError = (packageJson, queryClient) => {
|
|
560
430
|
const version = getPackageByQueryClient(packageJson, queryClient);
|
|
561
431
|
if (!version) return false;
|
|
562
432
|
const withoutRc = version.split("-")[0];
|
|
563
|
-
return
|
|
433
|
+
return compareVersions(withoutRc, "5.80.0");
|
|
564
434
|
};
|
|
565
435
|
const getPackageByQueryClient = (packageJson, queryClient) => {
|
|
566
436
|
switch (queryClient) {
|
|
@@ -578,12 +448,12 @@ const QueryType = {
|
|
|
578
448
|
const INFINITE_QUERY_PROPERTIES = new Set(["getNextPageParam", "getPreviousPageParam"]);
|
|
579
449
|
const generateQueryOptions = ({ params, options, type, outputClient }) => {
|
|
580
450
|
if (options === false) return "";
|
|
581
|
-
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)}` : "";
|
|
582
452
|
if (params.length === 0 || isSuspenseQuery(type)) {
|
|
583
453
|
if (options) return `${queryConfig} ...queryOptions`;
|
|
584
454
|
return "...queryOptions";
|
|
585
455
|
}
|
|
586
|
-
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`;
|
|
587
457
|
};
|
|
588
458
|
const isSuspenseQuery = (type) => {
|
|
589
459
|
return [QueryType.SUSPENSE_INFINITE, QueryType.SUSPENSE_QUERY].includes(type);
|
|
@@ -593,18 +463,18 @@ const getQueryOptionsDefinition = ({ operationName, mutator, definitions, type,
|
|
|
593
463
|
const prefix = hasSvelteQueryV4 ? "Create" : "Use";
|
|
594
464
|
const partialOptions = !isReturnType && hasQueryV5;
|
|
595
465
|
if (type) {
|
|
596
|
-
const funcReturnType = `Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${
|
|
466
|
+
const funcReturnType = `Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>`;
|
|
597
467
|
const optionTypeInitialDataPostfix = initialData && !isSuspenseQuery(type) ? ` & Pick<
|
|
598
|
-
${
|
|
468
|
+
${pascal(initialData)}InitialDataOptions<
|
|
599
469
|
${funcReturnType},
|
|
600
470
|
TError,
|
|
601
471
|
${funcReturnType}${hasQueryV5 && (type === QueryType.INFINITE || type === QueryType.SUSPENSE_INFINITE) && queryParam && queryParams ? `, QueryKey` : ""}
|
|
602
472
|
> , 'initialData'
|
|
603
473
|
>` : "";
|
|
604
|
-
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}']` : ""}>`;
|
|
605
475
|
return `${partialOptions ? "Partial<" : ""}${optionType}${partialOptions ? ">" : ""}${optionTypeInitialDataPostfix}`;
|
|
606
476
|
}
|
|
607
|
-
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>`;
|
|
608
478
|
};
|
|
609
479
|
const generateQueryArguments = ({ operationName, definitions, mutator, isRequestOptions, type, hasSvelteQueryV4, hasQueryV5, hasQueryV5WithInfiniteQueryOptionsError, queryParams, queryParam, initialData, httpClient }) => {
|
|
610
480
|
const definition = getQueryOptionsDefinition({
|
|
@@ -627,31 +497,31 @@ const generateQueryArguments = ({ operationName, definitions, mutator, isRequest
|
|
|
627
497
|
};
|
|
628
498
|
const generateQueryReturnType = ({ outputClient, type, isMutatorHook, operationName, hasVueQueryV4, hasSvelteQueryV4, hasQueryV5, hasQueryV5WithDataTagError, isInitialDataDefined }) => {
|
|
629
499
|
switch (outputClient) {
|
|
630
|
-
case
|
|
631
|
-
if (!hasSvelteQueryV4) return `Use${
|
|
632
|
-
return `Create${
|
|
633
|
-
case
|
|
634
|
-
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} }`;
|
|
635
505
|
if (type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE) return `UseQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
636
506
|
return `UseInfiniteQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
637
|
-
case
|
|
638
|
-
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"} }`;
|
|
639
509
|
}
|
|
640
510
|
};
|
|
641
511
|
const generateMutatorReturnType = ({ outputClient, dataType, variableType }) => {
|
|
642
|
-
if (outputClient ===
|
|
512
|
+
if (outputClient === OutputClient.REACT_QUERY) return `: UseMutationResult<
|
|
643
513
|
Awaited<ReturnType<${dataType}>>,
|
|
644
514
|
TError,
|
|
645
515
|
${variableType},
|
|
646
516
|
TContext
|
|
647
517
|
>`;
|
|
648
|
-
if (outputClient ===
|
|
518
|
+
if (outputClient === OutputClient.SVELTE_QUERY) return `: CreateMutationResult<
|
|
649
519
|
Awaited<ReturnType<${dataType}>>,
|
|
650
520
|
TError,
|
|
651
521
|
${variableType},
|
|
652
522
|
TContext
|
|
653
523
|
>`;
|
|
654
|
-
if (outputClient ===
|
|
524
|
+
if (outputClient === OutputClient.VUE_QUERY) return `: UseMutationReturnType<
|
|
655
525
|
Awaited<ReturnType<${dataType}>>,
|
|
656
526
|
TError,
|
|
657
527
|
${variableType},
|
|
@@ -667,9 +537,30 @@ const getQueryFnArguments = ({ hasQueryParam, hasSignal }) => {
|
|
|
667
537
|
}
|
|
668
538
|
return "{ signal }";
|
|
669
539
|
};
|
|
670
|
-
const
|
|
671
|
-
|
|
672
|
-
const
|
|
540
|
+
const generatePrefetch = ({ usePrefetch, type, useQuery, useInfinite, operationName, mutator, doc, queryProps, dataType, errorType, queryArguments, queryOptionsVarName, queryOptionsFnName, queryProperties, isRequestOptions, hasSvelteQueryV6 }) => {
|
|
541
|
+
if (!(usePrefetch && (type === QueryType.QUERY || type === QueryType.INFINITE || type === QueryType.SUSPENSE_QUERY && !useQuery || type === QueryType.SUSPENSE_INFINITE && !useInfinite))) return "";
|
|
542
|
+
const prefetchType = type === QueryType.QUERY || type === QueryType.SUSPENSE_QUERY ? "query" : "infinite-query";
|
|
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}) => {
|
|
545
|
+
const queryClient = useQueryClient();
|
|
546
|
+
const ${queryOptionsVarName} = ${queryOptionsFnName}(${queryProperties}${queryProperties ? "," : ""}${isRequestOptions ? "options" : "queryOptions"})
|
|
547
|
+
return useCallback(async (): Promise<QueryClient> => {
|
|
548
|
+
await queryClient.${prefetchFnName}(${queryOptionsVarName})
|
|
549
|
+
return queryClient;
|
|
550
|
+
},[queryClient, ${queryOptionsVarName}]);
|
|
551
|
+
};\n`;
|
|
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> => {
|
|
553
|
+
|
|
554
|
+
const ${queryOptionsVarName} = ${queryOptionsFnName}(${queryProperties}${queryProperties ? "," : ""}${isRequestOptions ? "options" : "queryOptions"})
|
|
555
|
+
|
|
556
|
+
await queryClient.${prefetchFnName}(${hasSvelteQueryV6 ? `() => ({ ...${queryOptionsVarName} })` : queryOptionsVarName});
|
|
557
|
+
|
|
558
|
+
return queryClient;
|
|
559
|
+
}\n`;
|
|
560
|
+
};
|
|
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 }) => {
|
|
562
|
+
const queryPropDefinitions = toObjectString(props, "definition");
|
|
563
|
+
const definedInitialDataQueryPropsDefinitions = toObjectString(props.map((prop) => {
|
|
673
564
|
const regex = /* @__PURE__ */ new RegExp(`^${prop.name}\\s*\\?:`);
|
|
674
565
|
if (!regex.test(prop.definition)) return prop;
|
|
675
566
|
const definitionWithUndefined = prop.definition.replace(regex, `${prop.name}: undefined | `);
|
|
@@ -678,10 +569,10 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
678
569
|
definition: definitionWithUndefined
|
|
679
570
|
};
|
|
680
571
|
}), "definition");
|
|
681
|
-
const queryProps =
|
|
572
|
+
const queryProps = toObjectString(props, "implementation");
|
|
682
573
|
const hasInfiniteQueryParam = queryParam && queryParams?.schema.name;
|
|
683
574
|
const httpFunctionProps = queryParam ? props.map((param) => {
|
|
684
|
-
if (param.type ===
|
|
575
|
+
if (param.type === GetterPropType.NAMED_PATH_PARAMS && !isVue(outputClient)) return param.destructured;
|
|
685
576
|
return param.name === "params" ? `{...${isVue(outputClient) ? `unref(params)` : "params"}, '${queryParam}': pageParam || ${isVue(outputClient) ? `unref(params)?.['${queryParam}']` : `params?.['${queryParam}']`}}` : param.name;
|
|
686
577
|
}).join(",") : getHttpFunctionQueryProps(isVue(outputClient), httpClient, queryProperties);
|
|
687
578
|
const definedInitialDataReturnType = generateQueryReturnType({
|
|
@@ -706,7 +597,7 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
706
597
|
hasQueryV5WithDataTagError
|
|
707
598
|
});
|
|
708
599
|
const errorType = getQueryErrorType(operationName, response, httpClient, mutator);
|
|
709
|
-
const dataType = mutator?.isHook ? `ReturnType<typeof use${
|
|
600
|
+
const dataType = mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`;
|
|
710
601
|
const definedInitialDataQueryArguments = generateQueryArguments({
|
|
711
602
|
operationName,
|
|
712
603
|
mutator,
|
|
@@ -782,7 +673,7 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
782
673
|
type,
|
|
783
674
|
outputClient
|
|
784
675
|
});
|
|
785
|
-
const queryOptionsFnName =
|
|
676
|
+
const queryOptionsFnName = camel(queryKeyMutator || queryOptionsMutator || mutator?.isHook ? `use-${name}-queryOptions` : `get-${name}-queryOptions`);
|
|
786
677
|
const queryOptionsVarName = isRequestOptions ? "queryOptions" : "options";
|
|
787
678
|
const queryResultVarName = props.some((prop) => prop.name === "query") ? "_query" : "query";
|
|
788
679
|
const infiniteParam = queryParams && queryParam ? `, ${queryParams?.schema.name}['${queryParam}']` : "";
|
|
@@ -793,11 +684,11 @@ ${hookOptions}
|
|
|
793
684
|
|
|
794
685
|
const queryKey = ${queryKeyMutator ? `${queryKeyMutator.name}({ ${queryProperties} }${queryKeyMutator.hasSecondArg ? `, { url: \`${route}\`, queryOptions }` : ""});` : `${hasVueQueryV4 ? "" : "queryOptions?.queryKey ?? "}${queryKeyFnName}(${queryKeyProperties});`}
|
|
795
686
|
|
|
796
|
-
${mutator?.isHook ? `const ${operationName} = use${
|
|
687
|
+
${mutator?.isHook ? `const ${operationName} = use${pascal(operationName)}Hook();` : ""}
|
|
797
688
|
|
|
798
|
-
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});
|
|
799
690
|
|
|
800
|
-
${isVue(outputClient) ? vueUnRefParams(props.filter((prop) => prop.type ===
|
|
691
|
+
${isVue(outputClient) ? vueUnRefParams(props.filter((prop) => prop.type === GetterPropType.NAMED_PATH_PARAMS)) : ""}
|
|
801
692
|
|
|
802
693
|
${queryOptionsMutator ? `const customOptions = ${queryOptionsMutator.name}({...queryOptions, queryKey, queryFn}${queryOptionsMutator.hasSecondArg ? `, { ${queryProperties} }` : ""}${queryOptionsMutator.hasThirdArg ? `, { url: \`${route}\` }` : ""});` : ""}
|
|
803
694
|
|
|
@@ -805,38 +696,53 @@ ${hookOptions}
|
|
|
805
696
|
}`;
|
|
806
697
|
const operationPrefix = hasSvelteQueryV4 ? "create" : "use";
|
|
807
698
|
const optionalQueryClientArgument = hasQueryV5 ? ", queryClient?: QueryClient" : "";
|
|
808
|
-
const queryHookName =
|
|
699
|
+
const queryHookName = camel(`${operationPrefix}-${name}`);
|
|
809
700
|
const overrideTypes = `
|
|
810
701
|
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${definedInitialDataQueryPropsDefinitions} ${definedInitialDataQueryArguments} ${optionalQueryClientArgument}\n ): ${definedInitialDataReturnType}
|
|
811
702
|
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${queryPropDefinitions} ${undefinedInitialDataQueryArguments} ${optionalQueryClientArgument}\n ): ${returnType}
|
|
812
703
|
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${queryPropDefinitions} ${queryArguments} ${optionalQueryClientArgument}\n ): ${returnType}`;
|
|
813
|
-
const
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
704
|
+
const prefetch = generatePrefetch({
|
|
705
|
+
usePrefetch,
|
|
706
|
+
type,
|
|
707
|
+
useQuery,
|
|
708
|
+
useInfinite,
|
|
709
|
+
operationName,
|
|
710
|
+
mutator,
|
|
711
|
+
queryProps,
|
|
712
|
+
dataType,
|
|
713
|
+
errorType,
|
|
714
|
+
hasSvelteQueryV6,
|
|
715
|
+
queryArguments,
|
|
716
|
+
queryOptionsVarName,
|
|
717
|
+
queryOptionsFnName,
|
|
718
|
+
queryProperties,
|
|
719
|
+
isRequestOptions,
|
|
720
|
+
doc
|
|
721
|
+
});
|
|
722
|
+
const shouldGenerateInvalidate = useInvalidate && (type === QueryType.QUERY || type === QueryType.INFINITE || type === QueryType.SUSPENSE_QUERY && !useQuery || type === QueryType.SUSPENSE_INFINITE && !useInfinite);
|
|
723
|
+
const invalidateFnName = camel(`invalidate-${operationName}`);
|
|
817
724
|
return `
|
|
818
725
|
${queryOptionsFn}
|
|
819
726
|
|
|
820
|
-
export type ${
|
|
821
|
-
export type ${
|
|
727
|
+
export type ${pascal(name)}QueryResult = NonNullable<Awaited<ReturnType<${dataType}>>>
|
|
728
|
+
export type ${pascal(name)}QueryError = ${errorType}
|
|
822
729
|
|
|
823
|
-
${hasQueryV5 &&
|
|
730
|
+
${hasQueryV5 && OutputClient.REACT_QUERY === outputClient ? overrideTypes : ""}
|
|
824
731
|
${doc}
|
|
825
732
|
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${queryProps} ${queryArguments} ${optionalQueryClientArgument} \n ): ${returnType} {
|
|
826
733
|
|
|
827
734
|
const ${queryOptionsVarName} = ${queryOptionsFnName}(${queryProperties}${queryProperties ? "," : ""}${isRequestOptions ? "options" : "queryOptions"})
|
|
828
735
|
|
|
829
|
-
const ${queryResultVarName} = ${
|
|
736
|
+
const ${queryResultVarName} = ${camel(`${operationPrefix}-${type}`)}(${hasSvelteQueryV6 ? `() => ({ ...${queryOptionsVarName}${optionalQueryClientArgument ? ", queryClient" : ""} })` : `${queryOptionsVarName}${optionalQueryClientArgument ? ", queryClient" : ""}`}) as ${returnType};
|
|
830
737
|
|
|
831
738
|
${queryResultVarName}.queryKey = ${isVue(outputClient) ? `unref(${queryOptionsVarName})` : queryOptionsVarName}.queryKey ${isVue(outputClient) ? `as ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"}` : ""};
|
|
832
739
|
|
|
833
740
|
return ${queryResultVarName};
|
|
834
741
|
}\n
|
|
835
|
-
${
|
|
742
|
+
${prefetch}
|
|
743
|
+
${shouldGenerateInvalidate ? `${doc}export const ${invalidateFnName} = async (\n queryClient: QueryClient, ${queryProps} options?: InvalidateOptions\n ): Promise<QueryClient> => {
|
|
836
744
|
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
await queryClient.${prefetchFnName}(${hasSvelteQueryV6 ? `() => ({ ...${queryOptionsVarName} })` : queryOptionsVarName});
|
|
745
|
+
await queryClient.invalidateQueries({ queryKey: ${queryKeyFnName}(${queryKeyProperties}) }, options);
|
|
840
746
|
|
|
841
747
|
return queryClient;
|
|
842
748
|
}\n` : ""}
|
|
@@ -850,34 +756,34 @@ const generateQueryHook = async ({ queryParams, operationName, body, props: _pro
|
|
|
850
756
|
const operationQueryOptions = operations[operationId]?.query;
|
|
851
757
|
const isExactOptionalPropertyTypes = !!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
|
|
852
758
|
const queryVersion = override.query.version ?? query?.version;
|
|
853
|
-
const hasVueQueryV4 =
|
|
854
|
-
const hasSvelteQueryV4 =
|
|
855
|
-
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);
|
|
856
762
|
const hasQueryV5 = queryVersion === 5 || isQueryV5(context.output.packageJson, outputClient);
|
|
857
763
|
const hasQueryV5WithDataTagError = queryVersion === 5 || isQueryV5WithDataTagError(context.output.packageJson, outputClient);
|
|
858
764
|
const hasQueryV5WithInfiniteQueryOptionsError = queryVersion === 5 || isQueryV5WithInfiniteQueryOptionsError(context.output.packageJson, outputClient);
|
|
859
765
|
const httpClient = context.output.httpClient;
|
|
860
|
-
const doc =
|
|
766
|
+
const doc = jsDoc({
|
|
861
767
|
summary,
|
|
862
768
|
deprecated
|
|
863
769
|
});
|
|
864
770
|
let implementation = "";
|
|
865
771
|
let mutators;
|
|
866
772
|
const hasOperationQueryOption = !!(operationQueryOptions && (operationQueryOptions.useQuery || operationQueryOptions.useSuspenseQuery || operationQueryOptions.useInfinite || operationQueryOptions.useSuspenseInfiniteQuery));
|
|
867
|
-
let isQuery =
|
|
868
|
-
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;
|
|
869
775
|
if (operationQueryOptions?.useMutation !== void 0) isMutation = operationQueryOptions.useMutation;
|
|
870
|
-
if (verb !==
|
|
871
|
-
if (verb ===
|
|
776
|
+
if (verb !== Verbs.GET && isQuery) isMutation = false;
|
|
777
|
+
if (verb === Verbs.GET && isMutation) isQuery = false;
|
|
872
778
|
if (isQuery) {
|
|
873
|
-
const queryKeyMutator = query.queryKey ? await
|
|
779
|
+
const queryKeyMutator = query.queryKey ? await generateMutator({
|
|
874
780
|
output,
|
|
875
781
|
mutator: query.queryKey,
|
|
876
782
|
name: `${operationName}QueryKey`,
|
|
877
783
|
workspace: context.workspace,
|
|
878
784
|
tsconfig: context.output.tsconfig
|
|
879
785
|
}) : void 0;
|
|
880
|
-
const queryOptionsMutator = query.queryOptions ? await
|
|
786
|
+
const queryOptionsMutator = query.queryOptions ? await generateMutator({
|
|
881
787
|
output,
|
|
882
788
|
mutator: query.queryOptions,
|
|
883
789
|
name: `${operationName}QueryOptions`,
|
|
@@ -885,39 +791,39 @@ const generateQueryHook = async ({ queryParams, operationName, body, props: _pro
|
|
|
885
791
|
tsconfig: context.output.tsconfig
|
|
886
792
|
}) : void 0;
|
|
887
793
|
const queryProperties = props.map((param) => {
|
|
888
|
-
if (param.type ===
|
|
889
|
-
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;
|
|
890
796
|
}).join(",");
|
|
891
|
-
const queryKeyProperties = props.filter((prop) => prop.type !==
|
|
892
|
-
if (param.type ===
|
|
893
|
-
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;
|
|
894
800
|
}).join(",");
|
|
895
801
|
const queries = [
|
|
896
802
|
...query?.useInfinite || operationQueryOptions?.useInfinite ? [{
|
|
897
|
-
name:
|
|
803
|
+
name: camel(`${operationName}-infinite`),
|
|
898
804
|
options: query?.options,
|
|
899
805
|
type: QueryType.INFINITE,
|
|
900
806
|
queryParam: query?.useInfiniteQueryParam,
|
|
901
|
-
queryKeyFnName:
|
|
807
|
+
queryKeyFnName: camel(`get-${operationName}-infinite-query-key`)
|
|
902
808
|
}] : [],
|
|
903
809
|
...query?.useQuery || operationQueryOptions?.useQuery ? [{
|
|
904
810
|
name: operationName,
|
|
905
811
|
options: query?.options,
|
|
906
812
|
type: QueryType.QUERY,
|
|
907
|
-
queryKeyFnName:
|
|
813
|
+
queryKeyFnName: camel(`get-${operationName}-query-key`)
|
|
908
814
|
}] : [],
|
|
909
815
|
...query?.useSuspenseQuery || operationQueryOptions?.useSuspenseQuery ? [{
|
|
910
|
-
name:
|
|
816
|
+
name: camel(`${operationName}-suspense`),
|
|
911
817
|
options: query?.options,
|
|
912
818
|
type: QueryType.SUSPENSE_QUERY,
|
|
913
|
-
queryKeyFnName:
|
|
819
|
+
queryKeyFnName: camel(`get-${operationName}-query-key`)
|
|
914
820
|
}] : [],
|
|
915
821
|
...query?.useSuspenseInfiniteQuery || operationQueryOptions?.useSuspenseInfiniteQuery ? [{
|
|
916
|
-
name:
|
|
822
|
+
name: camel(`${operationName}-suspense-infinite`),
|
|
917
823
|
options: query?.options,
|
|
918
824
|
type: QueryType.SUSPENSE_INFINITE,
|
|
919
825
|
queryParam: query?.useInfiniteQueryParam,
|
|
920
|
-
queryKeyFnName:
|
|
826
|
+
queryKeyFnName: camel(`get-${operationName}-infinite-query-key`)
|
|
921
827
|
}] : []
|
|
922
828
|
];
|
|
923
829
|
const makeParamsOptional = (params$1) => {
|
|
@@ -929,11 +835,11 @@ const generateQueryHook = async ({ queryParams, operationName, body, props: _pro
|
|
|
929
835
|
};
|
|
930
836
|
const uniqueQueryOptionsByKeys = queries.filter((obj, index, self) => index === self.findIndex((t) => t.queryKeyFnName === obj.queryKeyFnName));
|
|
931
837
|
implementation += `
|
|
932
|
-
${
|
|
933
|
-
const queryKeyProps = makeParamsOptional(
|
|
934
|
-
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}\``;
|
|
935
841
|
const queryKeyIdentifier = override.query.useOperationIdAsQueryKey ? `"${operationName}"` : routeString;
|
|
936
|
-
|
|
842
|
+
return acc + `
|
|
937
843
|
${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.queryKeyFnName} = (${queryKeyProps}) => {
|
|
938
844
|
return [
|
|
939
845
|
${[
|
|
@@ -945,8 +851,7 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
|
|
|
945
851
|
] as const;
|
|
946
852
|
}
|
|
947
853
|
`;
|
|
948
|
-
|
|
949
|
-
}, "") : ""}`;
|
|
854
|
+
}, "")}`;
|
|
950
855
|
implementation += `
|
|
951
856
|
${queries.reduce((acc, queryOption) => {
|
|
952
857
|
return acc + generateQueryImplementation({
|
|
@@ -979,24 +884,25 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
|
|
|
979
884
|
doc,
|
|
980
885
|
usePrefetch: query.usePrefetch,
|
|
981
886
|
useQuery: query.useQuery,
|
|
982
|
-
useInfinite: query.useInfinite
|
|
887
|
+
useInfinite: query.useInfinite,
|
|
888
|
+
useInvalidate: query.useInvalidate
|
|
983
889
|
});
|
|
984
890
|
}, "")}
|
|
985
891
|
`;
|
|
986
892
|
mutators = queryOptionsMutator || queryKeyMutator ? [...queryOptionsMutator ? [queryOptionsMutator] : [], ...queryKeyMutator ? [queryKeyMutator] : []] : void 0;
|
|
987
893
|
}
|
|
988
894
|
if (isMutation) {
|
|
989
|
-
const mutationOptionsMutator = query.mutationOptions ? await
|
|
895
|
+
const mutationOptionsMutator = query.mutationOptions ? await generateMutator({
|
|
990
896
|
output,
|
|
991
897
|
mutator: query.mutationOptions,
|
|
992
898
|
name: `${operationName}MutationOptions`,
|
|
993
899
|
workspace: context.workspace,
|
|
994
900
|
tsconfig: context.output.tsconfig
|
|
995
901
|
}) : void 0;
|
|
996
|
-
const definitions = props.map(({ definition, type }) => type ===
|
|
997
|
-
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(",");
|
|
998
904
|
const errorType = getQueryErrorType(operationName, response, httpClient, mutator);
|
|
999
|
-
const dataType = mutator?.isHook ? `ReturnType<typeof use${
|
|
905
|
+
const dataType = mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`;
|
|
1000
906
|
const mutationOptionFnReturnType = getQueryOptionsDefinition({
|
|
1001
907
|
operationName,
|
|
1002
908
|
mutator,
|
|
@@ -1016,15 +922,14 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
|
|
|
1016
922
|
hasQueryV5WithInfiniteQueryOptionsError,
|
|
1017
923
|
httpClient
|
|
1018
924
|
});
|
|
1019
|
-
const mutationOptionsFnName =
|
|
925
|
+
const mutationOptionsFnName = camel(mutationOptionsMutator || mutator?.isHook ? `use-${operationName}-mutationOptions` : `get-${operationName}-mutationOptions`);
|
|
1020
926
|
const mutationOptionsVarName = isRequestOptions ? "mutationOptions" : "options";
|
|
1021
|
-
const hooksOptionImplementation = getHooksOptionImplementation(isRequestOptions, httpClient, (0, __orval_core.camel)(operationName), mutator);
|
|
1022
927
|
const mutationOptionsFn = `export const ${mutationOptionsFnName} = <TError = ${errorType},
|
|
1023
928
|
TContext = unknown>(${mutationArguments}): ${mutationOptionFnReturnType} => {
|
|
1024
929
|
|
|
1025
|
-
${
|
|
930
|
+
${getHooksOptionImplementation(isRequestOptions, httpClient, camel(operationName), mutator)}
|
|
1026
931
|
|
|
1027
|
-
${mutator?.isHook ? `const ${operationName} = use${
|
|
932
|
+
${mutator?.isHook ? `const ${operationName} = use${pascal(operationName)}Hook()` : ""}
|
|
1028
933
|
|
|
1029
934
|
|
|
1030
935
|
const mutationFn: MutationFunction<Awaited<ReturnType<${dataType}>>, ${definitions ? `{${definitions}}` : "void"}> = (${properties ? "props" : ""}) => {
|
|
@@ -1042,11 +947,11 @@ ${hooksOptionImplementation}
|
|
|
1042
947
|
implementation += `
|
|
1043
948
|
${mutationOptionsFn}
|
|
1044
949
|
|
|
1045
|
-
export type ${
|
|
1046
|
-
${body.definition ? `export type ${
|
|
1047
|
-
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}
|
|
1048
953
|
|
|
1049
|
-
${doc}export const ${
|
|
954
|
+
${doc}export const ${camel(`${operationPrefix}-${operationName}`)} = <TError = ${errorType},
|
|
1050
955
|
TContext = unknown>(${mutationArguments} ${optionalQueryClientArgument})${generateMutatorReturnType({
|
|
1051
956
|
outputClient,
|
|
1052
957
|
dataType,
|
|
@@ -1073,7 +978,7 @@ ${getQueryHeader(params)}
|
|
|
1073
978
|
`;
|
|
1074
979
|
};
|
|
1075
980
|
const generateQuery = async (verbOptions, options, outputClient) => {
|
|
1076
|
-
const imports =
|
|
981
|
+
const imports = generateVerbImports(verbOptions);
|
|
1077
982
|
const functionImplementation = generateQueryRequestFunction(verbOptions, options, isVue(outputClient));
|
|
1078
983
|
const { implementation: hookImplementation, mutators } = await generateQueryHook(verbOptions, options, outputClient);
|
|
1079
984
|
return {
|
|
@@ -1092,8 +997,8 @@ const builder = ({ type = "react-query", options: queryOptions, output } = {}) =
|
|
|
1092
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`);
|
|
1093
998
|
if (queryOptions) {
|
|
1094
999
|
const normalizedQueryOptions = normalizeQueryOptions(queryOptions, options.context.workspace);
|
|
1095
|
-
verbOptions.override.query =
|
|
1096
|
-
options.override.query =
|
|
1000
|
+
verbOptions.override.query = mergeDeep(normalizedQueryOptions, verbOptions.override.query);
|
|
1001
|
+
options.override.query = mergeDeep(normalizedQueryOptions, verbOptions.override.query);
|
|
1097
1002
|
}
|
|
1098
1003
|
return generateQuery(verbOptions, options, outputClient, output);
|
|
1099
1004
|
};
|
|
@@ -1106,11 +1011,5 @@ const builder = ({ type = "react-query", options: queryOptions, output } = {}) =
|
|
|
1106
1011
|
var src_default = builder;
|
|
1107
1012
|
|
|
1108
1013
|
//#endregion
|
|
1109
|
-
|
|
1110
|
-
exports.default = src_default;
|
|
1111
|
-
exports.generateQuery = generateQuery;
|
|
1112
|
-
exports.generateQueryHeader = generateQueryHeader;
|
|
1113
|
-
exports.getReactQueryDependencies = getReactQueryDependencies;
|
|
1114
|
-
exports.getSvelteQueryDependencies = getSvelteQueryDependencies;
|
|
1115
|
-
exports.getVueQueryDependencies = getVueQueryDependencies;
|
|
1014
|
+
export { builder, src_default as default, generateQuery, generateQueryHeader, getReactQueryDependencies, getSvelteQueryDependencies, getVueQueryDependencies };
|
|
1116
1015
|
//# sourceMappingURL=index.js.map
|