@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.js CHANGED
@@ -30,6 +30,16 @@ function toCamelOrPascal(text, pascal) {
30
30
  function camelCase(text, { prefix = "", suffix = "" } = {}) {
31
31
  return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
32
32
  }
33
+ /**
34
+ * Uppercases only the first character of `text`, leaving the rest untouched.
35
+ * Unlike {@link pascalCase} it never re-splits word boundaries or strips characters.
36
+ *
37
+ * @example
38
+ * `capitalize('getPetById') // 'GetPetById'`
39
+ */
40
+ function capitalize(text) {
41
+ return `${text.charAt(0).toUpperCase()}${text.slice(1)}`;
42
+ }
33
43
  //#endregion
34
44
  //#region ../../internals/utils/src/reserved.ts
35
45
  /**
@@ -579,6 +589,20 @@ function resolveClient(options) {
579
589
  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."
580
590
  };
581
591
  }
592
+ /**
593
+ * Resolves the contract client during a consumer plugin's setup hook. Extracts the plugin names
594
+ * from the raw `plugins` config, applies {@link resolveClient}, and throws the diagnostic on a
595
+ * misconfiguration so every consumer fails fast with the same message.
596
+ */
597
+ function resolveContractClient(options) {
598
+ const { client, plugins = [] } = options;
599
+ const resolved = resolveClient({
600
+ client,
601
+ pluginNames: plugins.map((plugin) => plugin.name).filter((name) => Boolean(name))
602
+ });
603
+ if (resolved.kind === "error") throw new Error(resolved.message);
604
+ return resolved;
605
+ }
582
606
  //#endregion
583
607
  //#region ../../internals/client/src/resolveClientOperation.ts
584
608
  /**
@@ -634,6 +658,59 @@ function MutationKey$1({ name, node, transformer }) {
634
658
  }
635
659
  __name(MutationKey$1, "MutationKey");
636
660
  //#endregion
661
+ //#region ../../internals/tanstack-query/src/resolver.ts
662
+ /**
663
+ * Builds the shared query namespace for a variant. Spread the result into
664
+ * `createResolver` once per variant the plugin supports.
665
+ *
666
+ * @example
667
+ * ```ts
668
+ * createResolver<PluginReactQuery>({
669
+ * query: createQueryResolver(),
670
+ * suspenseQuery: createQueryResolver('Suspense'),
671
+ * })
672
+ * ```
673
+ */
674
+ function createQueryResolver(variant = "") {
675
+ return {
676
+ name(node) {
677
+ return `use${capitalize(this.name(node.operationId))}${variant}`;
678
+ },
679
+ optionsName(node) {
680
+ return `${this.name(node.operationId)}${variant}QueryOptions`;
681
+ },
682
+ keyName(node) {
683
+ return `${this.name(node.operationId)}${variant}QueryKey`;
684
+ },
685
+ keyTypeName(node) {
686
+ return `${capitalize(this.name(node.operationId))}${variant}QueryKey`;
687
+ },
688
+ clientName(node) {
689
+ return `${this.name(node.operationId)}${variant}`;
690
+ }
691
+ };
692
+ }
693
+ /**
694
+ * Builds the shared mutation namespace. Spread the result into
695
+ * `createResolver` and add plugin-specific methods (`optionsName`,
696
+ * `typeName`, `argTypeName`) next to it.
697
+ *
698
+ * @example
699
+ * ```ts
700
+ * createResolver<PluginSwr>({ mutation: { ...createMutationResolver(), argTypeName(node) {...} } })
701
+ * ```
702
+ */
703
+ function createMutationResolver() {
704
+ return {
705
+ name(node) {
706
+ return `use${capitalize(this.name(node.operationId))}`;
707
+ },
708
+ keyName(node) {
709
+ return `${this.name(node.operationId)}MutationKey`;
710
+ }
711
+ };
712
+ }
713
+ //#endregion
637
714
  //#region ../../internals/tanstack-query/src/utils.ts
638
715
  /**
639
716
  * The grouped request options, ordered for both the destructured signature and the
@@ -714,6 +791,9 @@ function buildQueryOptionsParams(node, options) {
714
791
  * shorthand alongside the spread `config`, with `throwOnError: true` pinned last so a caller's config
715
792
  * can't flip the query's error semantics. Queries thread the TanStack `signal`; mutations omit it.
716
793
  *
794
+ * When `unwrapName` is set, each request group is passed as an explicit member unwrapped through it,
795
+ * used by vue-query to resolve refs and getters with `toValue(...)` at call time.
796
+ *
717
797
  * @example
718
798
  * ```ts
719
799
  * buildClientCall(node, { clientName: 'getPetById', signal: true })
@@ -721,16 +801,46 @@ function buildQueryOptionsParams(node, options) {
721
801
  * ```
722
802
  */
723
803
  function buildClientCall(node, options) {
724
- const { clientName, signal = false } = options;
804
+ const { clientName, signal = false, unwrapName } = options;
725
805
  const { groups } = getRequestGroupOptionality(node);
726
806
  return `${clientName}({ ${[
727
807
  "...config",
728
- ...requestGroupOrder.filter((key) => groups[key]),
808
+ ...requestGroupOrder.filter((key) => groups[key]).map((name) => unwrapName ? `${name}: ${unwrapName(name)}` : name),
729
809
  signal ? "signal: config.signal ?? signal" : null,
730
810
  "throwOnError: true"
731
811
  ].filter((part) => part !== null).join(", ")} })`;
732
812
  }
733
813
  /**
814
+ * Applies the shared query defaults during plugin setup: `false` disables query generation, and an
815
+ * object merges over `methods: ['GET']` and the plugin's runtime `importPath`.
816
+ */
817
+ function resolveQueryConfig(query, options) {
818
+ if (query === false) return false;
819
+ return {
820
+ importPath: options.importPath,
821
+ methods: ["GET"],
822
+ ...query
823
+ };
824
+ }
825
+ /**
826
+ * Applies the shared mutation defaults during plugin setup: `false` disables mutation generation,
827
+ * and an object merges over `methods: ['POST', 'PUT', 'PATCH', 'DELETE']` and the plugin's runtime
828
+ * `importPath`.
829
+ */
830
+ function resolveMutationConfig(mutation, options) {
831
+ if (mutation === false) return false;
832
+ return {
833
+ importPath: options.importPath,
834
+ methods: [
835
+ "POST",
836
+ "PUT",
837
+ "PATCH",
838
+ "DELETE"
839
+ ],
840
+ ...mutation
841
+ };
842
+ }
843
+ /**
734
844
  * Build QueryKey params as the grouped `{ path, query, body }` object (NO headers, NO config),
735
845
  * typed from the operation's `Options` minus `url`. The query key transformer reads the
736
846
  * grouped `path`/`query`/`body` bindings.
@@ -1237,9 +1347,6 @@ const queryGenerator = defineGenerator({
1237
1347
  });
1238
1348
  //#endregion
1239
1349
  //#region src/resolvers/resolverSwr.ts
1240
- function capitalize(name) {
1241
- return `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
1242
- }
1243
1350
  /**
1244
1351
  * Default resolver used by `@kubb/plugin-swr`. Decides the names and file paths for every generated
1245
1352
  * SWR hook (`useFoo`, `useFooMutation`) and its companion helpers (`fooQueryKey`, `fooQueryOptions`,
@@ -1258,30 +1365,9 @@ function capitalize(name) {
1258
1365
  */
1259
1366
  const resolverSwr = createResolver({
1260
1367
  pluginName: "plugin-swr",
1261
- query: {
1262
- name(node) {
1263
- return `use${capitalize(this.name(node.operationId))}`;
1264
- },
1265
- optionsName(node) {
1266
- return `${this.name(node.operationId)}QueryOptions`;
1267
- },
1268
- keyName(node) {
1269
- return `${this.name(node.operationId)}QueryKey`;
1270
- },
1271
- keyTypeName(node) {
1272
- return `${capitalize(this.name(node.operationId))}QueryKey`;
1273
- },
1274
- clientName(node) {
1275
- return this.name(node.operationId);
1276
- }
1277
- },
1368
+ query: createQueryResolver(),
1278
1369
  mutation: {
1279
- name(node) {
1280
- return `use${capitalize(this.name(node.operationId))}`;
1281
- },
1282
- keyName(node) {
1283
- return `${this.name(node.operationId)}MutationKey`;
1284
- },
1370
+ ...createMutationResolver(),
1285
1371
  keyTypeName(node) {
1286
1372
  return `${capitalize(this.name(node.operationId))}MutationKey`;
1287
1373
  },
@@ -1309,7 +1395,7 @@ const pluginSwrName = "plugin-swr";
1309
1395
  * import { pluginSwr } from '@kubb/plugin-swr'
1310
1396
  *
1311
1397
  * export default defineConfig({
1312
- * input: { path: './petStore.yaml' },
1398
+ * input: './petStore.yaml',
1313
1399
  * output: { path: './src/gen' },
1314
1400
  * plugins: [
1315
1401
  * pluginTs(),
@@ -1331,35 +1417,16 @@ const pluginSwr = definePlugin((options) => {
1331
1417
  dependencies: [pluginTsName],
1332
1418
  hooks: { "kubb:plugin:setup"(ctx) {
1333
1419
  const resolver = userResolver ? Resolver.merge(resolverSwr, userResolver) : resolverSwr;
1334
- const resolvedClient = resolveClient({
1335
- client,
1336
- pluginNames: (ctx.config.plugins ?? []).map((p) => p.name).filter((name) => Boolean(name))
1337
- });
1338
- if (resolvedClient.kind === "error") throw new Error(resolvedClient.message);
1339
- const resolvedClientDescriptor = {
1340
- kind: "contract",
1341
- pluginName: resolvedClient.pluginName
1342
- };
1343
1420
  ctx.setOptions({
1344
1421
  output,
1345
- client: resolvedClientDescriptor,
1422
+ client: resolveContractClient({
1423
+ client,
1424
+ plugins: ctx.config.plugins
1425
+ }),
1346
1426
  queryKey,
1347
- query: query === false ? false : {
1348
- importPath: "swr",
1349
- methods: ["GET"],
1350
- ...query
1351
- },
1427
+ query: resolveQueryConfig(query, { importPath: "swr" }),
1352
1428
  mutationKey,
1353
- mutation: mutation === false ? false : {
1354
- importPath: "swr/mutation",
1355
- methods: [
1356
- "POST",
1357
- "PUT",
1358
- "PATCH",
1359
- "DELETE"
1360
- ],
1361
- ...mutation
1362
- },
1429
+ mutation: resolveMutationConfig(mutation, { importPath: "swr/mutation" }),
1363
1430
  group: groupConfig,
1364
1431
  exclude,
1365
1432
  include,
@@ -1373,6 +1440,6 @@ const pluginSwr = definePlugin((options) => {
1373
1440
  };
1374
1441
  });
1375
1442
  //#endregion
1376
- export { pluginSwr as default, pluginSwr, pluginSwrName };
1443
+ export { pluginSwr as default, pluginSwr, pluginSwrName, resolverSwr };
1377
1444
 
1378
1445
  //# sourceMappingURL=index.js.map