@kubb/plugin-swr 5.0.0-beta.85 → 5.0.0-beta.87
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 +152 -174
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +75 -46
- package/dist/index.js +152 -174
- package/dist/index.js.map +1 -1
- package/package.json +5 -6
package/dist/index.cjs
CHANGED
|
@@ -28,10 +28,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
enumerable: true
|
|
29
29
|
}) : target, mod));
|
|
30
30
|
//#endregion
|
|
31
|
-
let node_path = require("node:path");
|
|
32
|
-
node_path = __toESM(node_path, 1);
|
|
33
31
|
let kubb_kit = require("kubb/kit");
|
|
34
32
|
let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
33
|
+
let node_path = require("node:path");
|
|
34
|
+
node_path = __toESM(node_path, 1);
|
|
35
35
|
let kubb_jsx = require("kubb/jsx");
|
|
36
36
|
let kubb_jsx_jsx_runtime = require("kubb/jsx/jsx-runtime");
|
|
37
37
|
//#region ../../internals/utils/src/casing.ts
|
|
@@ -61,31 +61,6 @@ function camelCase(text, { prefix = "", suffix = "" } = {}) {
|
|
|
61
61
|
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
62
62
|
}
|
|
63
63
|
//#endregion
|
|
64
|
-
//#region ../../internals/utils/src/fs.ts
|
|
65
|
-
/**
|
|
66
|
-
* Builds a nested file path from a dotted name. Splits on dots that precede a letter
|
|
67
|
-
* (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases
|
|
68
|
-
* every earlier segment, applies `caseLast` to the final segment, and joins with `/`.
|
|
69
|
-
*
|
|
70
|
-
* Empty segments are dropped before joining. They arise when the name starts with a dot
|
|
71
|
-
* followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to
|
|
72
|
-
* an empty string). Without this a leading `/` would form, which `path.resolve` reads as an
|
|
73
|
-
* absolute path, letting generated files escape the configured output directory.
|
|
74
|
-
*
|
|
75
|
-
* @example Nested path from a dotted name
|
|
76
|
-
* `toFilePath('pet.petId') // 'pet/petId'`
|
|
77
|
-
*
|
|
78
|
-
* @example PascalCase the final segment
|
|
79
|
-
* `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`
|
|
80
|
-
*
|
|
81
|
-
* @example Suffix applied to the final segment only
|
|
82
|
-
* `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`
|
|
83
|
-
*/
|
|
84
|
-
function toFilePath(name, caseLast = camelCase) {
|
|
85
|
-
const parts = name.split(/\.(?=[a-zA-Z])/);
|
|
86
|
-
return parts.map((part, i) => i === parts.length - 1 ? caseLast(part) : camelCase(part)).filter(Boolean).join("/");
|
|
87
|
-
}
|
|
88
|
-
//#endregion
|
|
89
64
|
//#region ../../internals/utils/src/reserved.ts
|
|
90
65
|
/**
|
|
91
66
|
* JavaScript and Java reserved words.
|
|
@@ -186,6 +161,22 @@ const reservedWords = /* @__PURE__ */ new Set([
|
|
|
186
161
|
*/
|
|
187
162
|
function isValidVarName(name) {
|
|
188
163
|
if (!name || reservedWords.has(name)) return false;
|
|
164
|
+
return isIdentifier(name);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
|
|
168
|
+
*
|
|
169
|
+
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
|
|
170
|
+
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
|
|
171
|
+
* deciding whether an object key needs quoting.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* isIdentifier('name') // true
|
|
176
|
+
* isIdentifier('x-total')// false
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
function isIdentifier(name) {
|
|
189
180
|
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
190
181
|
}
|
|
191
182
|
//#endregion
|
|
@@ -341,13 +332,13 @@ function dedupeByCasedName(params) {
|
|
|
341
332
|
//#region ../../internals/shared/src/operation.ts
|
|
342
333
|
/**
|
|
343
334
|
* Builds the `ResolverFileParams` every operation generator passes to
|
|
344
|
-
* `resolver.
|
|
335
|
+
* `resolver.file`: a file named `name`, tagged by the operation's first
|
|
345
336
|
* tag (or `'default'`), at the operation's path. Centralizes the entry object
|
|
346
337
|
* that was repeated at dozens of call sites across the client and query plugins.
|
|
347
338
|
*
|
|
348
339
|
* @example
|
|
349
340
|
* ```ts
|
|
350
|
-
* resolver.
|
|
341
|
+
* resolver.file(operationFileEntry(node, node.operationId), { root, output, group })
|
|
351
342
|
* ```
|
|
352
343
|
*/
|
|
353
344
|
function operationFileEntry(node, name, extname = ".ts") {
|
|
@@ -364,8 +355,11 @@ function getOperationLink(node, link) {
|
|
|
364
355
|
if (link === "urlPath") return node.path ? `{@link ${Url.toPath(node.path)}}` : null;
|
|
365
356
|
return node.path ? `{@link ${node.path.replaceAll("{", ":").replaceAll("}", "")}}` : null;
|
|
366
357
|
}
|
|
367
|
-
|
|
368
|
-
|
|
358
|
+
/**
|
|
359
|
+
* Derives the shared `ContentTypeInfo` shape from a list of content types, tracking whether several
|
|
360
|
+
* are present and the union, default, and form-data flags the client uses to pick one.
|
|
361
|
+
*/
|
|
362
|
+
function buildContentTypeInfo(contentTypes) {
|
|
369
363
|
const isMultipleContentTypes = contentTypes.length > 1;
|
|
370
364
|
return {
|
|
371
365
|
contentTypes,
|
|
@@ -375,20 +369,15 @@ function getContentTypeInfo(node) {
|
|
|
375
369
|
hasFormData: contentTypes.some((ct) => ct === "multipart/form-data")
|
|
376
370
|
};
|
|
377
371
|
}
|
|
372
|
+
function getContentTypeInfo(node) {
|
|
373
|
+
return buildContentTypeInfo(node.requestBody?.content?.map((e) => e.contentType) ?? []);
|
|
374
|
+
}
|
|
378
375
|
/**
|
|
379
376
|
* The request-body counterpart for the primary success response: the content types it documents and
|
|
380
377
|
* whether several are present, so the client can let a caller pick which one to accept.
|
|
381
378
|
*/
|
|
382
379
|
function getResponseContentTypeInfo(node) {
|
|
383
|
-
|
|
384
|
-
const isMultipleContentTypes = contentTypes.length > 1;
|
|
385
|
-
return {
|
|
386
|
-
contentTypes,
|
|
387
|
-
isMultipleContentTypes,
|
|
388
|
-
contentTypeUnion: isMultipleContentTypes ? contentTypes.map((ct) => JSON.stringify(ct)).join(" | ") : "",
|
|
389
|
-
defaultContentType: contentTypes[0] ?? "application/json",
|
|
390
|
-
hasFormData: contentTypes.some((ct) => ct === "multipart/form-data")
|
|
391
|
-
};
|
|
380
|
+
return buildContentTypeInfo(getPrimarySuccessResponse(node)?.content?.map((e) => e.contentType) ?? []);
|
|
392
381
|
}
|
|
393
382
|
function buildRequestConfigType(node) {
|
|
394
383
|
const request = getContentTypeInfo(node);
|
|
@@ -485,10 +474,10 @@ function getPrimarySuccessResponse(node) {
|
|
|
485
474
|
return getOperationSuccessResponses(node)[0] ?? null;
|
|
486
475
|
}
|
|
487
476
|
function resolveErrorNames(node, resolver) {
|
|
488
|
-
return node.responses.filter((response) => isErrorStatusCode(response.statusCode)).map((response) => resolver.
|
|
477
|
+
return node.responses.filter((response) => isErrorStatusCode(response.statusCode)).map((response) => resolver.response.status(node, response.statusCode));
|
|
489
478
|
}
|
|
490
479
|
function resolveStatusCodeNames(node, resolver) {
|
|
491
|
-
return node.responses.map((response) => resolver.
|
|
480
|
+
return node.responses.map((response) => resolver.response.status(node, response.statusCode));
|
|
492
481
|
}
|
|
493
482
|
const typeNamesByResolver = /* @__PURE__ */ new WeakMap();
|
|
494
483
|
function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
@@ -505,11 +494,11 @@ function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
|
505
494
|
const responseStatusNames = options.responseStatusNames === "error" ? resolveErrorNames(node, resolver) : options.responseStatusNames === false ? [] : resolveStatusCodeNames(node, resolver);
|
|
506
495
|
const exclude = new Set(options.exclude ?? []);
|
|
507
496
|
const paramNames = options.includeParams === false ? [] : [
|
|
508
|
-
...path.map((param) => resolver.
|
|
509
|
-
...query.map((param) => resolver.
|
|
510
|
-
...header.map((param) => resolver.
|
|
497
|
+
...path.map((param) => resolver.param.path(node, param)),
|
|
498
|
+
...query.map((param) => resolver.param.query(node, param)),
|
|
499
|
+
...header.map((param) => resolver.param.headers(node, param))
|
|
511
500
|
];
|
|
512
|
-
const bodyAndResponseNames = [node.requestBody?.content?.[0]?.schema ? resolver.
|
|
501
|
+
const bodyAndResponseNames = [node.requestBody?.content?.[0]?.schema ? resolver.response.body(node) : null, resolver.response.response(node)];
|
|
513
502
|
const result = (options.order === "body-response-first" ? [
|
|
514
503
|
...bodyAndResponseNames,
|
|
515
504
|
...paramNames,
|
|
@@ -633,15 +622,15 @@ function resolveClientOperation(options) {
|
|
|
633
622
|
const { clientPlugin, driver, node, root, output } = options;
|
|
634
623
|
if (!clientPlugin) return null;
|
|
635
624
|
const resolver = driver.getResolver(clientPlugin.pluginName);
|
|
636
|
-
if (!resolver) return null;
|
|
637
625
|
const plugin = driver.getPlugin(clientPlugin.pluginName);
|
|
638
|
-
const file = resolver.
|
|
626
|
+
const file = resolver.file({
|
|
627
|
+
...operationFileEntry(node, node.operationId),
|
|
639
628
|
root,
|
|
640
629
|
output: plugin?.options?.output ?? output,
|
|
641
630
|
group: plugin?.options?.group ?? void 0
|
|
642
631
|
});
|
|
643
632
|
return {
|
|
644
|
-
name: resolver.
|
|
633
|
+
name: resolver.name(node.operationId),
|
|
645
634
|
path: file.path,
|
|
646
635
|
clientPath: node_path.default.resolve(root, ".kubb/client.ts")
|
|
647
636
|
};
|
|
@@ -689,12 +678,12 @@ const requestGroupOrder = [
|
|
|
689
678
|
/**
|
|
690
679
|
* Builds the grouped `{ path, query, body, headers }` parameter that mirrors the client
|
|
691
680
|
* function signature. Only the groups the operation carries are emitted, typed from the
|
|
692
|
-
* operation's `
|
|
681
|
+
* operation's `Options`. `keys` narrows the emitted groups, used by the query key which
|
|
693
682
|
* never carries `headers`.
|
|
694
683
|
*
|
|
695
|
-
* By default the whole group is typed as the single `
|
|
684
|
+
* By default the whole group is typed as the single `Options` reference. When
|
|
696
685
|
* `memberTypeWrapper` is set, each group is emitted as its own member typed from the matching
|
|
697
|
-
* `
|
|
686
|
+
* `Options['<group>']` slice and wrapped, used by vue-query to apply
|
|
698
687
|
* `MaybeRefOrGetter` per group.
|
|
699
688
|
*/
|
|
700
689
|
function buildGroupedRequestParam(node, options) {
|
|
@@ -709,13 +698,13 @@ function buildGroupedRequestParam(node, options) {
|
|
|
709
698
|
headers: hasRequiredHeader
|
|
710
699
|
};
|
|
711
700
|
const isOptional = names.every((name) => !requiredByGroup[name]);
|
|
712
|
-
const
|
|
701
|
+
const optionsName = resolver.response.options(node);
|
|
713
702
|
const omittedKeys = requestGroupOrder.filter((key) => !keys.includes(key));
|
|
714
|
-
const
|
|
703
|
+
const optionsType = omittedKeys.length > 0 ? `Omit<${optionsName}, ${omittedKeys.map((key) => `'${key}'`).join(" | ")}>` : optionsName;
|
|
715
704
|
if (memberTypeWrapper) {
|
|
716
705
|
const members = names.map((name) => ({
|
|
717
706
|
name,
|
|
718
|
-
type: memberTypeWrapper(`${
|
|
707
|
+
type: memberTypeWrapper(`${optionsType}['${name}']`),
|
|
719
708
|
optional: !requiredByGroup[name]
|
|
720
709
|
}));
|
|
721
710
|
return (0, _kubb_plugin_ts.createFunctionParameter)({
|
|
@@ -727,7 +716,7 @@ function buildGroupedRequestParam(node, options) {
|
|
|
727
716
|
}
|
|
728
717
|
return (0, _kubb_plugin_ts.createFunctionParameter)({
|
|
729
718
|
name: (0, _kubb_plugin_ts.createObjectBindingPattern)({ elements: names.map((name) => ({ name })) }),
|
|
730
|
-
type:
|
|
719
|
+
type: optionsType,
|
|
731
720
|
optional: false,
|
|
732
721
|
...isOptional ? { default: "{}" } : {}
|
|
733
722
|
});
|
|
@@ -773,7 +762,7 @@ function buildClientCall(node, options) {
|
|
|
773
762
|
}
|
|
774
763
|
/**
|
|
775
764
|
* Build QueryKey params as the grouped `{ path, query, body }` object (NO headers, NO config),
|
|
776
|
-
* typed from the operation's `
|
|
765
|
+
* typed from the operation's `Options` minus `url`. The query key transformer reads the
|
|
777
766
|
* grouped `path`/`query`/`body` bindings.
|
|
778
767
|
*/
|
|
779
768
|
function buildQueryKeyParams(node, options) {
|
|
@@ -835,7 +824,7 @@ const declarationPrinter$2 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "decla
|
|
|
835
824
|
const callPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
836
825
|
function buildMutationParamsNode(node, options) {
|
|
837
826
|
const { mutationKeyTypeName, mutationArgTypeName, resolver } = options;
|
|
838
|
-
const responseName = resolver.
|
|
827
|
+
const responseName = resolver.response.response(node);
|
|
839
828
|
const errorNames = resolveErrorNames(node, resolver);
|
|
840
829
|
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [(0, _kubb_plugin_ts.createFunctionParameter)({
|
|
841
830
|
name: "options",
|
|
@@ -848,14 +837,14 @@ function buildMutationParamsNode(node, options) {
|
|
|
848
837
|
})] });
|
|
849
838
|
}
|
|
850
839
|
function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, mutationArgTypeName, node, tsResolver }) {
|
|
851
|
-
const responseName = tsResolver.
|
|
840
|
+
const responseName = tsResolver.response.response(node);
|
|
852
841
|
const errorNames = resolveErrorNames(node, tsResolver);
|
|
853
842
|
const TData = responseName;
|
|
854
843
|
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
|
|
855
844
|
const groupedParam = buildGroupedRequestParam(node, { resolver: tsResolver });
|
|
856
845
|
const hasMutationParams = groupedParam !== null;
|
|
857
846
|
const groupedParamsNode = (0, _kubb_plugin_ts.createFunctionParameters)({ params: groupedParam ? [groupedParam] : [] });
|
|
858
|
-
const argTypeBody = hasMutationParams ? tsResolver.
|
|
847
|
+
const argTypeBody = hasMutationParams ? tsResolver.response.options(node) : "";
|
|
859
848
|
const argBindingStr = hasMutationParams ? callPrinter$1.print(groupedParamsNode) ?? "" : "";
|
|
860
849
|
const mutationFnBody = `const { data } = await ${buildClientCall(node, {
|
|
861
850
|
clientName,
|
|
@@ -928,44 +917,12 @@ function MutationKey({ name, typeName, node, transformer }) {
|
|
|
928
917
|
})] });
|
|
929
918
|
}
|
|
930
919
|
//#endregion
|
|
931
|
-
//#region src/components/QueryOptions.tsx
|
|
932
|
-
const declarationPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
933
|
-
function getQueryOptionsParams(node, options) {
|
|
934
|
-
return buildQueryOptionsParams(node, options);
|
|
935
|
-
}
|
|
936
|
-
function QueryOptions({ name, clientName, node, tsResolver }) {
|
|
937
|
-
const paramsNode = getQueryOptionsParams(node, { resolver: tsResolver });
|
|
938
|
-
const paramsSignature = declarationPrinter$1.print(paramsNode) ?? "";
|
|
939
|
-
const fetcherBody = `const { data } = await ${buildClientCall(node, {
|
|
940
|
-
clientName,
|
|
941
|
-
signal: false
|
|
942
|
-
})}
|
|
943
|
-
return data`;
|
|
944
|
-
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
|
|
945
|
-
name,
|
|
946
|
-
isExportable: true,
|
|
947
|
-
isIndexable: true,
|
|
948
|
-
children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Function, {
|
|
949
|
-
name,
|
|
950
|
-
export: true,
|
|
951
|
-
params: paramsSignature,
|
|
952
|
-
children: `
|
|
953
|
-
return {
|
|
954
|
-
fetcher: async () => {
|
|
955
|
-
${fetcherBody}
|
|
956
|
-
},
|
|
957
|
-
}
|
|
958
|
-
`
|
|
959
|
-
})
|
|
960
|
-
});
|
|
961
|
-
}
|
|
962
|
-
//#endregion
|
|
963
920
|
//#region src/components/Query.tsx
|
|
964
|
-
const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
921
|
+
const declarationPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
965
922
|
const callPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
966
923
|
function buildQueryParamsNode(node, options) {
|
|
967
924
|
const { resolver } = options;
|
|
968
|
-
const responseName = resolver.
|
|
925
|
+
const responseName = resolver.response.response(node);
|
|
969
926
|
const errorNames = resolveErrorNames(node, resolver);
|
|
970
927
|
const optionsParam = (0, _kubb_plugin_ts.createFunctionParameter)({
|
|
971
928
|
name: "options",
|
|
@@ -980,7 +937,7 @@ function buildQueryParamsNode(node, options) {
|
|
|
980
937
|
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [buildGroupedRequestParam(node, { resolver }), optionsParam].filter((param) => param !== null) });
|
|
981
938
|
}
|
|
982
939
|
function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver }) {
|
|
983
|
-
const responseName = tsResolver.
|
|
940
|
+
const responseName = tsResolver.response.response(node);
|
|
984
941
|
const errorNames = resolveErrorNames(node, tsResolver);
|
|
985
942
|
const generics = [
|
|
986
943
|
responseName,
|
|
@@ -989,10 +946,10 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, t
|
|
|
989
946
|
];
|
|
990
947
|
const queryKeyParamsNode = buildQueryKeyParams(node, { resolver: tsResolver });
|
|
991
948
|
const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? "";
|
|
992
|
-
const queryOptionsParamsNode =
|
|
949
|
+
const queryOptionsParamsNode = buildQueryOptionsParams(node, { resolver: tsResolver });
|
|
993
950
|
const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? "";
|
|
994
951
|
const paramsNode = buildQueryParamsNode(node, { resolver: tsResolver });
|
|
995
|
-
const paramsSignature = declarationPrinter.print(paramsNode) ?? "";
|
|
952
|
+
const paramsSignature = declarationPrinter$1.print(paramsNode) ?? "";
|
|
996
953
|
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
|
|
997
954
|
name,
|
|
998
955
|
isExportable: true,
|
|
@@ -1024,6 +981,35 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, t
|
|
|
1024
981
|
});
|
|
1025
982
|
}
|
|
1026
983
|
//#endregion
|
|
984
|
+
//#region src/components/QueryOptions.tsx
|
|
985
|
+
const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
986
|
+
function QueryOptions({ name, clientName, node, tsResolver }) {
|
|
987
|
+
const paramsNode = buildQueryOptionsParams(node, { resolver: tsResolver });
|
|
988
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? "";
|
|
989
|
+
const fetcherBody = `const { data } = await ${buildClientCall(node, {
|
|
990
|
+
clientName,
|
|
991
|
+
signal: false
|
|
992
|
+
})}
|
|
993
|
+
return data`;
|
|
994
|
+
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
|
|
995
|
+
name,
|
|
996
|
+
isExportable: true,
|
|
997
|
+
isIndexable: true,
|
|
998
|
+
children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Function, {
|
|
999
|
+
name,
|
|
1000
|
+
export: true,
|
|
1001
|
+
params: paramsSignature,
|
|
1002
|
+
children: `
|
|
1003
|
+
return {
|
|
1004
|
+
fetcher: async () => {
|
|
1005
|
+
${fetcherBody}
|
|
1006
|
+
},
|
|
1007
|
+
}
|
|
1008
|
+
`
|
|
1009
|
+
})
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
//#endregion
|
|
1027
1013
|
//#region src/generators/mutationGenerator.tsx
|
|
1028
1014
|
/**
|
|
1029
1015
|
* Built-in generator for `useSWRMutation` hooks. Emits one `useFooMutation` hook
|
|
@@ -1052,33 +1038,31 @@ const mutationGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1052
1038
|
output
|
|
1053
1039
|
});
|
|
1054
1040
|
if (!contractOp) return null;
|
|
1055
|
-
const mutationHookName = resolver.
|
|
1056
|
-
const mutationKeyName = resolver.
|
|
1057
|
-
const mutationKeyTypeName = resolver.
|
|
1058
|
-
const mutationArgTypeName = resolver.
|
|
1041
|
+
const mutationHookName = resolver.mutation.name(node);
|
|
1042
|
+
const mutationKeyName = resolver.mutation.keyName(node);
|
|
1043
|
+
const mutationKeyTypeName = resolver.mutation.keyTypeName(node);
|
|
1044
|
+
const mutationArgTypeName = resolver.mutation.argTypeName(node);
|
|
1059
1045
|
const meta = {
|
|
1060
|
-
file: resolver.
|
|
1046
|
+
file: resolver.file({
|
|
1061
1047
|
name: mutationHookName,
|
|
1062
1048
|
extname: ".ts",
|
|
1063
1049
|
tag: node.tags[0] ?? "default",
|
|
1064
|
-
path: node.path
|
|
1065
|
-
}, {
|
|
1050
|
+
path: node.path,
|
|
1066
1051
|
root,
|
|
1067
1052
|
output,
|
|
1068
1053
|
group
|
|
1069
1054
|
}),
|
|
1070
|
-
fileTs: tsResolver.
|
|
1055
|
+
fileTs: tsResolver.file({
|
|
1071
1056
|
name: node.operationId,
|
|
1072
1057
|
extname: ".ts",
|
|
1073
1058
|
tag: node.tags[0] ?? "default",
|
|
1074
|
-
path: node.path
|
|
1075
|
-
}, {
|
|
1059
|
+
path: node.path,
|
|
1076
1060
|
root,
|
|
1077
1061
|
output: pluginTs.options?.output ?? output,
|
|
1078
1062
|
group: pluginTs.options?.group
|
|
1079
1063
|
})
|
|
1080
1064
|
};
|
|
1081
|
-
const importedTypeNames = [tsResolver.
|
|
1065
|
+
const importedTypeNames = [tsResolver.response.options(node), ...resolveOperationTypeNames(node, tsResolver, {
|
|
1082
1066
|
order: "body-response-first",
|
|
1083
1067
|
includeParams: false
|
|
1084
1068
|
})].filter((name) => Boolean(name));
|
|
@@ -1087,7 +1071,7 @@ const mutationGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1087
1071
|
baseName: meta.file.baseName,
|
|
1088
1072
|
path: meta.file.path,
|
|
1089
1073
|
meta: meta.file.meta,
|
|
1090
|
-
banner: resolver.
|
|
1074
|
+
banner: resolver.default.banner(ctx.meta, {
|
|
1091
1075
|
output,
|
|
1092
1076
|
config,
|
|
1093
1077
|
file: {
|
|
@@ -1095,7 +1079,7 @@ const mutationGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1095
1079
|
baseName: meta.file.baseName
|
|
1096
1080
|
}
|
|
1097
1081
|
}),
|
|
1098
|
-
footer: resolver.
|
|
1082
|
+
footer: resolver.default.footer(ctx.meta, {
|
|
1099
1083
|
output,
|
|
1100
1084
|
config,
|
|
1101
1085
|
file: {
|
|
@@ -1163,14 +1147,11 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1163
1147
|
operation(node, ctx) {
|
|
1164
1148
|
if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
|
|
1165
1149
|
const { config, driver, resolver, root } = ctx;
|
|
1166
|
-
const { output, query,
|
|
1150
|
+
const { output, query, client, group } = ctx.options;
|
|
1167
1151
|
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
1168
1152
|
if (!pluginTs) return null;
|
|
1169
1153
|
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
1170
|
-
|
|
1171
|
-
const queryMethods = new Set(query ? query.methods : []);
|
|
1172
|
-
const isMutation = mutation !== false && !isQuery && (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase());
|
|
1173
|
-
if (!isQuery || isMutation) return null;
|
|
1154
|
+
if (!(query === false || !!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))) return null;
|
|
1174
1155
|
const importPath = query ? query.importPath : "swr";
|
|
1175
1156
|
const contractOp = resolveClientOperation({
|
|
1176
1157
|
clientPlugin: { pluginName: client.pluginName },
|
|
@@ -1180,33 +1161,31 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1180
1161
|
output
|
|
1181
1162
|
});
|
|
1182
1163
|
if (!contractOp) return null;
|
|
1183
|
-
const queryName = resolver.
|
|
1184
|
-
const queryOptionsName = resolver.
|
|
1185
|
-
const queryKeyName = resolver.
|
|
1186
|
-
const queryKeyTypeName = resolver.
|
|
1164
|
+
const queryName = resolver.query.name(node);
|
|
1165
|
+
const queryOptionsName = resolver.query.optionsName(node);
|
|
1166
|
+
const queryKeyName = resolver.query.keyName(node);
|
|
1167
|
+
const queryKeyTypeName = resolver.query.keyTypeName(node);
|
|
1187
1168
|
const meta = {
|
|
1188
|
-
file: resolver.
|
|
1169
|
+
file: resolver.file({
|
|
1189
1170
|
name: queryName,
|
|
1190
1171
|
extname: ".ts",
|
|
1191
1172
|
tag: node.tags[0] ?? "default",
|
|
1192
|
-
path: node.path
|
|
1193
|
-
}, {
|
|
1173
|
+
path: node.path,
|
|
1194
1174
|
root,
|
|
1195
1175
|
output,
|
|
1196
1176
|
group
|
|
1197
1177
|
}),
|
|
1198
|
-
fileTs: tsResolver.
|
|
1178
|
+
fileTs: tsResolver.file({
|
|
1199
1179
|
name: node.operationId,
|
|
1200
1180
|
extname: ".ts",
|
|
1201
1181
|
tag: node.tags[0] ?? "default",
|
|
1202
|
-
path: node.path
|
|
1203
|
-
}, {
|
|
1182
|
+
path: node.path,
|
|
1204
1183
|
root,
|
|
1205
1184
|
output: pluginTs.options?.output ?? output,
|
|
1206
1185
|
group: pluginTs.options?.group
|
|
1207
1186
|
})
|
|
1208
1187
|
};
|
|
1209
|
-
const importedTypeNames = [tsResolver.
|
|
1188
|
+
const importedTypeNames = [tsResolver.response.options(node), ...resolveOperationTypeNames(node, tsResolver, {
|
|
1210
1189
|
exclude: [queryKeyTypeName],
|
|
1211
1190
|
order: "body-response-first",
|
|
1212
1191
|
includeParams: false
|
|
@@ -1216,7 +1195,7 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1216
1195
|
baseName: meta.file.baseName,
|
|
1217
1196
|
path: meta.file.path,
|
|
1218
1197
|
meta: meta.file.meta,
|
|
1219
|
-
banner: resolver.
|
|
1198
|
+
banner: resolver.default.banner(ctx.meta, {
|
|
1220
1199
|
output,
|
|
1221
1200
|
config,
|
|
1222
1201
|
file: {
|
|
@@ -1224,7 +1203,7 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1224
1203
|
baseName: meta.file.baseName
|
|
1225
1204
|
}
|
|
1226
1205
|
}),
|
|
1227
|
-
footer: resolver.
|
|
1206
|
+
footer: resolver.default.footer(ctx.meta, {
|
|
1228
1207
|
output,
|
|
1229
1208
|
config,
|
|
1230
1209
|
file: {
|
|
@@ -1292,53 +1271,55 @@ function capitalize(name) {
|
|
|
1292
1271
|
return `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
|
|
1293
1272
|
}
|
|
1294
1273
|
/**
|
|
1295
|
-
*
|
|
1274
|
+
* Default resolver used by `@kubb/plugin-swr`. Decides the names and file paths for every generated
|
|
1275
|
+
* SWR hook (`useFoo`, `useFooMutation`) and its companion helpers (`fooQueryKey`, `fooQueryOptions`,
|
|
1276
|
+
* `fooMutationKey`).
|
|
1296
1277
|
*
|
|
1297
|
-
*
|
|
1278
|
+
* Functions and files use the built-in camelCase casing; hooks get the `use` prefix.
|
|
1298
1279
|
*
|
|
1299
|
-
* @example
|
|
1300
|
-
*
|
|
1280
|
+
* @example Resolve hook and helper names
|
|
1281
|
+
* ```ts
|
|
1282
|
+
* import { resolverSwr } from '@kubb/plugin-swr'
|
|
1283
|
+
*
|
|
1284
|
+
* resolverSwr.query.name(operationNode) // 'useGetPetById'
|
|
1285
|
+
* resolverSwr.query.keyName(operationNode) // 'getPetByIdQueryKey'
|
|
1286
|
+
* resolverSwr.mutation.name(operationNode) // 'useUpdatePet'
|
|
1287
|
+
* ```
|
|
1301
1288
|
*/
|
|
1302
|
-
const resolverSwr = (0, kubb_kit.
|
|
1303
|
-
name: "default",
|
|
1289
|
+
const resolverSwr = (0, kubb_kit.createResolver)({
|
|
1304
1290
|
pluginName: "plugin-swr",
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
return `${this.resolveName(node.operationId)}QueryOptions`;
|
|
1322
|
-
},
|
|
1323
|
-
resolveQueryKeyName(node) {
|
|
1324
|
-
return `${this.resolveName(node.operationId)}QueryKey`;
|
|
1325
|
-
},
|
|
1326
|
-
resolveMutationKeyName(node) {
|
|
1327
|
-
return `${this.resolveName(node.operationId)}MutationKey`;
|
|
1328
|
-
},
|
|
1329
|
-
resolveQueryKeyTypeName(node) {
|
|
1330
|
-
return `${capitalize(this.resolveName(node.operationId))}QueryKey`;
|
|
1331
|
-
},
|
|
1332
|
-
resolveMutationKeyTypeName(node) {
|
|
1333
|
-
return `${capitalize(this.resolveName(node.operationId))}MutationKey`;
|
|
1334
|
-
},
|
|
1335
|
-
resolveMutationArgTypeName(node) {
|
|
1336
|
-
return `${capitalize(this.resolveName(node.operationId))}MutationArg`;
|
|
1291
|
+
query: {
|
|
1292
|
+
name(node) {
|
|
1293
|
+
return `use${capitalize(this.name(node.operationId))}`;
|
|
1294
|
+
},
|
|
1295
|
+
optionsName(node) {
|
|
1296
|
+
return `${this.name(node.operationId)}QueryOptions`;
|
|
1297
|
+
},
|
|
1298
|
+
keyName(node) {
|
|
1299
|
+
return `${this.name(node.operationId)}QueryKey`;
|
|
1300
|
+
},
|
|
1301
|
+
keyTypeName(node) {
|
|
1302
|
+
return `${capitalize(this.name(node.operationId))}QueryKey`;
|
|
1303
|
+
},
|
|
1304
|
+
clientName(node) {
|
|
1305
|
+
return this.name(node.operationId);
|
|
1306
|
+
}
|
|
1337
1307
|
},
|
|
1338
|
-
|
|
1339
|
-
|
|
1308
|
+
mutation: {
|
|
1309
|
+
name(node) {
|
|
1310
|
+
return `use${capitalize(this.name(node.operationId))}`;
|
|
1311
|
+
},
|
|
1312
|
+
keyName(node) {
|
|
1313
|
+
return `${this.name(node.operationId)}MutationKey`;
|
|
1314
|
+
},
|
|
1315
|
+
keyTypeName(node) {
|
|
1316
|
+
return `${capitalize(this.name(node.operationId))}MutationKey`;
|
|
1317
|
+
},
|
|
1318
|
+
argTypeName(node) {
|
|
1319
|
+
return `${capitalize(this.name(node.operationId))}MutationArg`;
|
|
1320
|
+
}
|
|
1340
1321
|
}
|
|
1341
|
-
})
|
|
1322
|
+
});
|
|
1342
1323
|
//#endregion
|
|
1343
1324
|
//#region src/plugin.ts
|
|
1344
1325
|
/**
|
|
@@ -1372,17 +1353,14 @@ const pluginSwr = (0, kubb_kit.definePlugin)((options) => {
|
|
|
1372
1353
|
path: "hooks",
|
|
1373
1354
|
barrel: { type: "named" }
|
|
1374
1355
|
}, group, exclude = [], include, override = [], mutation = {}, query = {}, mutationKey = mutationKeyTransformer, queryKey = queryKeyTransformer, client, resolver: userResolver, macros: userMacros } = options;
|
|
1375
|
-
const selectedGenerators = [queryGenerator, mutationGenerator]
|
|
1356
|
+
const selectedGenerators = [queryGenerator, mutationGenerator];
|
|
1376
1357
|
const groupConfig = createGroupConfig(group) ?? void 0;
|
|
1377
1358
|
return {
|
|
1378
1359
|
name: pluginSwrName,
|
|
1379
1360
|
options,
|
|
1380
1361
|
dependencies: [_kubb_plugin_ts.pluginTsName],
|
|
1381
1362
|
hooks: { "kubb:plugin:setup"(ctx) {
|
|
1382
|
-
const resolver = userResolver ?
|
|
1383
|
-
...resolverSwr,
|
|
1384
|
-
...userResolver
|
|
1385
|
-
} : resolverSwr;
|
|
1363
|
+
const resolver = userResolver ? kubb_kit.Resolver.merge(resolverSwr, userResolver) : resolverSwr;
|
|
1386
1364
|
const resolvedClient = resolveClient({
|
|
1387
1365
|
client,
|
|
1388
1366
|
pluginNames: (ctx.config.plugins ?? []).map((p) => p.name).filter((name) => Boolean(name))
|