@kubb/plugin-swr 5.0.0-beta.80 → 5.0.0-beta.81
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 +33 -97
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +36 -164
- package/dist/index.js +33 -97
- package/dist/index.js.map +1 -1
- package/package.json +6 -7
package/dist/index.cjs
CHANGED
|
@@ -32,7 +32,6 @@ let node_path = require("node:path");
|
|
|
32
32
|
node_path = __toESM(node_path, 1);
|
|
33
33
|
let _kubb_core = require("@kubb/core");
|
|
34
34
|
let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
35
|
-
let _kubb_plugin_zod = require("@kubb/plugin-zod");
|
|
36
35
|
let _kubb_renderer_jsx = require("@kubb/renderer-jsx");
|
|
37
36
|
let _kubb_renderer_jsx_jsx_runtime = require("@kubb/renderer-jsx/jsx-runtime");
|
|
38
37
|
//#region ../../internals/utils/src/casing.ts
|
|
@@ -555,16 +554,6 @@ function createGroupConfig(group) {
|
|
|
555
554
|
};
|
|
556
555
|
}
|
|
557
556
|
//#endregion
|
|
558
|
-
//#region ../../internals/client/src/builders/validatorOptions.ts
|
|
559
|
-
/**
|
|
560
|
-
* Returns `true` when any direction of the validator uses zod (used for dependency checks).
|
|
561
|
-
*/
|
|
562
|
-
function isValidatorEnabled(validator) {
|
|
563
|
-
if (!validator) return false;
|
|
564
|
-
if (validator === "zod") return true;
|
|
565
|
-
return Boolean(validator.request || validator.response);
|
|
566
|
-
}
|
|
567
|
-
//#endregion
|
|
568
557
|
//#region ../../internals/client/src/resolveClient.ts
|
|
569
558
|
/**
|
|
570
559
|
* Resolves which client plugin a consumer (react-query, vue-query, swr, mcp) should call for an
|
|
@@ -783,51 +772,6 @@ function buildClientCall(node, options) {
|
|
|
783
772
|
].filter((part) => part !== null).join(", ")} })`;
|
|
784
773
|
}
|
|
785
774
|
/**
|
|
786
|
-
* Returns `'zod'` when response-direction parsing is enabled.
|
|
787
|
-
* The string shorthand `'zod'` also enables response parsing.
|
|
788
|
-
*/
|
|
789
|
-
function resolveResponseParser(parser) {
|
|
790
|
-
if (!parser) return null;
|
|
791
|
-
if (parser === "zod") return "zod";
|
|
792
|
-
return parser.response ?? null;
|
|
793
|
-
}
|
|
794
|
-
/**
|
|
795
|
-
* Returns `'zod'` when request body parsing is enabled.
|
|
796
|
-
* The string shorthand `'zod'` also enables request body parsing (existing behavior).
|
|
797
|
-
*/
|
|
798
|
-
function resolveRequestParser(parser) {
|
|
799
|
-
if (!parser) return null;
|
|
800
|
-
if (parser === "zod") return "zod";
|
|
801
|
-
return parser.request ?? null;
|
|
802
|
-
}
|
|
803
|
-
/**
|
|
804
|
-
* Returns `'zod'` when query-params parsing is enabled.
|
|
805
|
-
* Only the object form `{ request: 'zod' }` enables this. `parser: 'zod'` does not.
|
|
806
|
-
*/
|
|
807
|
-
function resolveQueryParamsParser(parser) {
|
|
808
|
-
if (!parser || parser === "zod") return null;
|
|
809
|
-
return parser.request ?? null;
|
|
810
|
-
}
|
|
811
|
-
/**
|
|
812
|
-
* Collects the Zod schema import names for an operation based on the active parser directions.
|
|
813
|
-
*
|
|
814
|
-
* - `parser: 'zod'`: response and request body names (backward-compatible behavior).
|
|
815
|
-
* - `parser: { request: 'zod' }`: request body and query params names.
|
|
816
|
-
* - `parser: { response: 'zod' }`: response name only.
|
|
817
|
-
* - `parser: { request: 'zod', response: 'zod' }`: all three.
|
|
818
|
-
*
|
|
819
|
-
* Returns an empty array when no resolver is provided or `parser` is falsy.
|
|
820
|
-
*/
|
|
821
|
-
function resolveZodSchemaNames(node, zodResolver, parser) {
|
|
822
|
-
if (!zodResolver || !parser) return [];
|
|
823
|
-
const { query: queryParams } = getOperationParameters(node, { paramsCasing: "original" });
|
|
824
|
-
return [
|
|
825
|
-
resolveResponseParser(parser) === "zod" ? zodResolver.resolveResponseName?.(node) : null,
|
|
826
|
-
resolveRequestParser(parser) === "zod" && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null,
|
|
827
|
-
resolveQueryParamsParser(parser) === "zod" && queryParams.length > 0 ? zodResolver.resolveQueryParamsName?.(node, queryParams[0]) : null
|
|
828
|
-
].filter((n) => Boolean(n));
|
|
829
|
-
}
|
|
830
|
-
/**
|
|
831
775
|
* Build QueryKey params as the grouped `{ path, query, body }` object (NO headers, NO config),
|
|
832
776
|
* typed from the operation's `RequestConfig` minus `url`. The query key transformer reads the
|
|
833
777
|
* grouped `path`/`query`/`body` bindings.
|
|
@@ -1092,7 +1036,7 @@ const mutationGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1092
1036
|
operation(node, ctx) {
|
|
1093
1037
|
if (!_kubb_core.ast.isHttpOperationNode(node)) return null;
|
|
1094
1038
|
const { config, driver, resolver, root } = ctx;
|
|
1095
|
-
const { output, query, mutation,
|
|
1039
|
+
const { output, query, mutation, client, group } = ctx.options;
|
|
1096
1040
|
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
1097
1041
|
if (!pluginTs) return null;
|
|
1098
1042
|
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
@@ -1138,19 +1082,6 @@ const mutationGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1138
1082
|
order: "body-response-first",
|
|
1139
1083
|
includeParams: false
|
|
1140
1084
|
})].filter((name) => Boolean(name));
|
|
1141
|
-
const pluginZod = isValidatorEnabled(validator) ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : void 0;
|
|
1142
|
-
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : void 0;
|
|
1143
|
-
const fileZod = zodResolver ? zodResolver.resolveFile({
|
|
1144
|
-
name: node.operationId,
|
|
1145
|
-
extname: ".ts",
|
|
1146
|
-
tag: node.tags[0] ?? "default",
|
|
1147
|
-
path: node.path
|
|
1148
|
-
}, {
|
|
1149
|
-
root,
|
|
1150
|
-
output: pluginZod?.options?.output ?? output,
|
|
1151
|
-
group: pluginZod?.options?.group
|
|
1152
|
-
}) : void 0;
|
|
1153
|
-
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, validator);
|
|
1154
1085
|
const calledClientName = contractOp.name;
|
|
1155
1086
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
1156
1087
|
baseName: meta.file.baseName,
|
|
@@ -1173,11 +1104,6 @@ const mutationGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1173
1104
|
}
|
|
1174
1105
|
}),
|
|
1175
1106
|
children: [
|
|
1176
|
-
fileZod && zodSchemaNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1177
|
-
name: zodSchemaNames,
|
|
1178
|
-
root: meta.file.path,
|
|
1179
|
-
path: fileZod.path
|
|
1180
|
-
}),
|
|
1181
1107
|
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1182
1108
|
name: [contractOp.name],
|
|
1183
1109
|
root: meta.file.path,
|
|
@@ -1227,13 +1153,17 @@ const mutationGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1227
1153
|
});
|
|
1228
1154
|
//#endregion
|
|
1229
1155
|
//#region src/generators/queryGenerator.tsx
|
|
1156
|
+
/**
|
|
1157
|
+
* Built-in generator for `useSWR` hooks. Emits one `useFoo` hook per read operation plus the
|
|
1158
|
+
* matching `fooQueryKey` helper and `fooQueryOptions` factory.
|
|
1159
|
+
*/
|
|
1230
1160
|
const queryGenerator = (0, _kubb_core.defineGenerator)({
|
|
1231
1161
|
name: "swr-query",
|
|
1232
1162
|
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
1233
1163
|
operation(node, ctx) {
|
|
1234
1164
|
if (!_kubb_core.ast.isHttpOperationNode(node)) return null;
|
|
1235
1165
|
const { config, driver, resolver, root } = ctx;
|
|
1236
|
-
const { output, query, mutation,
|
|
1166
|
+
const { output, query, mutation, client, group } = ctx.options;
|
|
1237
1167
|
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
1238
1168
|
if (!pluginTs) return null;
|
|
1239
1169
|
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
@@ -1281,19 +1211,6 @@ const queryGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1281
1211
|
order: "body-response-first",
|
|
1282
1212
|
includeParams: false
|
|
1283
1213
|
})].filter((name) => Boolean(name));
|
|
1284
|
-
const pluginZod = isValidatorEnabled(validator) ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : void 0;
|
|
1285
|
-
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : void 0;
|
|
1286
|
-
const fileZod = zodResolver ? zodResolver.resolveFile({
|
|
1287
|
-
name: node.operationId,
|
|
1288
|
-
extname: ".ts",
|
|
1289
|
-
tag: node.tags[0] ?? "default",
|
|
1290
|
-
path: node.path
|
|
1291
|
-
}, {
|
|
1292
|
-
root,
|
|
1293
|
-
output: pluginZod?.options?.output ?? output,
|
|
1294
|
-
group: pluginZod?.options?.group
|
|
1295
|
-
}) : void 0;
|
|
1296
|
-
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, validator);
|
|
1297
1214
|
const calledClientName = contractOp.name;
|
|
1298
1215
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
1299
1216
|
baseName: meta.file.baseName,
|
|
@@ -1316,11 +1233,6 @@ const queryGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1316
1233
|
}
|
|
1317
1234
|
}),
|
|
1318
1235
|
children: [
|
|
1319
|
-
fileZod && zodSchemaNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1320
|
-
name: zodSchemaNames,
|
|
1321
|
-
root: meta.file.path,
|
|
1322
|
-
path: fileZod.path
|
|
1323
|
-
}),
|
|
1324
1236
|
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1325
1237
|
name: [contractOp.name],
|
|
1326
1238
|
root: meta.file.path,
|
|
@@ -1429,18 +1341,43 @@ const resolverSwr = (0, _kubb_core.defineResolver)(() => ({
|
|
|
1429
1341
|
}));
|
|
1430
1342
|
//#endregion
|
|
1431
1343
|
//#region src/plugin.ts
|
|
1344
|
+
/**
|
|
1345
|
+
* Canonical plugin name for `@kubb/plugin-swr`. Used for driver lookups and cross-plugin
|
|
1346
|
+
* dependency references.
|
|
1347
|
+
*/
|
|
1432
1348
|
const pluginSwrName = "plugin-swr";
|
|
1349
|
+
/**
|
|
1350
|
+
* Generates SWR hooks from OpenAPI operations. Read operations become `useFoo` hooks built on
|
|
1351
|
+
* `useSWR` and write operations become `useFoo` hooks built on `useSWRMutation`, wrapping the
|
|
1352
|
+
* client functions from `@kubb/plugin-axios` or `@kubb/plugin-fetch`.
|
|
1353
|
+
*
|
|
1354
|
+
* @example
|
|
1355
|
+
* ```ts
|
|
1356
|
+
* import { defineConfig } from 'kubb'
|
|
1357
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
1358
|
+
* import { pluginSwr } from '@kubb/plugin-swr'
|
|
1359
|
+
*
|
|
1360
|
+
* export default defineConfig({
|
|
1361
|
+
* input: { path: './petStore.yaml' },
|
|
1362
|
+
* output: { path: './src/gen' },
|
|
1363
|
+
* plugins: [
|
|
1364
|
+
* pluginTs(),
|
|
1365
|
+
* pluginSwr({ output: { path: './hooks' } }),
|
|
1366
|
+
* ],
|
|
1367
|
+
* })
|
|
1368
|
+
* ```
|
|
1369
|
+
*/
|
|
1433
1370
|
const pluginSwr = (0, _kubb_core.definePlugin)((options) => {
|
|
1434
1371
|
const { output = {
|
|
1435
1372
|
path: "hooks",
|
|
1436
1373
|
barrel: { type: "named" }
|
|
1437
|
-
}, group, exclude = [], include, override = [],
|
|
1374
|
+
}, group, exclude = [], include, override = [], mutation = {}, query = {}, mutationKey = mutationKeyTransformer, queryKey = queryKeyTransformer, client, resolver: userResolver, macros: userMacros } = options;
|
|
1438
1375
|
const selectedGenerators = [queryGenerator, mutationGenerator].filter((generator) => Boolean(generator));
|
|
1439
1376
|
const groupConfig = createGroupConfig(group) ?? void 0;
|
|
1440
1377
|
return {
|
|
1441
1378
|
name: pluginSwrName,
|
|
1442
1379
|
options,
|
|
1443
|
-
dependencies: [_kubb_plugin_ts.pluginTsName
|
|
1380
|
+
dependencies: [_kubb_plugin_ts.pluginTsName],
|
|
1444
1381
|
hooks: { "kubb:plugin:setup"(ctx) {
|
|
1445
1382
|
const resolver = userResolver ? {
|
|
1446
1383
|
...resolverSwr,
|
|
@@ -1475,7 +1412,6 @@ const pluginSwr = (0, _kubb_core.definePlugin)((options) => {
|
|
|
1475
1412
|
],
|
|
1476
1413
|
...mutation
|
|
1477
1414
|
},
|
|
1478
|
-
validator,
|
|
1479
1415
|
group: groupConfig,
|
|
1480
1416
|
exclude,
|
|
1481
1417
|
include,
|