@kubb/plugin-swr 5.0.0-beta.85 → 5.0.0-beta.86
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +92 -122
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +75 -46
- package/dist/index.js +92 -122
- package/dist/index.js.map +1 -1
- package/package.json +5 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
|
|
2
|
-
import { Exclude, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ast } from "kubb/kit";
|
|
2
|
+
import { Exclude, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ResolverPatch, ast } from "kubb/kit";
|
|
3
3
|
//#region ../../internals/client/src/resolveClient.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* Resolves which client plugin a consumer (react-query, vue-query, swr, mcp) should call for an
|
|
@@ -31,56 +31,85 @@ type Transformer = (props: {
|
|
|
31
31
|
//#endregion
|
|
32
32
|
//#region src/types.d.ts
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
34
|
+
* The concrete resolver type for `@kubb/plugin-swr`.
|
|
35
|
+
* Extends the base `Resolver` (which provides `default` naming and option helpers, the top-level
|
|
36
|
+
* `name` casing, and the `file` builder) with plugin-specific naming namespaces for the generated
|
|
37
|
+
* `useSWR` and `useSWRMutation` hooks and their companion helpers.
|
|
35
38
|
*/
|
|
36
39
|
type ResolverSwr = Resolver & {
|
|
37
40
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* @example Resolving base operation names
|
|
41
|
-
* `resolver.resolveName('show pet by id') // -> 'showPetById'`
|
|
42
|
-
*/
|
|
43
|
-
resolveName(this: ResolverSwr, name: string): string;
|
|
44
|
-
/**
|
|
45
|
-
* Resolves the output file name for a hook module.
|
|
46
|
-
*/
|
|
47
|
-
resolvePathName(this: ResolverSwr, name: string, type?: 'file' | 'function' | 'type' | 'const'): string;
|
|
48
|
-
/**
|
|
49
|
-
* Resolves a query hook function name.
|
|
50
|
-
*/
|
|
51
|
-
resolveQueryName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
52
|
-
/**
|
|
53
|
-
* Resolves a mutation hook function name.
|
|
54
|
-
*/
|
|
55
|
-
resolveMutationName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
56
|
-
/**
|
|
57
|
-
* Resolves the query options helper name.
|
|
58
|
-
*/
|
|
59
|
-
resolveQueryOptionsName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
60
|
-
/**
|
|
61
|
-
* Resolves the query key helper name.
|
|
62
|
-
*/
|
|
63
|
-
resolveQueryKeyName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
64
|
-
/**
|
|
65
|
-
* Resolves the mutation key helper name.
|
|
66
|
-
*/
|
|
67
|
-
resolveMutationKeyName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
68
|
-
/**
|
|
69
|
-
* Resolves the query key type name.
|
|
70
|
-
*/
|
|
71
|
-
resolveQueryKeyTypeName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
72
|
-
/**
|
|
73
|
-
* Resolves the mutation key type name.
|
|
74
|
-
*/
|
|
75
|
-
resolveMutationKeyTypeName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
76
|
-
/**
|
|
77
|
-
* Resolves the mutation argument type name emitted alongside the mutation hook.
|
|
41
|
+
* Naming for the generated `useSWR` hook and its companion helpers.
|
|
78
42
|
*/
|
|
79
|
-
|
|
43
|
+
query: {
|
|
44
|
+
/**
|
|
45
|
+
* Resolves a query hook function name.
|
|
46
|
+
*
|
|
47
|
+
* @example Query hook names
|
|
48
|
+
* `resolver.query.name(node) // → 'useGetPetById'`
|
|
49
|
+
*/
|
|
50
|
+
name(node: ast.OperationNode): string;
|
|
51
|
+
/**
|
|
52
|
+
* Resolves the query options helper name.
|
|
53
|
+
*
|
|
54
|
+
* @example Query options helper names
|
|
55
|
+
* `resolver.query.optionsName(node) // → 'getPetByIdQueryOptions'`
|
|
56
|
+
*/
|
|
57
|
+
optionsName(node: ast.OperationNode): string;
|
|
58
|
+
/**
|
|
59
|
+
* Resolves the query key helper name.
|
|
60
|
+
*
|
|
61
|
+
* @example Query key helper names
|
|
62
|
+
* `resolver.query.keyName(node) // → 'getPetByIdQueryKey'`
|
|
63
|
+
*/
|
|
64
|
+
keyName(node: ast.OperationNode): string;
|
|
65
|
+
/**
|
|
66
|
+
* Resolves the query key type name.
|
|
67
|
+
*
|
|
68
|
+
* @example Query key type names
|
|
69
|
+
* `resolver.query.keyTypeName(node) // → 'GetPetByIdQueryKey'`
|
|
70
|
+
*/
|
|
71
|
+
keyTypeName(node: ast.OperationNode): string;
|
|
72
|
+
/**
|
|
73
|
+
* Resolves the client function name generated inline by query hooks.
|
|
74
|
+
*
|
|
75
|
+
* @example Client function names
|
|
76
|
+
* `resolver.query.clientName(node) // → 'getPetById'`
|
|
77
|
+
*/
|
|
78
|
+
clientName(node: ast.OperationNode): string;
|
|
79
|
+
};
|
|
80
80
|
/**
|
|
81
|
-
*
|
|
81
|
+
* Naming for the generated `useSWRMutation` hook and its companion helpers.
|
|
82
82
|
*/
|
|
83
|
-
|
|
83
|
+
mutation: {
|
|
84
|
+
/**
|
|
85
|
+
* Resolves a mutation hook function name.
|
|
86
|
+
*
|
|
87
|
+
* @example Mutation hook names
|
|
88
|
+
* `resolver.mutation.name(node) // → 'useUpdatePet'`
|
|
89
|
+
*/
|
|
90
|
+
name(node: ast.OperationNode): string;
|
|
91
|
+
/**
|
|
92
|
+
* Resolves the mutation key helper name.
|
|
93
|
+
*
|
|
94
|
+
* @example Mutation key helper names
|
|
95
|
+
* `resolver.mutation.keyName(node) // → 'updatePetMutationKey'`
|
|
96
|
+
*/
|
|
97
|
+
keyName(node: ast.OperationNode): string;
|
|
98
|
+
/**
|
|
99
|
+
* Resolves the mutation key type name.
|
|
100
|
+
*
|
|
101
|
+
* @example Mutation key type names
|
|
102
|
+
* `resolver.mutation.keyTypeName(node) // → 'UpdatePetMutationKey'`
|
|
103
|
+
*/
|
|
104
|
+
keyTypeName(node: ast.OperationNode): string;
|
|
105
|
+
/**
|
|
106
|
+
* Resolves the mutation argument type name emitted alongside the mutation hook.
|
|
107
|
+
*
|
|
108
|
+
* @example Mutation argument type names
|
|
109
|
+
* `resolver.mutation.argTypeName(node) // → 'UpdatePetMutationArg'`
|
|
110
|
+
*/
|
|
111
|
+
argTypeName(node: ast.OperationNode): string;
|
|
112
|
+
};
|
|
84
113
|
};
|
|
85
114
|
/**
|
|
86
115
|
* Customize the queryKey.
|
|
@@ -173,7 +202,7 @@ type Options = OutputOptions & {
|
|
|
173
202
|
/**
|
|
174
203
|
* Override naming conventions for function names and types.
|
|
175
204
|
*/
|
|
176
|
-
resolver?:
|
|
205
|
+
resolver?: ResolverPatch<ResolverSwr>;
|
|
177
206
|
/**
|
|
178
207
|
* Macros that rewrite generated nodes before printing.
|
|
179
208
|
*/
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
|
|
2
|
-
import
|
|
3
|
-
import { ast, defineGenerator, definePlugin, defineResolver } from "kubb/kit";
|
|
2
|
+
import { Resolver, ast, createResolver, defineGenerator, definePlugin } from "kubb/kit";
|
|
4
3
|
import { createFunctionParameter, createFunctionParameters, createObjectBindingPattern, createTypeLiteral, functionPrinter, pluginTsName } from "@kubb/plugin-ts";
|
|
4
|
+
import path from "node:path";
|
|
5
5
|
import { File, Function, Type, jsxRenderer } from "kubb/jsx";
|
|
6
6
|
import { Fragment, jsx, jsxs } from "kubb/jsx/jsx-runtime";
|
|
7
7
|
//#region ../../internals/utils/src/casing.ts
|
|
@@ -31,31 +31,6 @@ function camelCase(text, { prefix = "", suffix = "" } = {}) {
|
|
|
31
31
|
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
32
32
|
}
|
|
33
33
|
//#endregion
|
|
34
|
-
//#region ../../internals/utils/src/fs.ts
|
|
35
|
-
/**
|
|
36
|
-
* Builds a nested file path from a dotted name. Splits on dots that precede a letter
|
|
37
|
-
* (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases
|
|
38
|
-
* every earlier segment, applies `caseLast` to the final segment, and joins with `/`.
|
|
39
|
-
*
|
|
40
|
-
* Empty segments are dropped before joining. They arise when the name starts with a dot
|
|
41
|
-
* followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to
|
|
42
|
-
* an empty string). Without this a leading `/` would form, which `path.resolve` reads as an
|
|
43
|
-
* absolute path, letting generated files escape the configured output directory.
|
|
44
|
-
*
|
|
45
|
-
* @example Nested path from a dotted name
|
|
46
|
-
* `toFilePath('pet.petId') // 'pet/petId'`
|
|
47
|
-
*
|
|
48
|
-
* @example PascalCase the final segment
|
|
49
|
-
* `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`
|
|
50
|
-
*
|
|
51
|
-
* @example Suffix applied to the final segment only
|
|
52
|
-
* `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`
|
|
53
|
-
*/
|
|
54
|
-
function toFilePath(name, caseLast = camelCase) {
|
|
55
|
-
const parts = name.split(/\.(?=[a-zA-Z])/);
|
|
56
|
-
return parts.map((part, i) => i === parts.length - 1 ? caseLast(part) : camelCase(part)).filter(Boolean).join("/");
|
|
57
|
-
}
|
|
58
|
-
//#endregion
|
|
59
34
|
//#region ../../internals/utils/src/reserved.ts
|
|
60
35
|
/**
|
|
61
36
|
* JavaScript and Java reserved words.
|
|
@@ -311,13 +286,13 @@ function dedupeByCasedName(params) {
|
|
|
311
286
|
//#region ../../internals/shared/src/operation.ts
|
|
312
287
|
/**
|
|
313
288
|
* Builds the `ResolverFileParams` every operation generator passes to
|
|
314
|
-
* `resolver.
|
|
289
|
+
* `resolver.file`: a file named `name`, tagged by the operation's first
|
|
315
290
|
* tag (or `'default'`), at the operation's path. Centralizes the entry object
|
|
316
291
|
* that was repeated at dozens of call sites across the client and query plugins.
|
|
317
292
|
*
|
|
318
293
|
* @example
|
|
319
294
|
* ```ts
|
|
320
|
-
* resolver.
|
|
295
|
+
* resolver.file(operationFileEntry(node, node.operationId), { root, output, group })
|
|
321
296
|
* ```
|
|
322
297
|
*/
|
|
323
298
|
function operationFileEntry(node, name, extname = ".ts") {
|
|
@@ -455,10 +430,10 @@ function getPrimarySuccessResponse(node) {
|
|
|
455
430
|
return getOperationSuccessResponses(node)[0] ?? null;
|
|
456
431
|
}
|
|
457
432
|
function resolveErrorNames(node, resolver) {
|
|
458
|
-
return node.responses.filter((response) => isErrorStatusCode(response.statusCode)).map((response) => resolver.
|
|
433
|
+
return node.responses.filter((response) => isErrorStatusCode(response.statusCode)).map((response) => resolver.response.status(node, response.statusCode));
|
|
459
434
|
}
|
|
460
435
|
function resolveStatusCodeNames(node, resolver) {
|
|
461
|
-
return node.responses.map((response) => resolver.
|
|
436
|
+
return node.responses.map((response) => resolver.response.status(node, response.statusCode));
|
|
462
437
|
}
|
|
463
438
|
const typeNamesByResolver = /* @__PURE__ */ new WeakMap();
|
|
464
439
|
function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
@@ -475,11 +450,11 @@ function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
|
475
450
|
const responseStatusNames = options.responseStatusNames === "error" ? resolveErrorNames(node, resolver) : options.responseStatusNames === false ? [] : resolveStatusCodeNames(node, resolver);
|
|
476
451
|
const exclude = new Set(options.exclude ?? []);
|
|
477
452
|
const paramNames = options.includeParams === false ? [] : [
|
|
478
|
-
...path.map((param) => resolver.
|
|
479
|
-
...query.map((param) => resolver.
|
|
480
|
-
...header.map((param) => resolver.
|
|
453
|
+
...path.map((param) => resolver.param.path(node, param)),
|
|
454
|
+
...query.map((param) => resolver.param.query(node, param)),
|
|
455
|
+
...header.map((param) => resolver.param.headers(node, param))
|
|
481
456
|
];
|
|
482
|
-
const bodyAndResponseNames = [node.requestBody?.content?.[0]?.schema ? resolver.
|
|
457
|
+
const bodyAndResponseNames = [node.requestBody?.content?.[0]?.schema ? resolver.response.body(node) : null, resolver.response.response(node)];
|
|
483
458
|
const result = (options.order === "body-response-first" ? [
|
|
484
459
|
...bodyAndResponseNames,
|
|
485
460
|
...paramNames,
|
|
@@ -603,15 +578,15 @@ function resolveClientOperation(options) {
|
|
|
603
578
|
const { clientPlugin, driver, node, root, output } = options;
|
|
604
579
|
if (!clientPlugin) return null;
|
|
605
580
|
const resolver = driver.getResolver(clientPlugin.pluginName);
|
|
606
|
-
if (!resolver) return null;
|
|
607
581
|
const plugin = driver.getPlugin(clientPlugin.pluginName);
|
|
608
|
-
const file = resolver.
|
|
582
|
+
const file = resolver.file({
|
|
583
|
+
...operationFileEntry(node, node.operationId),
|
|
609
584
|
root,
|
|
610
585
|
output: plugin?.options?.output ?? output,
|
|
611
586
|
group: plugin?.options?.group ?? void 0
|
|
612
587
|
});
|
|
613
588
|
return {
|
|
614
|
-
name: resolver.
|
|
589
|
+
name: resolver.name(node.operationId),
|
|
615
590
|
path: file.path,
|
|
616
591
|
clientPath: path.resolve(root, ".kubb/client.ts")
|
|
617
592
|
};
|
|
@@ -659,12 +634,12 @@ const requestGroupOrder = [
|
|
|
659
634
|
/**
|
|
660
635
|
* Builds the grouped `{ path, query, body, headers }` parameter that mirrors the client
|
|
661
636
|
* function signature. Only the groups the operation carries are emitted, typed from the
|
|
662
|
-
* operation's `
|
|
637
|
+
* operation's `Options`. `keys` narrows the emitted groups, used by the query key which
|
|
663
638
|
* never carries `headers`.
|
|
664
639
|
*
|
|
665
|
-
* By default the whole group is typed as the single `
|
|
640
|
+
* By default the whole group is typed as the single `Options` reference. When
|
|
666
641
|
* `memberTypeWrapper` is set, each group is emitted as its own member typed from the matching
|
|
667
|
-
* `
|
|
642
|
+
* `Options['<group>']` slice and wrapped, used by vue-query to apply
|
|
668
643
|
* `MaybeRefOrGetter` per group.
|
|
669
644
|
*/
|
|
670
645
|
function buildGroupedRequestParam(node, options) {
|
|
@@ -679,13 +654,13 @@ function buildGroupedRequestParam(node, options) {
|
|
|
679
654
|
headers: hasRequiredHeader
|
|
680
655
|
};
|
|
681
656
|
const isOptional = names.every((name) => !requiredByGroup[name]);
|
|
682
|
-
const
|
|
657
|
+
const optionsName = resolver.response.options(node);
|
|
683
658
|
const omittedKeys = requestGroupOrder.filter((key) => !keys.includes(key));
|
|
684
|
-
const
|
|
659
|
+
const optionsType = omittedKeys.length > 0 ? `Omit<${optionsName}, ${omittedKeys.map((key) => `'${key}'`).join(" | ")}>` : optionsName;
|
|
685
660
|
if (memberTypeWrapper) {
|
|
686
661
|
const members = names.map((name) => ({
|
|
687
662
|
name,
|
|
688
|
-
type: memberTypeWrapper(`${
|
|
663
|
+
type: memberTypeWrapper(`${optionsType}['${name}']`),
|
|
689
664
|
optional: !requiredByGroup[name]
|
|
690
665
|
}));
|
|
691
666
|
return createFunctionParameter({
|
|
@@ -697,7 +672,7 @@ function buildGroupedRequestParam(node, options) {
|
|
|
697
672
|
}
|
|
698
673
|
return createFunctionParameter({
|
|
699
674
|
name: createObjectBindingPattern({ elements: names.map((name) => ({ name })) }),
|
|
700
|
-
type:
|
|
675
|
+
type: optionsType,
|
|
701
676
|
optional: false,
|
|
702
677
|
...isOptional ? { default: "{}" } : {}
|
|
703
678
|
});
|
|
@@ -743,7 +718,7 @@ function buildClientCall(node, options) {
|
|
|
743
718
|
}
|
|
744
719
|
/**
|
|
745
720
|
* Build QueryKey params as the grouped `{ path, query, body }` object (NO headers, NO config),
|
|
746
|
-
* typed from the operation's `
|
|
721
|
+
* typed from the operation's `Options` minus `url`. The query key transformer reads the
|
|
747
722
|
* grouped `path`/`query`/`body` bindings.
|
|
748
723
|
*/
|
|
749
724
|
function buildQueryKeyParams(node, options) {
|
|
@@ -805,7 +780,7 @@ const declarationPrinter$2 = functionPrinter({ mode: "declaration" });
|
|
|
805
780
|
const callPrinter$1 = functionPrinter({ mode: "call" });
|
|
806
781
|
function buildMutationParamsNode(node, options) {
|
|
807
782
|
const { mutationKeyTypeName, mutationArgTypeName, resolver } = options;
|
|
808
|
-
const responseName = resolver.
|
|
783
|
+
const responseName = resolver.response.response(node);
|
|
809
784
|
const errorNames = resolveErrorNames(node, resolver);
|
|
810
785
|
return createFunctionParameters({ params: [createFunctionParameter({
|
|
811
786
|
name: "options",
|
|
@@ -818,14 +793,14 @@ function buildMutationParamsNode(node, options) {
|
|
|
818
793
|
})] });
|
|
819
794
|
}
|
|
820
795
|
function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, mutationArgTypeName, node, tsResolver }) {
|
|
821
|
-
const responseName = tsResolver.
|
|
796
|
+
const responseName = tsResolver.response.response(node);
|
|
822
797
|
const errorNames = resolveErrorNames(node, tsResolver);
|
|
823
798
|
const TData = responseName;
|
|
824
799
|
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
|
|
825
800
|
const groupedParam = buildGroupedRequestParam(node, { resolver: tsResolver });
|
|
826
801
|
const hasMutationParams = groupedParam !== null;
|
|
827
802
|
const groupedParamsNode = createFunctionParameters({ params: groupedParam ? [groupedParam] : [] });
|
|
828
|
-
const argTypeBody = hasMutationParams ? tsResolver.
|
|
803
|
+
const argTypeBody = hasMutationParams ? tsResolver.response.options(node) : "";
|
|
829
804
|
const argBindingStr = hasMutationParams ? callPrinter$1.print(groupedParamsNode) ?? "" : "";
|
|
830
805
|
const mutationFnBody = `const { data } = await ${buildClientCall(node, {
|
|
831
806
|
clientName,
|
|
@@ -935,7 +910,7 @@ const declarationPrinter = functionPrinter({ mode: "declaration" });
|
|
|
935
910
|
const callPrinter = functionPrinter({ mode: "call" });
|
|
936
911
|
function buildQueryParamsNode(node, options) {
|
|
937
912
|
const { resolver } = options;
|
|
938
|
-
const responseName = resolver.
|
|
913
|
+
const responseName = resolver.response.response(node);
|
|
939
914
|
const errorNames = resolveErrorNames(node, resolver);
|
|
940
915
|
const optionsParam = createFunctionParameter({
|
|
941
916
|
name: "options",
|
|
@@ -950,7 +925,7 @@ function buildQueryParamsNode(node, options) {
|
|
|
950
925
|
return createFunctionParameters({ params: [buildGroupedRequestParam(node, { resolver }), optionsParam].filter((param) => param !== null) });
|
|
951
926
|
}
|
|
952
927
|
function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver }) {
|
|
953
|
-
const responseName = tsResolver.
|
|
928
|
+
const responseName = tsResolver.response.response(node);
|
|
954
929
|
const errorNames = resolveErrorNames(node, tsResolver);
|
|
955
930
|
const generics = [
|
|
956
931
|
responseName,
|
|
@@ -1022,33 +997,31 @@ const mutationGenerator = defineGenerator({
|
|
|
1022
997
|
output
|
|
1023
998
|
});
|
|
1024
999
|
if (!contractOp) return null;
|
|
1025
|
-
const mutationHookName = resolver.
|
|
1026
|
-
const mutationKeyName = resolver.
|
|
1027
|
-
const mutationKeyTypeName = resolver.
|
|
1028
|
-
const mutationArgTypeName = resolver.
|
|
1000
|
+
const mutationHookName = resolver.mutation.name(node);
|
|
1001
|
+
const mutationKeyName = resolver.mutation.keyName(node);
|
|
1002
|
+
const mutationKeyTypeName = resolver.mutation.keyTypeName(node);
|
|
1003
|
+
const mutationArgTypeName = resolver.mutation.argTypeName(node);
|
|
1029
1004
|
const meta = {
|
|
1030
|
-
file: resolver.
|
|
1005
|
+
file: resolver.file({
|
|
1031
1006
|
name: mutationHookName,
|
|
1032
1007
|
extname: ".ts",
|
|
1033
1008
|
tag: node.tags[0] ?? "default",
|
|
1034
|
-
path: node.path
|
|
1035
|
-
}, {
|
|
1009
|
+
path: node.path,
|
|
1036
1010
|
root,
|
|
1037
1011
|
output,
|
|
1038
1012
|
group
|
|
1039
1013
|
}),
|
|
1040
|
-
fileTs: tsResolver.
|
|
1014
|
+
fileTs: tsResolver.file({
|
|
1041
1015
|
name: node.operationId,
|
|
1042
1016
|
extname: ".ts",
|
|
1043
1017
|
tag: node.tags[0] ?? "default",
|
|
1044
|
-
path: node.path
|
|
1045
|
-
}, {
|
|
1018
|
+
path: node.path,
|
|
1046
1019
|
root,
|
|
1047
1020
|
output: pluginTs.options?.output ?? output,
|
|
1048
1021
|
group: pluginTs.options?.group
|
|
1049
1022
|
})
|
|
1050
1023
|
};
|
|
1051
|
-
const importedTypeNames = [tsResolver.
|
|
1024
|
+
const importedTypeNames = [tsResolver.response.options(node), ...resolveOperationTypeNames(node, tsResolver, {
|
|
1052
1025
|
order: "body-response-first",
|
|
1053
1026
|
includeParams: false
|
|
1054
1027
|
})].filter((name) => Boolean(name));
|
|
@@ -1057,7 +1030,7 @@ const mutationGenerator = defineGenerator({
|
|
|
1057
1030
|
baseName: meta.file.baseName,
|
|
1058
1031
|
path: meta.file.path,
|
|
1059
1032
|
meta: meta.file.meta,
|
|
1060
|
-
banner: resolver.
|
|
1033
|
+
banner: resolver.default.banner(ctx.meta, {
|
|
1061
1034
|
output,
|
|
1062
1035
|
config,
|
|
1063
1036
|
file: {
|
|
@@ -1065,7 +1038,7 @@ const mutationGenerator = defineGenerator({
|
|
|
1065
1038
|
baseName: meta.file.baseName
|
|
1066
1039
|
}
|
|
1067
1040
|
}),
|
|
1068
|
-
footer: resolver.
|
|
1041
|
+
footer: resolver.default.footer(ctx.meta, {
|
|
1069
1042
|
output,
|
|
1070
1043
|
config,
|
|
1071
1044
|
file: {
|
|
@@ -1150,33 +1123,31 @@ const queryGenerator = defineGenerator({
|
|
|
1150
1123
|
output
|
|
1151
1124
|
});
|
|
1152
1125
|
if (!contractOp) return null;
|
|
1153
|
-
const queryName = resolver.
|
|
1154
|
-
const queryOptionsName = resolver.
|
|
1155
|
-
const queryKeyName = resolver.
|
|
1156
|
-
const queryKeyTypeName = resolver.
|
|
1126
|
+
const queryName = resolver.query.name(node);
|
|
1127
|
+
const queryOptionsName = resolver.query.optionsName(node);
|
|
1128
|
+
const queryKeyName = resolver.query.keyName(node);
|
|
1129
|
+
const queryKeyTypeName = resolver.query.keyTypeName(node);
|
|
1157
1130
|
const meta = {
|
|
1158
|
-
file: resolver.
|
|
1131
|
+
file: resolver.file({
|
|
1159
1132
|
name: queryName,
|
|
1160
1133
|
extname: ".ts",
|
|
1161
1134
|
tag: node.tags[0] ?? "default",
|
|
1162
|
-
path: node.path
|
|
1163
|
-
}, {
|
|
1135
|
+
path: node.path,
|
|
1164
1136
|
root,
|
|
1165
1137
|
output,
|
|
1166
1138
|
group
|
|
1167
1139
|
}),
|
|
1168
|
-
fileTs: tsResolver.
|
|
1140
|
+
fileTs: tsResolver.file({
|
|
1169
1141
|
name: node.operationId,
|
|
1170
1142
|
extname: ".ts",
|
|
1171
1143
|
tag: node.tags[0] ?? "default",
|
|
1172
|
-
path: node.path
|
|
1173
|
-
}, {
|
|
1144
|
+
path: node.path,
|
|
1174
1145
|
root,
|
|
1175
1146
|
output: pluginTs.options?.output ?? output,
|
|
1176
1147
|
group: pluginTs.options?.group
|
|
1177
1148
|
})
|
|
1178
1149
|
};
|
|
1179
|
-
const importedTypeNames = [tsResolver.
|
|
1150
|
+
const importedTypeNames = [tsResolver.response.options(node), ...resolveOperationTypeNames(node, tsResolver, {
|
|
1180
1151
|
exclude: [queryKeyTypeName],
|
|
1181
1152
|
order: "body-response-first",
|
|
1182
1153
|
includeParams: false
|
|
@@ -1186,7 +1157,7 @@ const queryGenerator = defineGenerator({
|
|
|
1186
1157
|
baseName: meta.file.baseName,
|
|
1187
1158
|
path: meta.file.path,
|
|
1188
1159
|
meta: meta.file.meta,
|
|
1189
|
-
banner: resolver.
|
|
1160
|
+
banner: resolver.default.banner(ctx.meta, {
|
|
1190
1161
|
output,
|
|
1191
1162
|
config,
|
|
1192
1163
|
file: {
|
|
@@ -1194,7 +1165,7 @@ const queryGenerator = defineGenerator({
|
|
|
1194
1165
|
baseName: meta.file.baseName
|
|
1195
1166
|
}
|
|
1196
1167
|
}),
|
|
1197
|
-
footer: resolver.
|
|
1168
|
+
footer: resolver.default.footer(ctx.meta, {
|
|
1198
1169
|
output,
|
|
1199
1170
|
config,
|
|
1200
1171
|
file: {
|
|
@@ -1262,53 +1233,55 @@ function capitalize(name) {
|
|
|
1262
1233
|
return `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
|
|
1263
1234
|
}
|
|
1264
1235
|
/**
|
|
1265
|
-
*
|
|
1236
|
+
* Default resolver used by `@kubb/plugin-swr`. Decides the names and file paths for every generated
|
|
1237
|
+
* SWR hook (`useFoo`, `useFooMutation`) and its companion helpers (`fooQueryKey`, `fooQueryOptions`,
|
|
1238
|
+
* `fooMutationKey`).
|
|
1266
1239
|
*
|
|
1267
|
-
*
|
|
1240
|
+
* Functions and files use the built-in camelCase casing; hooks get the `use` prefix.
|
|
1268
1241
|
*
|
|
1269
|
-
* @example
|
|
1270
|
-
*
|
|
1242
|
+
* @example Resolve hook and helper names
|
|
1243
|
+
* ```ts
|
|
1244
|
+
* import { resolverSwr } from '@kubb/plugin-swr'
|
|
1245
|
+
*
|
|
1246
|
+
* resolverSwr.query.name(operationNode) // 'useGetPetById'
|
|
1247
|
+
* resolverSwr.query.keyName(operationNode) // 'getPetByIdQueryKey'
|
|
1248
|
+
* resolverSwr.mutation.name(operationNode) // 'useUpdatePet'
|
|
1249
|
+
* ```
|
|
1271
1250
|
*/
|
|
1272
|
-
const resolverSwr =
|
|
1273
|
-
name: "default",
|
|
1251
|
+
const resolverSwr = createResolver({
|
|
1274
1252
|
pluginName: "plugin-swr",
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
return `${this.resolveName(node.operationId)}QueryOptions`;
|
|
1292
|
-
},
|
|
1293
|
-
resolveQueryKeyName(node) {
|
|
1294
|
-
return `${this.resolveName(node.operationId)}QueryKey`;
|
|
1295
|
-
},
|
|
1296
|
-
resolveMutationKeyName(node) {
|
|
1297
|
-
return `${this.resolveName(node.operationId)}MutationKey`;
|
|
1298
|
-
},
|
|
1299
|
-
resolveQueryKeyTypeName(node) {
|
|
1300
|
-
return `${capitalize(this.resolveName(node.operationId))}QueryKey`;
|
|
1301
|
-
},
|
|
1302
|
-
resolveMutationKeyTypeName(node) {
|
|
1303
|
-
return `${capitalize(this.resolveName(node.operationId))}MutationKey`;
|
|
1304
|
-
},
|
|
1305
|
-
resolveMutationArgTypeName(node) {
|
|
1306
|
-
return `${capitalize(this.resolveName(node.operationId))}MutationArg`;
|
|
1253
|
+
query: {
|
|
1254
|
+
name(node) {
|
|
1255
|
+
return `use${capitalize(this.name(node.operationId))}`;
|
|
1256
|
+
},
|
|
1257
|
+
optionsName(node) {
|
|
1258
|
+
return `${this.name(node.operationId)}QueryOptions`;
|
|
1259
|
+
},
|
|
1260
|
+
keyName(node) {
|
|
1261
|
+
return `${this.name(node.operationId)}QueryKey`;
|
|
1262
|
+
},
|
|
1263
|
+
keyTypeName(node) {
|
|
1264
|
+
return `${capitalize(this.name(node.operationId))}QueryKey`;
|
|
1265
|
+
},
|
|
1266
|
+
clientName(node) {
|
|
1267
|
+
return this.name(node.operationId);
|
|
1268
|
+
}
|
|
1307
1269
|
},
|
|
1308
|
-
|
|
1309
|
-
|
|
1270
|
+
mutation: {
|
|
1271
|
+
name(node) {
|
|
1272
|
+
return `use${capitalize(this.name(node.operationId))}`;
|
|
1273
|
+
},
|
|
1274
|
+
keyName(node) {
|
|
1275
|
+
return `${this.name(node.operationId)}MutationKey`;
|
|
1276
|
+
},
|
|
1277
|
+
keyTypeName(node) {
|
|
1278
|
+
return `${capitalize(this.name(node.operationId))}MutationKey`;
|
|
1279
|
+
},
|
|
1280
|
+
argTypeName(node) {
|
|
1281
|
+
return `${capitalize(this.name(node.operationId))}MutationArg`;
|
|
1282
|
+
}
|
|
1310
1283
|
}
|
|
1311
|
-
})
|
|
1284
|
+
});
|
|
1312
1285
|
//#endregion
|
|
1313
1286
|
//#region src/plugin.ts
|
|
1314
1287
|
/**
|
|
@@ -1349,10 +1322,7 @@ const pluginSwr = definePlugin((options) => {
|
|
|
1349
1322
|
options,
|
|
1350
1323
|
dependencies: [pluginTsName],
|
|
1351
1324
|
hooks: { "kubb:plugin:setup"(ctx) {
|
|
1352
|
-
const resolver = userResolver ?
|
|
1353
|
-
...resolverSwr,
|
|
1354
|
-
...userResolver
|
|
1355
|
-
} : resolverSwr;
|
|
1325
|
+
const resolver = userResolver ? Resolver.merge(resolverSwr, userResolver) : resolverSwr;
|
|
1356
1326
|
const resolvedClient = resolveClient({
|
|
1357
1327
|
client,
|
|
1358
1328
|
pluginNames: (ctx.config.plugins ?? []).map((p) => p.name).filter((name) => Boolean(name))
|