@orval/query 8.0.0-rc.4 → 8.0.0-rc.6
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/README.md +1 -1
- package/dist/index.d.mts +8 -3
- package/dist/index.mjs +513 -141
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { GetterPropType, OutputClient, OutputHttpClient, TEMPLATE_TAG_REGEX, Verbs, camel, compareVersions, generateFormDataAndUrlEncodedFunction, generateMutator, generateMutatorConfig, generateMutatorRequestOptions, generateOptions, generateVerbImports,
|
|
2
|
-
import { omitBy } from "remeda";
|
|
1
|
+
import { GetterPropType, OutputClient, OutputHttpClient, TEMPLATE_TAG_REGEX, Verbs, camel, compareVersions, generateFormDataAndUrlEncodedFunction, generateMutator, generateMutatorConfig, generateMutatorRequestOptions, generateOptions, generateVerbImports, getRouteAsArray, isObject, isString, isSyntheticDefaultImportsAllow, jsDoc, mergeDeep, pascal, stringify, toObjectString, upath } from "@orval/core";
|
|
3
2
|
import { generateFetchHeader, generateRequestFunction } from "@orval/fetch";
|
|
4
3
|
import chalk from "chalk";
|
|
4
|
+
import { omitBy } from "remeda";
|
|
5
5
|
|
|
6
6
|
//#region src/utils.ts
|
|
7
7
|
const normalizeQueryOptions = (queryOptions = {}, outputWorkspace) => {
|
|
@@ -12,9 +12,9 @@ const normalizeQueryOptions = (queryOptions = {}, outputWorkspace) => {
|
|
|
12
12
|
...queryOptions.useInfinite ? { useInfinite: true } : {},
|
|
13
13
|
...queryOptions.useInfiniteQueryParam ? { useInfiniteQueryParam: queryOptions.useInfiniteQueryParam } : {},
|
|
14
14
|
...queryOptions.options ? { options: queryOptions.options } : {},
|
|
15
|
-
...queryOptions
|
|
16
|
-
...queryOptions
|
|
17
|
-
...queryOptions
|
|
15
|
+
...queryOptions.queryKey ? { queryKey: normalizeMutator(outputWorkspace, queryOptions.queryKey) } : {},
|
|
16
|
+
...queryOptions.queryOptions ? { queryOptions: normalizeMutator(outputWorkspace, queryOptions.queryOptions) } : {},
|
|
17
|
+
...queryOptions.mutationOptions ? { mutationOptions: normalizeMutator(outputWorkspace, queryOptions.mutationOptions) } : {},
|
|
18
18
|
...queryOptions.signal ? { signal: true } : {},
|
|
19
19
|
...queryOptions.shouldExportMutatorHooks ? { shouldExportMutatorHooks: true } : {},
|
|
20
20
|
...queryOptions.shouldExportQueryKey ? { shouldExportQueryKey: true } : {},
|
|
@@ -29,7 +29,7 @@ const normalizeMutator = (workspace, mutator) => {
|
|
|
29
29
|
return {
|
|
30
30
|
...mutator,
|
|
31
31
|
path: upath.resolve(workspace, mutator.path),
|
|
32
|
-
default:
|
|
32
|
+
default: mutator.default ?? !mutator.name
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
if (isString(mutator)) return {
|
|
@@ -59,7 +59,16 @@ const vueUnRefParams = (props) => {
|
|
|
59
59
|
const wrapRouteParameters = (route, prepend, append) => route.replaceAll(TEMPLATE_TAG_REGEX, `\${${prepend}$1${append}}`);
|
|
60
60
|
const makeRouteSafe = (route) => wrapRouteParameters(route, "encodeURIComponent(String(", "))");
|
|
61
61
|
const isVue = (client) => OutputClient.VUE_QUERY === client;
|
|
62
|
-
const
|
|
62
|
+
const isSolid = (client) => OutputClient.SOLID_QUERY === client;
|
|
63
|
+
const isAngular = (client) => OutputClient.ANGULAR_QUERY === client;
|
|
64
|
+
const getQueryTypeForFramework = (type) => {
|
|
65
|
+
switch (type) {
|
|
66
|
+
case "suspenseQuery": return "query";
|
|
67
|
+
case "suspenseInfiniteQuery": return "infiniteQuery";
|
|
68
|
+
default: return type;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const getHasSignal = ({ overrideQuerySignal = false }) => overrideQuerySignal;
|
|
63
72
|
|
|
64
73
|
//#endregion
|
|
65
74
|
//#region src/client.ts
|
|
@@ -77,21 +86,130 @@ const AXIOS_DEPENDENCIES = [{
|
|
|
77
86
|
],
|
|
78
87
|
dependency: "axios"
|
|
79
88
|
}];
|
|
80
|
-
const
|
|
89
|
+
const ANGULAR_HTTP_DEPENDENCIES = [
|
|
90
|
+
{
|
|
91
|
+
exports: [
|
|
92
|
+
{
|
|
93
|
+
name: "HttpClient",
|
|
94
|
+
values: true
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: "HttpHeaders",
|
|
98
|
+
values: true
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
name: "HttpParams",
|
|
102
|
+
values: true
|
|
103
|
+
},
|
|
104
|
+
{ name: "HttpContext" }
|
|
105
|
+
],
|
|
106
|
+
dependency: "@angular/common/http"
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
exports: [{
|
|
110
|
+
name: "lastValueFrom",
|
|
111
|
+
values: true
|
|
112
|
+
}, {
|
|
113
|
+
name: "fromEvent",
|
|
114
|
+
values: true
|
|
115
|
+
}],
|
|
116
|
+
dependency: "rxjs"
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
exports: [{
|
|
120
|
+
name: "takeUntil",
|
|
121
|
+
values: true
|
|
122
|
+
}],
|
|
123
|
+
dependency: "rxjs/operators"
|
|
124
|
+
}
|
|
125
|
+
];
|
|
126
|
+
const generateQueryRequestFunction = (verbOptions, options, isVue$1, isAngularClient = false) => {
|
|
127
|
+
if (isAngularClient || options.context.output.httpClient === OutputHttpClient.ANGULAR) return generateAngularHttpRequestFunction(verbOptions, options);
|
|
81
128
|
return options.context.output.httpClient === OutputHttpClient.AXIOS ? generateAxiosRequestFunction(verbOptions, options, isVue$1) : generateRequestFunction(verbOptions, options);
|
|
82
129
|
};
|
|
130
|
+
const generateAngularHttpRequestFunction = ({ headers, queryParams, operationName, response, mutator, body, props, verb, formData, formUrlEncoded, override }, { route, context }) => {
|
|
131
|
+
const isRequestOptions = override.requestOptions !== false;
|
|
132
|
+
const isFormData = !override.formData.disabled;
|
|
133
|
+
const isFormUrlEncoded = override.formUrlEncoded !== false;
|
|
134
|
+
const hasSignal = getHasSignal({ overrideQuerySignal: override.query.signal });
|
|
135
|
+
const bodyForm = generateFormDataAndUrlEncodedFunction({
|
|
136
|
+
formData,
|
|
137
|
+
formUrlEncoded,
|
|
138
|
+
body,
|
|
139
|
+
isFormData,
|
|
140
|
+
isFormUrlEncoded
|
|
141
|
+
});
|
|
142
|
+
if (mutator) {
|
|
143
|
+
const mutatorConfig = generateMutatorConfig({
|
|
144
|
+
route,
|
|
145
|
+
body,
|
|
146
|
+
headers,
|
|
147
|
+
queryParams,
|
|
148
|
+
response,
|
|
149
|
+
verb,
|
|
150
|
+
isFormData,
|
|
151
|
+
isFormUrlEncoded,
|
|
152
|
+
hasSignal,
|
|
153
|
+
isExactOptionalPropertyTypes: !!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes,
|
|
154
|
+
isVue: false
|
|
155
|
+
});
|
|
156
|
+
const requestOptions = isRequestOptions ? generateMutatorRequestOptions(override.requestOptions, mutator.hasSecondArg) : "";
|
|
157
|
+
const propsImplementation = toObjectString(props, "implementation");
|
|
158
|
+
return String.raw`${override.query.shouldExportHttpClient ? "export " : ""}const ${operationName} = (\n ${propsImplementation}\n ${isRequestOptions && mutator.hasSecondArg ? `options${context.output.optionsParamRequired ? "" : "?"}: SecondParameter<typeof ${mutator.name}>,` : ""}${hasSignal ? String.raw`signal?: AbortSignal\n` : ""}) => {
|
|
159
|
+
${bodyForm}
|
|
160
|
+
return ${mutator.name}<${response.definition.success || "unknown"}>(
|
|
161
|
+
${mutatorConfig},
|
|
162
|
+
${requestOptions});
|
|
163
|
+
}
|
|
164
|
+
`;
|
|
165
|
+
}
|
|
166
|
+
const queryProps = toObjectString(props, "implementation").replace(/,\s*$/, "");
|
|
167
|
+
const dataType = response.definition.success || "unknown";
|
|
168
|
+
const hasQueryParams = queryParams?.schema.name;
|
|
169
|
+
const urlConstruction = hasQueryParams ? `const httpParams = params ? new HttpParams({ fromObject: params as Record<string, string> }) : undefined;
|
|
170
|
+
const url = \`${route}\`;` : `const url = \`${route}\`;`;
|
|
171
|
+
const httpOptions = [];
|
|
172
|
+
if (hasQueryParams) httpOptions.push("params: httpParams");
|
|
173
|
+
if (headers) httpOptions.push("headers: new HttpHeaders(headers)");
|
|
174
|
+
const optionsStr = httpOptions.length > 0 ? `, { ${httpOptions.join(", ")} }` : "";
|
|
175
|
+
let httpCall;
|
|
176
|
+
const bodyArg = body.definition ? toObjectString([body], "implementation").replace(/,\s*$/, "") : "";
|
|
177
|
+
switch (verb) {
|
|
178
|
+
case "get":
|
|
179
|
+
case "head":
|
|
180
|
+
httpCall = `http.${verb}<${dataType}>(url${optionsStr})`;
|
|
181
|
+
break;
|
|
182
|
+
case "delete":
|
|
183
|
+
httpCall = bodyArg ? `http.${verb}<${dataType}>(url, { ${httpOptions.length > 0 ? httpOptions.join(", ") + ", " : ""}body: ${bodyArg} })` : `http.${verb}<${dataType}>(url${optionsStr})`;
|
|
184
|
+
break;
|
|
185
|
+
default:
|
|
186
|
+
httpCall = `http.${verb}<${dataType}>(url, ${bodyArg || "undefined"}${optionsStr})`;
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
const optionsParam = hasSignal ? ", options?: { signal?: AbortSignal | null }" : "";
|
|
190
|
+
return `${override.query.shouldExportHttpClient ? "export " : ""}const ${operationName} = (
|
|
191
|
+
http: HttpClient,
|
|
192
|
+
${queryProps}${optionsParam}
|
|
193
|
+
): Promise<${dataType}> => {
|
|
194
|
+
${bodyForm}
|
|
195
|
+
${urlConstruction}
|
|
196
|
+
const request$ = ${httpCall};
|
|
197
|
+
if (options?.signal) {
|
|
198
|
+
return lastValueFrom(request$.pipe(takeUntil(fromEvent(options.signal, 'abort'))));
|
|
199
|
+
}
|
|
200
|
+
return lastValueFrom(request$);
|
|
201
|
+
}
|
|
202
|
+
`;
|
|
203
|
+
};
|
|
83
204
|
const generateAxiosRequestFunction = ({ headers, queryParams, operationName, response, mutator, body, props: _props, verb, formData, formUrlEncoded, override, paramsSerializer }, { route: _route, context }, isVue$1) => {
|
|
84
205
|
let props = _props;
|
|
85
206
|
let route = _route;
|
|
86
207
|
if (isVue$1) props = vueWrapTypeWithMaybeRef(_props);
|
|
87
|
-
if (context.output
|
|
208
|
+
if (context.output.urlEncodeParameters) route = makeRouteSafe(route);
|
|
88
209
|
const isRequestOptions = override.requestOptions !== false;
|
|
89
210
|
const isFormData = !override.formData.disabled;
|
|
90
211
|
const isFormUrlEncoded = override.formUrlEncoded !== false;
|
|
91
|
-
const hasSignal = getHasSignal({
|
|
92
|
-
overrideQuerySignal: override.query.signal,
|
|
93
|
-
verb
|
|
94
|
-
});
|
|
212
|
+
const hasSignal = getHasSignal({ overrideQuerySignal: override.query.signal });
|
|
95
213
|
const isExactOptionalPropertyTypes = !!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
|
|
96
214
|
const bodyForm = generateFormDataAndUrlEncodedFunction({
|
|
97
215
|
formData,
|
|
@@ -115,7 +233,7 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
115
233
|
isVue: isVue$1
|
|
116
234
|
});
|
|
117
235
|
const bodyDefinition = body.definition.replace("[]", String.raw`\[\]`);
|
|
118
|
-
const propsImplementation = mutator
|
|
236
|
+
const propsImplementation = mutator.bodyTypeName && body.definition ? toObjectString(props, "implementation").replace(new RegExp(String.raw`(\w*):\s?${bodyDefinition}`), `$1: ${mutator.bodyTypeName}<${body.definition}>`) : toObjectString(props, "implementation");
|
|
119
237
|
const requestOptions = isRequestOptions ? generateMutatorRequestOptions(override.requestOptions, mutator.hasSecondArg) : "";
|
|
120
238
|
if (mutator.isHook) {
|
|
121
239
|
const ret = `${override.query.shouldExportMutatorHooks ? "export " : ""}const use${pascal(operationName)}Hook = () => {
|
|
@@ -157,11 +275,11 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
157
275
|
queryParams,
|
|
158
276
|
response,
|
|
159
277
|
verb,
|
|
160
|
-
requestOptions: override
|
|
278
|
+
requestOptions: override.requestOptions,
|
|
161
279
|
isFormData,
|
|
162
280
|
isFormUrlEncoded,
|
|
163
281
|
paramsSerializer,
|
|
164
|
-
paramsSerializerOptions: override
|
|
282
|
+
paramsSerializerOptions: override.paramsSerializerOptions,
|
|
165
283
|
isExactOptionalPropertyTypes,
|
|
166
284
|
hasSignal,
|
|
167
285
|
isVue: isVue$1
|
|
@@ -224,7 +342,7 @@ const getQueryErrorType = (operationName, response, httpClient, mutator) => {
|
|
|
224
342
|
const getHooksOptionImplementation = (isRequestOptions, httpClient, operationName, mutator) => {
|
|
225
343
|
const options = httpClient === OutputHttpClient.AXIOS ? ", axios: axiosOptions" : ", fetch: fetchOptions";
|
|
226
344
|
return isRequestOptions ? `const mutationKey = ['${operationName}'];
|
|
227
|
-
const {mutation: mutationOptions${mutator ? mutator
|
|
345
|
+
const {mutation: mutationOptions${mutator ? mutator.hasSecondArg ? ", request: requestOptions" : "" : options}} = options ?
|
|
228
346
|
options.mutation && 'mutationKey' in options.mutation && options.mutation.mutationKey ?
|
|
229
347
|
options
|
|
230
348
|
: {...options, mutation: {...options.mutation, mutationKey}}
|
|
@@ -232,18 +350,19 @@ const {mutation: mutationOptions${mutator ? mutator?.hasSecondArg ? ", request:
|
|
|
232
350
|
};
|
|
233
351
|
const getMutationRequestArgs = (isRequestOptions, httpClient, mutator) => {
|
|
234
352
|
const options = httpClient === OutputHttpClient.AXIOS ? "axiosOptions" : "fetchOptions";
|
|
235
|
-
return isRequestOptions ? mutator ? mutator
|
|
353
|
+
return isRequestOptions ? mutator ? mutator.hasSecondArg ? "requestOptions" : "" : options : "";
|
|
236
354
|
};
|
|
237
|
-
const getHttpFunctionQueryProps = (isVue$1, httpClient, queryProperties) => {
|
|
238
|
-
|
|
239
|
-
return
|
|
355
|
+
const getHttpFunctionQueryProps = (isVue$1, httpClient, queryProperties, isAngular$1 = false) => {
|
|
356
|
+
const result = isVue$1 && httpClient === OutputHttpClient.FETCH && queryProperties ? queryProperties.split(",").map((prop) => `unref(${prop})`).join(",") : queryProperties;
|
|
357
|
+
if (isAngular$1 || httpClient === OutputHttpClient.ANGULAR) return result ? `http, ${result}` : "http";
|
|
358
|
+
return result;
|
|
240
359
|
};
|
|
241
360
|
const getQueryHeader = (params) => {
|
|
242
361
|
return params.output.httpClient === OutputHttpClient.FETCH ? generateFetchHeader(params) : "";
|
|
243
362
|
};
|
|
244
363
|
|
|
245
364
|
//#endregion
|
|
246
|
-
//#region src/
|
|
365
|
+
//#region src/dependencies.ts
|
|
247
366
|
const REACT_DEPENDENCIES = [{
|
|
248
367
|
exports: [{
|
|
249
368
|
name: "useCallback",
|
|
@@ -418,7 +537,7 @@ const REACT_QUERY_DEPENDENCIES = [{
|
|
|
418
537
|
const getReactQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient, hasTagsMutator, override) => {
|
|
419
538
|
const hasReactQuery = packageJson?.dependencies?.["react-query"] ?? packageJson?.devDependencies?.["react-query"] ?? packageJson?.peerDependencies?.["react-query"];
|
|
420
539
|
const hasReactQueryV4 = packageJson?.dependencies?.["@tanstack/react-query"] ?? packageJson?.devDependencies?.["@tanstack/react-query"] ?? packageJson?.peerDependencies?.["@tanstack/react-query"];
|
|
421
|
-
const useReactQueryV3 = override
|
|
540
|
+
const useReactQueryV3 = override.query.version === void 0 ? hasReactQuery && !hasReactQueryV4 : override.query.version <= 3;
|
|
422
541
|
return [
|
|
423
542
|
...hasGlobalMutator || hasTagsMutator ? REACT_DEPENDENCIES : [],
|
|
424
543
|
...!hasGlobalMutator && httpClient === OutputHttpClient.AXIOS ? AXIOS_DEPENDENCIES : [],
|
|
@@ -517,6 +636,85 @@ const VUE_QUERY_DEPENDENCIES = [{
|
|
|
517
636
|
],
|
|
518
637
|
dependency: "vue"
|
|
519
638
|
}];
|
|
639
|
+
const SOLID_QUERY_DEPENDENCIES = [{
|
|
640
|
+
exports: [
|
|
641
|
+
{
|
|
642
|
+
name: "createQuery",
|
|
643
|
+
values: true
|
|
644
|
+
},
|
|
645
|
+
{
|
|
646
|
+
name: "createInfiniteQuery",
|
|
647
|
+
values: true
|
|
648
|
+
},
|
|
649
|
+
{
|
|
650
|
+
name: "createMutation",
|
|
651
|
+
values: true
|
|
652
|
+
},
|
|
653
|
+
{ name: "CreateQueryOptions" },
|
|
654
|
+
{ name: "CreateInfiniteQueryOptions" },
|
|
655
|
+
{ name: "CreateMutationOptions" },
|
|
656
|
+
{ name: "QueryFunction" },
|
|
657
|
+
{ name: "MutationFunction" },
|
|
658
|
+
{ name: "CreateQueryResult" },
|
|
659
|
+
{ name: "CreateInfiniteQueryResult" },
|
|
660
|
+
{ name: "QueryKey" },
|
|
661
|
+
{ name: "InfiniteData" },
|
|
662
|
+
{ name: "CreateMutationResult" },
|
|
663
|
+
{ name: "DataTag" },
|
|
664
|
+
{ name: "QueryClient" },
|
|
665
|
+
{ name: "InvalidateOptions" }
|
|
666
|
+
],
|
|
667
|
+
dependency: "@tanstack/solid-query"
|
|
668
|
+
}];
|
|
669
|
+
const ANGULAR_QUERY_DEPENDENCIES = [{
|
|
670
|
+
exports: [
|
|
671
|
+
{
|
|
672
|
+
name: "injectQuery",
|
|
673
|
+
values: true
|
|
674
|
+
},
|
|
675
|
+
{
|
|
676
|
+
name: "injectInfiniteQuery",
|
|
677
|
+
values: true
|
|
678
|
+
},
|
|
679
|
+
{
|
|
680
|
+
name: "injectMutation",
|
|
681
|
+
values: true
|
|
682
|
+
},
|
|
683
|
+
{ name: "InjectQueryOptions" },
|
|
684
|
+
{ name: "InjectMutationOptions" },
|
|
685
|
+
{ name: "CreateQueryOptions" },
|
|
686
|
+
{ name: "CreateInfiniteQueryOptions" },
|
|
687
|
+
{ name: "CreateMutationOptions" },
|
|
688
|
+
{ name: "QueryFunction" },
|
|
689
|
+
{ name: "MutationFunction" },
|
|
690
|
+
{ name: "QueryKey" },
|
|
691
|
+
{ name: "CreateQueryResult" },
|
|
692
|
+
{ name: "CreateInfiniteQueryResult" },
|
|
693
|
+
{ name: "InfiniteData" },
|
|
694
|
+
{ name: "CreateMutationResult" },
|
|
695
|
+
{ name: "DataTag" },
|
|
696
|
+
{
|
|
697
|
+
name: "QueryClient",
|
|
698
|
+
values: true
|
|
699
|
+
},
|
|
700
|
+
{ name: "InvalidateOptions" },
|
|
701
|
+
{ name: "MutationFunctionContext" }
|
|
702
|
+
],
|
|
703
|
+
dependency: "@tanstack/angular-query-experimental"
|
|
704
|
+
}, {
|
|
705
|
+
exports: [
|
|
706
|
+
{
|
|
707
|
+
name: "inject",
|
|
708
|
+
values: true
|
|
709
|
+
},
|
|
710
|
+
{ name: "Signal" },
|
|
711
|
+
{
|
|
712
|
+
name: "computed",
|
|
713
|
+
values: true
|
|
714
|
+
}
|
|
715
|
+
],
|
|
716
|
+
dependency: "@angular/core"
|
|
717
|
+
}];
|
|
520
718
|
const isVueQueryV3 = (packageJson) => {
|
|
521
719
|
const hasVueQuery = packageJson?.dependencies?.["vue-query"] ?? packageJson?.devDependencies?.["vue-query"] ?? packageJson?.peerDependencies?.["vue-query"];
|
|
522
720
|
const hasVueQueryV4 = packageJson?.dependencies?.["@tanstack/vue-query"] ?? packageJson?.devDependencies?.["@tanstack/vue-query"] ?? packageJson?.peerDependencies?.["@tanstack/vue-query"];
|
|
@@ -530,7 +728,25 @@ const getVueQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, p
|
|
|
530
728
|
...hasVueQueryV3 ? VUE_QUERY_DEPENDENCIES_V3 : VUE_QUERY_DEPENDENCIES
|
|
531
729
|
];
|
|
532
730
|
};
|
|
731
|
+
const getSolidQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient) => {
|
|
732
|
+
return [
|
|
733
|
+
...!hasGlobalMutator && httpClient === OutputHttpClient.AXIOS ? AXIOS_DEPENDENCIES : [],
|
|
734
|
+
...hasParamsSerializerOptions ? PARAMS_SERIALIZER_DEPENDENCIES : [],
|
|
735
|
+
...SOLID_QUERY_DEPENDENCIES
|
|
736
|
+
];
|
|
737
|
+
};
|
|
738
|
+
const getAngularQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient) => {
|
|
739
|
+
const useAngularHttp = !hasGlobalMutator && httpClient === OutputHttpClient.ANGULAR;
|
|
740
|
+
const useAxios = !hasGlobalMutator && httpClient === OutputHttpClient.AXIOS;
|
|
741
|
+
return [
|
|
742
|
+
...useAngularHttp ? ANGULAR_HTTP_DEPENDENCIES : [],
|
|
743
|
+
...useAxios ? AXIOS_DEPENDENCIES : [],
|
|
744
|
+
...hasParamsSerializerOptions ? PARAMS_SERIALIZER_DEPENDENCIES : [],
|
|
745
|
+
...ANGULAR_QUERY_DEPENDENCIES
|
|
746
|
+
];
|
|
747
|
+
};
|
|
533
748
|
const isQueryV5 = (packageJson, queryClient) => {
|
|
749
|
+
if (queryClient === "angular-query") return true;
|
|
534
750
|
const version = getPackageByQueryClient(packageJson, queryClient);
|
|
535
751
|
if (!version) return false;
|
|
536
752
|
const withoutRc = version.split("-")[0];
|
|
@@ -543,12 +759,14 @@ const isQueryV6 = (packageJson, queryClient) => {
|
|
|
543
759
|
return compareVersions(withoutRc, "6.0.0");
|
|
544
760
|
};
|
|
545
761
|
const isQueryV5WithDataTagError = (packageJson, queryClient) => {
|
|
762
|
+
if (queryClient === "angular-query") return true;
|
|
546
763
|
const version = getPackageByQueryClient(packageJson, queryClient);
|
|
547
764
|
if (!version) return false;
|
|
548
765
|
const withoutRc = version.split("-")[0];
|
|
549
766
|
return compareVersions(withoutRc, "5.62.0");
|
|
550
767
|
};
|
|
551
768
|
const isQueryV5WithInfiniteQueryOptionsError = (packageJson, queryClient) => {
|
|
769
|
+
if (queryClient === "angular-query") return true;
|
|
552
770
|
const version = getPackageByQueryClient(packageJson, queryClient);
|
|
553
771
|
if (!version) return false;
|
|
554
772
|
const withoutRc = version.split("-")[0];
|
|
@@ -559,8 +777,13 @@ const getPackageByQueryClient = (packageJson, queryClient) => {
|
|
|
559
777
|
case "react-query": return packageJson?.dependencies?.["@tanstack/react-query"] ?? packageJson?.devDependencies?.["@tanstack/react-query"] ?? packageJson?.peerDependencies?.["@tanstack/react-query"];
|
|
560
778
|
case "svelte-query": return packageJson?.dependencies?.["@tanstack/svelte-query"] ?? packageJson?.devDependencies?.["@tanstack/svelte-query"] ?? packageJson?.peerDependencies?.["@tanstack/svelte-query"];
|
|
561
779
|
case "vue-query": return packageJson?.dependencies?.["@tanstack/vue-query"] ?? packageJson?.devDependencies?.["@tanstack/vue-query"] ?? packageJson?.peerDependencies?.["@tanstack/vue-query"];
|
|
780
|
+
case "angular-query": return packageJson?.dependencies?.["@tanstack/angular-query-experimental"] ?? packageJson?.devDependencies?.["@tanstack/angular-query-experimental"] ?? packageJson?.peerDependencies?.["@tanstack/angular-query-experimental"];
|
|
781
|
+
case "solid-query": return packageJson?.dependencies?.["@tanstack/solid-query"] ?? packageJson?.devDependencies?.["@tanstack/solid-query"] ?? packageJson?.peerDependencies?.["@tanstack/solid-query"];
|
|
562
782
|
}
|
|
563
783
|
};
|
|
784
|
+
|
|
785
|
+
//#endregion
|
|
786
|
+
//#region src/query-options.ts
|
|
564
787
|
const QueryType = {
|
|
565
788
|
INFINITE: "infiniteQuery",
|
|
566
789
|
QUERY: "query",
|
|
@@ -570,19 +793,19 @@ const QueryType = {
|
|
|
570
793
|
const INFINITE_QUERY_PROPERTIES = new Set(["getNextPageParam", "getPreviousPageParam"]);
|
|
571
794
|
const generateQueryOptions = ({ params, options, type, outputClient }) => {
|
|
572
795
|
if (options === false) return "";
|
|
573
|
-
const queryConfig = isObject(options) ? ` ${stringify(omitBy(options, (_, key) =>
|
|
796
|
+
const queryConfig = isObject(options) ? ` ${stringify(omitBy(options, (_, key) => type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE && INFINITE_QUERY_PROPERTIES.has(key)))?.slice(1, -1)}` : "";
|
|
574
797
|
if (params.length === 0 || isSuspenseQuery(type)) {
|
|
575
798
|
if (options) return `${queryConfig} ...queryOptions`;
|
|
576
799
|
return "...queryOptions";
|
|
577
800
|
}
|
|
578
|
-
return `${!isObject(options) || !
|
|
801
|
+
return `${!isObject(options) || !Object.hasOwn(options, "enabled") ? isVue(outputClient) ? `enabled: computed(() => !!(${params.map(({ name }) => `unref(${name})`).join(" && ")})),` : `enabled: !!(${params.map(({ name }) => name).join(" && ")}),` : ""}${queryConfig} ...queryOptions`;
|
|
579
802
|
};
|
|
580
803
|
const isSuspenseQuery = (type) => {
|
|
581
804
|
return [QueryType.SUSPENSE_INFINITE, QueryType.SUSPENSE_QUERY].includes(type);
|
|
582
805
|
};
|
|
583
|
-
const getQueryOptionsDefinition = ({ operationName, mutator, definitions, type, hasSvelteQueryV4, hasQueryV5, hasQueryV5WithInfiniteQueryOptionsError, queryParams, queryParam, isReturnType, initialData }) => {
|
|
806
|
+
const getQueryOptionsDefinition = ({ operationName, mutator, definitions, type, hasSvelteQueryV4, hasQueryV5, hasQueryV5WithInfiniteQueryOptionsError, queryParams, queryParam, isReturnType, initialData, isAngularClient }) => {
|
|
584
807
|
const isMutatorHook = mutator?.isHook;
|
|
585
|
-
const prefix = hasSvelteQueryV4 ? "
|
|
808
|
+
const prefix = !hasSvelteQueryV4 && !isAngularClient ? "Use" : "Create";
|
|
586
809
|
const partialOptions = !isReturnType && hasQueryV5;
|
|
587
810
|
if (type) {
|
|
588
811
|
const funcReturnType = `Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>`;
|
|
@@ -593,12 +816,12 @@ const getQueryOptionsDefinition = ({ operationName, mutator, definitions, type,
|
|
|
593
816
|
${funcReturnType}${hasQueryV5 && (type === QueryType.INFINITE || type === QueryType.SUSPENSE_INFINITE) && queryParam && queryParams ? `, QueryKey` : ""}
|
|
594
817
|
> , 'initialData'
|
|
595
818
|
>` : "";
|
|
596
|
-
const optionType = `${prefix}${pascal(type)}Options<${funcReturnType}, TError, TData${hasQueryV5 && (type === QueryType.INFINITE || type === QueryType.SUSPENSE_INFINITE) && queryParam && queryParams ? hasQueryV5WithInfiniteQueryOptionsError ? `, QueryKey, ${queryParams
|
|
819
|
+
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}']` : ""}>`;
|
|
597
820
|
return `${partialOptions ? "Partial<" : ""}${optionType}${partialOptions ? ">" : ""}${optionTypeInitialDataPostfix}`;
|
|
598
821
|
}
|
|
599
822
|
return `${prefix}MutationOptions<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>, TError,${definitions ? `{${definitions}}` : "void"}, TContext>`;
|
|
600
823
|
};
|
|
601
|
-
const generateQueryArguments = ({ operationName, definitions, mutator, isRequestOptions, type, hasSvelteQueryV4, hasQueryV5, hasQueryV5WithInfiniteQueryOptionsError, queryParams, queryParam, initialData, httpClient }) => {
|
|
824
|
+
const generateQueryArguments = ({ operationName, definitions, mutator, isRequestOptions, type, hasSvelteQueryV4, hasQueryV5, hasQueryV5WithInfiniteQueryOptionsError, queryParams, queryParam, initialData, httpClient, isAngularClient }) => {
|
|
602
825
|
const definition = getQueryOptionsDefinition({
|
|
603
826
|
operationName,
|
|
604
827
|
mutator,
|
|
@@ -610,15 +833,25 @@ const generateQueryArguments = ({ operationName, definitions, mutator, isRequest
|
|
|
610
833
|
queryParams,
|
|
611
834
|
queryParam,
|
|
612
835
|
isReturnType: false,
|
|
613
|
-
initialData
|
|
836
|
+
initialData,
|
|
837
|
+
isAngularClient
|
|
614
838
|
});
|
|
615
839
|
if (!isRequestOptions) return `${type ? "queryOptions" : "mutationOptions"}${initialData === "defined" ? "" : "?"}: ${definition}`;
|
|
616
840
|
const requestType = getQueryArgumentsRequestType(httpClient, mutator);
|
|
617
841
|
const isQueryRequired = initialData === "defined";
|
|
618
842
|
return `options${isQueryRequired ? "" : "?"}: { ${type ? "query" : "mutation"}${isQueryRequired ? "" : "?"}:${definition}, ${requestType}}\n`;
|
|
619
843
|
};
|
|
844
|
+
|
|
845
|
+
//#endregion
|
|
846
|
+
//#region src/return-types.ts
|
|
620
847
|
const generateQueryReturnType = ({ outputClient, type, isMutatorHook, operationName, hasVueQueryV4, hasSvelteQueryV4, hasQueryV5, hasQueryV5WithDataTagError, isInitialDataDefined }) => {
|
|
621
848
|
switch (outputClient) {
|
|
849
|
+
case OutputClient.ANGULAR_QUERY:
|
|
850
|
+
if (type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE) return `CreateQueryResult<TData, TError>`;
|
|
851
|
+
return `CreateInfiniteQueryResult<TData, TError>`;
|
|
852
|
+
case OutputClient.SOLID_QUERY:
|
|
853
|
+
if (type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE) return `CreateQueryResult<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
854
|
+
return `CreateInfiniteQueryResult<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
622
855
|
case OutputClient.SVELTE_QUERY:
|
|
623
856
|
if (!hasSvelteQueryV4) return `Use${pascal(type)}StoreResult<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>, TError, TData, QueryKey> & { queryKey: QueryKey} }`;
|
|
624
857
|
return `Create${pascal(type)}Result<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
@@ -626,16 +859,27 @@ const generateQueryReturnType = ({ outputClient, type, isMutatorHook, operationN
|
|
|
626
859
|
if (!hasVueQueryV4) return ` UseQueryReturnType<TData, TError, Use${pascal(type)}Result<TData, TError>> & { queryKey: QueryKey} }`;
|
|
627
860
|
if (type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE) return `UseQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
628
861
|
return `UseInfiniteQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
629
|
-
case OutputClient.REACT_QUERY:
|
|
630
862
|
default: return ` ${isInitialDataDefined && !isSuspenseQuery(type) ? "Defined" : ""}Use${pascal(type)}Result<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
631
863
|
}
|
|
632
864
|
};
|
|
633
865
|
const generateMutatorReturnType = ({ outputClient, dataType, variableType }) => {
|
|
866
|
+
if (outputClient === OutputClient.ANGULAR_QUERY) return `: CreateMutationResult<
|
|
867
|
+
Awaited<ReturnType<${dataType}>>,
|
|
868
|
+
TError,
|
|
869
|
+
${variableType},
|
|
870
|
+
TContext
|
|
871
|
+
>`;
|
|
634
872
|
if (outputClient === OutputClient.REACT_QUERY) return `: UseMutationResult<
|
|
635
873
|
Awaited<ReturnType<${dataType}>>,
|
|
636
874
|
TError,
|
|
637
875
|
${variableType},
|
|
638
876
|
TContext
|
|
877
|
+
>`;
|
|
878
|
+
if (outputClient === OutputClient.SOLID_QUERY) return `: CreateMutationResult<
|
|
879
|
+
Awaited<ReturnType<${dataType}>>,
|
|
880
|
+
TError,
|
|
881
|
+
${variableType},
|
|
882
|
+
TContext
|
|
639
883
|
>`;
|
|
640
884
|
if (outputClient === OutputClient.SVELTE_QUERY) return `: CreateMutationResult<
|
|
641
885
|
Awaited<ReturnType<${dataType}>>,
|
|
@@ -659,6 +903,154 @@ const getQueryFnArguments = ({ hasQueryParam, hasSignal }) => {
|
|
|
659
903
|
}
|
|
660
904
|
return "{ signal }";
|
|
661
905
|
};
|
|
906
|
+
const getQueryReturnStatement = ({ outputClient, hasSvelteQueryV4, hasSvelteQueryV6, hasQueryV5, hasQueryV5WithDataTagError, queryResultVarName, queryOptionsVarName }) => {
|
|
907
|
+
if (isAngular(outputClient)) return `return ${queryResultVarName};`;
|
|
908
|
+
if (isVue(outputClient)) return `${queryResultVarName}.queryKey = unref(${queryOptionsVarName}).queryKey as ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"};
|
|
909
|
+
|
|
910
|
+
return ${queryResultVarName};`;
|
|
911
|
+
if (hasSvelteQueryV6) return `return ${queryResultVarName}`;
|
|
912
|
+
if (hasSvelteQueryV4) return `${queryResultVarName}.queryKey = ${queryOptionsVarName}.queryKey;
|
|
913
|
+
|
|
914
|
+
return ${queryResultVarName};`;
|
|
915
|
+
return `return { ...${queryResultVarName}, queryKey: ${queryOptionsVarName}.queryKey };`;
|
|
916
|
+
};
|
|
917
|
+
|
|
918
|
+
//#endregion
|
|
919
|
+
//#region src/mutation-generator.ts
|
|
920
|
+
const normalizeTarget = (target) => typeof target === "string" ? { query: target } : target;
|
|
921
|
+
const serializeTarget = (target) => JSON.stringify({
|
|
922
|
+
query: target.query,
|
|
923
|
+
params: target.params ?? []
|
|
924
|
+
});
|
|
925
|
+
const generateVariableRef = (varName) => {
|
|
926
|
+
const parts = varName.split(".");
|
|
927
|
+
if (parts.length === 1) return `variables.${varName}`;
|
|
928
|
+
return `variables.${parts[0]}?.${parts.slice(1).join("?.")}`;
|
|
929
|
+
};
|
|
930
|
+
const generateParamArgs = (params) => {
|
|
931
|
+
if (Array.isArray(params)) return params.map((v) => generateVariableRef(v)).join(", ");
|
|
932
|
+
return Object.values(params).map((v) => generateVariableRef(v)).join(", ");
|
|
933
|
+
};
|
|
934
|
+
const generateInvalidateCall = (target) => {
|
|
935
|
+
return ` queryClient.invalidateQueries({ queryKey: ${camel(`get-${target.query}-query-key`)}(${target.params ? generateParamArgs(target.params) : ""}) });`;
|
|
936
|
+
};
|
|
937
|
+
const generateMutationHook = async ({ verbOptions, options, outputClient, hasQueryV5, hasQueryV5WithInfiniteQueryOptionsError, hasSvelteQueryV4, hasSvelteQueryV6, isRequestOptions, httpClient, doc, isAngularHttp }) => {
|
|
938
|
+
const { operationName, body, props, mutator, response, operationId, override } = verbOptions;
|
|
939
|
+
const { route, context, output } = options;
|
|
940
|
+
const query = override.query;
|
|
941
|
+
const mutationOptionsMutator = query.mutationOptions ? await generateMutator({
|
|
942
|
+
output,
|
|
943
|
+
mutator: query.mutationOptions,
|
|
944
|
+
name: `${operationName}MutationOptions`,
|
|
945
|
+
workspace: context.workspace,
|
|
946
|
+
tsconfig: context.output.tsconfig
|
|
947
|
+
}) : void 0;
|
|
948
|
+
const definitions = props.map(({ definition, type }) => type === GetterPropType.BODY ? mutator?.bodyTypeName ? `data: ${mutator.bodyTypeName}<${body.definition}>` : `data: ${body.definition}` : definition).join(";");
|
|
949
|
+
const properties = props.map(({ name, type }) => type === GetterPropType.BODY ? "data" : name).join(",");
|
|
950
|
+
const errorType = getQueryErrorType(operationName, response, httpClient, mutator);
|
|
951
|
+
const dataType = mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`;
|
|
952
|
+
const isAngularClient = isAngular(outputClient);
|
|
953
|
+
const mutationOptionFnReturnType = getQueryOptionsDefinition({
|
|
954
|
+
operationName,
|
|
955
|
+
mutator,
|
|
956
|
+
definitions,
|
|
957
|
+
hasSvelteQueryV4,
|
|
958
|
+
hasQueryV5,
|
|
959
|
+
hasQueryV5WithInfiniteQueryOptionsError,
|
|
960
|
+
isReturnType: true,
|
|
961
|
+
isAngularClient
|
|
962
|
+
});
|
|
963
|
+
const mutationArguments = generateQueryArguments({
|
|
964
|
+
operationName,
|
|
965
|
+
definitions,
|
|
966
|
+
mutator,
|
|
967
|
+
isRequestOptions,
|
|
968
|
+
hasSvelteQueryV4,
|
|
969
|
+
hasQueryV5,
|
|
970
|
+
hasQueryV5WithInfiniteQueryOptionsError,
|
|
971
|
+
httpClient,
|
|
972
|
+
isAngularClient
|
|
973
|
+
});
|
|
974
|
+
const mutationOptionsFnName = camel(mutationOptionsMutator || mutator?.isHook ? `use-${operationName}-mutationOptions` : `get-${operationName}-mutationOptions`);
|
|
975
|
+
const hooksOptionImplementation = getHooksOptionImplementation(isRequestOptions, httpClient, camel(operationName), mutator);
|
|
976
|
+
const invalidatesConfig = (query.mutationInvalidates ?? []).filter((rule) => rule.onMutations.includes(operationName)).flatMap((rule) => rule.invalidates).map((t) => normalizeTarget(t));
|
|
977
|
+
const seenTargets = /* @__PURE__ */ new Set();
|
|
978
|
+
const uniqueInvalidates = invalidatesConfig.filter((target) => {
|
|
979
|
+
const key = serializeTarget(target);
|
|
980
|
+
if (seenTargets.has(key)) return false;
|
|
981
|
+
seenTargets.add(key);
|
|
982
|
+
return true;
|
|
983
|
+
});
|
|
984
|
+
const hasInvalidation = uniqueInvalidates.length > 0 && isAngularClient;
|
|
985
|
+
const mutationOptionsFn = `export const ${mutationOptionsFnName} = <TError = ${errorType},
|
|
986
|
+
TContext = unknown>(${mutationArguments}): ${mutationOptionFnReturnType} => {
|
|
987
|
+
|
|
988
|
+
${hooksOptionImplementation}
|
|
989
|
+
${isAngularHttp ? " const http = inject(HttpClient);" : ""}
|
|
990
|
+
${hasInvalidation ? " const queryClient = inject(QueryClient);" : ""}
|
|
991
|
+
|
|
992
|
+
${mutator?.isHook ? `const ${operationName} = use${pascal(operationName)}Hook()` : ""}
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
const mutationFn: MutationFunction<Awaited<ReturnType<${dataType}>>, ${definitions ? `{${definitions}}` : "void"}> = (${properties ? "props" : ""}) => {
|
|
996
|
+
${properties ? `const {${properties}} = props ?? {};` : ""}
|
|
997
|
+
|
|
998
|
+
return ${operationName}(${isAngularHttp ? "http, " : ""}${properties}${properties ? "," : ""}${getMutationRequestArgs(isRequestOptions, httpClient, mutator)})
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
${hasInvalidation ? ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${definitions ? `{${definitions}}` : "void"}, onMutateResult: TContext, context: MutationFunctionContext) => {
|
|
1002
|
+
${uniqueInvalidates.map((t) => generateInvalidateCall(t)).join("\n")}
|
|
1003
|
+
mutationOptions?.onSuccess?.(data, variables, onMutateResult, context);
|
|
1004
|
+
};` : ""}
|
|
1005
|
+
|
|
1006
|
+
${mutationOptionsMutator ? `const customOptions = ${mutationOptionsMutator.name}({...mutationOptions, mutationFn}${mutationOptionsMutator.hasSecondArg ? `, { url: \`${route.replaceAll("/${", "/{")}\` }` : ""}${mutationOptionsMutator.hasThirdArg ? `, { operationId: '${operationId}', operationName: '${operationName}' }` : ""});` : ""}
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
return ${mutationOptionsMutator ? "customOptions" : hasInvalidation ? "{ mutationFn, onSuccess, ...mutationOptions }" : "{ mutationFn, ...mutationOptions }"}}`;
|
|
1010
|
+
const operationPrefix = getFrameworkPrefix(hasSvelteQueryV4, isAngular(outputClient), isSolid(outputClient));
|
|
1011
|
+
const optionalQueryClientArgument = hasQueryV5 && !isAngular(outputClient) ? ", queryClient?: QueryClient" : "";
|
|
1012
|
+
const mutationImplementation = `${mutationOptionsFnName}(${isRequestOptions ? "options" : "mutationOptions"})`;
|
|
1013
|
+
const mutationOptionsVarName = camel(`${operationName}-mutation-options`);
|
|
1014
|
+
return {
|
|
1015
|
+
implementation: `
|
|
1016
|
+
${mutationOptionsFn}
|
|
1017
|
+
|
|
1018
|
+
export type ${pascal(operationName)}MutationResult = NonNullable<Awaited<ReturnType<${dataType}>>>
|
|
1019
|
+
${body.definition ? `export type ${pascal(operationName)}MutationBody = ${mutator?.bodyTypeName ? `${mutator.bodyTypeName}<${body.definition}>` : body.definition}` : ""}
|
|
1020
|
+
export type ${pascal(operationName)}MutationError = ${errorType}
|
|
1021
|
+
|
|
1022
|
+
${doc}export const ${camel(`${operationPrefix}-${operationName}`)} = <TError = ${errorType},
|
|
1023
|
+
TContext = unknown>(${mutationArguments} ${optionalQueryClientArgument})${generateMutatorReturnType({
|
|
1024
|
+
outputClient,
|
|
1025
|
+
dataType,
|
|
1026
|
+
variableType: definitions ? `{${definitions}}` : "void"
|
|
1027
|
+
})} => {
|
|
1028
|
+
${isAngular(outputClient) ? ` const ${mutationOptionsVarName} = ${mutationImplementation};
|
|
1029
|
+
|
|
1030
|
+
return ${operationPrefix}Mutation(() => ${mutationOptionsVarName});` : ` return ${operationPrefix}Mutation(${hasSvelteQueryV6 ? `() => ({ ...${mutationImplementation}${optionalQueryClientArgument ? ", queryClient" : ""} })` : `${mutationImplementation}${optionalQueryClientArgument ? ", queryClient" : ""}`});`}
|
|
1031
|
+
}
|
|
1032
|
+
`,
|
|
1033
|
+
mutators: mutationOptionsMutator ? [mutationOptionsMutator] : void 0
|
|
1034
|
+
};
|
|
1035
|
+
};
|
|
1036
|
+
|
|
1037
|
+
//#endregion
|
|
1038
|
+
//#region src/query-generator.ts
|
|
1039
|
+
/**
|
|
1040
|
+
* Get framework-aware prefix for hook names and type definitions
|
|
1041
|
+
* @param hasSvelteQueryV4 - Whether using Svelte Query v4
|
|
1042
|
+
* @param isAngularClient - Whether using Angular client
|
|
1043
|
+
* @param isSolidClient - Whether using Solid Query client
|
|
1044
|
+
* @param capitalize - Whether to capitalize the prefix (for type definitions)
|
|
1045
|
+
* @returns The appropriate prefix string
|
|
1046
|
+
*/
|
|
1047
|
+
const getFrameworkPrefix = (hasSvelteQueryV4, isAngularClient, isSolidClient, capitalize = false) => {
|
|
1048
|
+
let prefix;
|
|
1049
|
+
if (hasSvelteQueryV4 || isSolidClient) prefix = "create";
|
|
1050
|
+
else if (isAngularClient) prefix = "inject";
|
|
1051
|
+
else prefix = "use";
|
|
1052
|
+
return capitalize ? prefix.charAt(0).toUpperCase() + prefix.slice(1) : prefix;
|
|
1053
|
+
};
|
|
662
1054
|
const generatePrefetch = ({ usePrefetch, type, useQuery, useInfinite, operationName, mutator, doc, queryProps, dataType, errorType, queryArguments, queryOptionsVarName, queryOptionsFnName, queryProperties, isRequestOptions, hasSvelteQueryV6 }) => {
|
|
663
1055
|
if (!(usePrefetch && (type === QueryType.QUERY || type === QueryType.INFINITE || type === QueryType.SUSPENSE_QUERY && !useQuery || type === QueryType.SUSPENSE_INFINITE && !useInfinite))) return "";
|
|
664
1056
|
const prefetchType = type === QueryType.QUERY || type === QueryType.SUSPENSE_QUERY ? "query" : "infinite-query";
|
|
@@ -683,7 +1075,7 @@ const generatePrefetch = ({ usePrefetch, type, useQuery, useInfinite, operationN
|
|
|
683
1075
|
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 }) => {
|
|
684
1076
|
const queryPropDefinitions = toObjectString(props, "definition");
|
|
685
1077
|
const definedInitialDataQueryPropsDefinitions = toObjectString(props.map((prop) => {
|
|
686
|
-
const regex =
|
|
1078
|
+
const regex = new RegExp(String.raw`^${prop.name}\s*\?:`);
|
|
687
1079
|
if (!regex.test(prop.definition)) return prop;
|
|
688
1080
|
const definitionWithUndefined = prop.definition.replace(regex, `${prop.name}: undefined | `);
|
|
689
1081
|
return {
|
|
@@ -693,10 +1085,12 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
693
1085
|
}), "definition");
|
|
694
1086
|
const queryProps = toObjectString(props, "implementation");
|
|
695
1087
|
const hasInfiniteQueryParam = queryParam && queryParams?.schema.name;
|
|
696
|
-
const
|
|
1088
|
+
const isAngularHttp = isAngular(outputClient) || httpClient === OutputHttpClient.ANGULAR;
|
|
1089
|
+
let httpFunctionProps = queryParam ? props.map((param) => {
|
|
697
1090
|
if (param.type === GetterPropType.NAMED_PATH_PARAMS && !isVue(outputClient)) return param.destructured;
|
|
698
1091
|
return param.name === "params" ? `{...${isVue(outputClient) ? `unref(params)` : "params"}, '${queryParam}': pageParam || ${isVue(outputClient) ? `unref(params)?.['${queryParam}']` : `params?.['${queryParam}']`}}` : param.name;
|
|
699
|
-
}).join(",") : getHttpFunctionQueryProps(isVue(outputClient), httpClient, queryProperties);
|
|
1092
|
+
}).join(",") : getHttpFunctionQueryProps(isVue(outputClient), httpClient, queryProperties, isAngularHttp);
|
|
1093
|
+
if (queryParam && isAngularHttp) httpFunctionProps = httpFunctionProps ? `http, ${httpFunctionProps}` : "http";
|
|
700
1094
|
const definedInitialDataReturnType = generateQueryReturnType({
|
|
701
1095
|
outputClient,
|
|
702
1096
|
type,
|
|
@@ -732,7 +1126,8 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
732
1126
|
queryParams,
|
|
733
1127
|
queryParam,
|
|
734
1128
|
initialData: "defined",
|
|
735
|
-
httpClient
|
|
1129
|
+
httpClient,
|
|
1130
|
+
isAngularClient: isAngular(outputClient)
|
|
736
1131
|
});
|
|
737
1132
|
const undefinedInitialDataQueryArguments = generateQueryArguments({
|
|
738
1133
|
operationName,
|
|
@@ -746,7 +1141,8 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
746
1141
|
queryParams,
|
|
747
1142
|
queryParam,
|
|
748
1143
|
initialData: "undefined",
|
|
749
|
-
httpClient
|
|
1144
|
+
httpClient,
|
|
1145
|
+
isAngularClient: isAngular(outputClient)
|
|
750
1146
|
});
|
|
751
1147
|
const queryArguments = generateQueryArguments({
|
|
752
1148
|
operationName,
|
|
@@ -759,7 +1155,8 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
759
1155
|
hasQueryV5WithInfiniteQueryOptionsError,
|
|
760
1156
|
queryParams,
|
|
761
1157
|
queryParam,
|
|
762
|
-
httpClient
|
|
1158
|
+
httpClient,
|
|
1159
|
+
isAngularClient: isAngular(outputClient)
|
|
763
1160
|
});
|
|
764
1161
|
const queryOptions = getQueryOptions({
|
|
765
1162
|
isRequestOptions,
|
|
@@ -787,7 +1184,8 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
787
1184
|
hasQueryV5WithInfiniteQueryOptionsError,
|
|
788
1185
|
queryParams,
|
|
789
1186
|
queryParam,
|
|
790
|
-
isReturnType: true
|
|
1187
|
+
isReturnType: true,
|
|
1188
|
+
isAngularClient: isAngular(outputClient)
|
|
791
1189
|
});
|
|
792
1190
|
const queryOptionsImp = generateQueryOptions({
|
|
793
1191
|
params,
|
|
@@ -798,26 +1196,27 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
798
1196
|
const queryOptionsFnName = camel(queryKeyMutator || queryOptionsMutator || mutator?.isHook ? `use-${name}-queryOptions` : `get-${name}-queryOptions`);
|
|
799
1197
|
const queryOptionsVarName = isRequestOptions ? "queryOptions" : "options";
|
|
800
1198
|
const queryResultVarName = props.some((prop) => prop.name === "query") ? "_query" : "query";
|
|
801
|
-
const infiniteParam = queryParams && queryParam ? `, ${queryParams
|
|
1199
|
+
const infiniteParam = queryParams && queryParam ? `, ${queryParams.schema.name}['${queryParam}']` : "";
|
|
802
1200
|
const TData = hasQueryV5 && (type === QueryType.INFINITE || type === QueryType.SUSPENSE_INFINITE) ? `InfiniteData<Awaited<ReturnType<${dataType}>>${infiniteParam}>` : `Awaited<ReturnType<${dataType}>>`;
|
|
803
1201
|
const queryOptionsFn = `export const ${queryOptionsFnName} = <TData = ${TData}, TError = ${errorType}>(${queryProps} ${queryArguments}) => {
|
|
804
1202
|
|
|
805
1203
|
${hookOptions}
|
|
1204
|
+
${isAngularHttp ? " const http = inject(HttpClient);" : ""}
|
|
806
1205
|
|
|
807
1206
|
const queryKey = ${queryKeyMutator ? `${queryKeyMutator.name}({ ${queryProperties} }${queryKeyMutator.hasSecondArg ? `, { url: \`${route}\`, queryOptions }` : ""});` : `${hasVueQueryV4 ? "" : "queryOptions?.queryKey ?? "}${queryKeyFnName}(${queryKeyProperties});`}
|
|
808
1207
|
|
|
809
1208
|
${mutator?.isHook ? `const ${operationName} = use${pascal(operationName)}Hook();` : ""}
|
|
810
1209
|
|
|
811
|
-
const queryFn: QueryFunction<Awaited<ReturnType<${mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>${hasQueryV5 && hasInfiniteQueryParam ? `, QueryKey, ${queryParams
|
|
1210
|
+
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});
|
|
812
1211
|
|
|
813
1212
|
${isVue(outputClient) ? vueUnRefParams(props.filter((prop) => prop.type === GetterPropType.NAMED_PATH_PARAMS)) : ""}
|
|
814
1213
|
|
|
815
1214
|
${queryOptionsMutator ? `const customOptions = ${queryOptionsMutator.name}({...queryOptions, queryKey, queryFn}${queryOptionsMutator.hasSecondArg ? `, { ${queryProperties} }` : ""}${queryOptionsMutator.hasThirdArg ? `, { url: \`${route}\` }` : ""});` : ""}
|
|
816
1215
|
|
|
817
|
-
return ${queryOptionsMutator ? "customOptions" : `{ queryKey, queryFn, ${queryOptionsImp}}`} as ${queryOptionFnReturnType} ${isVue(outputClient) ? "" : `& { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`}
|
|
1216
|
+
return ${queryOptionsMutator ? "customOptions" : `{ queryKey, queryFn, ${queryOptionsImp}}`} as ${queryOptionFnReturnType} ${isVue(outputClient) || isAngular(outputClient) ? "" : `& { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`}
|
|
818
1217
|
}`;
|
|
819
|
-
const operationPrefix = hasSvelteQueryV4
|
|
820
|
-
const optionalQueryClientArgument = hasQueryV5 ? ", queryClient?: QueryClient" : "";
|
|
1218
|
+
const operationPrefix = getFrameworkPrefix(hasSvelteQueryV4, isAngular(outputClient), isSolid(outputClient));
|
|
1219
|
+
const optionalQueryClientArgument = hasSvelteQueryV6 ? `, queryClient?: () => QueryClient` : hasQueryV5 && !isAngular(outputClient) ? ", queryClient?: QueryClient" : "";
|
|
821
1220
|
const queryHookName = camel(`${operationPrefix}-${name}`);
|
|
822
1221
|
const overrideTypes = `
|
|
823
1222
|
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${definedInitialDataQueryPropsDefinitions} ${definedInitialDataQueryArguments} ${optionalQueryClientArgument}\n ): ${definedInitialDataReturnType}
|
|
@@ -830,7 +1229,7 @@ export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${q
|
|
|
830
1229
|
useInfinite,
|
|
831
1230
|
operationName,
|
|
832
1231
|
mutator,
|
|
833
|
-
queryProps,
|
|
1232
|
+
queryProps: hasSvelteQueryV6 ? queryProps.replace(":", ": () => ") : queryProps,
|
|
834
1233
|
dataType,
|
|
835
1234
|
errorType,
|
|
836
1235
|
hasSvelteQueryV6,
|
|
@@ -851,15 +1250,27 @@ export type ${pascal(name)}QueryError = ${errorType}
|
|
|
851
1250
|
|
|
852
1251
|
${hasQueryV5 && OutputClient.REACT_QUERY === outputClient ? overrideTypes : ""}
|
|
853
1252
|
${doc}
|
|
854
|
-
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${
|
|
855
|
-
|
|
856
|
-
|
|
1253
|
+
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${hasSvelteQueryV6 ? toObjectString(props.map((p) => ({
|
|
1254
|
+
...p,
|
|
1255
|
+
definition: p.definition.replace(":", ": () => ")
|
|
1256
|
+
})), "definition") : queryProps} ${hasSvelteQueryV6 ? queryArguments.replace(":", ": () => (") + ")" : queryArguments} ${optionalQueryClientArgument} \n ): ${returnType} {
|
|
857
1257
|
|
|
858
|
-
const ${
|
|
1258
|
+
${hasSvelteQueryV6 ? "" : `const ${queryOptionsVarName} = ${queryOptionsFnName}(${queryProperties}${queryProperties ? "," : ""}${isRequestOptions ? "options" : "queryOptions"})`}
|
|
859
1259
|
|
|
860
|
-
${queryResultVarName}
|
|
1260
|
+
const ${queryResultVarName} = ${camel(`${operationPrefix}-${isAngular(outputClient) || hasSvelteQueryV4 ? getQueryTypeForFramework(type) : type}`)}(${isAngular(outputClient) ? `() => ${queryOptionsVarName}` : hasSvelteQueryV6 ? `() => ${queryOptionsFnName}(${toObjectString(props.map((p) => ({
|
|
1261
|
+
...p,
|
|
1262
|
+
name: p.default || !p.required ? `${p.name}?.()` : `${p.name}()`
|
|
1263
|
+
})), "name")}${isRequestOptions ? "options?.()" : "queryOptions?.()"})` : `${queryOptionsVarName}${!isAngular(outputClient) && optionalQueryClientArgument ? ", queryClient" : ""}`}${hasSvelteQueryV6 ? `, queryClient` : ""}) as ${returnType};
|
|
861
1264
|
|
|
862
|
-
|
|
1265
|
+
${getQueryReturnStatement({
|
|
1266
|
+
outputClient,
|
|
1267
|
+
hasSvelteQueryV4,
|
|
1268
|
+
hasSvelteQueryV6,
|
|
1269
|
+
hasQueryV5,
|
|
1270
|
+
hasQueryV5WithDataTagError,
|
|
1271
|
+
queryResultVarName,
|
|
1272
|
+
queryOptionsVarName
|
|
1273
|
+
})}
|
|
863
1274
|
}\n
|
|
864
1275
|
${prefetch}
|
|
865
1276
|
${shouldGenerateInvalidate ? `${doc}export const ${invalidateFnName} = async (\n queryClient: QueryClient, ${queryProps} options?: InvalidateOptions\n ): Promise<QueryClient> => {
|
|
@@ -870,14 +1281,16 @@ ${shouldGenerateInvalidate ? `${doc}export const ${invalidateFnName} = async (\n
|
|
|
870
1281
|
}\n` : ""}
|
|
871
1282
|
`;
|
|
872
1283
|
};
|
|
873
|
-
const generateQueryHook = async (
|
|
1284
|
+
const generateQueryHook = async (verbOptions, options, outputClient) => {
|
|
1285
|
+
const { queryParams, operationName, body, props: _props, verb, params, override, mutator, response, operationId, summary, deprecated } = verbOptions;
|
|
1286
|
+
const { route, override: { operations }, context, output } = options;
|
|
874
1287
|
let props = _props;
|
|
875
1288
|
if (isVue(outputClient)) props = vueWrapTypeWithMaybeRef(_props);
|
|
876
|
-
const query = override
|
|
877
|
-
const isRequestOptions = override
|
|
1289
|
+
const query = override.query;
|
|
1290
|
+
const isRequestOptions = override.requestOptions !== false;
|
|
878
1291
|
const operationQueryOptions = operations[operationId]?.query;
|
|
879
1292
|
const isExactOptionalPropertyTypes = !!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
|
|
880
|
-
const queryVersion =
|
|
1293
|
+
const queryVersion = query.version;
|
|
881
1294
|
const hasVueQueryV4 = OutputClient.VUE_QUERY === outputClient && (!isVueQueryV3(context.output.packageJson) || queryVersion === 4);
|
|
882
1295
|
const hasSvelteQueryV4 = OutputClient.SVELTE_QUERY === outputClient && (!isSvelteQueryV3(context.output.packageJson) || queryVersion === 4);
|
|
883
1296
|
const hasSvelteQueryV6 = OutputClient.SVELTE_QUERY === outputClient && isSvelteQueryV6(context.output.packageJson);
|
|
@@ -885,14 +1298,25 @@ const generateQueryHook = async ({ queryParams, operationName, body, props: _pro
|
|
|
885
1298
|
const hasQueryV5WithDataTagError = queryVersion === 5 || isQueryV5WithDataTagError(context.output.packageJson, outputClient);
|
|
886
1299
|
const hasQueryV5WithInfiniteQueryOptionsError = queryVersion === 5 || isQueryV5WithInfiniteQueryOptionsError(context.output.packageJson, outputClient);
|
|
887
1300
|
const httpClient = context.output.httpClient;
|
|
1301
|
+
const isAngularHttp = isAngular(outputClient) || httpClient === OutputHttpClient.ANGULAR;
|
|
888
1302
|
const doc = jsDoc({
|
|
889
1303
|
summary,
|
|
890
1304
|
deprecated
|
|
891
1305
|
});
|
|
892
1306
|
let implementation = "";
|
|
893
1307
|
let mutators;
|
|
894
|
-
const hasOperationQueryOption =
|
|
895
|
-
|
|
1308
|
+
const hasOperationQueryOption = [
|
|
1309
|
+
operationQueryOptions?.useQuery,
|
|
1310
|
+
operationQueryOptions?.useSuspenseQuery,
|
|
1311
|
+
operationQueryOptions?.useInfinite,
|
|
1312
|
+
operationQueryOptions?.useSuspenseInfiniteQuery
|
|
1313
|
+
].some(Boolean);
|
|
1314
|
+
let isQuery = Verbs.GET === verb && [
|
|
1315
|
+
override.query.useQuery,
|
|
1316
|
+
override.query.useSuspenseQuery,
|
|
1317
|
+
override.query.useInfinite,
|
|
1318
|
+
override.query.useSuspenseInfiniteQuery
|
|
1319
|
+
].some(Boolean) || hasOperationQueryOption;
|
|
896
1320
|
let isMutation = override.query.useMutation && verb !== Verbs.GET;
|
|
897
1321
|
if (operationQueryOptions?.useMutation !== void 0) isMutation = operationQueryOptions.useMutation;
|
|
898
1322
|
if (verb !== Verbs.GET && isQuery) isMutation = false;
|
|
@@ -921,36 +1345,36 @@ const generateQueryHook = async ({ queryParams, operationName, body, props: _pro
|
|
|
921
1345
|
return param.type === GetterPropType.BODY ? body.implementation : param.name;
|
|
922
1346
|
}).join(",");
|
|
923
1347
|
const queries = [
|
|
924
|
-
...query
|
|
1348
|
+
...query.useInfinite || operationQueryOptions?.useInfinite ? [{
|
|
925
1349
|
name: camel(`${operationName}-infinite`),
|
|
926
|
-
options: query
|
|
1350
|
+
options: query.options,
|
|
927
1351
|
type: QueryType.INFINITE,
|
|
928
|
-
queryParam: query
|
|
1352
|
+
queryParam: query.useInfiniteQueryParam,
|
|
929
1353
|
queryKeyFnName: camel(`get-${operationName}-infinite-query-key`)
|
|
930
1354
|
}] : [],
|
|
931
|
-
...query
|
|
1355
|
+
...query.useQuery || operationQueryOptions?.useQuery ? [{
|
|
932
1356
|
name: operationName,
|
|
933
|
-
options: query
|
|
1357
|
+
options: query.options,
|
|
934
1358
|
type: QueryType.QUERY,
|
|
935
1359
|
queryKeyFnName: camel(`get-${operationName}-query-key`)
|
|
936
1360
|
}] : [],
|
|
937
|
-
...query
|
|
1361
|
+
...query.useSuspenseQuery || operationQueryOptions?.useSuspenseQuery ? [{
|
|
938
1362
|
name: camel(`${operationName}-suspense`),
|
|
939
|
-
options: query
|
|
1363
|
+
options: query.options,
|
|
940
1364
|
type: QueryType.SUSPENSE_QUERY,
|
|
941
1365
|
queryKeyFnName: camel(`get-${operationName}-query-key`)
|
|
942
1366
|
}] : [],
|
|
943
|
-
...query
|
|
1367
|
+
...query.useSuspenseInfiniteQuery || operationQueryOptions?.useSuspenseInfiniteQuery ? [{
|
|
944
1368
|
name: camel(`${operationName}-suspense-infinite`),
|
|
945
|
-
options: query
|
|
1369
|
+
options: query.options,
|
|
946
1370
|
type: QueryType.SUSPENSE_INFINITE,
|
|
947
|
-
queryParam: query
|
|
1371
|
+
queryParam: query.useInfiniteQueryParam,
|
|
948
1372
|
queryKeyFnName: camel(`get-${operationName}-infinite-query-key`)
|
|
949
1373
|
}] : []
|
|
950
1374
|
];
|
|
951
1375
|
const makeParamsOptional = (params$1) => {
|
|
952
1376
|
if (!params$1) return "";
|
|
953
|
-
return params$1.replaceAll(/(\w+)(\?)?:\s*([^=,}]*?)\s*(=\s*[^,}]*)?([,}]|$)/g, (
|
|
1377
|
+
return params$1.replaceAll(/(\w+)(\?)?:\s*([^=,}]*?)\s*(=\s*[^,}]*)?([,}]|$)/g, (_match, paramName, _optionalMarker, type, defaultValue, suffix) => {
|
|
954
1378
|
if (defaultValue) return `${paramName}: ${type.trim()}${defaultValue}${suffix}`;
|
|
955
1379
|
return `${paramName}?: ${type.trim()}${suffix}`;
|
|
956
1380
|
});
|
|
@@ -991,10 +1415,7 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
|
|
|
991
1415
|
outputClient,
|
|
992
1416
|
httpClient,
|
|
993
1417
|
isExactOptionalPropertyTypes,
|
|
994
|
-
hasSignal: getHasSignal({
|
|
995
|
-
overrideQuerySignal: override.query.signal,
|
|
996
|
-
verb
|
|
997
|
-
}),
|
|
1418
|
+
hasSignal: getHasSignal({ overrideQuerySignal: override.query.signal }),
|
|
998
1419
|
queryOptionsMutator,
|
|
999
1420
|
queryKeyMutator,
|
|
1000
1421
|
route,
|
|
@@ -1015,84 +1436,33 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
|
|
|
1015
1436
|
mutators = queryOptionsMutator || queryKeyMutator ? [...queryOptionsMutator ? [queryOptionsMutator] : [], ...queryKeyMutator ? [queryKeyMutator] : []] : void 0;
|
|
1016
1437
|
}
|
|
1017
1438
|
if (isMutation) {
|
|
1018
|
-
const
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
const definitions = props.map(({ definition, type }) => type === GetterPropType.BODY ? mutator?.bodyTypeName ? `data: ${mutator.bodyTypeName}<${body.definition}>` : `data: ${body.definition}` : definition).join(";");
|
|
1026
|
-
const properties = props.map(({ name, type }) => type === GetterPropType.BODY ? "data" : name).join(",");
|
|
1027
|
-
const errorType = getQueryErrorType(operationName, response, httpClient, mutator);
|
|
1028
|
-
const dataType = mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`;
|
|
1029
|
-
const mutationOptionFnReturnType = getQueryOptionsDefinition({
|
|
1030
|
-
operationName,
|
|
1031
|
-
mutator,
|
|
1032
|
-
definitions,
|
|
1033
|
-
hasSvelteQueryV4,
|
|
1439
|
+
const mutationResult = await generateMutationHook({
|
|
1440
|
+
verbOptions: {
|
|
1441
|
+
...verbOptions,
|
|
1442
|
+
props
|
|
1443
|
+
},
|
|
1444
|
+
options,
|
|
1445
|
+
outputClient,
|
|
1034
1446
|
hasQueryV5,
|
|
1035
1447
|
hasQueryV5WithInfiniteQueryOptionsError,
|
|
1036
|
-
isReturnType: true
|
|
1037
|
-
});
|
|
1038
|
-
const mutationArguments = generateQueryArguments({
|
|
1039
|
-
operationName,
|
|
1040
|
-
definitions,
|
|
1041
|
-
mutator,
|
|
1042
|
-
isRequestOptions,
|
|
1043
1448
|
hasSvelteQueryV4,
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
httpClient
|
|
1449
|
+
hasSvelteQueryV6,
|
|
1450
|
+
isRequestOptions,
|
|
1451
|
+
httpClient,
|
|
1452
|
+
doc,
|
|
1453
|
+
isAngularHttp
|
|
1047
1454
|
});
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
const mutationOptionsFn = `export const ${mutationOptionsFnName} = <TError = ${errorType},
|
|
1051
|
-
TContext = unknown>(${mutationArguments}): ${mutationOptionFnReturnType} => {
|
|
1052
|
-
|
|
1053
|
-
${getHooksOptionImplementation(isRequestOptions, httpClient, camel(operationName), mutator)}
|
|
1054
|
-
|
|
1055
|
-
${mutator?.isHook ? `const ${operationName} = use${pascal(operationName)}Hook()` : ""}
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
const mutationFn: MutationFunction<Awaited<ReturnType<${dataType}>>, ${definitions ? `{${definitions}}` : "void"}> = (${properties ? "props" : ""}) => {
|
|
1059
|
-
${properties ? `const {${properties}} = props ?? {};` : ""}
|
|
1060
|
-
|
|
1061
|
-
return ${operationName}(${properties}${properties ? "," : ""}${getMutationRequestArgs(isRequestOptions, httpClient, mutator)})
|
|
1062
|
-
}
|
|
1063
|
-
|
|
1064
|
-
${mutationOptionsMutator ? `const customOptions = ${mutationOptionsMutator.name}({...mutationOptions, mutationFn}${mutationOptionsMutator.hasSecondArg ? `, { url: \`${route.replaceAll("/${", "/{")}\` }` : ""}${mutationOptionsMutator.hasThirdArg ? `, { operationId: '${operationId}', operationName: '${operationName}' }` : ""});` : ""}
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
return ${mutationOptionsMutator ? "customOptions" : "{ mutationFn, ...mutationOptions }"}}`;
|
|
1068
|
-
const operationPrefix = hasSvelteQueryV4 ? "create" : "use";
|
|
1069
|
-
const optionalQueryClientArgument = hasQueryV5 ? ", queryClient?: QueryClient" : "";
|
|
1070
|
-
implementation += `
|
|
1071
|
-
${mutationOptionsFn}
|
|
1072
|
-
|
|
1073
|
-
export type ${pascal(operationName)}MutationResult = NonNullable<Awaited<ReturnType<${dataType}>>>
|
|
1074
|
-
${body.definition ? `export type ${pascal(operationName)}MutationBody = ${mutator?.bodyTypeName ? `${mutator.bodyTypeName}<${body.definition}>` : body.definition}` : ""}
|
|
1075
|
-
export type ${pascal(operationName)}MutationError = ${errorType}
|
|
1076
|
-
|
|
1077
|
-
${doc}export const ${camel(`${operationPrefix}-${operationName}`)} = <TError = ${errorType},
|
|
1078
|
-
TContext = unknown>(${mutationArguments} ${optionalQueryClientArgument})${generateMutatorReturnType({
|
|
1079
|
-
outputClient,
|
|
1080
|
-
dataType,
|
|
1081
|
-
variableType: definitions ? `{${definitions}}` : "void"
|
|
1082
|
-
})} => {
|
|
1083
|
-
|
|
1084
|
-
const ${mutationOptionsVarName} = ${mutationOptionsFnName}(${isRequestOptions ? "options" : "mutationOptions"});
|
|
1085
|
-
|
|
1086
|
-
return ${operationPrefix}Mutation(${hasSvelteQueryV6 ? `() => ({ ...${mutationOptionsVarName}${optionalQueryClientArgument ? ", queryClient" : ""} })` : `${mutationOptionsVarName}${optionalQueryClientArgument ? ", queryClient" : ""}`});
|
|
1087
|
-
}
|
|
1088
|
-
`;
|
|
1089
|
-
mutators = mutationOptionsMutator ? [...mutators ?? [], mutationOptionsMutator] : mutators;
|
|
1455
|
+
implementation += mutationResult.implementation;
|
|
1456
|
+
mutators = mutationResult.mutators ? [...mutators ?? [], ...mutationResult.mutators] : mutators;
|
|
1090
1457
|
}
|
|
1091
1458
|
return {
|
|
1092
1459
|
implementation,
|
|
1093
1460
|
mutators
|
|
1094
1461
|
};
|
|
1095
1462
|
};
|
|
1463
|
+
|
|
1464
|
+
//#endregion
|
|
1465
|
+
//#region src/index.ts
|
|
1096
1466
|
const generateQueryHeader = (params) => {
|
|
1097
1467
|
return `${params.hasAwaitedType ? "" : `type AwaitedInput<T> = PromiseLike<T> | T;\n
|
|
1098
1468
|
type Awaited<O> = O extends AwaitedInput<infer T> ? T : never;\n\n`}
|
|
@@ -1113,7 +1483,9 @@ const generateQuery = async (verbOptions, options, outputClient) => {
|
|
|
1113
1483
|
const dependenciesBuilder = {
|
|
1114
1484
|
"react-query": getReactQueryDependencies,
|
|
1115
1485
|
"vue-query": getVueQueryDependencies,
|
|
1116
|
-
"svelte-query": getSvelteQueryDependencies
|
|
1486
|
+
"svelte-query": getSvelteQueryDependencies,
|
|
1487
|
+
"angular-query": getAngularQueryDependencies,
|
|
1488
|
+
"solid-query": getSolidQueryDependencies
|
|
1117
1489
|
};
|
|
1118
1490
|
const builder = ({ type = "react-query", options: queryOptions, output } = {}) => () => {
|
|
1119
1491
|
const client = (verbOptions, options, outputClient) => {
|
|
@@ -1134,5 +1506,5 @@ const builder = ({ type = "react-query", options: queryOptions, output } = {}) =
|
|
|
1134
1506
|
var src_default = builder;
|
|
1135
1507
|
|
|
1136
1508
|
//#endregion
|
|
1137
|
-
export { builder, src_default as default, generateQuery, generateQueryHeader, getReactQueryDependencies, getSvelteQueryDependencies, getVueQueryDependencies };
|
|
1509
|
+
export { builder, src_default as default, generateQuery, generateQueryHeader, getAngularQueryDependencies, getReactQueryDependencies, getSolidQueryDependencies, getSvelteQueryDependencies, getVueQueryDependencies };
|
|
1138
1510
|
//# sourceMappingURL=index.mjs.map
|