@kubb/plugin-swr 5.8.0-canary.20261029T145027 → 5.8.1
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 +90 -37
- 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 +90 -37
- package/dist/index.js.map +1 -1
- package/docs/options.md +32 -0
- package/kubb-extension.json +1 -1
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -110,12 +110,18 @@ function getPrimarySuccessContentType(node) {
|
|
|
110
110
|
return contentTypes[0].split(";")[0].trim().toLowerCase();
|
|
111
111
|
}
|
|
112
112
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
|
|
113
|
+
* The `text/event-stream` content of the primary success response, when it is the only content type. A response that also
|
|
114
|
+
* declares another type stays a normal call with its query hooks.
|
|
115
|
+
*/
|
|
116
|
+
function getEventStreamContent(node) {
|
|
117
|
+
return getPrimarySuccessContentType(node) === "text/event-stream" ? getPrimarySuccessResponse(node)?.content?.[0] : void 0;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Whether an operation streams its primary success response as Server-Sent Events (`text/event-stream`). The client
|
|
121
|
+
* generator uses this to return a typed event stream instead of a one-shot `RequestResult`.
|
|
116
122
|
*/
|
|
117
123
|
function isEventStream(node) {
|
|
118
|
-
return
|
|
124
|
+
return getEventStreamContent(node) !== void 0;
|
|
119
125
|
}
|
|
120
126
|
const requestConfigBaseType = `Partial<Omit<RequestConfig, 'path' | 'query' | 'body' | 'headers' | 'url'>>`;
|
|
121
127
|
function buildRequestConfigType(node) {
|
|
@@ -183,6 +189,22 @@ function buildOperationComments(node, options = {}) {
|
|
|
183
189
|
}
|
|
184
190
|
const operationParameterGroupsByNode = /* @__PURE__ */ new WeakMap();
|
|
185
191
|
/**
|
|
192
|
+
* An OpenAPI 3.2 `querystring` parameter is the whole query string, so a form-encoded object becomes one query parameter
|
|
193
|
+
* per property, typed and serialized like any other.
|
|
194
|
+
* Another content type, such as JSON, stays one parameter named after it, and the runtime sends its value as the whole query string.
|
|
195
|
+
*/
|
|
196
|
+
function spreadQuerystring(param) {
|
|
197
|
+
const schema = param.schema.type === "ref" ? param.schema.schema : param.schema;
|
|
198
|
+
const formEncoded = !param.contentType || param.contentType.split(";")[0].trim().toLowerCase() === "application/x-www-form-urlencoded";
|
|
199
|
+
if (schema?.type !== "object" || !formEncoded) return [param];
|
|
200
|
+
return schema.properties.map((property) => _kubb_kit.ast.factory.createParameter({
|
|
201
|
+
name: property.name,
|
|
202
|
+
in: "query",
|
|
203
|
+
schema: property.schema,
|
|
204
|
+
required: param.required && property.required
|
|
205
|
+
}));
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
186
208
|
* Groups an operation's parameters by location (`path`/`query`/`header`/`cookie`), deduping each
|
|
187
209
|
* group by name. Every plugin generator visiting the same `OperationNode` shares one AST instance
|
|
188
210
|
* (see `KubbDriver`), so the result is cached per node to avoid re-filtering and re-deduping the
|
|
@@ -191,17 +213,18 @@ const operationParameterGroupsByNode = /* @__PURE__ */ new WeakMap();
|
|
|
191
213
|
function getOperationParameters(node) {
|
|
192
214
|
const cached = operationParameterGroupsByNode.get(node);
|
|
193
215
|
if (cached) return cached;
|
|
194
|
-
const
|
|
216
|
+
const dedupe = (params) => {
|
|
195
217
|
const seen = /* @__PURE__ */ new Set();
|
|
196
|
-
return
|
|
197
|
-
if (
|
|
218
|
+
return params.filter((param) => {
|
|
219
|
+
if (seen.has(param.name)) return false;
|
|
198
220
|
seen.add(param.name);
|
|
199
221
|
return true;
|
|
200
222
|
});
|
|
201
223
|
};
|
|
224
|
+
const group = (location) => dedupe(node.parameters.filter((param) => param.in === location));
|
|
202
225
|
const groups = {
|
|
203
226
|
path: group("path"),
|
|
204
|
-
query: group("query"),
|
|
227
|
+
query: dedupe([...group("query"), ...group("querystring").flatMap(spreadQuerystring)]),
|
|
205
228
|
header: group("header"),
|
|
206
229
|
cookie: group("cookie")
|
|
207
230
|
};
|
|
@@ -488,11 +511,11 @@ function createGroupConfig(group) {
|
|
|
488
511
|
}
|
|
489
512
|
// kubb-extension.json
|
|
490
513
|
var compatibility = {
|
|
491
|
-
"kubb": ">=5.
|
|
514
|
+
"kubb": ">=5.16.0",
|
|
492
515
|
"node": ">=22.22"
|
|
493
516
|
};
|
|
494
517
|
// package.json
|
|
495
|
-
var version = "5.8.
|
|
518
|
+
var version = "5.8.1";
|
|
496
519
|
// ../../internals/client/src/resolveOperationTypes.ts
|
|
497
520
|
/**
|
|
498
521
|
* A `plugin-ts` shaped resolver whose operation type names point at the types `pluginZod({ inferred })`
|
|
@@ -741,8 +764,16 @@ function resolveClientOperation(options) {
|
|
|
741
764
|
* `pluginZod({ inferred: 'codec' })`), banner and footer.
|
|
742
765
|
*/
|
|
743
766
|
function resolveHookFile(node, ctx, options) {
|
|
744
|
-
const { name, output, group, contractOp, keyTypeName, extraTypeNames = [] } = options;
|
|
767
|
+
const { name, output, group, contractOp, keyTypeName, extraTypeNames = [], returnType = "data" } = options;
|
|
745
768
|
const { config, root, resolver } = ctx;
|
|
769
|
+
if (returnType !== "data" && contractOp.returnType === "data") throw new _kubb_kit.Diagnostics.Error({
|
|
770
|
+
code: _kubb_kit.Diagnostics.code.invalidPluginOptions,
|
|
771
|
+
severity: "error",
|
|
772
|
+
message: `Option \`returnType\` is '${returnType}' for ${node.operationId}, but the client plugin has \`returnType: 'data'\` and only returns the response body.`,
|
|
773
|
+
help: "Set the client plugin's `returnType` to 'full', or set the hook plugin's `returnType` to 'data'.",
|
|
774
|
+
location: { kind: "config" },
|
|
775
|
+
plugin: resolver.pluginName
|
|
776
|
+
});
|
|
746
777
|
const types = resolveOperationTypes(ctx.driver, resolver.pluginName);
|
|
747
778
|
const tsResolver = types.resolver;
|
|
748
779
|
const file = resolver.file({
|
|
@@ -761,9 +792,10 @@ function resolveHookFile(node, ctx, options) {
|
|
|
761
792
|
});
|
|
762
793
|
const importedTypeNames = [
|
|
763
794
|
tsResolver.response.options(node),
|
|
795
|
+
tsResolver.response.responses(node),
|
|
764
796
|
...extraTypeNames,
|
|
765
797
|
...resolveOperationTypeNames(node, tsResolver, {
|
|
766
|
-
exclude: keyTypeName ?
|
|
798
|
+
exclude: [keyTypeName, returnType !== "data" ? tsResolver.response.response(node) : null].filter((name) => Boolean(name)),
|
|
767
799
|
order: "body-response-first",
|
|
768
800
|
includeParams: false
|
|
769
801
|
})
|
|
@@ -778,6 +810,7 @@ function resolveHookFile(node, ctx, options) {
|
|
|
778
810
|
tsResolver,
|
|
779
811
|
contractOp,
|
|
780
812
|
importedTypeNames,
|
|
813
|
+
returnType,
|
|
781
814
|
banner: resolver.default.banner(ctx.meta, {
|
|
782
815
|
output,
|
|
783
816
|
config,
|
|
@@ -808,7 +841,11 @@ function HookFile({ hook, imports, children }) {
|
|
|
808
841
|
path: contractOp.path
|
|
809
842
|
}),
|
|
810
843
|
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
811
|
-
name: [
|
|
844
|
+
name: [
|
|
845
|
+
"RequestConfig",
|
|
846
|
+
"ResponseErrorConfig",
|
|
847
|
+
"RequestResult"
|
|
848
|
+
],
|
|
812
849
|
root: contractOp.clientPathIsFile ? file.path : void 0,
|
|
813
850
|
path: contractOp.clientPath,
|
|
814
851
|
isTypeOnly: true
|
|
@@ -935,11 +972,12 @@ function buildClientCall(node, options) {
|
|
|
935
972
|
}
|
|
936
973
|
/**
|
|
937
974
|
* Builds the query/mutation body that resolves a `buildClientCall` expression down to the bare
|
|
938
|
-
* success body. `
|
|
975
|
+
* success body. `clientReturnType` mirrors the registered client plugin's own `returnType` option: with
|
|
939
976
|
* `'data'` the call already resolves to the bare body, so it is returned directly; with `'full'`
|
|
940
977
|
* (the default) the call carries `unwrap()`, so the body calls it instead of destructuring `data`
|
|
941
978
|
* 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`.
|
|
979
|
+
* `RequestResult`, so the body awaits it and reads `data`. With `returnType` `'full'` the body
|
|
980
|
+
* resolves to the whole `RequestResult`; `resolveHookFile` rejects that for a client that already stripped it.
|
|
943
981
|
*
|
|
944
982
|
* @example
|
|
945
983
|
* ```ts
|
|
@@ -948,22 +986,24 @@ function buildClientCall(node, options) {
|
|
|
948
986
|
* ```
|
|
949
987
|
*/
|
|
950
988
|
function buildCallResultBody(call, options = {}) {
|
|
951
|
-
const {
|
|
952
|
-
if (returnType === "data") return `return
|
|
953
|
-
if (
|
|
989
|
+
const { clientReturnType = "full", returnType = "data" } = options;
|
|
990
|
+
if (returnType !== "data" || clientReturnType === "data") return `return ${call}`;
|
|
991
|
+
if (clientReturnType === "plain") return `return (await ${call}).data`;
|
|
954
992
|
return `return ${call}.unwrap()`;
|
|
955
993
|
}
|
|
956
994
|
/**
|
|
957
995
|
* Builds the `TData` / `TError` type expressions shared by every generated hook, joining the resolved
|
|
958
996
|
* success responses into `TData` and wrapping the error responses in `ResponseErrorConfig<...>`.
|
|
997
|
+
* With `returnType` `'full'`, `TData` is the client's `RequestResult` over the operation's responses record.
|
|
959
998
|
*/
|
|
960
|
-
function buildResponseTypes(node, resolver) {
|
|
961
|
-
|
|
999
|
+
function buildResponseTypes(node, resolver, options = {}) {
|
|
1000
|
+
const { returnType = "data" } = options;
|
|
1001
|
+
return resolver.memo(`responseTypes:${returnType}:${node.method}\0${node.path}`, () => {
|
|
962
1002
|
const successNames = resolveSuccessNames(node, resolver);
|
|
963
1003
|
const responseName = successNames.length > 0 ? successNames.join(" | ") : resolver.response.response(node);
|
|
964
1004
|
const errorNames = resolveErrorNames(node, resolver);
|
|
965
1005
|
return {
|
|
966
|
-
TData: responseName,
|
|
1006
|
+
TData: returnType !== "data" ? `RequestResult<${resolver.response.responses(node)}, true>` : responseName,
|
|
967
1007
|
TError: `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`
|
|
968
1008
|
};
|
|
969
1009
|
});
|
|
@@ -1239,8 +1279,8 @@ function QueryKey({ name, node, tsResolver, typeName, transformer, variant = "qu
|
|
|
1239
1279
|
const declarationPrinter$2 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1240
1280
|
const callPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1241
1281
|
function buildMutationParamsNode(node, options) {
|
|
1242
|
-
const { mutationKeyTypeName, mutationArgTypeName, resolver } = options;
|
|
1243
|
-
const { TData, TError } = buildResponseTypes(node, resolver);
|
|
1282
|
+
const { mutationKeyTypeName, mutationArgTypeName, resolver, returnType } = options;
|
|
1283
|
+
const { TData, TError } = buildResponseTypes(node, resolver, { returnType });
|
|
1244
1284
|
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [(0, _kubb_plugin_ts.createFunctionParameter)({
|
|
1245
1285
|
name: "options",
|
|
1246
1286
|
type: `{
|
|
@@ -1251,8 +1291,8 @@ function buildMutationParamsNode(node, options) {
|
|
|
1251
1291
|
default: "{}"
|
|
1252
1292
|
})] });
|
|
1253
1293
|
}
|
|
1254
|
-
function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, mutationArgTypeName, node, tsResolver, returnType
|
|
1255
|
-
const { TData, TError } = buildResponseTypes(node, tsResolver);
|
|
1294
|
+
function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, mutationArgTypeName, node, tsResolver, clientReturnType, returnType }) {
|
|
1295
|
+
const { TData, TError } = buildResponseTypes(node, tsResolver, { returnType });
|
|
1256
1296
|
const groupedParam = buildGroupedRequestParam(node, {
|
|
1257
1297
|
resolver: tsResolver,
|
|
1258
1298
|
withContentType: true
|
|
@@ -1265,8 +1305,11 @@ function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, muta
|
|
|
1265
1305
|
clientName,
|
|
1266
1306
|
signal: false,
|
|
1267
1307
|
withContentType: true,
|
|
1308
|
+
returnType: clientReturnType
|
|
1309
|
+
}), {
|
|
1310
|
+
clientReturnType,
|
|
1268
1311
|
returnType
|
|
1269
|
-
})
|
|
1312
|
+
});
|
|
1270
1313
|
const generics = [
|
|
1271
1314
|
TData,
|
|
1272
1315
|
TError,
|
|
@@ -1276,7 +1319,8 @@ function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, muta
|
|
|
1276
1319
|
const paramsNode = buildMutationParamsNode(node, {
|
|
1277
1320
|
mutationKeyTypeName,
|
|
1278
1321
|
mutationArgTypeName,
|
|
1279
|
-
resolver: tsResolver
|
|
1322
|
+
resolver: tsResolver,
|
|
1323
|
+
returnType
|
|
1280
1324
|
});
|
|
1281
1325
|
const paramsSignature = declarationPrinter$2.print(paramsNode) ?? "";
|
|
1282
1326
|
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 +1393,8 @@ const mutationGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1349
1393
|
name: mutationHookName,
|
|
1350
1394
|
output,
|
|
1351
1395
|
group,
|
|
1352
|
-
contractOp
|
|
1396
|
+
contractOp,
|
|
1397
|
+
returnType: ctx.options.returnType
|
|
1353
1398
|
});
|
|
1354
1399
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(HookFile, {
|
|
1355
1400
|
hook,
|
|
@@ -1376,7 +1421,8 @@ const mutationGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1376
1421
|
mutationArgTypeName,
|
|
1377
1422
|
node,
|
|
1378
1423
|
tsResolver: hook.tsResolver,
|
|
1379
|
-
|
|
1424
|
+
clientReturnType: contractOp.returnType,
|
|
1425
|
+
returnType: hook.returnType
|
|
1380
1426
|
})
|
|
1381
1427
|
] })]
|
|
1382
1428
|
});
|
|
@@ -1385,8 +1431,8 @@ const mutationGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1385
1431
|
// src/components/Query.tsx
|
|
1386
1432
|
const declarationPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1387
1433
|
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);
|
|
1434
|
+
function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver, returnType }) {
|
|
1435
|
+
const { TData, TError } = buildResponseTypes(node, tsResolver, { returnType });
|
|
1390
1436
|
const generics = [
|
|
1391
1437
|
TData,
|
|
1392
1438
|
TError,
|
|
@@ -1438,13 +1484,16 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, t
|
|
|
1438
1484
|
}
|
|
1439
1485
|
// src/components/QueryOptions.tsx
|
|
1440
1486
|
const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1441
|
-
function QueryOptions({ name, clientName, node, tsResolver, returnType
|
|
1487
|
+
function QueryOptions({ name, clientName, node, tsResolver, clientReturnType, returnType }) {
|
|
1442
1488
|
const paramsNode = buildQueryOptionsParams(node, { resolver: tsResolver });
|
|
1443
1489
|
const paramsSignature = declarationPrinter.print(paramsNode) ?? "";
|
|
1444
1490
|
const fetcherBody = buildCallResultBody(buildClientCall(node, {
|
|
1445
1491
|
clientName,
|
|
1446
1492
|
signal: false
|
|
1447
|
-
}), {
|
|
1493
|
+
}), {
|
|
1494
|
+
clientReturnType,
|
|
1495
|
+
returnType
|
|
1496
|
+
});
|
|
1448
1497
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1449
1498
|
name,
|
|
1450
1499
|
isExportable: true,
|
|
@@ -1499,7 +1548,8 @@ const queryGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1499
1548
|
output,
|
|
1500
1549
|
group,
|
|
1501
1550
|
contractOp,
|
|
1502
|
-
keyTypeName: queryKeyTypeName
|
|
1551
|
+
keyTypeName: queryKeyTypeName,
|
|
1552
|
+
returnType: ctx.options.returnType
|
|
1503
1553
|
});
|
|
1504
1554
|
const { tsResolver } = hook;
|
|
1505
1555
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(HookFile, {
|
|
@@ -1518,7 +1568,8 @@ const queryGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1518
1568
|
clientName: contractOp.name,
|
|
1519
1569
|
node,
|
|
1520
1570
|
tsResolver,
|
|
1521
|
-
|
|
1571
|
+
clientReturnType: contractOp.returnType,
|
|
1572
|
+
returnType: hook.returnType
|
|
1522
1573
|
}),
|
|
1523
1574
|
query && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [
|
|
1524
1575
|
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
@@ -1536,7 +1587,8 @@ const queryGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1536
1587
|
queryKeyName,
|
|
1537
1588
|
queryKeyTypeName,
|
|
1538
1589
|
node,
|
|
1539
|
-
tsResolver
|
|
1590
|
+
tsResolver,
|
|
1591
|
+
returnType: hook.returnType
|
|
1540
1592
|
})
|
|
1541
1593
|
] })
|
|
1542
1594
|
]
|
|
@@ -1621,7 +1673,7 @@ const pluginSwr = (0, _kubb_kit.defineExtension)({
|
|
|
1621
1673
|
const { output = {
|
|
1622
1674
|
path: "hooks",
|
|
1623
1675
|
barrel: { type: "named" }
|
|
1624
|
-
}, group, exclude = [], include, override = [], mutation = {}, query = {}, mutationKey = mutationKeyTransformer, queryKey = queryKeyTransformer, client, resolver: userResolver, macros: userMacros } = options;
|
|
1676
|
+
}, group, exclude = [], include, override = [], mutation = {}, query = {}, mutationKey = mutationKeyTransformer, queryKey = queryKeyTransformer, client, returnType = "data", resolver: userResolver, macros: userMacros } = options;
|
|
1625
1677
|
const resolver = userResolver ? _kubb_kit.Resolver.merge(resolverSwr, userResolver) : resolverSwr;
|
|
1626
1678
|
(0, _kubb_kit.setResolver)(resolver);
|
|
1627
1679
|
if (userMacros?.length) (0, _kubb_kit.setMacros)(userMacros);
|
|
@@ -1633,6 +1685,7 @@ const pluginSwr = (0, _kubb_kit.defineExtension)({
|
|
|
1633
1685
|
client,
|
|
1634
1686
|
plugins: (0, _kubb_kit.useConfig)().plugins
|
|
1635
1687
|
}),
|
|
1688
|
+
returnType,
|
|
1636
1689
|
queryKey,
|
|
1637
1690
|
query: resolveQueryConfig(query, { importPath: "swr" }),
|
|
1638
1691
|
mutationKey,
|