@kubb/plugin-swr 5.8.0-canary.20261029T145027 → 5.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +58 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +58 -28
- package/dist/index.js.map +1 -1
- package/docs/options.md +32 -0
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -492,7 +492,7 @@ var compatibility = {
|
|
|
492
492
|
"node": ">=22.22"
|
|
493
493
|
};
|
|
494
494
|
// package.json
|
|
495
|
-
var version = "5.8.0
|
|
495
|
+
var version = "5.8.0";
|
|
496
496
|
// ../../internals/client/src/resolveOperationTypes.ts
|
|
497
497
|
/**
|
|
498
498
|
* A `plugin-ts` shaped resolver whose operation type names point at the types `pluginZod({ inferred })`
|
|
@@ -741,8 +741,16 @@ function resolveClientOperation(options) {
|
|
|
741
741
|
* `pluginZod({ inferred: 'codec' })`), banner and footer.
|
|
742
742
|
*/
|
|
743
743
|
function resolveHookFile(node, ctx, options) {
|
|
744
|
-
const { name, output, group, contractOp, keyTypeName, extraTypeNames = [] } = options;
|
|
744
|
+
const { name, output, group, contractOp, keyTypeName, extraTypeNames = [], returnType = "data" } = options;
|
|
745
745
|
const { config, root, resolver } = ctx;
|
|
746
|
+
if (returnType !== "data" && contractOp.returnType === "data") throw new _kubb_kit.Diagnostics.Error({
|
|
747
|
+
code: _kubb_kit.Diagnostics.code.invalidPluginOptions,
|
|
748
|
+
severity: "error",
|
|
749
|
+
message: `Option \`returnType\` is '${returnType}' for ${node.operationId}, but the client plugin has \`returnType: 'data'\` and only returns the response body.`,
|
|
750
|
+
help: "Set the client plugin's `returnType` to 'full', or set the hook plugin's `returnType` to 'data'.",
|
|
751
|
+
location: { kind: "config" },
|
|
752
|
+
plugin: resolver.pluginName
|
|
753
|
+
});
|
|
746
754
|
const types = resolveOperationTypes(ctx.driver, resolver.pluginName);
|
|
747
755
|
const tsResolver = types.resolver;
|
|
748
756
|
const file = resolver.file({
|
|
@@ -761,9 +769,10 @@ function resolveHookFile(node, ctx, options) {
|
|
|
761
769
|
});
|
|
762
770
|
const importedTypeNames = [
|
|
763
771
|
tsResolver.response.options(node),
|
|
772
|
+
tsResolver.response.responses(node),
|
|
764
773
|
...extraTypeNames,
|
|
765
774
|
...resolveOperationTypeNames(node, tsResolver, {
|
|
766
|
-
exclude: keyTypeName ?
|
|
775
|
+
exclude: [keyTypeName, returnType !== "data" ? tsResolver.response.response(node) : null].filter((name) => Boolean(name)),
|
|
767
776
|
order: "body-response-first",
|
|
768
777
|
includeParams: false
|
|
769
778
|
})
|
|
@@ -778,6 +787,7 @@ function resolveHookFile(node, ctx, options) {
|
|
|
778
787
|
tsResolver,
|
|
779
788
|
contractOp,
|
|
780
789
|
importedTypeNames,
|
|
790
|
+
returnType,
|
|
781
791
|
banner: resolver.default.banner(ctx.meta, {
|
|
782
792
|
output,
|
|
783
793
|
config,
|
|
@@ -808,7 +818,11 @@ function HookFile({ hook, imports, children }) {
|
|
|
808
818
|
path: contractOp.path
|
|
809
819
|
}),
|
|
810
820
|
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
811
|
-
name: [
|
|
821
|
+
name: [
|
|
822
|
+
"RequestConfig",
|
|
823
|
+
"ResponseErrorConfig",
|
|
824
|
+
"RequestResult"
|
|
825
|
+
],
|
|
812
826
|
root: contractOp.clientPathIsFile ? file.path : void 0,
|
|
813
827
|
path: contractOp.clientPath,
|
|
814
828
|
isTypeOnly: true
|
|
@@ -935,11 +949,12 @@ function buildClientCall(node, options) {
|
|
|
935
949
|
}
|
|
936
950
|
/**
|
|
937
951
|
* Builds the query/mutation body that resolves a `buildClientCall` expression down to the bare
|
|
938
|
-
* success body. `
|
|
952
|
+
* success body. `clientReturnType` mirrors the registered client plugin's own `returnType` option: with
|
|
939
953
|
* `'data'` the call already resolves to the bare body, so it is returned directly; with `'full'`
|
|
940
954
|
* (the default) the call carries `unwrap()`, so the body calls it instead of destructuring `data`
|
|
941
955
|
* off the full result by hand. With `'plain'` (plugin-client) the call resolves to the user's
|
|
942
|
-
* `RequestResult`, so the body awaits it and reads `data`.
|
|
956
|
+
* `RequestResult`, so the body awaits it and reads `data`. With `returnType` `'full'` the body
|
|
957
|
+
* resolves to the whole `RequestResult`; `resolveHookFile` rejects that for a client that already stripped it.
|
|
943
958
|
*
|
|
944
959
|
* @example
|
|
945
960
|
* ```ts
|
|
@@ -948,22 +963,24 @@ function buildClientCall(node, options) {
|
|
|
948
963
|
* ```
|
|
949
964
|
*/
|
|
950
965
|
function buildCallResultBody(call, options = {}) {
|
|
951
|
-
const {
|
|
952
|
-
if (returnType === "data") return `return
|
|
953
|
-
if (
|
|
966
|
+
const { clientReturnType = "full", returnType = "data" } = options;
|
|
967
|
+
if (returnType !== "data" || clientReturnType === "data") return `return ${call}`;
|
|
968
|
+
if (clientReturnType === "plain") return `return (await ${call}).data`;
|
|
954
969
|
return `return ${call}.unwrap()`;
|
|
955
970
|
}
|
|
956
971
|
/**
|
|
957
972
|
* Builds the `TData` / `TError` type expressions shared by every generated hook, joining the resolved
|
|
958
973
|
* success responses into `TData` and wrapping the error responses in `ResponseErrorConfig<...>`.
|
|
974
|
+
* With `returnType` `'full'`, `TData` is the client's `RequestResult` over the operation's responses record.
|
|
959
975
|
*/
|
|
960
|
-
function buildResponseTypes(node, resolver) {
|
|
961
|
-
|
|
976
|
+
function buildResponseTypes(node, resolver, options = {}) {
|
|
977
|
+
const { returnType = "data" } = options;
|
|
978
|
+
return resolver.memo(`responseTypes:${returnType}:${node.method}\0${node.path}`, () => {
|
|
962
979
|
const successNames = resolveSuccessNames(node, resolver);
|
|
963
980
|
const responseName = successNames.length > 0 ? successNames.join(" | ") : resolver.response.response(node);
|
|
964
981
|
const errorNames = resolveErrorNames(node, resolver);
|
|
965
982
|
return {
|
|
966
|
-
TData: responseName,
|
|
983
|
+
TData: returnType !== "data" ? `RequestResult<${resolver.response.responses(node)}, true>` : responseName,
|
|
967
984
|
TError: `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`
|
|
968
985
|
};
|
|
969
986
|
});
|
|
@@ -1239,8 +1256,8 @@ function QueryKey({ name, node, tsResolver, typeName, transformer, variant = "qu
|
|
|
1239
1256
|
const declarationPrinter$2 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1240
1257
|
const callPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1241
1258
|
function buildMutationParamsNode(node, options) {
|
|
1242
|
-
const { mutationKeyTypeName, mutationArgTypeName, resolver } = options;
|
|
1243
|
-
const { TData, TError } = buildResponseTypes(node, resolver);
|
|
1259
|
+
const { mutationKeyTypeName, mutationArgTypeName, resolver, returnType } = options;
|
|
1260
|
+
const { TData, TError } = buildResponseTypes(node, resolver, { returnType });
|
|
1244
1261
|
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [(0, _kubb_plugin_ts.createFunctionParameter)({
|
|
1245
1262
|
name: "options",
|
|
1246
1263
|
type: `{
|
|
@@ -1251,8 +1268,8 @@ function buildMutationParamsNode(node, options) {
|
|
|
1251
1268
|
default: "{}"
|
|
1252
1269
|
})] });
|
|
1253
1270
|
}
|
|
1254
|
-
function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, mutationArgTypeName, node, tsResolver, returnType
|
|
1255
|
-
const { TData, TError } = buildResponseTypes(node, tsResolver);
|
|
1271
|
+
function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, mutationArgTypeName, node, tsResolver, clientReturnType, returnType }) {
|
|
1272
|
+
const { TData, TError } = buildResponseTypes(node, tsResolver, { returnType });
|
|
1256
1273
|
const groupedParam = buildGroupedRequestParam(node, {
|
|
1257
1274
|
resolver: tsResolver,
|
|
1258
1275
|
withContentType: true
|
|
@@ -1265,8 +1282,11 @@ function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, muta
|
|
|
1265
1282
|
clientName,
|
|
1266
1283
|
signal: false,
|
|
1267
1284
|
withContentType: true,
|
|
1285
|
+
returnType: clientReturnType
|
|
1286
|
+
}), {
|
|
1287
|
+
clientReturnType,
|
|
1268
1288
|
returnType
|
|
1269
|
-
})
|
|
1289
|
+
});
|
|
1270
1290
|
const generics = [
|
|
1271
1291
|
TData,
|
|
1272
1292
|
TError,
|
|
@@ -1276,7 +1296,8 @@ function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, muta
|
|
|
1276
1296
|
const paramsNode = buildMutationParamsNode(node, {
|
|
1277
1297
|
mutationKeyTypeName,
|
|
1278
1298
|
mutationArgTypeName,
|
|
1279
|
-
resolver: tsResolver
|
|
1299
|
+
resolver: tsResolver,
|
|
1300
|
+
returnType
|
|
1280
1301
|
});
|
|
1281
1302
|
const paramsSignature = declarationPrinter$2.print(paramsNode) ?? "";
|
|
1282
1303
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
@@ -1349,7 +1370,8 @@ const mutationGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1349
1370
|
name: mutationHookName,
|
|
1350
1371
|
output,
|
|
1351
1372
|
group,
|
|
1352
|
-
contractOp
|
|
1373
|
+
contractOp,
|
|
1374
|
+
returnType: ctx.options.returnType
|
|
1353
1375
|
});
|
|
1354
1376
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(HookFile, {
|
|
1355
1377
|
hook,
|
|
@@ -1376,7 +1398,8 @@ const mutationGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1376
1398
|
mutationArgTypeName,
|
|
1377
1399
|
node,
|
|
1378
1400
|
tsResolver: hook.tsResolver,
|
|
1379
|
-
|
|
1401
|
+
clientReturnType: contractOp.returnType,
|
|
1402
|
+
returnType: hook.returnType
|
|
1380
1403
|
})
|
|
1381
1404
|
] })]
|
|
1382
1405
|
});
|
|
@@ -1385,8 +1408,8 @@ const mutationGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1385
1408
|
// src/components/Query.tsx
|
|
1386
1409
|
const declarationPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1387
1410
|
const callPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1388
|
-
function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver }) {
|
|
1389
|
-
const { TData, TError } = buildResponseTypes(node, tsResolver);
|
|
1411
|
+
function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver, returnType }) {
|
|
1412
|
+
const { TData, TError } = buildResponseTypes(node, tsResolver, { returnType });
|
|
1390
1413
|
const generics = [
|
|
1391
1414
|
TData,
|
|
1392
1415
|
TError,
|
|
@@ -1438,13 +1461,16 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, t
|
|
|
1438
1461
|
}
|
|
1439
1462
|
// src/components/QueryOptions.tsx
|
|
1440
1463
|
const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1441
|
-
function QueryOptions({ name, clientName, node, tsResolver, returnType
|
|
1464
|
+
function QueryOptions({ name, clientName, node, tsResolver, clientReturnType, returnType }) {
|
|
1442
1465
|
const paramsNode = buildQueryOptionsParams(node, { resolver: tsResolver });
|
|
1443
1466
|
const paramsSignature = declarationPrinter.print(paramsNode) ?? "";
|
|
1444
1467
|
const fetcherBody = buildCallResultBody(buildClientCall(node, {
|
|
1445
1468
|
clientName,
|
|
1446
1469
|
signal: false
|
|
1447
|
-
}), {
|
|
1470
|
+
}), {
|
|
1471
|
+
clientReturnType,
|
|
1472
|
+
returnType
|
|
1473
|
+
});
|
|
1448
1474
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1449
1475
|
name,
|
|
1450
1476
|
isExportable: true,
|
|
@@ -1499,7 +1525,8 @@ const queryGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1499
1525
|
output,
|
|
1500
1526
|
group,
|
|
1501
1527
|
contractOp,
|
|
1502
|
-
keyTypeName: queryKeyTypeName
|
|
1528
|
+
keyTypeName: queryKeyTypeName,
|
|
1529
|
+
returnType: ctx.options.returnType
|
|
1503
1530
|
});
|
|
1504
1531
|
const { tsResolver } = hook;
|
|
1505
1532
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(HookFile, {
|
|
@@ -1518,7 +1545,8 @@ const queryGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1518
1545
|
clientName: contractOp.name,
|
|
1519
1546
|
node,
|
|
1520
1547
|
tsResolver,
|
|
1521
|
-
|
|
1548
|
+
clientReturnType: contractOp.returnType,
|
|
1549
|
+
returnType: hook.returnType
|
|
1522
1550
|
}),
|
|
1523
1551
|
query && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [
|
|
1524
1552
|
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
@@ -1536,7 +1564,8 @@ const queryGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1536
1564
|
queryKeyName,
|
|
1537
1565
|
queryKeyTypeName,
|
|
1538
1566
|
node,
|
|
1539
|
-
tsResolver
|
|
1567
|
+
tsResolver,
|
|
1568
|
+
returnType: hook.returnType
|
|
1540
1569
|
})
|
|
1541
1570
|
] })
|
|
1542
1571
|
]
|
|
@@ -1621,7 +1650,7 @@ const pluginSwr = (0, _kubb_kit.defineExtension)({
|
|
|
1621
1650
|
const { output = {
|
|
1622
1651
|
path: "hooks",
|
|
1623
1652
|
barrel: { type: "named" }
|
|
1624
|
-
}, group, exclude = [], include, override = [], mutation = {}, query = {}, mutationKey = mutationKeyTransformer, queryKey = queryKeyTransformer, client, resolver: userResolver, macros: userMacros } = options;
|
|
1653
|
+
}, group, exclude = [], include, override = [], mutation = {}, query = {}, mutationKey = mutationKeyTransformer, queryKey = queryKeyTransformer, client, returnType = "data", resolver: userResolver, macros: userMacros } = options;
|
|
1625
1654
|
const resolver = userResolver ? _kubb_kit.Resolver.merge(resolverSwr, userResolver) : resolverSwr;
|
|
1626
1655
|
(0, _kubb_kit.setResolver)(resolver);
|
|
1627
1656
|
if (userMacros?.length) (0, _kubb_kit.setMacros)(userMacros);
|
|
@@ -1633,6 +1662,7 @@ const pluginSwr = (0, _kubb_kit.defineExtension)({
|
|
|
1633
1662
|
client,
|
|
1634
1663
|
plugins: (0, _kubb_kit.useConfig)().plugins
|
|
1635
1664
|
}),
|
|
1665
|
+
returnType,
|
|
1636
1666
|
queryKey,
|
|
1637
1667
|
query: resolveQueryConfig(query, { importPath: "swr" }),
|
|
1638
1668
|
mutationKey,
|