@orval/query 8.0.0-rc.5 → 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 +7 -3
- package/dist/index.mjs +302 -150
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
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},
|
|
@@ -207,14 +205,11 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
207
205
|
let props = _props;
|
|
208
206
|
let route = _route;
|
|
209
207
|
if (isVue$1) props = vueWrapTypeWithMaybeRef(_props);
|
|
210
|
-
if (context.output
|
|
208
|
+
if (context.output.urlEncodeParameters) route = makeRouteSafe(route);
|
|
211
209
|
const isRequestOptions = override.requestOptions !== false;
|
|
212
210
|
const isFormData = !override.formData.disabled;
|
|
213
211
|
const isFormUrlEncoded = override.formUrlEncoded !== false;
|
|
214
|
-
const hasSignal = getHasSignal({
|
|
215
|
-
overrideQuerySignal: override.query.signal,
|
|
216
|
-
verb
|
|
217
|
-
});
|
|
212
|
+
const hasSignal = getHasSignal({ overrideQuerySignal: override.query.signal });
|
|
218
213
|
const isExactOptionalPropertyTypes = !!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
|
|
219
214
|
const bodyForm = generateFormDataAndUrlEncodedFunction({
|
|
220
215
|
formData,
|
|
@@ -238,7 +233,7 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
238
233
|
isVue: isVue$1
|
|
239
234
|
});
|
|
240
235
|
const bodyDefinition = body.definition.replace("[]", String.raw`\[\]`);
|
|
241
|
-
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");
|
|
242
237
|
const requestOptions = isRequestOptions ? generateMutatorRequestOptions(override.requestOptions, mutator.hasSecondArg) : "";
|
|
243
238
|
if (mutator.isHook) {
|
|
244
239
|
const ret = `${override.query.shouldExportMutatorHooks ? "export " : ""}const use${pascal(operationName)}Hook = () => {
|
|
@@ -280,11 +275,11 @@ const generateAxiosRequestFunction = ({ headers, queryParams, operationName, res
|
|
|
280
275
|
queryParams,
|
|
281
276
|
response,
|
|
282
277
|
verb,
|
|
283
|
-
requestOptions: override
|
|
278
|
+
requestOptions: override.requestOptions,
|
|
284
279
|
isFormData,
|
|
285
280
|
isFormUrlEncoded,
|
|
286
281
|
paramsSerializer,
|
|
287
|
-
paramsSerializerOptions: override
|
|
282
|
+
paramsSerializerOptions: override.paramsSerializerOptions,
|
|
288
283
|
isExactOptionalPropertyTypes,
|
|
289
284
|
hasSignal,
|
|
290
285
|
isVue: isVue$1
|
|
@@ -347,7 +342,7 @@ const getQueryErrorType = (operationName, response, httpClient, mutator) => {
|
|
|
347
342
|
const getHooksOptionImplementation = (isRequestOptions, httpClient, operationName, mutator) => {
|
|
348
343
|
const options = httpClient === OutputHttpClient.AXIOS ? ", axios: axiosOptions" : ", fetch: fetchOptions";
|
|
349
344
|
return isRequestOptions ? `const mutationKey = ['${operationName}'];
|
|
350
|
-
const {mutation: mutationOptions${mutator ? mutator
|
|
345
|
+
const {mutation: mutationOptions${mutator ? mutator.hasSecondArg ? ", request: requestOptions" : "" : options}} = options ?
|
|
351
346
|
options.mutation && 'mutationKey' in options.mutation && options.mutation.mutationKey ?
|
|
352
347
|
options
|
|
353
348
|
: {...options, mutation: {...options.mutation, mutationKey}}
|
|
@@ -355,11 +350,10 @@ const {mutation: mutationOptions${mutator ? mutator?.hasSecondArg ? ", request:
|
|
|
355
350
|
};
|
|
356
351
|
const getMutationRequestArgs = (isRequestOptions, httpClient, mutator) => {
|
|
357
352
|
const options = httpClient === OutputHttpClient.AXIOS ? "axiosOptions" : "fetchOptions";
|
|
358
|
-
return isRequestOptions ? mutator ? mutator
|
|
353
|
+
return isRequestOptions ? mutator ? mutator.hasSecondArg ? "requestOptions" : "" : options : "";
|
|
359
354
|
};
|
|
360
355
|
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;
|
|
356
|
+
const result = isVue$1 && httpClient === OutputHttpClient.FETCH && queryProperties ? queryProperties.split(",").map((prop) => `unref(${prop})`).join(",") : queryProperties;
|
|
363
357
|
if (isAngular$1 || httpClient === OutputHttpClient.ANGULAR) return result ? `http, ${result}` : "http";
|
|
364
358
|
return result;
|
|
365
359
|
};
|
|
@@ -368,21 +362,7 @@ const getQueryHeader = (params) => {
|
|
|
368
362
|
};
|
|
369
363
|
|
|
370
364
|
//#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
|
-
};
|
|
365
|
+
//#region src/dependencies.ts
|
|
386
366
|
const REACT_DEPENDENCIES = [{
|
|
387
367
|
exports: [{
|
|
388
368
|
name: "useCallback",
|
|
@@ -557,7 +537,7 @@ const REACT_QUERY_DEPENDENCIES = [{
|
|
|
557
537
|
const getReactQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient, hasTagsMutator, override) => {
|
|
558
538
|
const hasReactQuery = packageJson?.dependencies?.["react-query"] ?? packageJson?.devDependencies?.["react-query"] ?? packageJson?.peerDependencies?.["react-query"];
|
|
559
539
|
const hasReactQueryV4 = packageJson?.dependencies?.["@tanstack/react-query"] ?? packageJson?.devDependencies?.["@tanstack/react-query"] ?? packageJson?.peerDependencies?.["@tanstack/react-query"];
|
|
560
|
-
const useReactQueryV3 = override
|
|
540
|
+
const useReactQueryV3 = override.query.version === void 0 ? hasReactQuery && !hasReactQueryV4 : override.query.version <= 3;
|
|
561
541
|
return [
|
|
562
542
|
...hasGlobalMutator || hasTagsMutator ? REACT_DEPENDENCIES : [],
|
|
563
543
|
...!hasGlobalMutator && httpClient === OutputHttpClient.AXIOS ? AXIOS_DEPENDENCIES : [],
|
|
@@ -656,6 +636,36 @@ const VUE_QUERY_DEPENDENCIES = [{
|
|
|
656
636
|
],
|
|
657
637
|
dependency: "vue"
|
|
658
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
|
+
}];
|
|
659
669
|
const ANGULAR_QUERY_DEPENDENCIES = [{
|
|
660
670
|
exports: [
|
|
661
671
|
{
|
|
@@ -683,8 +693,12 @@ const ANGULAR_QUERY_DEPENDENCIES = [{
|
|
|
683
693
|
{ name: "InfiniteData" },
|
|
684
694
|
{ name: "CreateMutationResult" },
|
|
685
695
|
{ name: "DataTag" },
|
|
686
|
-
{
|
|
687
|
-
|
|
696
|
+
{
|
|
697
|
+
name: "QueryClient",
|
|
698
|
+
values: true
|
|
699
|
+
},
|
|
700
|
+
{ name: "InvalidateOptions" },
|
|
701
|
+
{ name: "MutationFunctionContext" }
|
|
688
702
|
],
|
|
689
703
|
dependency: "@tanstack/angular-query-experimental"
|
|
690
704
|
}, {
|
|
@@ -714,6 +728,13 @@ const getVueQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, p
|
|
|
714
728
|
...hasVueQueryV3 ? VUE_QUERY_DEPENDENCIES_V3 : VUE_QUERY_DEPENDENCIES
|
|
715
729
|
];
|
|
716
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
|
+
};
|
|
717
738
|
const getAngularQueryDependencies = (hasGlobalMutator, hasParamsSerializerOptions, packageJson, httpClient) => {
|
|
718
739
|
const useAngularHttp = !hasGlobalMutator && httpClient === OutputHttpClient.ANGULAR;
|
|
719
740
|
const useAxios = !hasGlobalMutator && httpClient === OutputHttpClient.AXIOS;
|
|
@@ -757,8 +778,12 @@ const getPackageByQueryClient = (packageJson, queryClient) => {
|
|
|
757
778
|
case "svelte-query": return packageJson?.dependencies?.["@tanstack/svelte-query"] ?? packageJson?.devDependencies?.["@tanstack/svelte-query"] ?? packageJson?.peerDependencies?.["@tanstack/svelte-query"];
|
|
758
779
|
case "vue-query": return packageJson?.dependencies?.["@tanstack/vue-query"] ?? packageJson?.devDependencies?.["@tanstack/vue-query"] ?? packageJson?.peerDependencies?.["@tanstack/vue-query"];
|
|
759
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"];
|
|
760
782
|
}
|
|
761
783
|
};
|
|
784
|
+
|
|
785
|
+
//#endregion
|
|
786
|
+
//#region src/query-options.ts
|
|
762
787
|
const QueryType = {
|
|
763
788
|
INFINITE: "infiniteQuery",
|
|
764
789
|
QUERY: "query",
|
|
@@ -768,12 +793,12 @@ const QueryType = {
|
|
|
768
793
|
const INFINITE_QUERY_PROPERTIES = new Set(["getNextPageParam", "getPreviousPageParam"]);
|
|
769
794
|
const generateQueryOptions = ({ params, options, type, outputClient }) => {
|
|
770
795
|
if (options === false) return "";
|
|
771
|
-
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)}` : "";
|
|
772
797
|
if (params.length === 0 || isSuspenseQuery(type)) {
|
|
773
798
|
if (options) return `${queryConfig} ...queryOptions`;
|
|
774
799
|
return "...queryOptions";
|
|
775
800
|
}
|
|
776
|
-
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`;
|
|
777
802
|
};
|
|
778
803
|
const isSuspenseQuery = (type) => {
|
|
779
804
|
return [QueryType.SUSPENSE_INFINITE, QueryType.SUSPENSE_QUERY].includes(type);
|
|
@@ -791,7 +816,7 @@ const getQueryOptionsDefinition = ({ operationName, mutator, definitions, type,
|
|
|
791
816
|
${funcReturnType}${hasQueryV5 && (type === QueryType.INFINITE || type === QueryType.SUSPENSE_INFINITE) && queryParam && queryParams ? `, QueryKey` : ""}
|
|
792
817
|
> , 'initialData'
|
|
793
818
|
>` : "";
|
|
794
|
-
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}']` : ""}>`;
|
|
795
820
|
return `${partialOptions ? "Partial<" : ""}${optionType}${partialOptions ? ">" : ""}${optionTypeInitialDataPostfix}`;
|
|
796
821
|
}
|
|
797
822
|
return `${prefix}MutationOptions<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>, TError,${definitions ? `{${definitions}}` : "void"}, TContext>`;
|
|
@@ -816,11 +841,17 @@ const generateQueryArguments = ({ operationName, definitions, mutator, isRequest
|
|
|
816
841
|
const isQueryRequired = initialData === "defined";
|
|
817
842
|
return `options${isQueryRequired ? "" : "?"}: { ${type ? "query" : "mutation"}${isQueryRequired ? "" : "?"}:${definition}, ${requestType}}\n`;
|
|
818
843
|
};
|
|
844
|
+
|
|
845
|
+
//#endregion
|
|
846
|
+
//#region src/return-types.ts
|
|
819
847
|
const generateQueryReturnType = ({ outputClient, type, isMutatorHook, operationName, hasVueQueryV4, hasSvelteQueryV4, hasQueryV5, hasQueryV5WithDataTagError, isInitialDataDefined }) => {
|
|
820
848
|
switch (outputClient) {
|
|
821
849
|
case OutputClient.ANGULAR_QUERY:
|
|
822
850
|
if (type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE) return `CreateQueryResult<TData, TError>`;
|
|
823
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"} }`;
|
|
824
855
|
case OutputClient.SVELTE_QUERY:
|
|
825
856
|
if (!hasSvelteQueryV4) return `Use${pascal(type)}StoreResult<Awaited<ReturnType<${isMutatorHook ? `ReturnType<typeof use${pascal(operationName)}Hook>` : `typeof ${operationName}`}>>, TError, TData, QueryKey> & { queryKey: QueryKey} }`;
|
|
826
857
|
return `Create${pascal(type)}Result<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
@@ -828,7 +859,6 @@ const generateQueryReturnType = ({ outputClient, type, isMutatorHook, operationN
|
|
|
828
859
|
if (!hasVueQueryV4) return ` UseQueryReturnType<TData, TError, Use${pascal(type)}Result<TData, TError>> & { queryKey: QueryKey} }`;
|
|
829
860
|
if (type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE) return `UseQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
830
861
|
return `UseInfiniteQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
831
|
-
case OutputClient.REACT_QUERY:
|
|
832
862
|
default: return ` ${isInitialDataDefined && !isSuspenseQuery(type) ? "Defined" : ""}Use${pascal(type)}Result<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`;
|
|
833
863
|
}
|
|
834
864
|
};
|
|
@@ -844,6 +874,12 @@ const generateMutatorReturnType = ({ outputClient, dataType, variableType }) =>
|
|
|
844
874
|
TError,
|
|
845
875
|
${variableType},
|
|
846
876
|
TContext
|
|
877
|
+
>`;
|
|
878
|
+
if (outputClient === OutputClient.SOLID_QUERY) return `: CreateMutationResult<
|
|
879
|
+
Awaited<ReturnType<${dataType}>>,
|
|
880
|
+
TError,
|
|
881
|
+
${variableType},
|
|
882
|
+
TContext
|
|
847
883
|
>`;
|
|
848
884
|
if (outputClient === OutputClient.SVELTE_QUERY) return `: CreateMutationResult<
|
|
849
885
|
Awaited<ReturnType<${dataType}>>,
|
|
@@ -867,6 +903,154 @@ const getQueryFnArguments = ({ hasQueryParam, hasSignal }) => {
|
|
|
867
903
|
}
|
|
868
904
|
return "{ signal }";
|
|
869
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
|
+
};
|
|
870
1054
|
const generatePrefetch = ({ usePrefetch, type, useQuery, useInfinite, operationName, mutator, doc, queryProps, dataType, errorType, queryArguments, queryOptionsVarName, queryOptionsFnName, queryProperties, isRequestOptions, hasSvelteQueryV6 }) => {
|
|
871
1055
|
if (!(usePrefetch && (type === QueryType.QUERY || type === QueryType.INFINITE || type === QueryType.SUSPENSE_QUERY && !useQuery || type === QueryType.SUSPENSE_INFINITE && !useInfinite))) return "";
|
|
872
1056
|
const prefetchType = type === QueryType.QUERY || type === QueryType.SUSPENSE_QUERY ? "query" : "infinite-query";
|
|
@@ -1012,7 +1196,7 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
1012
1196
|
const queryOptionsFnName = camel(queryKeyMutator || queryOptionsMutator || mutator?.isHook ? `use-${name}-queryOptions` : `get-${name}-queryOptions`);
|
|
1013
1197
|
const queryOptionsVarName = isRequestOptions ? "queryOptions" : "options";
|
|
1014
1198
|
const queryResultVarName = props.some((prop) => prop.name === "query") ? "_query" : "query";
|
|
1015
|
-
const infiniteParam = queryParams && queryParam ? `, ${queryParams
|
|
1199
|
+
const infiniteParam = queryParams && queryParam ? `, ${queryParams.schema.name}['${queryParam}']` : "";
|
|
1016
1200
|
const TData = hasQueryV5 && (type === QueryType.INFINITE || type === QueryType.SUSPENSE_INFINITE) ? `InfiniteData<Awaited<ReturnType<${dataType}>>${infiniteParam}>` : `Awaited<ReturnType<${dataType}>>`;
|
|
1017
1201
|
const queryOptionsFn = `export const ${queryOptionsFnName} = <TData = ${TData}, TError = ${errorType}>(${queryProps} ${queryArguments}) => {
|
|
1018
1202
|
|
|
@@ -1023,7 +1207,7 @@ ${isAngularHttp ? " const http = inject(HttpClient);" : ""}
|
|
|
1023
1207
|
|
|
1024
1208
|
${mutator?.isHook ? `const ${operationName} = use${pascal(operationName)}Hook();` : ""}
|
|
1025
1209
|
|
|
1026
|
-
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});
|
|
1027
1211
|
|
|
1028
1212
|
${isVue(outputClient) ? vueUnRefParams(props.filter((prop) => prop.type === GetterPropType.NAMED_PATH_PARAMS)) : ""}
|
|
1029
1213
|
|
|
@@ -1031,8 +1215,8 @@ ${isAngularHttp ? " const http = inject(HttpClient);" : ""}
|
|
|
1031
1215
|
|
|
1032
1216
|
return ${queryOptionsMutator ? "customOptions" : `{ queryKey, queryFn, ${queryOptionsImp}}`} as ${queryOptionFnReturnType} ${isVue(outputClient) || isAngular(outputClient) ? "" : `& { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ", TError" : ""}>` : "QueryKey"} }`}
|
|
1033
1217
|
}`;
|
|
1034
|
-
const operationPrefix = getFrameworkPrefix(hasSvelteQueryV4, isAngular(outputClient));
|
|
1035
|
-
const optionalQueryClientArgument = hasQueryV5 && !isAngular(outputClient) ? ", queryClient?: QueryClient" : "";
|
|
1218
|
+
const operationPrefix = getFrameworkPrefix(hasSvelteQueryV4, isAngular(outputClient), isSolid(outputClient));
|
|
1219
|
+
const optionalQueryClientArgument = hasSvelteQueryV6 ? `, queryClient?: () => QueryClient` : hasQueryV5 && !isAngular(outputClient) ? ", queryClient?: QueryClient" : "";
|
|
1036
1220
|
const queryHookName = camel(`${operationPrefix}-${name}`);
|
|
1037
1221
|
const overrideTypes = `
|
|
1038
1222
|
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${definedInitialDataQueryPropsDefinitions} ${definedInitialDataQueryArguments} ${optionalQueryClientArgument}\n ): ${definedInitialDataReturnType}
|
|
@@ -1045,7 +1229,7 @@ export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${q
|
|
|
1045
1229
|
useInfinite,
|
|
1046
1230
|
operationName,
|
|
1047
1231
|
mutator,
|
|
1048
|
-
queryProps,
|
|
1232
|
+
queryProps: hasSvelteQueryV6 ? queryProps.replace(":", ": () => ") : queryProps,
|
|
1049
1233
|
dataType,
|
|
1050
1234
|
errorType,
|
|
1051
1235
|
hasSvelteQueryV6,
|
|
@@ -1066,15 +1250,27 @@ export type ${pascal(name)}QueryError = ${errorType}
|
|
|
1066
1250
|
|
|
1067
1251
|
${hasQueryV5 && OutputClient.REACT_QUERY === outputClient ? overrideTypes : ""}
|
|
1068
1252
|
${doc}
|
|
1069
|
-
export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${
|
|
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} {
|
|
1070
1257
|
|
|
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};
|
|
1258
|
+
${hasSvelteQueryV6 ? "" : `const ${queryOptionsVarName} = ${queryOptionsFnName}(${queryProperties}${queryProperties ? "," : ""}${isRequestOptions ? "options" : "queryOptions"})`}
|
|
1074
1259
|
|
|
1075
|
-
${
|
|
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};
|
|
1076
1264
|
|
|
1077
|
-
|
|
1265
|
+
${getQueryReturnStatement({
|
|
1266
|
+
outputClient,
|
|
1267
|
+
hasSvelteQueryV4,
|
|
1268
|
+
hasSvelteQueryV6,
|
|
1269
|
+
hasQueryV5,
|
|
1270
|
+
hasQueryV5WithDataTagError,
|
|
1271
|
+
queryResultVarName,
|
|
1272
|
+
queryOptionsVarName
|
|
1273
|
+
})}
|
|
1078
1274
|
}\n
|
|
1079
1275
|
${prefetch}
|
|
1080
1276
|
${shouldGenerateInvalidate ? `${doc}export const ${invalidateFnName} = async (\n queryClient: QueryClient, ${queryProps} options?: InvalidateOptions\n ): Promise<QueryClient> => {
|
|
@@ -1085,14 +1281,16 @@ ${shouldGenerateInvalidate ? `${doc}export const ${invalidateFnName} = async (\n
|
|
|
1085
1281
|
}\n` : ""}
|
|
1086
1282
|
`;
|
|
1087
1283
|
};
|
|
1088
|
-
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;
|
|
1089
1287
|
let props = _props;
|
|
1090
1288
|
if (isVue(outputClient)) props = vueWrapTypeWithMaybeRef(_props);
|
|
1091
|
-
const query = override
|
|
1092
|
-
const isRequestOptions = override
|
|
1289
|
+
const query = override.query;
|
|
1290
|
+
const isRequestOptions = override.requestOptions !== false;
|
|
1093
1291
|
const operationQueryOptions = operations[operationId]?.query;
|
|
1094
1292
|
const isExactOptionalPropertyTypes = !!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
|
|
1095
|
-
const queryVersion =
|
|
1293
|
+
const queryVersion = query.version;
|
|
1096
1294
|
const hasVueQueryV4 = OutputClient.VUE_QUERY === outputClient && (!isVueQueryV3(context.output.packageJson) || queryVersion === 4);
|
|
1097
1295
|
const hasSvelteQueryV4 = OutputClient.SVELTE_QUERY === outputClient && (!isSvelteQueryV3(context.output.packageJson) || queryVersion === 4);
|
|
1098
1296
|
const hasSvelteQueryV6 = OutputClient.SVELTE_QUERY === outputClient && isSvelteQueryV6(context.output.packageJson);
|
|
@@ -1107,8 +1305,18 @@ const generateQueryHook = async ({ queryParams, operationName, body, props: _pro
|
|
|
1107
1305
|
});
|
|
1108
1306
|
let implementation = "";
|
|
1109
1307
|
let mutators;
|
|
1110
|
-
const hasOperationQueryOption =
|
|
1111
|
-
|
|
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;
|
|
1112
1320
|
let isMutation = override.query.useMutation && verb !== Verbs.GET;
|
|
1113
1321
|
if (operationQueryOptions?.useMutation !== void 0) isMutation = operationQueryOptions.useMutation;
|
|
1114
1322
|
if (verb !== Verbs.GET && isQuery) isMutation = false;
|
|
@@ -1137,36 +1345,36 @@ const generateQueryHook = async ({ queryParams, operationName, body, props: _pro
|
|
|
1137
1345
|
return param.type === GetterPropType.BODY ? body.implementation : param.name;
|
|
1138
1346
|
}).join(",");
|
|
1139
1347
|
const queries = [
|
|
1140
|
-
...query
|
|
1348
|
+
...query.useInfinite || operationQueryOptions?.useInfinite ? [{
|
|
1141
1349
|
name: camel(`${operationName}-infinite`),
|
|
1142
|
-
options: query
|
|
1350
|
+
options: query.options,
|
|
1143
1351
|
type: QueryType.INFINITE,
|
|
1144
|
-
queryParam: query
|
|
1352
|
+
queryParam: query.useInfiniteQueryParam,
|
|
1145
1353
|
queryKeyFnName: camel(`get-${operationName}-infinite-query-key`)
|
|
1146
1354
|
}] : [],
|
|
1147
|
-
...query
|
|
1355
|
+
...query.useQuery || operationQueryOptions?.useQuery ? [{
|
|
1148
1356
|
name: operationName,
|
|
1149
|
-
options: query
|
|
1357
|
+
options: query.options,
|
|
1150
1358
|
type: QueryType.QUERY,
|
|
1151
1359
|
queryKeyFnName: camel(`get-${operationName}-query-key`)
|
|
1152
1360
|
}] : [],
|
|
1153
|
-
...query
|
|
1361
|
+
...query.useSuspenseQuery || operationQueryOptions?.useSuspenseQuery ? [{
|
|
1154
1362
|
name: camel(`${operationName}-suspense`),
|
|
1155
|
-
options: query
|
|
1363
|
+
options: query.options,
|
|
1156
1364
|
type: QueryType.SUSPENSE_QUERY,
|
|
1157
1365
|
queryKeyFnName: camel(`get-${operationName}-query-key`)
|
|
1158
1366
|
}] : [],
|
|
1159
|
-
...query
|
|
1367
|
+
...query.useSuspenseInfiniteQuery || operationQueryOptions?.useSuspenseInfiniteQuery ? [{
|
|
1160
1368
|
name: camel(`${operationName}-suspense-infinite`),
|
|
1161
|
-
options: query
|
|
1369
|
+
options: query.options,
|
|
1162
1370
|
type: QueryType.SUSPENSE_INFINITE,
|
|
1163
|
-
queryParam: query
|
|
1371
|
+
queryParam: query.useInfiniteQueryParam,
|
|
1164
1372
|
queryKeyFnName: camel(`get-${operationName}-infinite-query-key`)
|
|
1165
1373
|
}] : []
|
|
1166
1374
|
];
|
|
1167
1375
|
const makeParamsOptional = (params$1) => {
|
|
1168
1376
|
if (!params$1) return "";
|
|
1169
|
-
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) => {
|
|
1170
1378
|
if (defaultValue) return `${paramName}: ${type.trim()}${defaultValue}${suffix}`;
|
|
1171
1379
|
return `${paramName}?: ${type.trim()}${suffix}`;
|
|
1172
1380
|
});
|
|
@@ -1207,10 +1415,7 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
|
|
|
1207
1415
|
outputClient,
|
|
1208
1416
|
httpClient,
|
|
1209
1417
|
isExactOptionalPropertyTypes,
|
|
1210
|
-
hasSignal: getHasSignal({
|
|
1211
|
-
overrideQuerySignal: override.query.signal,
|
|
1212
|
-
verb
|
|
1213
|
-
}),
|
|
1418
|
+
hasSignal: getHasSignal({ overrideQuerySignal: override.query.signal }),
|
|
1214
1419
|
queryOptionsMutator,
|
|
1215
1420
|
queryKeyMutator,
|
|
1216
1421
|
route,
|
|
@@ -1231,87 +1436,33 @@ ${override.query.shouldExportQueryKey ? "export " : ""}const ${queryOption.query
|
|
|
1231
1436
|
mutators = queryOptionsMutator || queryKeyMutator ? [...queryOptionsMutator ? [queryOptionsMutator] : [], ...queryKeyMutator ? [queryKeyMutator] : []] : void 0;
|
|
1232
1437
|
}
|
|
1233
1438
|
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,
|
|
1439
|
+
const mutationResult = await generateMutationHook({
|
|
1440
|
+
verbOptions: {
|
|
1441
|
+
...verbOptions,
|
|
1442
|
+
props
|
|
1443
|
+
},
|
|
1444
|
+
options,
|
|
1445
|
+
outputClient,
|
|
1250
1446
|
hasQueryV5,
|
|
1251
1447
|
hasQueryV5WithInfiniteQueryOptionsError,
|
|
1252
|
-
isReturnType: true,
|
|
1253
|
-
isAngularClient: isAngular(outputClient)
|
|
1254
|
-
});
|
|
1255
|
-
const mutationArguments = generateQueryArguments({
|
|
1256
|
-
operationName,
|
|
1257
|
-
definitions,
|
|
1258
|
-
mutator,
|
|
1259
|
-
isRequestOptions,
|
|
1260
1448
|
hasSvelteQueryV4,
|
|
1261
|
-
|
|
1262
|
-
|
|
1449
|
+
hasSvelteQueryV6,
|
|
1450
|
+
isRequestOptions,
|
|
1263
1451
|
httpClient,
|
|
1264
|
-
|
|
1452
|
+
doc,
|
|
1453
|
+
isAngularHttp
|
|
1265
1454
|
});
|
|
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;
|
|
1455
|
+
implementation += mutationResult.implementation;
|
|
1456
|
+
mutators = mutationResult.mutators ? [...mutators ?? [], ...mutationResult.mutators] : mutators;
|
|
1309
1457
|
}
|
|
1310
1458
|
return {
|
|
1311
1459
|
implementation,
|
|
1312
1460
|
mutators
|
|
1313
1461
|
};
|
|
1314
1462
|
};
|
|
1463
|
+
|
|
1464
|
+
//#endregion
|
|
1465
|
+
//#region src/index.ts
|
|
1315
1466
|
const generateQueryHeader = (params) => {
|
|
1316
1467
|
return `${params.hasAwaitedType ? "" : `type AwaitedInput<T> = PromiseLike<T> | T;\n
|
|
1317
1468
|
type Awaited<O> = O extends AwaitedInput<infer T> ? T : never;\n\n`}
|
|
@@ -1333,7 +1484,8 @@ const dependenciesBuilder = {
|
|
|
1333
1484
|
"react-query": getReactQueryDependencies,
|
|
1334
1485
|
"vue-query": getVueQueryDependencies,
|
|
1335
1486
|
"svelte-query": getSvelteQueryDependencies,
|
|
1336
|
-
"angular-query": getAngularQueryDependencies
|
|
1487
|
+
"angular-query": getAngularQueryDependencies,
|
|
1488
|
+
"solid-query": getSolidQueryDependencies
|
|
1337
1489
|
};
|
|
1338
1490
|
const builder = ({ type = "react-query", options: queryOptions, output } = {}) => () => {
|
|
1339
1491
|
const client = (verbOptions, options, outputClient) => {
|
|
@@ -1354,5 +1506,5 @@ const builder = ({ type = "react-query", options: queryOptions, output } = {}) =
|
|
|
1354
1506
|
var src_default = builder;
|
|
1355
1507
|
|
|
1356
1508
|
//#endregion
|
|
1357
|
-
export { builder, src_default as default, generateQuery, generateQueryHeader, getAngularQueryDependencies, getReactQueryDependencies, getSvelteQueryDependencies, getVueQueryDependencies };
|
|
1509
|
+
export { builder, src_default as default, generateQuery, generateQueryHeader, getAngularQueryDependencies, getReactQueryDependencies, getSolidQueryDependencies, getSvelteQueryDependencies, getVueQueryDependencies };
|
|
1358
1510
|
//# sourceMappingURL=index.mjs.map
|