@kubb/plugin-swr 5.0.0-beta.87 → 5.0.0-beta.95
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 +122 -54
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +21 -2
- package/dist/index.js +122 -55
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
package/dist/index.cjs
CHANGED
|
@@ -60,6 +60,16 @@ function toCamelOrPascal(text, pascal) {
|
|
|
60
60
|
function camelCase(text, { prefix = "", suffix = "" } = {}) {
|
|
61
61
|
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Uppercases only the first character of `text`, leaving the rest untouched.
|
|
65
|
+
* Unlike {@link pascalCase} it never re-splits word boundaries or strips characters.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* `capitalize('getPetById') // 'GetPetById'`
|
|
69
|
+
*/
|
|
70
|
+
function capitalize(text) {
|
|
71
|
+
return `${text.charAt(0).toUpperCase()}${text.slice(1)}`;
|
|
72
|
+
}
|
|
63
73
|
//#endregion
|
|
64
74
|
//#region ../../internals/utils/src/reserved.ts
|
|
65
75
|
/**
|
|
@@ -609,6 +619,20 @@ function resolveClient(options) {
|
|
|
609
619
|
message: "No client plugin is registered. Add `@kubb/plugin-axios` or `@kubb/plugin-fetch` to `plugins` so the generated code has an HTTP client to call."
|
|
610
620
|
};
|
|
611
621
|
}
|
|
622
|
+
/**
|
|
623
|
+
* Resolves the contract client during a consumer plugin's setup hook. Extracts the plugin names
|
|
624
|
+
* from the raw `plugins` config, applies {@link resolveClient}, and throws the diagnostic on a
|
|
625
|
+
* misconfiguration so every consumer fails fast with the same message.
|
|
626
|
+
*/
|
|
627
|
+
function resolveContractClient(options) {
|
|
628
|
+
const { client, plugins = [] } = options;
|
|
629
|
+
const resolved = resolveClient({
|
|
630
|
+
client,
|
|
631
|
+
pluginNames: plugins.map((plugin) => plugin.name).filter((name) => Boolean(name))
|
|
632
|
+
});
|
|
633
|
+
if (resolved.kind === "error") throw new Error(resolved.message);
|
|
634
|
+
return resolved;
|
|
635
|
+
}
|
|
612
636
|
//#endregion
|
|
613
637
|
//#region ../../internals/client/src/resolveClientOperation.ts
|
|
614
638
|
/**
|
|
@@ -664,6 +688,59 @@ function MutationKey$1({ name, node, transformer }) {
|
|
|
664
688
|
}
|
|
665
689
|
__name(MutationKey$1, "MutationKey");
|
|
666
690
|
//#endregion
|
|
691
|
+
//#region ../../internals/tanstack-query/src/resolver.ts
|
|
692
|
+
/**
|
|
693
|
+
* Builds the shared query namespace for a variant. Spread the result into
|
|
694
|
+
* `createResolver` once per variant the plugin supports.
|
|
695
|
+
*
|
|
696
|
+
* @example
|
|
697
|
+
* ```ts
|
|
698
|
+
* createResolver<PluginReactQuery>({
|
|
699
|
+
* query: createQueryResolver(),
|
|
700
|
+
* suspenseQuery: createQueryResolver('Suspense'),
|
|
701
|
+
* })
|
|
702
|
+
* ```
|
|
703
|
+
*/
|
|
704
|
+
function createQueryResolver(variant = "") {
|
|
705
|
+
return {
|
|
706
|
+
name(node) {
|
|
707
|
+
return `use${capitalize(this.name(node.operationId))}${variant}`;
|
|
708
|
+
},
|
|
709
|
+
optionsName(node) {
|
|
710
|
+
return `${this.name(node.operationId)}${variant}QueryOptions`;
|
|
711
|
+
},
|
|
712
|
+
keyName(node) {
|
|
713
|
+
return `${this.name(node.operationId)}${variant}QueryKey`;
|
|
714
|
+
},
|
|
715
|
+
keyTypeName(node) {
|
|
716
|
+
return `${capitalize(this.name(node.operationId))}${variant}QueryKey`;
|
|
717
|
+
},
|
|
718
|
+
clientName(node) {
|
|
719
|
+
return `${this.name(node.operationId)}${variant}`;
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Builds the shared mutation namespace. Spread the result into
|
|
725
|
+
* `createResolver` and add plugin-specific methods (`optionsName`,
|
|
726
|
+
* `typeName`, `argTypeName`) next to it.
|
|
727
|
+
*
|
|
728
|
+
* @example
|
|
729
|
+
* ```ts
|
|
730
|
+
* createResolver<PluginSwr>({ mutation: { ...createMutationResolver(), argTypeName(node) {...} } })
|
|
731
|
+
* ```
|
|
732
|
+
*/
|
|
733
|
+
function createMutationResolver() {
|
|
734
|
+
return {
|
|
735
|
+
name(node) {
|
|
736
|
+
return `use${capitalize(this.name(node.operationId))}`;
|
|
737
|
+
},
|
|
738
|
+
keyName(node) {
|
|
739
|
+
return `${this.name(node.operationId)}MutationKey`;
|
|
740
|
+
}
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
//#endregion
|
|
667
744
|
//#region ../../internals/tanstack-query/src/utils.ts
|
|
668
745
|
/**
|
|
669
746
|
* The grouped request options, ordered for both the destructured signature and the
|
|
@@ -744,6 +821,9 @@ function buildQueryOptionsParams(node, options) {
|
|
|
744
821
|
* shorthand alongside the spread `config`, with `throwOnError: true` pinned last so a caller's config
|
|
745
822
|
* can't flip the query's error semantics. Queries thread the TanStack `signal`; mutations omit it.
|
|
746
823
|
*
|
|
824
|
+
* When `unwrapName` is set, each request group is passed as an explicit member unwrapped through it,
|
|
825
|
+
* used by vue-query to resolve refs and getters with `toValue(...)` at call time.
|
|
826
|
+
*
|
|
747
827
|
* @example
|
|
748
828
|
* ```ts
|
|
749
829
|
* buildClientCall(node, { clientName: 'getPetById', signal: true })
|
|
@@ -751,16 +831,46 @@ function buildQueryOptionsParams(node, options) {
|
|
|
751
831
|
* ```
|
|
752
832
|
*/
|
|
753
833
|
function buildClientCall(node, options) {
|
|
754
|
-
const { clientName, signal = false } = options;
|
|
834
|
+
const { clientName, signal = false, unwrapName } = options;
|
|
755
835
|
const { groups } = getRequestGroupOptionality(node);
|
|
756
836
|
return `${clientName}({ ${[
|
|
757
837
|
"...config",
|
|
758
|
-
...requestGroupOrder.filter((key) => groups[key]),
|
|
838
|
+
...requestGroupOrder.filter((key) => groups[key]).map((name) => unwrapName ? `${name}: ${unwrapName(name)}` : name),
|
|
759
839
|
signal ? "signal: config.signal ?? signal" : null,
|
|
760
840
|
"throwOnError: true"
|
|
761
841
|
].filter((part) => part !== null).join(", ")} })`;
|
|
762
842
|
}
|
|
763
843
|
/**
|
|
844
|
+
* Applies the shared query defaults during plugin setup: `false` disables query generation, and an
|
|
845
|
+
* object merges over `methods: ['GET']` and the plugin's runtime `importPath`.
|
|
846
|
+
*/
|
|
847
|
+
function resolveQueryConfig(query, options) {
|
|
848
|
+
if (query === false) return false;
|
|
849
|
+
return {
|
|
850
|
+
importPath: options.importPath,
|
|
851
|
+
methods: ["GET"],
|
|
852
|
+
...query
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
* Applies the shared mutation defaults during plugin setup: `false` disables mutation generation,
|
|
857
|
+
* and an object merges over `methods: ['POST', 'PUT', 'PATCH', 'DELETE']` and the plugin's runtime
|
|
858
|
+
* `importPath`.
|
|
859
|
+
*/
|
|
860
|
+
function resolveMutationConfig(mutation, options) {
|
|
861
|
+
if (mutation === false) return false;
|
|
862
|
+
return {
|
|
863
|
+
importPath: options.importPath,
|
|
864
|
+
methods: [
|
|
865
|
+
"POST",
|
|
866
|
+
"PUT",
|
|
867
|
+
"PATCH",
|
|
868
|
+
"DELETE"
|
|
869
|
+
],
|
|
870
|
+
...mutation
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
764
874
|
* Build QueryKey params as the grouped `{ path, query, body }` object (NO headers, NO config),
|
|
765
875
|
* typed from the operation's `Options` minus `url`. The query key transformer reads the
|
|
766
876
|
* grouped `path`/`query`/`body` bindings.
|
|
@@ -1267,9 +1377,6 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1267
1377
|
});
|
|
1268
1378
|
//#endregion
|
|
1269
1379
|
//#region src/resolvers/resolverSwr.ts
|
|
1270
|
-
function capitalize(name) {
|
|
1271
|
-
return `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
|
|
1272
|
-
}
|
|
1273
1380
|
/**
|
|
1274
1381
|
* Default resolver used by `@kubb/plugin-swr`. Decides the names and file paths for every generated
|
|
1275
1382
|
* SWR hook (`useFoo`, `useFooMutation`) and its companion helpers (`fooQueryKey`, `fooQueryOptions`,
|
|
@@ -1288,30 +1395,9 @@ function capitalize(name) {
|
|
|
1288
1395
|
*/
|
|
1289
1396
|
const resolverSwr = (0, kubb_kit.createResolver)({
|
|
1290
1397
|
pluginName: "plugin-swr",
|
|
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
|
-
}
|
|
1307
|
-
},
|
|
1398
|
+
query: createQueryResolver(),
|
|
1308
1399
|
mutation: {
|
|
1309
|
-
|
|
1310
|
-
return `use${capitalize(this.name(node.operationId))}`;
|
|
1311
|
-
},
|
|
1312
|
-
keyName(node) {
|
|
1313
|
-
return `${this.name(node.operationId)}MutationKey`;
|
|
1314
|
-
},
|
|
1400
|
+
...createMutationResolver(),
|
|
1315
1401
|
keyTypeName(node) {
|
|
1316
1402
|
return `${capitalize(this.name(node.operationId))}MutationKey`;
|
|
1317
1403
|
},
|
|
@@ -1339,7 +1425,7 @@ const pluginSwrName = "plugin-swr";
|
|
|
1339
1425
|
* import { pluginSwr } from '@kubb/plugin-swr'
|
|
1340
1426
|
*
|
|
1341
1427
|
* export default defineConfig({
|
|
1342
|
-
* input:
|
|
1428
|
+
* input: './petStore.yaml',
|
|
1343
1429
|
* output: { path: './src/gen' },
|
|
1344
1430
|
* plugins: [
|
|
1345
1431
|
* pluginTs(),
|
|
@@ -1361,35 +1447,16 @@ const pluginSwr = (0, kubb_kit.definePlugin)((options) => {
|
|
|
1361
1447
|
dependencies: [_kubb_plugin_ts.pluginTsName],
|
|
1362
1448
|
hooks: { "kubb:plugin:setup"(ctx) {
|
|
1363
1449
|
const resolver = userResolver ? kubb_kit.Resolver.merge(resolverSwr, userResolver) : resolverSwr;
|
|
1364
|
-
const resolvedClient = resolveClient({
|
|
1365
|
-
client,
|
|
1366
|
-
pluginNames: (ctx.config.plugins ?? []).map((p) => p.name).filter((name) => Boolean(name))
|
|
1367
|
-
});
|
|
1368
|
-
if (resolvedClient.kind === "error") throw new Error(resolvedClient.message);
|
|
1369
|
-
const resolvedClientDescriptor = {
|
|
1370
|
-
kind: "contract",
|
|
1371
|
-
pluginName: resolvedClient.pluginName
|
|
1372
|
-
};
|
|
1373
1450
|
ctx.setOptions({
|
|
1374
1451
|
output,
|
|
1375
|
-
client:
|
|
1452
|
+
client: resolveContractClient({
|
|
1453
|
+
client,
|
|
1454
|
+
plugins: ctx.config.plugins
|
|
1455
|
+
}),
|
|
1376
1456
|
queryKey,
|
|
1377
|
-
query: query
|
|
1378
|
-
importPath: "swr",
|
|
1379
|
-
methods: ["GET"],
|
|
1380
|
-
...query
|
|
1381
|
-
},
|
|
1457
|
+
query: resolveQueryConfig(query, { importPath: "swr" }),
|
|
1382
1458
|
mutationKey,
|
|
1383
|
-
mutation: mutation
|
|
1384
|
-
importPath: "swr/mutation",
|
|
1385
|
-
methods: [
|
|
1386
|
-
"POST",
|
|
1387
|
-
"PUT",
|
|
1388
|
-
"PATCH",
|
|
1389
|
-
"DELETE"
|
|
1390
|
-
],
|
|
1391
|
-
...mutation
|
|
1392
|
-
},
|
|
1459
|
+
mutation: resolveMutationConfig(mutation, { importPath: "swr/mutation" }),
|
|
1393
1460
|
group: groupConfig,
|
|
1394
1461
|
exclude,
|
|
1395
1462
|
include,
|
|
@@ -1406,5 +1473,6 @@ const pluginSwr = (0, kubb_kit.definePlugin)((options) => {
|
|
|
1406
1473
|
exports.default = pluginSwr;
|
|
1407
1474
|
exports.pluginSwr = pluginSwr;
|
|
1408
1475
|
exports.pluginSwrName = pluginSwrName;
|
|
1476
|
+
exports.resolverSwr = resolverSwr;
|
|
1409
1477
|
|
|
1410
1478
|
//# sourceMappingURL=index.cjs.map
|