@kubb/core 4.33.5 → 4.35.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.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { A as linters, C as UserLogger, D as PluginManager, E as KubbEvents, M as AsyncEventEmitter, O as getMode, S as UserConfig, T as UserPluginWithLifeCycle, _ as PluginParameter, a as InputData, b as ResolvePathParams, c as LoggerContext, d as Plugin, f as PluginContext, g as PluginLifecycleHooks, h as PluginLifecycle, i as Group, j as logLevel, k as formatters, l as LoggerOptions, m as PluginKey, n as Config, o as InputPath, p as PluginFactoryOptions, r as GetPluginFactoryOptions, s as Logger, t as BarrelType, u as Output, v as PluginWithLifeCycle, w as UserPlugin, x as UnknownUserPlugin, y as ResolveNameParams } from "./types-DfjjJb2r.js";
2
+ import { A as UserPluginWithLifeCycle, C as Printer, D as UserConfig, E as UnknownUserPlugin, F as linters, I as logLevel, L as AsyncEventEmitter, M as PluginManager, N as getMode, O as UserLogger, P as formatters, S as PluginWithLifeCycle, T as ResolvePathParams, _ as PluginFactoryOptions, a as Config, b as PluginLifecycleHooks, c as Group, d as Logger, f as LoggerContext, g as PluginContext, h as Plugin, i as BarrelType, j as KubbEvents, k as UserPlugin, l as InputData, m as Output, n as AdapterFactoryOptions, o as DevtoolsOptions, p as LoggerOptions, r as AdapterSource, s as GetPluginFactoryOptions, t as Adapter, u as InputPath, v as PluginKey, w as ResolveNameParams, x as PluginParameter, y as PluginLifecycle } from "./types-7DgxNmCG.js";
3
+ import { definePrinter } from "@kubb/ast";
3
4
  import { Fabric } from "@kubb/react-fabric";
4
5
  import { KubbFile } from "@kubb/fabric-core/types";
5
6
 
@@ -71,6 +72,25 @@ declare function defineConfig(config: PossiblePromise<UserConfig | UserConfig[]>
71
72
  */
72
73
  declare function isInputPath(config: UserConfig | undefined): config is UserConfig<InputPath>;
73
74
  //#endregion
75
+ //#region src/defineAdapter.d.ts
76
+ type AdapterBuilder<T extends AdapterFactoryOptions> = (options: T['options']) => Adapter<T>;
77
+ /**
78
+ * Wraps an adapter builder to make the options parameter optional.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * export const adapterOas = defineAdapter<OasAdapter>((options) => {
83
+ * const { validate = true, dateType = 'string' } = options
84
+ * return {
85
+ * name: adapterOasName,
86
+ * options: { validate, dateType, ... },
87
+ * parse(source) { ... },
88
+ * }
89
+ * })
90
+ * ```
91
+ */
92
+ declare function defineAdapter<T extends AdapterFactoryOptions = AdapterFactoryOptions>(build: AdapterBuilder<T>): (options?: T['options']) => Adapter<T>;
93
+ //#endregion
74
94
  //#region src/defineLogger.d.ts
75
95
  declare function defineLogger<Options extends LoggerOptions = LoggerOptions>(logger: UserLogger<Options>): Logger<Options>;
76
96
  //#endregion
@@ -245,5 +265,5 @@ declare function getConfigs(config: ConfigInput | UserConfig, args: CLIOptions):
245
265
  type Linter = keyof typeof linters;
246
266
  declare function detectLinter(): Promise<Linter | undefined>;
247
267
  //#endregion
248
- export { BarrelType, type CLIOptions, Config, type ConfigInput, type FileMetaBase, FunctionParams, type FunctionParamsAST, GetPluginFactoryOptions, Group, InputData, InputPath, KubbEvents, Logger, LoggerContext, LoggerOptions, Output, PackageManager, Plugin, PluginContext, PluginFactoryOptions, PluginKey, PluginLifecycle, PluginLifecycleHooks, PluginManager, PluginParameter, PluginWithLifeCycle, PromiseManager, ResolveNameParams, ResolvePathParams, UnknownUserPlugin, UserConfig, UserLogger, UserPlugin, UserPluginWithLifeCycle, build, build as default, defineConfig, defineLogger, definePlugin, detectFormatter, detectLinter, formatters, getBarrelFiles, getConfigs, getMode, isInputPath, linters, logLevel, safeBuild, setup };
268
+ export { Adapter, AdapterFactoryOptions, AdapterSource, AsyncEventEmitter, BarrelType, type CLIOptions, Config, type ConfigInput, DevtoolsOptions, type FileMetaBase, FunctionParams, type FunctionParamsAST, GetPluginFactoryOptions, Group, InputData, InputPath, KubbEvents, Logger, LoggerContext, LoggerOptions, Output, PackageManager, Plugin, PluginContext, PluginFactoryOptions, PluginKey, PluginLifecycle, PluginLifecycleHooks, PluginManager, PluginParameter, PluginWithLifeCycle, Printer, PromiseManager, ResolveNameParams, ResolvePathParams, UnknownUserPlugin, UserConfig, UserLogger, UserPlugin, UserPluginWithLifeCycle, build, build as default, defineAdapter, defineConfig, defineLogger, definePlugin, definePrinter, detectFormatter, detectLinter, formatters, getBarrelFiles, getConfigs, getMode, isInputPath, linters, logLevel, safeBuild, setup };
249
269
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,21 +1,23 @@
1
1
  import "./chunk--u3MIqq1.js";
2
2
  import mod from "node:module";
3
- import path, { dirname, join, posix, resolve } from "node:path";
4
3
  import { EventEmitter } from "node:events";
5
4
  import { parseArgs, styleText } from "node:util";
6
5
  import { readFileSync } from "node:fs";
7
6
  import { access, mkdir, readFile, rm, writeFile } from "node:fs/promises";
7
+ import path, { dirname, join, posix, resolve } from "node:path";
8
+ import { definePrinter } from "@kubb/ast";
8
9
  import { createFabric } from "@kubb/react-fabric";
9
10
  import { typescriptParser } from "@kubb/react-fabric/parsers";
10
11
  import { fsPlugin } from "@kubb/react-fabric/plugins";
11
12
  import { performance } from "node:perf_hooks";
13
+ import { deflateSync } from "fflate";
14
+ import { x } from "tinyexec";
12
15
  import { version } from "node:process";
13
16
  import os from "node:os";
14
17
  import { pathToFileURL } from "node:url";
15
18
  import * as pkg from "empathic/package";
16
19
  import { coerce, satisfies } from "semver";
17
20
  import { sortBy } from "remeda";
18
- import { x } from "tinyexec";
19
21
  //#region ../../internals/utils/dist/index.js
20
22
  /** Thrown when a plugin's configuration or input fails validation. */
21
23
  var ValidationPluginError = class extends Error {};
@@ -662,6 +664,9 @@ function defineConfig(config) {
662
664
  function isInputPath(config) {
663
665
  return typeof config?.input === "object" && config.input !== null && "path" in config.input;
664
666
  }
667
+ //#endregion
668
+ //#region src/constants.ts
669
+ const DEFAULT_STUDIO_URL = "https://studio.kubb.dev";
665
670
  const BARREL_FILENAME = "index.ts";
666
671
  const DEFAULT_BANNER = "simple";
667
672
  const DEFAULT_EXTENSION = { ".ts": ".ts" };
@@ -721,6 +726,50 @@ const formatters = {
721
726
  }
722
727
  };
723
728
  //#endregion
729
+ //#region src/devtools.ts
730
+ /**
731
+ * Encodes a `RootNode` as a compressed, URL-safe string.
732
+ *
733
+ * The JSON representation is deflate-compressed with {@link deflateSync} before
734
+ * base64url encoding, which typically reduces payload size by 70–80 % and
735
+ * keeps URLs well within browser and server path-length limits.
736
+ *
737
+ * Use {@link decodeAst} to reverse.
738
+ */
739
+ function encodeAst(root) {
740
+ const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(root)));
741
+ return Buffer.from(compressed).toString("base64url");
742
+ }
743
+ /**
744
+ * Constructs the Kubb Studio URL for the given `RootNode`.
745
+ * When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
746
+ * The `root` is encoded and attached as the `?root=` query parameter so Studio
747
+ * can decode and render it without a round-trip to any server.
748
+ */
749
+ function getStudioUrl(root, studioUrl, options = {}) {
750
+ return `${studioUrl.replace(/\/$/, "")}${options.ast ? "/ast" : ""}?root=${encodeAst(root)}`;
751
+ }
752
+ /**
753
+ * Opens the Kubb Studio URL for the given `RootNode` in the default browser —
754
+ *
755
+ * Falls back to printing the URL if the browser cannot be launched.
756
+ */
757
+ async function openInStudio(root, studioUrl, options = {}) {
758
+ const url = getStudioUrl(root, studioUrl, options);
759
+ const cmd = process.platform === "win32" ? "cmd" : process.platform === "darwin" ? "open" : "xdg-open";
760
+ const args = process.platform === "win32" ? [
761
+ "/c",
762
+ "start",
763
+ "",
764
+ url
765
+ ] : [url];
766
+ try {
767
+ await x(cmd, args);
768
+ } catch {
769
+ console.log(`\n ${url}\n`);
770
+ }
771
+ }
772
+ //#endregion
724
773
  //#region ../../node_modules/.pnpm/yocto-queue@1.2.2/node_modules/yocto-queue/index.js
725
774
  var Node = class {
726
775
  value;
@@ -907,6 +956,12 @@ function getMode(fileOrFolder) {
907
956
  var PluginManager = class {
908
957
  config;
909
958
  options;
959
+ /**
960
+ * The universal `@kubb/ast` `RootNode` produced by the adapter, set by
961
+ * the build pipeline after the adapter's `parse()` resolves.
962
+ */
963
+ rootNode = void 0;
964
+ #studioIsOpen = false;
910
965
  #plugins = /* @__PURE__ */ new Set();
911
966
  #usedPluginNames = {};
912
967
  #promiseManager;
@@ -924,6 +979,7 @@ var PluginManager = class {
924
979
  }
925
980
  getContext(plugin) {
926
981
  const plugins = [...this.#plugins];
982
+ const pluginManager = this;
927
983
  const baseContext = {
928
984
  fabric: this.options.fabric,
929
985
  config: this.config,
@@ -936,6 +992,17 @@ var PluginManager = class {
936
992
  },
937
993
  upsertFile: async (...files) => {
938
994
  await this.options.fabric.upsertFile(...files);
995
+ },
996
+ get rootNode() {
997
+ return pluginManager.rootNode;
998
+ },
999
+ openInStudio(options) {
1000
+ if (typeof pluginManager.config.devtools !== "object") throw new Error("Devtools must be an object");
1001
+ if (!pluginManager.rootNode) throw new Error("RootNode is not defined, make sure you have set the parser in kubb.config.ts");
1002
+ if (pluginManager.#studioIsOpen) return;
1003
+ pluginManager.#studioIsOpen = true;
1004
+ const studioUrl = pluginManager.config.devtools?.studioUrl ?? "https://studio.kubb.dev";
1005
+ return openInStudio(pluginManager.rootNode, studioUrl, options);
939
1006
  }
940
1007
  };
941
1008
  const mergedExtras = {};
@@ -1293,7 +1360,7 @@ var PluginManager = class {
1293
1360
  };
1294
1361
  //#endregion
1295
1362
  //#region package.json
1296
- var version$1 = "4.33.5";
1363
+ var version$1 = "4.35.0";
1297
1364
  //#endregion
1298
1365
  //#region src/utils/diagnostics.ts
1299
1366
  /**
@@ -1355,6 +1422,10 @@ async function setup(options) {
1355
1422
  defaultBanner: DEFAULT_BANNER,
1356
1423
  ...userConfig.output
1357
1424
  },
1425
+ devtools: userConfig.devtools ? {
1426
+ studioUrl: DEFAULT_STUDIO_URL,
1427
+ ...typeof userConfig.devtools === "boolean" ? {} : userConfig.devtools
1428
+ } : void 0,
1358
1429
  plugins: userConfig.plugins
1359
1430
  };
1360
1431
  if (definedConfig.output.clean) {
@@ -1401,14 +1472,31 @@ async function setup(options) {
1401
1472
  ` • Barrel type: ${definedConfig.output.barrelType || "none"}`
1402
1473
  ]
1403
1474
  });
1475
+ const pluginManager = new PluginManager(definedConfig, {
1476
+ fabric,
1477
+ events,
1478
+ concurrency: 15
1479
+ });
1480
+ if (definedConfig.adapter) {
1481
+ const source = inputToAdapterSource(definedConfig);
1482
+ await events.emit("debug", {
1483
+ date: /* @__PURE__ */ new Date(),
1484
+ logs: [`Running adapter: ${definedConfig.adapter.name}`]
1485
+ });
1486
+ pluginManager.rootNode = await definedConfig.adapter.parse(source);
1487
+ await events.emit("debug", {
1488
+ date: /* @__PURE__ */ new Date(),
1489
+ logs: [
1490
+ `✓ Adapter '${definedConfig.adapter.name}' resolved RootNode`,
1491
+ ` • Schemas: ${pluginManager.rootNode.schemas.length}`,
1492
+ ` • Operations: ${pluginManager.rootNode.operations.length}`
1493
+ ]
1494
+ });
1495
+ }
1404
1496
  return {
1405
1497
  events,
1406
1498
  fabric,
1407
- pluginManager: new PluginManager(definedConfig, {
1408
- fabric,
1409
- events,
1410
- concurrency: 15
1411
- }),
1499
+ pluginManager,
1412
1500
  sources
1413
1501
  };
1414
1502
  }
@@ -1563,6 +1651,44 @@ function buildBarrelExports({ barrelFiles, rootDir, existingExports, config, plu
1563
1651
  });
1564
1652
  });
1565
1653
  }
1654
+ /**
1655
+ * Maps the resolved `Config['input']` shape into an `AdapterSource` that
1656
+ * the adapter's `parse()` can consume.
1657
+ */
1658
+ function inputToAdapterSource(config) {
1659
+ if (Array.isArray(config.input)) return {
1660
+ type: "paths",
1661
+ paths: config.input.map((i) => resolve(config.root, i.path))
1662
+ };
1663
+ if ("data" in config.input) return {
1664
+ type: "data",
1665
+ data: config.input.data
1666
+ };
1667
+ return {
1668
+ type: "path",
1669
+ path: resolve(config.root, config.input.path)
1670
+ };
1671
+ }
1672
+ //#endregion
1673
+ //#region src/defineAdapter.ts
1674
+ /**
1675
+ * Wraps an adapter builder to make the options parameter optional.
1676
+ *
1677
+ * @example
1678
+ * ```ts
1679
+ * export const adapterOas = defineAdapter<OasAdapter>((options) => {
1680
+ * const { validate = true, dateType = 'string' } = options
1681
+ * return {
1682
+ * name: adapterOasName,
1683
+ * options: { validate, dateType, ... },
1684
+ * parse(source) { ... },
1685
+ * }
1686
+ * })
1687
+ * ```
1688
+ */
1689
+ function defineAdapter(build) {
1690
+ return (options) => build(options ?? {});
1691
+ }
1566
1692
  //#endregion
1567
1693
  //#region src/defineLogger.ts
1568
1694
  function defineLogger(logger) {
@@ -2029,6 +2155,6 @@ async function detectLinter() {
2029
2155
  ]) if (await isLinterAvailable(linter)) return linter;
2030
2156
  }
2031
2157
  //#endregion
2032
- export { FunctionParams, PackageManager, PluginManager, PromiseManager, build, build as default, defineConfig, defineLogger, definePlugin, detectFormatter, detectLinter, formatters, getBarrelFiles, getConfigs, getMode, isInputPath, linters, logLevel, safeBuild, setup };
2158
+ export { AsyncEventEmitter, FunctionParams, PackageManager, PluginManager, PromiseManager, build, build as default, defineAdapter, defineConfig, defineLogger, definePlugin, definePrinter, detectFormatter, detectLinter, formatters, getBarrelFiles, getConfigs, getMode, isInputPath, linters, logLevel, safeBuild, setup };
2033
2159
 
2034
2160
  //# sourceMappingURL=index.js.map