@orval/query 8.0.0-rc.5 → 8.0.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/README.md +1 -1
- package/dist/index.d.mts +7 -3
- package/dist/index.mjs +305 -155
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -5
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,6 +59,7 @@ 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 isSolid = (client) => OutputClient.SOLID_QUERY === client;
|
|
62
63
|
const isAngular = (client) => OutputClient.ANGULAR_QUERY === client;
|
|
63
64
|
const getQueryTypeForFramework = (type) => {
|
|
64
65
|
switch (type) {
|
|
@@ -67,7 +68,7 @@ const getQueryTypeForFramework = (type) => {
|
|
|
67
68
|
default: return type;
|
|
68
69
|
}
|
|
69
70
|
};
|
|
70
|
-
const getHasSignal = ({ overrideQuerySignal = false
|
|
71
|
+
const getHasSignal = ({ overrideQuerySignal = false }) => overrideQuerySignal;
|
|
71
72
|
|
|
72
73
|
//#endregion
|
|
73
74
|
//#region src/client.ts
|
|
@@ -130,10 +131,7 @@ const generateAngularHttpRequestFunction = ({ headers, queryParams, operationNam
|
|
|
130
131
|
const isRequestOptions = override.requestOptions !== false;
|
|
131
132
|
const isFormData = !override.formData.disabled;
|
|
132
133
|
const isFormUrlEncoded = override.formUrlEncoded !== false;
|
|
133
|
-
const hasSignal = getHasSignal({
|
|
134
|
-
overrideQuerySignal: override.query.signal,
|
|
135
|
-
verb
|
|
136
|
-
});
|
|
134
|
+
const hasSignal = getHasSignal({ overrideQuerySignal: override.query.signal });
|
|
137
135
|
const bodyForm = generateFormDataAndUrlEncodedFunction({
|
|
138
136
|
formData,
|
|
139
137
|
formUrlEncoded,
|
|
@@ -157,7 +155,7 @@ const generateAngularHttpRequestFunction = ({ headers, queryParams, operationNam
|
|
|
157
155
|
});
|
|
158
156
|
const requestOptions = isRequestOptions ? generateMutatorRequestOptions(override.requestOptions, mutator.hasSecondArg) : "";
|
|
159
157
|
const propsImplementation = toObjectString(props, "implementation");
|
|
160
|
-
return `${override.query.shouldExportHttpClient ? "export " : ""}const ${operationName} = (
|
|
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` : ""}) => {
|
|
161
159
|
${bodyForm}
|
|
162
160
|
return ${mutator.name}<${response.definition.success || "unknown"}>(
|
|
163
161
|
${mutatorConfig},
|
|
@@ -188,10 +186,9 @@ const generateAngularHttpRequestFunction = ({ headers, queryParams, operationNam
|
|
|
188
186
|
httpCall = `http.${verb}<${dataType}>(url, ${bodyArg || "undefined"}${optionsStr})`;
|
|
189
187
|
break;
|
|
190
188
|
}
|
|
191
|
-
const
|
|
189
|
+
const additionalParams = [queryProps, hasSignal ? "options?: { signal?: AbortSignal | null }" : ""].filter(Boolean).join(", ");
|
|
192
190
|
return `${override.query.shouldExportHttpClient ? "export " : ""}const ${operationName} = (
|
|
193
|
-
http: HttpClient
|
|
194
|
-
${queryProps}${optionsParam}
|
|
191
|
+
http: HttpClient${additionalParams ? `,\n ${additionalParams}` : ""}
|
|
195
192
|
): Promise<${dataType}> => {
|
|
196
193
|
${bodyForm}
|
|
197
194
|
${urlConstruction}
|
|
@@ -207,14 +204,11 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
207
204
|
let props = _props;
|
|
208
205
|
let route = _route;
|
|
209
206
|
if (isVue$1) props = vueWrapTypeWithMaybeRef(_props);
|
|
210
|
-
if (context.output
|
|
207
|
+
if (context.output.urlEncodeParameters) route = makeRouteSafe(route);
|
|
211
208
|
const isRequestOptions = override.requestOptions !== false;
|
|
212
209
|
const isFormData = !override.formData.disabled;
|
|
213
210
|
const isFormUrlEncoded = override.formUrlEncoded !== false;
|
|
214
|
-
const hasSignal = getHasSignal({
|
|
215
|
-
overrideQuerySignal: override.query.signal,
|
|
216
|
-
verb
|
|
217
|
-
});
|
|
211
|
+
const hasSignal = getHasSignal({ overrideQuerySignal: override.query.signal });
|
|
218
212
|
const isExactOptionalPropertyTypes = !!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
|
|
219
213
|
const bodyForm = generateFormDataAndUrlEncodedFunction({
|
|
220
214
|
formData,
|
|
@@ -238,7 +232,7 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
238
232
|
isVue: isVue$1
|
|
239
233
|
});
|
|
240
234
|
const bodyDefinition = body.definition.replace("[]", String.raw`\[\]`);
|
|
241
|
-
const propsImplementation = mutator
|
|
235
|
+
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");
|
|
242
236
|
const requestOptions = isRequestOptions ? generateMutatorRequestOptions(override.requestOptions, mutator.hasSecondArg) : "";
|
|
243
237
|
if (mutator.isHook) {
|
|
244
238
|
const ret = `${override.query.shouldExportMutatorHooks ? "export " : ""}const use${pascal(operationName)}Hook = () => {
|
|
@@ -280,11 +274,11 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
280
274
|
queryParams,
|
|
281
275
|
response,
|
|
282
276
|
verb,
|
|
283
|
-
requestOptions: override
|
|
277
|
+
requestOptions: override.requestOptions,
|
|
284
278
|
isFormData,
|
|
285
279
|
isFormUrlEncoded,
|
|
286
280
|
paramsSerializer,
|
|
287
|
-
paramsSerializerOptions: override
|
|
281
|
+
paramsSerializerOptions: override.paramsSerializerOptions,
|
|
288
282
|
isExactOptionalPropertyTypes,
|
|
289
283
|
hasSignal,
|
|
290
284
|
isVue: isVue$1
|
|
@@ -347,7 +341,7 @@ const getQueryErrorType = (operationName, response, httpClient, mutator) => {
|
|
|
347
341
|
const getHooksOptionImplementation = (isRequestOptions, httpClient, operationName, mutator) => {
|
|
348
342
|
const options = httpClient === OutputHttpClient.AXIOS ? ", axios: axiosOptions" : ", fetch: fetchOptions";
|
|
349
343
|
return isRequestOptions ? `const mutationKey = ['${operationName}'];
|
|
350
|
-
const {mutation: mutationOptions${mutator ? mutator
|
|
344
|
+
const {mutation: mutationOptions${mutator ? mutator.hasSecondArg ? ", request: requestOptions" : "" : options}} = options ?
|
|
351
345
|
options.mutation && 'mutationKey' in options.mutation && options.mutation.mutationKey ?
|
|
352
346
|
options
|
|
353
347
|
: {...options, mutation: {...options.mutation, mutationKey}}
|
|
@@ -355,11 +349,10 @@ const {mutation: mutationOptions${mutator ? mutator?.hasSecondArg ? ", request:
|
|
|
355
349
|
};
|
|
356
350
|
const getMutationRequestArgs = (isRequestOptions, httpClient, mutator) => {
|
|
357
351
|
const options = httpClient === OutputHttpClient.AXIOS ? "axiosOptions" : "fetchOptions";
|
|
358
|
-
return isRequestOptions ? mutator ? mutator
|
|
352
|
+
return isRequestOptions ? mutator ? mutator.hasSecondArg ? "requestOptions" : "" : options : "";
|
|
359
353
|
};
|
|
360
354
|
const getHttpFunctionQueryProps = (isVue$1, httpClient, queryProperties, isAngular$1 = false) => {
|
|
361
|
-
|
|
362
|
-
result = isVue$1 && httpClient === OutputHttpClient.FETCH && queryProperties ? queryProperties.split(",").map((prop) => `unref(${prop})`).join(",") : queryProperties;
|
|
355
|
+
const result = isVue$1 && httpClient === OutputHttpClient.FETCH && queryProperties ? queryProperties.split(",").map((prop) => `unref(${prop})`).join(",") : queryProperties;
|
|
363
356
|
if (isAngular$1 || httpClient === OutputHttpClient.ANGULAR) return result ? `http, ${result}` : "http";
|
|
364
357
|
return result;
|
|
365
358
|
};
|
|
@@ -368,21 +361,7 @@ const getQueryHeader = (params) => {
|
|
|
368
361
|
};
|
|
369
362
|
|
|
370
363
|
//#endregion
|
|
371
|
-
//#region src/
|
|
372
|
-
/**
|
|
373
|
-
* Get framework-aware prefix for hook names and type definitions
|
|
374
|
-
* @param hasSvelteQueryV4 - Whether using Svelte Query v4
|
|
375
|
-
* @param isAngularClient - Whether using Angular client
|
|
376
|
-
* @param capitalize - Whether to capitalize the prefix (for type definitions)
|
|
377
|
-
* @returns The appropriate prefix string
|
|
378
|
-
*/
|
|
379
|
-
const getFrameworkPrefix = (hasSvelteQueryV4, isAngularClient, capitalize = false) => {
|
|
380
|
-
let prefix;
|
|
381
|
-
if (hasSvelteQueryV4) prefix = "create";
|
|
382
|
-
else if (isAngularClient) prefix = "inject";
|
|
383
|
-
else prefix = "use";
|
|
384
|
-
return capitalize ? prefix.charAt(0).toUpperCase() + prefix.slice(1) : prefix;
|
|
385
|
-
};
|
|
364
|
+
//#region src/dependencies.ts
|
|
386
365
|
const REACT_DEPENDENCIES = [{
|
|
387
366
|
exports: [{
|
|
388
367
|
name: "useCallback",
|
|
@@ -557,7 +536,7 @@ const REACT_QUERY_DEPENDENCIES = [{
|
|
|
557
536
|
const getReactQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient, hasTagsMutator, override) => {
|
|
558
537
|
const hasReactQuery = packageJson?.dependencies?.["react-query"] ?? packageJson?.devDependencies?.["react-query"] ?? packageJson?.peerDependencies?.["react-query"];
|
|
559
538
|
const hasReactQueryV4 = packageJson?.dependencies?.["@tanstack/react-query"] ?? packageJson?.devDependencies?.["@tanstack/react-query"] ?? packageJson?.peerDependencies?.["@tanstack/react-query"];
|
|
560
|
-
const useReactQueryV3 = override
|
|
539
|
+
const useReactQueryV3 = override.query.version === void 0 ? hasReactQuery && !hasReactQueryV4 : override.query.version <= 3;
|
|
561
540
|
return [
|
|
562
541
|
...hasGlobalMutator || hasTagsMutator ? REACT_DEPENDENCIES : [],
|
|
563
542
|
...!hasGlobalMutator && httpClient === OutputHttpClient.AXIOS ? AXIOS_DEPENDENCIES : [],
|
|
@@ -656,6 +635,36 @@ const VUE_QUERY_DEPENDENCIES = [{
|
|
|
656
635
|
],
|
|
657
636
|
dependency: "vue"
|
|
658
637
|
}];
|
|
638
|
+
const SOLID_QUERY_DEPENDENCIES = [{
|
|
639
|
+
exports: [
|
|
640
|
+
{
|
|
641
|
+
name: "createQuery",
|
|
642
|
+
values: true
|
|
643
|
+
},
|
|
644
|
+
{
|
|
645
|
+
name: "createInfiniteQuery",
|
|
646
|
+
values: true
|
|
647
|
+
},
|
|
648
|
+
{
|
|
649
|
+
name: "createMutation",
|
|
650
|
+
values: true
|
|
651
|
+
},
|
|
652
|
+
{ name: "CreateQueryOptions" },
|
|
653
|
+
{ name: "CreateInfiniteQueryOptions" },
|
|
654
|
+
{ name: "CreateMutationOptions" },
|
|
655
|
+
{ name: "QueryFunction" },
|
|
656
|
+
{ name: "MutationFunction" },
|
|
657
|
+
{ name: "CreateQueryResult" },
|
|
658
|
+
{ name: "CreateInfiniteQueryResult" },
|
|
659
|
+
{ name: "QueryKey" },
|
|
660
|
+
{ name: "InfiniteData" },
|
|
661
|
+
{ name: "CreateMutationResult" },
|
|
662
|
+
{ name: "DataTag" },
|
|
663
|
+
{ name: "QueryClient" },
|
|
664
|
+
{ name: "InvalidateOptions" }
|
|
665
|
+
],
|
|
666
|
+
dependency: "@tanstack/solid-query"
|
|
667
|
+
}];
|
|
659
668
|
const ANGULAR_QUERY_DEPENDENCIES = [{
|
|
660
669
|
exports: [
|
|
661
670
|
{
|
|
@@ -683,8 +692,12 @@ const ANGULAR_QUERY_DEPENDENCIES = [{
|
|
|
683
692
|
{ name: "InfiniteData" },
|
|
684
693
|
{ name: "CreateMutationResult" },
|
|
685
694
|
{ name: "DataTag" },
|
|
686
|
-
{
|
|
687
|
-
|
|
695
|
+
{
|
|
696
|
+
name: "QueryClient",
|
|
697
|
+
values: true
|
|
698
|
+
},
|
|
699
|
+
{ name: "InvalidateOptions" },
|
|
700
|
+
{ name: "MutationFunctionContext" }
|
|
688
701
|
],
|
|
689
702
|
dependency: "@tanstack/angular-query-experimental"
|
|
690
703
|
}, {
|
|
@@ -714,6 +727,13 @@ const getVueQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, p
|
|
|
714
727
|
...hasVueQueryV3 ? VUE_QUERY_DEPENDENCIES_V3 : VUE_QUERY_DEPENDENCIES
|
|
715
728
|
];
|
|
716
729
|
};
|
|
730
|
+
const getSolidQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient) => {
|
|
731
|
+
return [
|
|
732
|
+
...!hasGlobalMutator && httpClient === OutputHttpClient.AXIOS ? AXIOS_DEPENDENCIES : [],
|
|
733
|
+
...hasParamsSerializerOptions ? PARAMS_SERIALIZER_DEPENDENCIES : [],
|
|
734
|
+
...SOLID_QUERY_DEPENDENCIES
|
|
735
|
+
];
|
|
736
|
+
};
|
|
717
737
|
const getAngularQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient) => {
|
|
718
738
|
const useAngularHttp = !hasGlobalMutator && httpClient === OutputHttpClient.ANGULAR;
|
|
719
739
|
const useAxios = !hasGlobalMutator && httpClient === OutputHttpClient.AXIOS;
|
|
@@ -757,8 +777,12 @@ const getPackageByQueryClient = (packageJson, queryClient) => {
|
|
|
757
777
|
case "svelte-query": return packageJson?.dependencies?.["@tanstack/svelte-query"] ?? packageJson?.devDependencies?.["@tanstack/svelte-query"] ?? packageJson?.peerDependencies?.["@tanstack/svelte-query"];
|
|
758
778
|
case "vue-query": return packageJson?.dependencies?.["@tanstack/vue-query"] ?? packageJson?.devDependencies?.["@tanstack/vue-query"] ?? packageJson?.peerDependencies?.["@tanstack/vue-query"];
|
|
759
779
|
case "angular-query": return packageJson?.dependencies?.["@tanstack/angular-query-experimental"] ?? packageJson?.devDependencies?.["@tanstack/angular-query-experimental"] ?? packageJson?.peerDependencies?.["@tanstack/angular-query-experimental"];
|
|
780
|
+
case "solid-query": return packageJson?.dependencies?.["@tanstack/solid-query"] ?? packageJson?.devDependencies?.["@tanstack/solid-query"] ?? packageJson?.peerDependencies?.["@tanstack/solid-query"];
|
|
760
781
|
}
|
|
761
782
|
};
|
|
783
|
+
|
|
784
|
+
//#endregion
|
|
785
|
+
//#region src/query-options.ts
|
|
762
786
|
const QueryType = {
|
|
763
787
|
INFINITE: "infiniteQuery",
|
|
764
788
|
QUERY: "query",
|
|
@@ -768,12 +792,12 @@ const QueryType = {
|
|
|
768
792
|
const INFINITE_QUERY_PROPERTIES = new Set(["getNextPageParam", "getPreviousPageParam"]);
|
|
769
793
|
const generateQueryOptions = ({ params, options, type, outputClient }) => {
|
|
770
794
|
if (options === false) return "";
|
|
771
|
-
const queryConfig = isObject(options) ? ` ${stringify(omitBy(options, (_, key) =>
|
|
795
|
+
const queryConfig = isObject(options) ? ` ${stringify(omitBy(options, (_, key) => type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE && INFINITE_QUERY_PROPERTIES.has(key)))?.slice(1, -1)}` : "";
|
|
772
796
|
if (params.length === 0 || isSuspenseQuery(type)) {
|
|
773
797
|
if (options) return `${queryConfig} ...queryOptions`;
|
|
774
798
|
return "...queryOptions";
|
|
775
799
|
}
|
|
776
|
-
return `${!isObject(options) || !
|
|
800
|
+
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`;
|
|
777
801
|
};
|
|
778
802
|
const isSuspenseQuery = (type) => {
|
|
779
803
|
return [QueryType.SUSPENSE_INFINITE, QueryType.SUSPENSE_QUERY].includes(type);
|
|
@@ -791,7 +815,7 @@ const getQueryOptionsDefinition = ({ operationName, mutator, definitions, type,
|
|
|
791
815
|
${funcReturnType}${hasQueryV5 && (type === QueryType.INFINITE || type === QueryType.SUSPENSE_INFINITE) && queryParam && queryParams ? `, QueryKey` : ""}
|
|
792
816
|
> , 'initialData'
|
|
793
817
|
>` : "";
|
|
794
|
-
const optionType = `${prefix}${pascal(type)}Options<${funcReturnType}, TError, TData${hasQueryV5 && (type === QueryType.INFINITE || type === QueryType.SUSPENSE_INFINITE) && queryParam && queryParams ? hasQueryV5WithInfiniteQueryOptionsError ? `, QueryKey, ${queryParams
|
|
818
|
+
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}']` : ""}>`;
|
|
795
819
|
return `${partialOptions ? "Partial<" : ""}${optionType}${partialOptions ? ">" : ""}${optionTypeInitialDataPostfix}`;
|
|
796
820
|
}
|
|
797
821
|
return `${prefix}MutationOptions<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>, TError,${definitions ? `{${definitions}}` : "void"}, TContext>`;
|
|
@@ -816,11 +840,17 @@ const generateQueryArguments = ({ operationName, definitions, mutator, isRequest
|
|
|
816
840
|
const isQueryRequired = initialData === "defined";
|
|
817
841
|
return `options${isQueryRequired ? "" : "?"}: { ${type ? "query" : "mutation"}${isQueryRequired ? "" : "?"}:${definition}, ${requestType}}\n`;
|
|
818
842
|
};
|
|
843
|
+
|
|
844
|
+
//#endregion
|
|
845
|
+
//#region src/return-types.ts
|
|
819
846
|
const generateQueryReturnType = ({ outputClient, type, isMutatorHook, operationName, hasVueQueryV4, hasSvelteQueryV4, hasQueryV5, hasQueryV5WithDataTagError, isInitialDataDefined }) => {
|
|
820
847
|
switch (outputClient) {
|
|
821
848
|
case OutputClient.ANGULAR_QUERY:
|
|
822
849
|
if (type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE) return `CreateQueryResult<TData, TError>`;
|
|
823
850
|
return `CreateInfiniteQueryResult<TData, TError>`;
|
|
851
|
+
case OutputClient.SOLID_QUERY:
|
|
852
|
+
if (type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE) return `CreateQueryResult<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
853
|
+
return `CreateInfiniteQueryResult<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
824
854
|
case OutputClient.SVELTE_QUERY:
|
|
825
855
|
if (!hasSvelteQueryV4) return `Use${pascal(type)}StoreResult<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>, TError, TData, QueryKey> & { queryKey: QueryKey} }`;
|
|
826
856
|
return `Create${pascal(type)}Result<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
@@ -828,7 +858,6 @@ const generateQueryReturnType = ({ outputClient, type, isMutatorHook, operationN
|
|
|
828
858
|
if (!hasVueQueryV4) return ` UseQueryReturnType<TData, TError, Use${pascal(type)}Result<TData, TError>> & { queryKey: QueryKey} }`;
|
|
829
859
|
if (type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE) return `UseQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
830
860
|
return `UseInfiniteQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
831
|
-
case OutputClient.REACT_QUERY:
|
|
832
861
|
default: return ` ${isInitialDataDefined && !isSuspenseQuery(type) ? "Defined" : ""}Use${pascal(type)}Result<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
833
862
|
}
|
|
834
863
|
};
|
|
@@ -844,6 +873,12 @@ const generateMutatorReturnType = ({ outputClient, dataType, variableType }) =>
|
|
|
844
873
|
TError,
|
|
845
874
|
${variableType},
|
|
846
875
|
TContext
|
|
876
|
+
>`;
|
|
877
|
+
if (outputClient === OutputClient.SOLID_QUERY) return `: CreateMutationResult<
|
|
878
|
+
Awaited<ReturnType<${dataType}>>,
|
|
879
|
+
TError,
|
|
880
|
+
${variableType},
|
|
881
|
+
TContext
|
|
847
882
|
>`;
|
|
848
883
|
if (outputClient === OutputClient.SVELTE_QUERY) return `: CreateMutationResult<
|
|
849
884
|
Awaited<ReturnType<${dataType}>>,
|
|
@@ -867,7 +902,155 @@ const getQueryFnArguments = ({ hasQueryParam, hasSignal }) => {
|
|
|
867
902
|
}
|
|
868
903
|
return "{ signal }";
|
|
869
904
|
};
|
|
870
|
-
const
|
|
905
|
+
const getQueryReturnStatement = ({ outputClient, hasSvelteQueryV4, hasSvelteQueryV6, hasQueryV5, hasQueryV5WithDataTagError, queryResultVarName, queryOptionsVarName }) => {
|
|
906
|
+
if (isAngular(outputClient)) return `return ${queryResultVarName};`;
|
|
907
|
+
if (isVue(outputClient)) return `${queryResultVarName}.queryKey = unref(${queryOptionsVarName}).queryKey as ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"};
|
|
908
|
+
|
|
909
|
+
return ${queryResultVarName};`;
|
|
910
|
+
if (hasSvelteQueryV6) return `return ${queryResultVarName}`;
|
|
911
|
+
if (hasSvelteQueryV4) return `${queryResultVarName}.queryKey = ${queryOptionsVarName}.queryKey;
|
|
912
|
+
|
|
913
|
+
return ${queryResultVarName};`;
|
|
914
|
+
return `return { ...${queryResultVarName}, queryKey: ${queryOptionsVarName}.queryKey };`;
|
|
915
|
+
};
|
|
916
|
+
|
|
917
|
+
//#endregion
|
|
918
|
+
//#region src/mutation-generator.ts
|
|
919
|
+
const normalizeTarget = (target) => typeof target === "string" ? { query: target } : target;
|
|
920
|
+
const serializeTarget = (target) => JSON.stringify({
|
|
921
|
+
query: target.query,
|
|
922
|
+
params: target.params ?? []
|
|
923
|
+
});
|
|
924
|
+
const generateVariableRef = (varName) => {
|
|
925
|
+
const parts = varName.split(".");
|
|
926
|
+
if (parts.length === 1) return `variables.${varName}`;
|
|
927
|
+
return `variables.${parts[0]}?.${parts.slice(1).join("?.")}`;
|
|
928
|
+
};
|
|
929
|
+
const generateParamArgs = (params) => {
|
|
930
|
+
if (Array.isArray(params)) return params.map((v) => generateVariableRef(v)).join(", ");
|
|
931
|
+
return Object.values(params).map((v) => generateVariableRef(v)).join(", ");
|
|
932
|
+
};
|
|
933
|
+
const generateInvalidateCall = (target) => {
|
|
934
|
+
return ` queryClient.invalidateQueries({ queryKey: ${camel(`get-${target.query}-query-key`)}(${target.params ? generateParamArgs(target.params) : ""}) });`;
|
|
935
|
+
};
|
|
936
|
+
const generateMutationHook = async ({ verbOptions, options, outputClient, hasQueryV5, hasQueryV5WithInfiniteQueryOptionsError, hasSvelteQueryV4, hasSvelteQueryV6, isRequestOptions, httpClient, doc, isAngularHttp }) => {
|
|
937
|
+
const { operationName, body, props, mutator, response, operationId, override } = verbOptions;
|
|
938
|
+
const { route, context, output } = options;
|
|
939
|
+
const query = override.query;
|
|
940
|
+
const mutationOptionsMutator = query.mutationOptions ? await generateMutator({
|
|
941
|
+
output,
|
|
942
|
+
mutator: query.mutationOptions,
|
|
943
|
+
name: `${operationName}MutationOptions`,
|
|
944
|
+
workspace: context.workspace,
|
|
945
|
+
tsconfig: context.output.tsconfig
|
|
946
|
+
}) : void 0;
|
|
947
|
+
const definitions = props.map(({ definition, type }) => type === GetterPropType.BODY ? mutator?.bodyTypeName ? `data: ${mutator.bodyTypeName}<${body.definition}>` : `data: ${body.definition}` : definition).join(";");
|
|
948
|
+
const properties = props.map(({ name, type }) => type === GetterPropType.BODY ? "data" : name).join(",");
|
|
949
|
+
const errorType = getQueryErrorType(operationName, response, httpClient, mutator);
|
|
950
|
+
const dataType = mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`;
|
|
951
|
+
const isAngularClient = isAngular(outputClient);
|
|
952
|
+
const mutationOptionFnReturnType = getQueryOptionsDefinition({
|
|
953
|
+
operationName,
|
|
954
|
+
mutator,
|
|
955
|
+
definitions,
|
|
956
|
+
hasSvelteQueryV4,
|
|
957
|
+
hasQueryV5,
|
|
958
|
+
hasQueryV5WithInfiniteQueryOptionsError,
|
|
959
|
+
isReturnType: true,
|
|
960
|
+
isAngularClient
|
|
961
|
+
});
|
|
962
|
+
const mutationArguments = generateQueryArguments({
|
|
963
|
+
operationName,
|
|
964
|
+
definitions,
|
|
965
|
+
mutator,
|
|
966
|
+
isRequestOptions,
|
|
967
|
+
hasSvelteQueryV4,
|
|
968
|
+
hasQueryV5,
|
|
969
|
+
hasQueryV5WithInfiniteQueryOptionsError,
|
|
970
|
+
httpClient,
|
|
971
|
+
isAngularClient
|
|
972
|
+
});
|
|
973
|
+
const mutationOptionsFnName = camel(mutationOptionsMutator || mutator?.isHook ? `use-${operationName}-mutationOptions` : `get-${operationName}-mutationOptions`);
|
|
974
|
+
const hooksOptionImplementation = getHooksOptionImplementation(isRequestOptions, httpClient, camel(operationName), mutator);
|
|
975
|
+
const invalidatesConfig = (query.mutationInvalidates ?? []).filter((rule) => rule.onMutations.includes(operationName)).flatMap((rule) => rule.invalidates).map((t) => normalizeTarget(t));
|
|
976
|
+
const seenTargets = /* @__PURE__ */ new Set();
|
|
977
|
+
const uniqueInvalidates = invalidatesConfig.filter((target) => {
|
|
978
|
+
const key = serializeTarget(target);
|
|
979
|
+
if (seenTargets.has(key)) return false;
|
|
980
|
+
seenTargets.add(key);
|
|
981
|
+
return true;
|
|
982
|
+
});
|
|
983
|
+
const hasInvalidation = uniqueInvalidates.length > 0 && isAngularClient;
|
|
984
|
+
const mutationOptionsFn = `export const ${mutationOptionsFnName} = <TError = ${errorType},
|
|
985
|
+
TContext = unknown>(${mutationArguments}): ${mutationOptionFnReturnType} => {
|
|
986
|
+
|
|
987
|
+
${hooksOptionImplementation}
|
|
988
|
+
${isAngularHttp ? " const http = inject(HttpClient);" : ""}
|
|
989
|
+
${hasInvalidation ? " const queryClient = inject(QueryClient);" : ""}
|
|
990
|
+
|
|
991
|
+
${mutator?.isHook ? `const ${operationName} = use${pascal(operationName)}Hook()` : ""}
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
const mutationFn: MutationFunction<Awaited<ReturnType<${dataType}>>, ${definitions ? `{${definitions}}` : "void"}> = (${properties ? "props" : ""}) => {
|
|
995
|
+
${properties ? `const {${properties}} = props ?? {};` : ""}
|
|
996
|
+
|
|
997
|
+
return ${operationName}(${isAngularHttp ? "http, " : ""}${properties}${properties ? "," : ""}${getMutationRequestArgs(isRequestOptions, httpClient, mutator)})
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
${hasInvalidation ? ` const onSuccess = (data: Awaited<ReturnType<typeof ${operationName}>>, variables: ${definitions ? `{${definitions}}` : "void"}, onMutateResult: TContext, context: MutationFunctionContext) => {
|
|
1001
|
+
${uniqueInvalidates.map((t) => generateInvalidateCall(t)).join("\n")}
|
|
1002
|
+
mutationOptions?.onSuccess?.(data, variables, onMutateResult, context);
|
|
1003
|
+
};` : ""}
|
|
1004
|
+
|
|
1005
|
+
${mutationOptionsMutator ? `const customOptions = ${mutationOptionsMutator.name}({...mutationOptions, mutationFn}${mutationOptionsMutator.hasSecondArg ? `, { url: \`${route.replaceAll("/${", "/{")}\` }` : ""}${mutationOptionsMutator.hasThirdArg ? `, { operationId: '${operationId}', operationName: '${operationName}' }` : ""});` : ""}
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
return ${mutationOptionsMutator ? "customOptions" : hasInvalidation ? "{ mutationFn, onSuccess, ...mutationOptions }" : "{ mutationFn, ...mutationOptions }"}}`;
|
|
1009
|
+
const operationPrefix = getFrameworkPrefix(hasSvelteQueryV4, isAngular(outputClient), isSolid(outputClient));
|
|
1010
|
+
const optionalQueryClientArgument = hasQueryV5 && !isAngular(outputClient) ? ", queryClient?: QueryClient" : "";
|
|
1011
|
+
const mutationImplementation = `${mutationOptionsFnName}(${isRequestOptions ? "options" : "mutationOptions"})`;
|
|
1012
|
+
const mutationOptionsVarName = camel(`${operationName}-mutation-options`);
|
|
1013
|
+
return {
|
|
1014
|
+
implementation: `
|
|
1015
|
+
${mutationOptionsFn}
|
|
1016
|
+
|
|
1017
|
+
export type ${pascal(operationName)}MutationResult = NonNullable<Awaited<ReturnType<${dataType}>>>
|
|
1018
|
+
${body.definition ? `export type ${pascal(operationName)}MutationBody = ${mutator?.bodyTypeName ? `${mutator.bodyTypeName}<${body.definition}>` : body.definition}` : ""}
|
|
1019
|
+
export type ${pascal(operationName)}MutationError = ${errorType}
|
|
1020
|
+
|
|
1021
|
+
${doc}export const ${camel(`${operationPrefix}-${operationName}`)} = <TError = ${errorType},
|
|
1022
|
+
TContext = unknown>(${mutationArguments} ${optionalQueryClientArgument})${generateMutatorReturnType({
|
|
1023
|
+
outputClient,
|
|
1024
|
+
dataType,
|
|
1025
|
+
variableType: definitions ? `{${definitions}}` : "void"
|
|
1026
|
+
})} => {
|
|
1027
|
+
${isAngular(outputClient) ? ` const ${mutationOptionsVarName} = ${mutationImplementation};
|
|
1028
|
+
|
|
1029
|
+
return ${operationPrefix}Mutation(() => ${mutationOptionsVarName});` : ` return ${operationPrefix}Mutation(${hasSvelteQueryV6 ? `() => ({ ...${mutationImplementation}${optionalQueryClientArgument ? ", queryClient" : ""} })` : `${mutationImplementation}${optionalQueryClientArgument ? ", queryClient" : ""}`});`}
|
|
1030
|
+
}
|
|
1031
|
+
`,
|
|
1032
|
+
mutators: mutationOptionsMutator ? [mutationOptionsMutator] : void 0
|
|
1033
|
+
};
|
|
1034
|
+
};
|
|
1035
|
+
|
|
1036
|
+
//#endregion
|
|
1037
|
+
//#region src/query-generator.ts
|
|
1038
|
+
/**
|
|
1039
|
+
* Get framework-aware prefix for hook names and type definitions
|
|
1040
|
+
* @param hasSvelteQueryV4 - Whether using Svelte Query v4
|
|
1041
|
+
* @param isAngularClient - Whether using Angular client
|
|
1042
|
+
* @param isSolidClient - Whether using Solid Query client
|
|
1043
|
+
* @param capitalize - Whether to capitalize the prefix (for type definitions)
|
|
1044
|
+
* @returns The appropriate prefix string
|
|
1045
|
+
*/
|
|
1046
|
+
const getFrameworkPrefix = (hasSvelteQueryV4, isAngularClient, isSolidClient, capitalize = false) => {
|
|
1047
|
+
let prefix;
|
|
1048
|
+
if (hasSvelteQueryV4 || isSolidClient) prefix = "create";
|
|
1049
|
+
else if (isAngularClient) prefix = "inject";
|
|
1050
|
+
else prefix = "use";
|
|
1051
|
+
return capitalize ? prefix.charAt(0).toUpperCase() + prefix.slice(1) : prefix;
|
|
1052
|
+
};
|
|
1053
|
+
const generatePrefetch = ({ usePrefetch, type, useQuery, useInfinite, operationName, mutator, doc, queryProps, dataType, errorType, queryArguments, queryOptionsVarName, queryOptionsFnName, queryProperties, isRequestOptions }) => {
|
|
871
1054
|
if (!(usePrefetch && (type === QueryType.QUERY || type === QueryType.INFINITE || type === QueryType.SUSPENSE_QUERY && !useQuery || type === QueryType.SUSPENSE_INFINITE && !useInfinite))) return "";
|
|
872
1055
|
const prefetchType = type === QueryType.QUERY || type === QueryType.SUSPENSE_QUERY ? "query" : "infinite-query";
|
|
873
1056
|
const prefetchFnName = camel(`prefetch-${prefetchType}`);
|
|
@@ -883,7 +1066,7 @@ const generatePrefetch = ({ usePrefetch, type, useQuery, useInfinite, operationN
|
|
|
883
1066
|
|
|
884
1067
|
const ${queryOptionsVarName} = ${queryOptionsFnName}(${queryProperties}${queryProperties ? "," : ""}${isRequestOptions ? "options" : "queryOptions"})
|
|
885
1068
|
|
|
886
|
-
await queryClient.${prefetchFnName}(${
|
|
1069
|
+
await queryClient.${prefetchFnName}(${queryOptionsVarName});
|
|
887
1070
|
|
|
888
1071
|
return queryClient;
|
|
889
1072
|
}\n`;
|
|
@@ -1012,7 +1195,7 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
1012
1195
|
const queryOptionsFnName = camel(queryKeyMutator || queryOptionsMutator || mutator?.isHook ? `use-${name}-queryOptions` : `get-${name}-queryOptions`);
|
|
1013
1196
|
const queryOptionsVarName = isRequestOptions ? "queryOptions" : "options";
|
|
1014
1197
|
const queryResultVarName = props.some((prop) => prop.name === "query") ? "_query" : "query";
|
|
1015
|
-
const infiniteParam = queryParams && queryParam ? `, ${queryParams
|
|
1198
|
+
const infiniteParam = queryParams && queryParam ? `, ${queryParams.schema.name}['${queryParam}']` : "";
|
|
1016
1199
|
const TData = hasQueryV5 && (type === QueryType.INFINITE || type === QueryType.SUSPENSE_INFINITE) ? `InfiniteData<Awaited<ReturnType<${dataType}>>${infiniteParam}>` : `Awaited<ReturnType<${dataType}>>`;
|
|
1017
1200
|
const queryOptionsFn = `export const ${queryOptionsFnName} = <TData = ${TData}, TError = ${errorType}>(${queryProps} ${queryArguments}) => {
|
|
1018
1201
|
|
|
@@ -1023,7 +1206,7 @@ ${isAngularHttp ? " const http = inject(HttpClient);" : ""}
|
|
|
1023
1206
|
|
|
1024
1207
|
${mutator?.isHook ? `const ${operationName} = use${pascal(operationName)}Hook();` : ""}
|
|
1025
1208
|
|
|
1026
|
-
const queryFn: QueryFunction<Awaited<ReturnType<${mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>${hasQueryV5 && hasInfiniteQueryParam ? `, QueryKey, ${queryParams
|
|
1209
|
+
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});
|
|
1027
1210
|
|
|
1028
1211
|
${isVue(outputClient) ? vueUnRefParams(props.filter((prop) => prop.type === GetterPropType.NAMED_PATH_PARAMS)) : ""}
|
|
1029
1212
|
|
|
@@ -1031,8 +1214,8 @@ ${isAngularHttp ? " const http = inject(HttpClient);" : ""}
|
|
|
1031
1214
|
|
|
1032
1215
|
return ${queryOptionsMutator ? "customOptions" : `{ queryKey, queryFn, ${queryOptionsImp}}`} as ${queryOptionFnReturnType} ${isVue(outputClient) || isAngular(outputClient) ? "" : `& { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`}
|
|
1033
1216
|
}`;
|
|
1034
|
-
const operationPrefix = getFrameworkPrefix(hasSvelteQueryV4, isAngular(outputClient));
|
|
1035
|
-
const optionalQueryClientArgument = hasQueryV5 && !isAngular(outputClient) ? ", queryClient?: QueryClient" : "";
|
|
1217
|
+
const operationPrefix = getFrameworkPrefix(hasSvelteQueryV4, isAngular(outputClient), isSolid(outputClient));
|
|
1218
|
+
const optionalQueryClientArgument = hasSvelteQueryV6 ? `, queryClient?: () => QueryClient` : hasQueryV5 && !isAngular(outputClient) ? ", queryClient?: QueryClient" : "";
|
|
1036
1219
|
const queryHookName = camel(`${operationPrefix}-${name}`);
|
|
1037
1220
|
const overrideTypes = `
|
|
1038
1221
|
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${definedInitialDataQueryPropsDefinitions} ${definedInitialDataQueryArguments} ${optionalQueryClientArgument}\n ): ${definedInitialDataReturnType}
|
|
@@ -1048,7 +1231,6 @@ export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${q
|
|
|
1048
1231
|
queryProps,
|
|
1049
1232
|
dataType,
|
|
1050
1233
|
errorType,
|
|
1051
|
-
hasSvelteQueryV6,
|
|
1052
1234
|
queryArguments,
|
|
1053
1235
|
queryOptionsVarName,
|
|
1054
1236
|
queryOptionsFnName,
|
|
@@ -1066,15 +1248,27 @@ export type ${pascal(name)}QueryError = ${errorType}
|
|
|
1066
1248
|
|
|
1067
1249
|
${hasQueryV5 && OutputClient.REACT_QUERY === outputClient ? overrideTypes : ""}
|
|
1068
1250
|
${doc}
|
|
1069
|
-
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${
|
|
1251
|
+
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${hasSvelteQueryV6 ? toObjectString(props.map((p) => ({
|
|
1252
|
+
...p,
|
|
1253
|
+
definition: p.definition.replace(":", ": () => ")
|
|
1254
|
+
})), "definition") : queryProps} ${hasSvelteQueryV6 ? queryArguments.replace(":", ": () => (") + ")" : queryArguments} ${optionalQueryClientArgument} \n ): ${returnType} {
|
|
1070
1255
|
|
|
1071
|
-
const ${queryOptionsVarName} = ${queryOptionsFnName}(${queryProperties}${queryProperties ? "," : ""}${isRequestOptions ? "options" : "queryOptions"})
|
|
1072
|
-
|
|
1073
|
-
const ${queryResultVarName} = ${camel(`${operationPrefix}-${isAngular(outputClient) || hasSvelteQueryV4 ? getQueryTypeForFramework(type) : type}`)}(${isAngular(outputClient) ? `() => ${queryOptionsVarName}` : hasSvelteQueryV6 ? `() => ({ ...${queryOptionsVarName}${optionalQueryClientArgument ? ", queryClient" : ""} })` : `${queryOptionsVarName}${!isAngular(outputClient) && optionalQueryClientArgument ? ", queryClient" : ""}`}) as ${returnType};
|
|
1256
|
+
${hasSvelteQueryV6 ? "" : `const ${queryOptionsVarName} = ${queryOptionsFnName}(${queryProperties}${queryProperties ? "," : ""}${isRequestOptions ? "options" : "queryOptions"})`}
|
|
1074
1257
|
|
|
1075
|
-
${
|
|
1258
|
+
const ${queryResultVarName} = ${camel(`${operationPrefix}-${isAngular(outputClient) || hasSvelteQueryV4 ? getQueryTypeForFramework(type) : type}`)}(${isAngular(outputClient) ? `() => ${queryOptionsVarName}` : hasSvelteQueryV6 ? `() => ${queryOptionsFnName}(${toObjectString(props.map((p) => ({
|
|
1259
|
+
...p,
|
|
1260
|
+
name: p.default || !p.required ? `${p.name}?.()` : `${p.name}()`
|
|
1261
|
+
})), "name")}${isRequestOptions ? "options?.()" : "queryOptions?.()"})` : `${queryOptionsVarName}${!isAngular(outputClient) && optionalQueryClientArgument ? ", queryClient" : ""}`}${hasSvelteQueryV6 ? `, queryClient` : ""}) as ${returnType};
|
|
1076
1262
|
|
|
1077
|
-
|
|
1263
|
+
${getQueryReturnStatement({
|
|
1264
|
+
outputClient,
|
|
1265
|
+
hasSvelteQueryV4,
|
|
1266
|
+
hasSvelteQueryV6,
|
|
1267
|
+
hasQueryV5,
|
|
1268
|
+
hasQueryV5WithDataTagError,
|
|
1269
|
+
queryResultVarName,
|
|
1270
|
+
queryOptionsVarName
|
|
1271
|
+
})}
|
|
1078
1272
|
}\n
|
|
1079
1273
|
${prefetch}
|
|
1080
1274
|
${shouldGenerateInvalidate ? `${doc}export const ${invalidateFnName} = async (\n queryClient: QueryClient, ${queryProps} options?: InvalidateOptions\n ): Promise<QueryClient> => {
|
|
@@ -1085,14 +1279,16 @@ ${shouldGenerateInvalidate ? `${doc}export const ${invalidateFnName} = async (\n
|
|
|
1085
1279
|
}\n` : ""}
|
|
1086
1280
|
`;
|
|
1087
1281
|
};
|
|
1088
|
-
const generateQueryHook = async (
|
|
1282
|
+
const generateQueryHook = async (verbOptions, options, outputClient) => {
|
|
1283
|
+
const { queryParams, operationName, body, props: _props, verb, params, override, mutator, response, operationId, summary, deprecated } = verbOptions;
|
|
1284
|
+
const { route, override: { operations }, context, output } = options;
|
|
1089
1285
|
let props = _props;
|
|
1090
1286
|
if (isVue(outputClient)) props = vueWrapTypeWithMaybeRef(_props);
|
|
1091
|
-
const query = override
|
|
1092
|
-
const isRequestOptions = override
|
|
1287
|
+
const query = override.query;
|
|
1288
|
+
const isRequestOptions = override.requestOptions !== false;
|
|
1093
1289
|
const operationQueryOptions = operations[operationId]?.query;
|
|
1094
1290
|
const isExactOptionalPropertyTypes = !!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
|
|
1095
|
-
const queryVersion =
|
|
1291
|
+
const queryVersion = query.version;
|
|
1096
1292
|
const hasVueQueryV4 = OutputClient.VUE_QUERY === outputClient && (!isVueQueryV3(context.output.packageJson) || queryVersion === 4);
|
|
1097
1293
|
const hasSvelteQueryV4 = OutputClient.SVELTE_QUERY === outputClient && (!isSvelteQueryV3(context.output.packageJson) || queryVersion === 4);
|
|
1098
1294
|
const hasSvelteQueryV6 = OutputClient.SVELTE_QUERY === outputClient && isSvelteQueryV6(context.output.packageJson);
|
|
@@ -1107,8 +1303,18 @@ const generateQueryHook = async ({ queryParams, operationName, body, props: _pro
|
|
|
1107
1303
|
});
|
|
1108
1304
|
let implementation = "";
|
|
1109
1305
|
let mutators;
|
|
1110
|
-
const hasOperationQueryOption =
|
|
1111
|
-
|
|
1306
|
+
const hasOperationQueryOption = [
|
|
1307
|
+
operationQueryOptions?.useQuery,
|
|
1308
|
+
operationQueryOptions?.useSuspenseQuery,
|
|
1309
|
+
operationQueryOptions?.useInfinite,
|
|
1310
|
+
operationQueryOptions?.useSuspenseInfiniteQuery
|
|
1311
|
+
].some(Boolean);
|
|
1312
|
+
let isQuery = Verbs.GET === verb && [
|
|
1313
|
+
override.query.useQuery,
|
|
1314
|
+
override.query.useSuspenseQuery,
|
|
1315
|
+
override.query.useInfinite,
|
|
1316
|
+
override.query.useSuspenseInfiniteQuery
|
|
1317
|
+
].some(Boolean) || hasOperationQueryOption;
|
|
1112
1318
|
let isMutation = override.query.useMutation && verb !== Verbs.GET;
|
|
1113
1319
|
if (operationQueryOptions?.useMutation !== void 0) isMutation = operationQueryOptions.useMutation;
|
|
1114
1320
|
if (verb !== Verbs.GET && isQuery) isMutation = false;
|
|
@@ -1137,36 +1343,36 @@ const generateQueryHook = async ({ queryParams, operationName, body, props: _pro
|
|
|
1137
1343
|
return param.type === GetterPropType.BODY ? body.implementation : param.name;
|
|
1138
1344
|
}).join(",");
|
|
1139
1345
|
const queries = [
|
|
1140
|
-
...query
|
|
1346
|
+
...query.useInfinite || operationQueryOptions?.useInfinite ? [{
|
|
1141
1347
|
name: camel(`${operationName}-infinite`),
|
|
1142
|
-
options: query
|
|
1348
|
+
options: query.options,
|
|
1143
1349
|
type: QueryType.INFINITE,
|
|
1144
|
-
queryParam: query
|
|
1350
|
+
queryParam: query.useInfiniteQueryParam,
|
|
1145
1351
|
queryKeyFnName: camel(`get-${operationName}-infinite-query-key`)
|
|
1146
1352
|
}] : [],
|
|
1147
|
-
...query
|
|
1353
|
+
...query.useQuery || operationQueryOptions?.useQuery ? [{
|
|
1148
1354
|
name: operationName,
|
|
1149
|
-
options: query
|
|
1355
|
+
options: query.options,
|
|
1150
1356
|
type: QueryType.QUERY,
|
|
1151
1357
|
queryKeyFnName: camel(`get-${operationName}-query-key`)
|
|
1152
1358
|
}] : [],
|
|
1153
|
-
...query
|
|
1359
|
+
...query.useSuspenseQuery || operationQueryOptions?.useSuspenseQuery ? [{
|
|
1154
1360
|
name: camel(`${operationName}-suspense`),
|
|
1155
|
-
options: query
|
|
1361
|
+
options: query.options,
|
|
1156
1362
|
type: QueryType.SUSPENSE_QUERY,
|
|
1157
1363
|
queryKeyFnName: camel(`get-${operationName}-query-key`)
|
|
1158
1364
|
}] : [],
|
|
1159
|
-
...query
|
|
1365
|
+
...query.useSuspenseInfiniteQuery || operationQueryOptions?.useSuspenseInfiniteQuery ? [{
|
|
1160
1366
|
name: camel(`${operationName}-suspense-infinite`),
|
|
1161
|
-
options: query
|
|
1367
|
+
options: query.options,
|
|
1162
1368
|
type: QueryType.SUSPENSE_INFINITE,
|
|
1163
|
-
queryParam: query
|
|
1369
|
+
queryParam: query.useInfiniteQueryParam,
|
|
1164
1370
|
queryKeyFnName: camel(`get-${operationName}-infinite-query-key`)
|
|
1165
1371
|
}] : []
|
|
1166
1372
|
];
|
|
1167
1373
|
const makeParamsOptional = (params$1) => {
|
|
1168
1374
|
if (!params$1) return "";
|
|
1169
|
-
return params$1.replaceAll(/(\w+)(\?)?:\s*([^=,}]*?)\s*(=\s*[^,}]*)?([,}]|$)/g, (
|
|
1375
|
+
return params$1.replaceAll(/(\w+)(\?)?:\s*([^=,}]*?)\s*(=\s*[^,}]*)?([,}]|$)/g, (_match, paramName, _optionalMarker, type, defaultValue, suffix) => {
|
|
1170
1376
|
if (defaultValue) return `${paramName}: ${type.trim()}${defaultValue}${suffix}`;
|
|
1171
1377
|
return `${paramName}?: ${type.trim()}${suffix}`;
|
|
1172
1378
|
});
|
|
@@ -1207,10 +1413,7 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
|
|
|
1207
1413
|
outputClient,
|
|
1208
1414
|
httpClient,
|
|
1209
1415
|
isExactOptionalPropertyTypes,
|
|
1210
|
-
hasSignal: getHasSignal({
|
|
1211
|
-
overrideQuerySignal: override.query.signal,
|
|
1212
|
-
verb
|
|
1213
|
-
}),
|
|
1416
|
+
hasSignal: getHasSignal({ overrideQuerySignal: override.query.signal }),
|
|
1214
1417
|
queryOptionsMutator,
|
|
1215
1418
|
queryKeyMutator,
|
|
1216
1419
|
route,
|
|
@@ -1231,87 +1434,33 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
|
|
|
1231
1434
|
mutators = queryOptionsMutator || queryKeyMutator ? [...queryOptionsMutator ? [queryOptionsMutator] : [], ...queryKeyMutator ? [queryKeyMutator] : []] : void 0;
|
|
1232
1435
|
}
|
|
1233
1436
|
if (isMutation) {
|
|
1234
|
-
const
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
const definitions = props.map(({ definition, type }) => type === GetterPropType.BODY ? mutator?.bodyTypeName ? `data: ${mutator.bodyTypeName}<${body.definition}>` : `data: ${body.definition}` : definition).join(";");
|
|
1242
|
-
const properties = props.map(({ name, type }) => type === GetterPropType.BODY ? "data" : name).join(",");
|
|
1243
|
-
const errorType = getQueryErrorType(operationName, response, httpClient, mutator);
|
|
1244
|
-
const dataType = mutator?.isHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`;
|
|
1245
|
-
const mutationOptionFnReturnType = getQueryOptionsDefinition({
|
|
1246
|
-
operationName,
|
|
1247
|
-
mutator,
|
|
1248
|
-
definitions,
|
|
1249
|
-
hasSvelteQueryV4,
|
|
1437
|
+
const mutationResult = await generateMutationHook({
|
|
1438
|
+
verbOptions: {
|
|
1439
|
+
...verbOptions,
|
|
1440
|
+
props
|
|
1441
|
+
},
|
|
1442
|
+
options,
|
|
1443
|
+
outputClient,
|
|
1250
1444
|
hasQueryV5,
|
|
1251
1445
|
hasQueryV5WithInfiniteQueryOptionsError,
|
|
1252
|
-
isReturnType: true,
|
|
1253
|
-
isAngularClient: isAngular(outputClient)
|
|
1254
|
-
});
|
|
1255
|
-
const mutationArguments = generateQueryArguments({
|
|
1256
|
-
operationName,
|
|
1257
|
-
definitions,
|
|
1258
|
-
mutator,
|
|
1259
|
-
isRequestOptions,
|
|
1260
1446
|
hasSvelteQueryV4,
|
|
1261
|
-
|
|
1262
|
-
|
|
1447
|
+
hasSvelteQueryV6,
|
|
1448
|
+
isRequestOptions,
|
|
1263
1449
|
httpClient,
|
|
1264
|
-
|
|
1450
|
+
doc,
|
|
1451
|
+
isAngularHttp
|
|
1265
1452
|
});
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
const mutationOptionsFn = `export const ${mutationOptionsFnName} = <TError = ${errorType},
|
|
1269
|
-
TContext = unknown>(${mutationArguments}): ${mutationOptionFnReturnType} => {
|
|
1270
|
-
|
|
1271
|
-
${getHooksOptionImplementation(isRequestOptions, httpClient, camel(operationName), mutator)}
|
|
1272
|
-
${isAngularHttp ? " const http = inject(HttpClient);" : ""}
|
|
1273
|
-
|
|
1274
|
-
${mutator?.isHook ? `const ${operationName} = use${pascal(operationName)}Hook()` : ""}
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
const mutationFn: MutationFunction<Awaited<ReturnType<${dataType}>>, ${definitions ? `{${definitions}}` : "void"}> = (${properties ? "props" : ""}) => {
|
|
1278
|
-
${properties ? `const {${properties}} = props ?? {};` : ""}
|
|
1279
|
-
|
|
1280
|
-
return ${operationName}(${isAngularHttp ? "http, " : ""}${properties}${properties ? "," : ""}${getMutationRequestArgs(isRequestOptions, httpClient, mutator)})
|
|
1281
|
-
}
|
|
1282
|
-
|
|
1283
|
-
${mutationOptionsMutator ? `const customOptions = ${mutationOptionsMutator.name}({...mutationOptions, mutationFn}${mutationOptionsMutator.hasSecondArg ? `, { url: \`${route.replaceAll("/${", "/{")}\` }` : ""}${mutationOptionsMutator.hasThirdArg ? `, { operationId: '${operationId}', operationName: '${operationName}' }` : ""});` : ""}
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
return ${mutationOptionsMutator ? "customOptions" : "{ mutationFn, ...mutationOptions }"}}`;
|
|
1287
|
-
const operationPrefix = getFrameworkPrefix(hasSvelteQueryV4, isAngular(outputClient));
|
|
1288
|
-
const optionalQueryClientArgument = hasQueryV5 && !isAngular(outputClient) ? ", queryClient?: QueryClient" : "";
|
|
1289
|
-
implementation += `
|
|
1290
|
-
${mutationOptionsFn}
|
|
1291
|
-
|
|
1292
|
-
export type ${pascal(operationName)}MutationResult = NonNullable<Awaited<ReturnType<${dataType}>>>
|
|
1293
|
-
${body.definition ? `export type ${pascal(operationName)}MutationBody = ${mutator?.bodyTypeName ? `${mutator.bodyTypeName}<${body.definition}>` : body.definition}` : ""}
|
|
1294
|
-
export type ${pascal(operationName)}MutationError = ${errorType}
|
|
1295
|
-
|
|
1296
|
-
${doc}export const ${camel(`${operationPrefix}-${operationName}`)} = <TError = ${errorType},
|
|
1297
|
-
TContext = unknown>(${mutationArguments} ${optionalQueryClientArgument})${generateMutatorReturnType({
|
|
1298
|
-
outputClient,
|
|
1299
|
-
dataType,
|
|
1300
|
-
variableType: definitions ? `{${definitions}}` : "void"
|
|
1301
|
-
})} => {
|
|
1302
|
-
|
|
1303
|
-
const ${mutationOptionsVarName} = ${mutationOptionsFnName}(${isRequestOptions ? "options" : "mutationOptions"});
|
|
1304
|
-
|
|
1305
|
-
return ${operationPrefix}Mutation(${isAngular(outputClient) ? `() => ${mutationOptionsVarName}` : hasSvelteQueryV6 ? `() => ({ ...${mutationOptionsVarName}${optionalQueryClientArgument ? ", queryClient" : ""} })` : `${mutationOptionsVarName}${!isAngular(outputClient) && optionalQueryClientArgument ? ", queryClient" : ""}`});
|
|
1306
|
-
}
|
|
1307
|
-
`;
|
|
1308
|
-
mutators = mutationOptionsMutator ? [...mutators ?? [], mutationOptionsMutator] : mutators;
|
|
1453
|
+
implementation += mutationResult.implementation;
|
|
1454
|
+
mutators = mutationResult.mutators ? [...mutators ?? [], ...mutationResult.mutators] : mutators;
|
|
1309
1455
|
}
|
|
1310
1456
|
return {
|
|
1311
1457
|
implementation,
|
|
1312
1458
|
mutators
|
|
1313
1459
|
};
|
|
1314
1460
|
};
|
|
1461
|
+
|
|
1462
|
+
//#endregion
|
|
1463
|
+
//#region src/index.ts
|
|
1315
1464
|
const generateQueryHeader = (params) => {
|
|
1316
1465
|
return `${params.hasAwaitedType ? "" : `type AwaitedInput<T> = PromiseLike<T> | T;\n
|
|
1317
1466
|
type Awaited<O> = O extends AwaitedInput<infer T> ? T : never;\n\n`}
|
|
@@ -1333,7 +1482,8 @@ const dependenciesBuilder = {
|
|
|
1333
1482
|
"react-query": getReactQueryDependencies,
|
|
1334
1483
|
"vue-query": getVueQueryDependencies,
|
|
1335
1484
|
"svelte-query": getSvelteQueryDependencies,
|
|
1336
|
-
"angular-query": getAngularQueryDependencies
|
|
1485
|
+
"angular-query": getAngularQueryDependencies,
|
|
1486
|
+
"solid-query": getSolidQueryDependencies
|
|
1337
1487
|
};
|
|
1338
1488
|
const builder = ({ type = "react-query", options: queryOptions, output } = {}) => () => {
|
|
1339
1489
|
const client = (verbOptions, options, outputClient) => {
|
|
@@ -1354,5 +1504,5 @@ const builder = ({ type = "react-query", options: queryOptions, output } = {}) =
|
|
|
1354
1504
|
var src_default = builder;
|
|
1355
1505
|
|
|
1356
1506
|
//#endregion
|
|
1357
|
-
export { builder, src_default as default, generateQuery, generateQueryHeader, getAngularQueryDependencies, getReactQueryDependencies, getSvelteQueryDependencies, getVueQueryDependencies };
|
|
1507
|
+
export { builder, src_default as default, generateQuery, generateQueryHeader, getAngularQueryDependencies, getReactQueryDependencies, getSolidQueryDependencies, getSvelteQueryDependencies, getVueQueryDependencies };
|
|
1358
1508
|
//# sourceMappingURL=index.mjs.map
|