@kubb/core 5.18.5 → 5.18.7

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 CHANGED
@@ -45,7 +45,7 @@ let node_readline = require("node:readline");
45
45
  let node_url = require("node:url");
46
46
  let node_module = require("node:module");
47
47
  // package.json
48
- var version = "5.18.5";
48
+ var version = "5.18.7";
49
49
  // ../../internals/utils/src/casing.ts
50
50
  /**
51
51
  * Shared implementation for camelCase and PascalCase conversion.
@@ -573,7 +573,7 @@ const diagnosticCatalog = {
573
573
  code: "KUBB_INVALID_DOCUMENT",
574
574
  title: "Invalid document",
575
575
  cause: "The `input` cannot be read as an OpenAPI or Swagger document: its JSON or YAML has a syntax error, it declares no `openapi` or `swagger` version field, or its version cannot be upgraded to OpenAPI 3.1.",
576
- fix: "Fix the syntax at the reported line, and check that `input` points at the spec and not an HTML page. Point `input` at a Swagger 2.0, OpenAPI 3.0, or OpenAPI 3.1 document, and check that a passed object is the spec itself rather than a wrapper around it."
576
+ fix: "Fix the syntax at the reported line, and check that `input` points at the spec and not an HTML page. Point `input` at a Swagger 2.0 or OpenAPI 3.0, 3.1, or 3.2 document, and check that a passed object is the spec itself rather than a wrapper around it."
577
577
  },
578
578
  invalidSpec: {
579
579
  code: "KUBB_INVALID_SPEC",
@@ -698,8 +698,8 @@ const diagnosticCatalog = {
698
698
  invalidPluginOptions: {
699
699
  code: "KUBB_INVALID_PLUGIN_OPTIONS",
700
700
  title: "Invalid plugin options",
701
- cause: "A plugin was configured with options that cannot be honored, for example `output.mode: 'file'` paired with a `group` option.",
702
- fix: "Fix the plugin options. A single-file output has nothing to group, so remove the `group` option or use `output.mode: 'directory'`."
701
+ cause: "A plugin was configured with options that cannot be honored, for example a single-file output (`output.mode: 'file'`, or an `output.path` with a file extension) paired with a `group` option.",
702
+ fix: "Fix the plugin options. A single-file output has nothing to group, so remove the `group` option, give `output.path` a directory name, or set `output.mode: 'directory'`."
703
703
  },
704
704
  postGenerateFailed: {
705
705
  code: "KUBB_POST_GENERATE_FAILED",
@@ -1742,11 +1742,16 @@ function inputToAdapterSource(config) {
1742
1742
  }
1743
1743
  // src/resolveOptions.ts
1744
1744
  const patterns = /* @__PURE__ */ new Map();
1745
+ const methodPatterns = /* @__PURE__ */ new Map();
1745
1746
  const nodeCaches = /* @__PURE__ */ new WeakMap();
1746
1747
  function testPattern(value, pattern) {
1747
1748
  if (typeof pattern === "string") return memo$1(patterns, pattern, () => new RegExp(pattern)).test(value);
1748
1749
  return value.match(pattern) !== null;
1749
1750
  }
1751
+ function testMethod(method, pattern) {
1752
+ if (typeof pattern === "string") return memo$1(methodPatterns, pattern, () => new RegExp(pattern, "i")).test(method);
1753
+ return method.match(pattern.ignoreCase ? pattern : new RegExp(pattern.source, `${pattern.flags}i`)) !== null;
1754
+ }
1750
1755
  function matchesOperation(node, filter) {
1751
1756
  if (filter.type === "extension") {
1752
1757
  const { extensions } = node;
@@ -1756,7 +1761,7 @@ function matchesOperation(node, filter) {
1756
1761
  if (type === "tag") return node.tags.some((tag) => testPattern(tag, pattern));
1757
1762
  if (type === "operationId") return testPattern(node.operationId, pattern);
1758
1763
  if (type === "path") return node.path !== void 0 && testPattern(node.path, pattern);
1759
- if (type === "method") return node.method !== void 0 && testPattern(node.method.toLowerCase(), pattern);
1764
+ if (type === "method") return node.method !== void 0 && testMethod(node.method, pattern);
1760
1765
  if (type === "contentType") return node.requestBody?.content?.some((c) => testPattern(c.contentType, pattern)) ?? false;
1761
1766
  return false;
1762
1767
  }
@@ -1765,7 +1770,7 @@ function matchesSchema(node, filter) {
1765
1770
  return null;
1766
1771
  }
1767
1772
  function computeOptions(node, { options, exclude = [], include, override = [] }) {
1768
- if (node.kind === _kubb_ast.ast.nodeKinds.operation) {
1773
+ if (node.kind === _kubb_ast.ast.node.kinds.operation) {
1769
1774
  if (exclude.some((filter) => matchesOperation(node, filter))) return null;
1770
1775
  if (include && !include.some((filter) => matchesOperation(node, filter))) return null;
1771
1776
  return {
@@ -1773,7 +1778,7 @@ function computeOptions(node, { options, exclude = [], include, override = [] })
1773
1778
  ...override.find((filter) => matchesOperation(node, filter))?.options
1774
1779
  };
1775
1780
  }
1776
- if (node.kind === _kubb_ast.ast.nodeKinds.schema) {
1781
+ if (node.kind === _kubb_ast.ast.node.kinds.schema) {
1777
1782
  if (exclude.some((filter) => matchesSchema(node, filter) === true)) return null;
1778
1783
  if (include) {
1779
1784
  const applicable = include.map((filter) => matchesSchema(node, filter)).filter((result) => result !== null);
@@ -1952,19 +1957,19 @@ var Resolver = class Resolver {
1952
1957
  }
1953
1958
  /**
1954
1959
  * Builds one `ImportNode` per unique schema referenced in the tree, in first-occurrence
1955
- * order. Each ref's target resolves through `resolveRefName`, so collision- or macro-renamed
1960
+ * order. Each ref's target resolves through `ast.ref.getName`, so collision- or macro-renamed
1956
1961
  * schemas (`targetName`) import the emitted name. Names and paths go through the top-level
1957
1962
  * `name` and `file`, so import entries follow the plugin's conventions, and a per-call
1958
1963
  * `name` override wins over both.
1959
1964
  *
1960
- * The subtree scan runs through `collectImportedRefNames`, which memoizes by node identity, so a
1965
+ * The subtree scan runs through `ast.graph.getImportedNames`, which memoizes by node identity, so a
1961
1966
  * schema shared across the ts, zod, and faker plugins is walked once and every plugin's resolver
1962
1967
  * reads the same ref set instead of re-scanning it per plugin.
1963
1968
  */
1964
1969
  imports(options) {
1965
1970
  const { node, root, output, group, extname = ".ts", name, checks } = options;
1966
1971
  const resolveName = name ?? ((schemaName) => this.name(schemaName));
1967
- return (0, _kubb_ast.collectImportedRefNames)(node, { checks }).map((schemaName) => _kubb_ast.ast.factory.createImport({
1972
+ return _kubb_ast.ast.graph.getImportedNames(node, { checks }).map((schemaName) => _kubb_ast.ast.factory.createImport({
1968
1973
  name: [resolveName(schemaName)],
1969
1974
  path: this.#resolveFilePath({
1970
1975
  name: schemaName,
@@ -2018,11 +2023,17 @@ var Resolver = class Resolver {
2018
2023
  }
2019
2024
  }
2020
2025
  /**
2021
- * A custom `group.name` wins; otherwise `tag` groups use the camelCased tag, nested in the
2022
- * folders of its `parent` tags, and `path` groups use the first non-traversal segment (`''`
2023
- * when none remain, placing the file in the output root, kept safe by the caller's boundary check).
2026
+ * The folder a group puts a file in, relative to the output directory. A custom `group.name` wins; otherwise `tag`
2027
+ * groups use the camelCased tag, nested in the folders of its `parent` tags, and `path` groups use the first
2028
+ * non-traversal segment (`''` when none remain, placing the file in the output root, kept safe by the caller's
2029
+ * boundary check).
2030
+ *
2031
+ * @example
2032
+ * ```ts
2033
+ * resolver.groupDir({ type: 'tag' }, 'pet store') // 'petStore'
2034
+ * ```
2024
2035
  */
2025
- #resolveGroupDir(group, groupValue) {
2036
+ groupDir(group, groupValue) {
2026
2037
  if (group.name) return group.name({ group: groupValue });
2027
2038
  if (group.type === "tag") {
2028
2039
  const chain = [groupValue];
@@ -2041,7 +2052,7 @@ var Resolver = class Resolver {
2041
2052
  if (output.mode !== "directory") return node_path.default.resolve(root, output.path);
2042
2053
  const outputDir = node_path.default.resolve(root, output.path);
2043
2054
  const groupValue = group?.type === "path" ? groupPath : tag;
2044
- const result = group && groupValue ? node_path.default.resolve(outputDir, this.#resolveGroupDir(group, groupValue), baseName) : node_path.default.resolve(outputDir, baseName);
2055
+ const result = group && groupValue ? node_path.default.resolve(outputDir, this.groupDir(group, groupValue), baseName) : node_path.default.resolve(outputDir, baseName);
2045
2056
  assertInside({
2046
2057
  target: result,
2047
2058
  parent: outputDir,
@@ -2053,15 +2064,15 @@ var Resolver = class Resolver {
2053
2064
  /**
2054
2065
  * Resolves a resolver-supplied full path (`file.path`) against `root`, bypassing `output.path`
2055
2066
  * and `group`. The path may not escape `root`, which keeps a `file.path` that interpolates
2056
- * spec-derived values from writing outside the project.
2067
+ * spec-derived values from writing outside the output directory.
2057
2068
  */
2058
2069
  #resolveOverridePath(filePath, root) {
2059
2070
  const resolved = node_path.default.resolve(root, filePath);
2060
2071
  assertInside({
2061
2072
  target: resolved,
2062
2073
  parent: root,
2063
- message: `Resolved path "${resolved}" is outside the project root "${root}".`,
2064
- help: "A resolver `file.path` must return a path inside the project root."
2074
+ message: `Resolved path "${resolved}" is outside the output directory "${root}".`,
2075
+ help: "A resolver `file.path` must return a path inside the output directory (`config.output.path`)."
2065
2076
  });
2066
2077
  return resolved;
2067
2078
  }
@@ -2204,7 +2215,7 @@ var Resolver = class Resolver {
2204
2215
  function createResolver(options) {
2205
2216
  return new Resolver(options);
2206
2217
  }
2207
- // \0@oxc-project+runtime@0.155.0/helpers/esm/usingCtx.js
2218
+ // \0@oxc-project+runtime@0.157.0/helpers/esm/usingCtx.js
2208
2219
  function _usingCtx() {
2209
2220
  var r = "function" == typeof SuppressedError ? SuppressedError : function(r, e) {
2210
2221
  var n = Error();
@@ -2264,7 +2275,7 @@ function _usingCtx() {
2264
2275
  };
2265
2276
  }
2266
2277
  function joinSources(file) {
2267
- return file.sources.map((source) => (0, _kubb_ast.extractStringsFromNodes)(source.nodes)).filter(Boolean).join("\n\n");
2278
+ return file.sources.map((source) => _kubb_ast.ast.node.getText(source.nodes)).filter(Boolean).join("\n\n");
2268
2279
  }
2269
2280
  /** Runs a parser call and turns a crash into a `KUBB_PARSER_FAILED` diagnostic that names the parser. */
2270
2281
  function runParser(parser, print) {
@@ -2574,6 +2585,22 @@ function normalizePluginOptions(rawOptions, pluginName) {
2574
2585
  group,
2575
2586
  pluginName
2576
2587
  });
2588
+ options.override = options.override?.map((entry) => {
2589
+ const { output } = entry.options;
2590
+ if (!output) return entry;
2591
+ const entryGroup = "group" in entry.options ? entry.options.group : group;
2592
+ return {
2593
+ ...entry,
2594
+ options: {
2595
+ ...entry.options,
2596
+ output: normalizeOutput({
2597
+ output,
2598
+ group: entryGroup,
2599
+ pluginName
2600
+ })
2601
+ }
2602
+ };
2603
+ });
2577
2604
  return options;
2578
2605
  }
2579
2606
  /**
@@ -2596,7 +2623,7 @@ function assertPluginEntry(entry, index) {
2596
2623
  * pointer into the input.
2597
2624
  */
2598
2625
  function getCrashSite(node) {
2599
- if (node.kind === _kubb_ast.ast.nodeKinds.schema) return {
2626
+ if (node.kind === _kubb_ast.ast.node.kinds.schema) return {
2600
2627
  label: node.name ? `schema "${node.name}"` : "an inline schema",
2601
2628
  location: node.pointer ? {
2602
2629
  kind: "schema",
@@ -2696,7 +2723,7 @@ var KubbDriver = class {
2696
2723
  * config are ignored here and surface later through `requirePlugin`.
2697
2724
  */
2698
2725
  #sortPlugins(plugins) {
2699
- const queue = [...plugins].sort((a, b) => (0, _kubb_ast.compareEnforce)(a.enforce, b.enforce));
2726
+ const queue = [...plugins].sort((a, b) => _kubb_ast.ast.macro.compareOrder(a.enforce, b.enforce));
2700
2727
  const names = new Set(queue.map((plugin) => plugin.name));
2701
2728
  const blockedBy = new Map(queue.map((plugin) => {
2702
2729
  const dependencies = Array.isArray(plugin.dependencies) ? plugin.dependencies : Object.keys(plugin.dependencies ?? {});
@@ -2904,7 +2931,7 @@ var KubbDriver = class {
2904
2931
  * (`kubb:plugins:end`) and the write see them.
2905
2932
  */
2906
2933
  #inlineFiles(parsers) {
2907
- const { files, virtualPaths, problems } = _kubb_ast.ast.inlineFiles(this.fileManager.files, { language: (file) => parsers.get(file.extname)?.inline });
2934
+ const { files, virtualPaths, problems } = _kubb_ast.ast.file.inline(this.fileManager.files, { language: (file) => parsers.get(file.extname)?.inline });
2908
2935
  if (virtualPaths.length === 0) return;
2909
2936
  this.fileManager.add(...files);
2910
2937
  this.fileManager.delete(...virtualPaths);
@@ -2991,7 +3018,7 @@ var KubbDriver = class {
2991
3018
  include,
2992
3019
  override
2993
3020
  }) !== null);
2994
- allowedSchemaNamesByPlugin.set(plugin.name, (0, _kubb_ast.collectUsedSchemaNames)(includedOps, schemas));
3021
+ allowedSchemaNamesByPlugin.set(plugin.name, _kubb_ast.ast.graph.getUsedNames(includedOps, schemas));
2995
3022
  }
2996
3023
  const states = entries.map(({ plugin, context, hrStart }) => {
2997
3024
  const generatorContext = {
@@ -3019,7 +3046,7 @@ var KubbDriver = class {
3019
3046
  };
3020
3047
  });
3021
3048
  const resolveForPlugin = (state, node) => {
3022
- const transformedNode = _kubb_ast.ast.applyMacros(node, state.plugin.macros ?? []);
3049
+ const transformedNode = _kubb_ast.ast.macro.apply(node, state.plugin.macros ?? []);
3023
3050
  if (state.optionsAreStatic) return {
3024
3051
  transformedNode,
3025
3052
  options: state.plugin.options
@@ -3804,7 +3831,7 @@ function hasOutputPasses({ output, storage }) {
3804
3831
  * const { files, diagnostics } = await kubb.safeBuild()
3805
3832
  * ```
3806
3833
  */
3807
- var Kubb = class {
3834
+ var Kubb = class Kubb {
3808
3835
  hooks;
3809
3836
  config;
3810
3837
  #driver = null;
@@ -3815,6 +3842,34 @@ var Kubb = class {
3815
3842
  this.hooks = options.hooks ?? new Hookable();
3816
3843
  this.#signal = options.signal;
3817
3844
  }
3845
+ /**
3846
+ * Generates every config of a `defineConfig([...])` array in order and yields each result as its config finishes, so
3847
+ * memory holds one config at a time. A config that fails does not stop the others. Only an aborted `signal` rejects.
3848
+ */
3849
+ static async *generate(configs, { hooks = new Hookable(), signal } = {}) {
3850
+ for (const config of configs) {
3851
+ const kubb = new Kubb(config, {
3852
+ hooks,
3853
+ signal
3854
+ });
3855
+ try {
3856
+ yield {
3857
+ ...await kubb.generate(),
3858
+ kubb
3859
+ };
3860
+ } catch (error) {
3861
+ if (signal?.aborted) throw error;
3862
+ yield {
3863
+ success: false,
3864
+ files: [],
3865
+ diagnostics: [Diagnostics.from(error)],
3866
+ input: null,
3867
+ kubb,
3868
+ error
3869
+ };
3870
+ }
3871
+ }
3872
+ }
3818
3873
  get storage() {
3819
3874
  return this.config.storage;
3820
3875
  }
@@ -3885,7 +3940,8 @@ var Kubb = class {
3885
3940
  diagnostics,
3886
3941
  files: driver.fileManager.files,
3887
3942
  driver,
3888
- storage
3943
+ storage,
3944
+ input: driver.inputNode
3889
3945
  };
3890
3946
  } catch (_) {
3891
3947
  _usingCtx$1.e = _;
@@ -3921,15 +3977,16 @@ var Kubb = class {
3921
3977
  if (signal?.aborted) throw caughtError;
3922
3978
  output = {
3923
3979
  files: [],
3924
- diagnostics: [Diagnostics.from(caughtError)]
3980
+ diagnostics: [Diagnostics.from(caughtError)],
3981
+ input: null
3925
3982
  };
3926
3983
  }
3927
- const { files, diagnostics } = output;
3984
+ const { files, diagnostics, input } = output;
3928
3985
  const storage = this.storage;
3929
3986
  signal?.throwIfAborted();
3930
3987
  for (const diagnostic of diagnostics) if (Diagnostics.isProblem(diagnostic)) await Diagnostics.emit(hooks, diagnostic);
3931
3988
  if (Diagnostics.hasError(diagnostics)) {
3932
- await hooks.callHook("kubb:generation:end", {
3989
+ await this.#end({
3933
3990
  config,
3934
3991
  storage,
3935
3992
  diagnostics,
@@ -3940,7 +3997,8 @@ var Kubb = class {
3940
3997
  return {
3941
3998
  success: false,
3942
3999
  files,
3943
- diagnostics
4000
+ diagnostics,
4001
+ input
3944
4002
  };
3945
4003
  }
3946
4004
  const processOutput = options.processOutput ?? runOutputPasses;
@@ -3953,7 +4011,7 @@ var Kubb = class {
3953
4011
  const finalDiagnostics = [...diagnostics, ...outputDiagnostics];
3954
4012
  const failed = Diagnostics.hasError(outputDiagnostics);
3955
4013
  if (!failed) await this.#manifest?.commit();
3956
- await hooks.callHook("kubb:generation:end", {
4014
+ await this.#end({
3957
4015
  config,
3958
4016
  storage,
3959
4017
  diagnostics: finalDiagnostics,
@@ -3964,11 +4022,23 @@ var Kubb = class {
3964
4022
  return {
3965
4023
  success: !failed,
3966
4024
  files,
3967
- diagnostics: finalDiagnostics
4025
+ diagnostics: finalDiagnostics,
4026
+ input
3968
4027
  };
3969
4028
  }
4029
+ /** Ends a run: `kubb:generation:end`, then the totals on `kubb:generation:summary`. */
4030
+ async #end(ctx) {
4031
+ await this.hooks.callHook("kubb:generation:end", ctx);
4032
+ await this.hooks.callHook("kubb:generation:summary", {
4033
+ duration: Math.round(getElapsedMs(ctx.hrStart)),
4034
+ fileCount: ctx.filesCreated,
4035
+ failedPlugins: Diagnostics.failedPlugins(ctx.diagnostics).length,
4036
+ status: ctx.status
4037
+ });
4038
+ }
3970
4039
  dispose() {
3971
4040
  this.#driver?.dispose();
4041
+ this.#driver = null;
3972
4042
  }
3973
4043
  [Symbol.dispose]() {
3974
4044
  this.dispose();
@@ -4577,6 +4647,7 @@ async function readInstalledPackages({ root, packages = [], resolveFrom = [] })
4577
4647
  }
4578
4648
  exports.Diagnostics = Diagnostics;
4579
4649
  exports.Hookable = Hookable;
4650
+ exports.Kubb = Kubb;
4580
4651
  exports.KubbDriver = KubbDriver;
4581
4652
  exports.Resolver = Resolver;
4582
4653
  exports.addGenerator = addGenerator;