@kubb/core 5.14.3 → 5.14.5

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.
@@ -19,7 +19,7 @@ var __name = (target, value) => __defProp(target, "name", {
19
19
  });
20
20
  //#endregion
21
21
  //#region package.json
22
- var version = "5.14.3";
22
+ var version = "5.14.5";
23
23
  //#endregion
24
24
  //#region ../../internals/utils/src/casing.ts
25
25
  /**
@@ -1515,7 +1515,8 @@ function isLegacyInput(input) {
1515
1515
  /**
1516
1516
  * Normalizes `config.input` into an `AdapterSource` the adapter can parse.
1517
1517
  *
1518
- * A parsed object and inline content become `{ type: 'data' }`; a URL or git input is kept
1518
+ * A parsed object and inline content become `{ type: 'data' }` with `config.root` as the folder
1519
+ * their relative `$ref`s resolve from; a URL or git input is kept
1519
1520
  * verbatim and a local path is resolved against `config.root`, all as `{ type: 'path' }`. The
1520
1521
  * driver checks out a git input before the adapter parses it.
1521
1522
  */
@@ -1537,12 +1538,14 @@ function inputToAdapterSource(config) {
1537
1538
  });
1538
1539
  if (typeof input !== "string") return {
1539
1540
  type: "data",
1540
- data: input
1541
+ data: input,
1542
+ root: resolve(config.root)
1541
1543
  };
1542
1544
  const kind = getInputKind(input);
1543
1545
  if (kind === "inline") return {
1544
1546
  type: "data",
1545
- data: input
1547
+ data: input,
1548
+ root: resolve(config.root)
1546
1549
  };
1547
1550
  if (kind === "url" || kind === "git") return {
1548
1551
  type: "path",
@@ -1554,6 +1557,75 @@ function inputToAdapterSource(config) {
1554
1557
  };
1555
1558
  }
1556
1559
  //#endregion
1560
+ //#region src/resolveOptions.ts
1561
+ const patternCache = /* @__PURE__ */ new Map();
1562
+ const optionsCache = /* @__PURE__ */ new WeakMap();
1563
+ function testPattern(value, pattern) {
1564
+ if (typeof pattern === "string") {
1565
+ let regex = patternCache.get(pattern);
1566
+ regex ??= new RegExp(pattern);
1567
+ patternCache.set(pattern, regex);
1568
+ return regex.test(value);
1569
+ }
1570
+ return value.match(pattern) !== null;
1571
+ }
1572
+ function matchesOperation(node, filter) {
1573
+ if (filter.type === "extension") {
1574
+ const { extensions } = node;
1575
+ return extensions !== void 0 && Object.hasOwn(extensions, filter.key) && (filter.value === void 0 || extensions[filter.key] === filter.value);
1576
+ }
1577
+ const { type, pattern } = filter;
1578
+ if (type === "tag") return node.tags.some((tag) => testPattern(tag, pattern));
1579
+ if (type === "operationId") return testPattern(node.operationId, pattern);
1580
+ if (type === "path") return node.path !== void 0 && testPattern(node.path, pattern);
1581
+ if (type === "method") return node.method !== void 0 && testPattern(node.method.toLowerCase(), pattern);
1582
+ if (type === "contentType") return node.requestBody?.content?.some((c) => testPattern(c.contentType, pattern)) ?? false;
1583
+ return false;
1584
+ }
1585
+ function matchesSchema(node, filter) {
1586
+ if (filter.type === "schemaName") return node.name ? testPattern(node.name, filter.pattern) : false;
1587
+ return null;
1588
+ }
1589
+ function computeOptions(node, { options, exclude = [], include, override = [] }) {
1590
+ if (node.kind === ast.nodeKinds.operation) {
1591
+ if (exclude.some((filter) => matchesOperation(node, filter))) return null;
1592
+ if (include && !include.some((filter) => matchesOperation(node, filter))) return null;
1593
+ return {
1594
+ ...options,
1595
+ ...override.find((filter) => matchesOperation(node, filter))?.options
1596
+ };
1597
+ }
1598
+ if (node.kind === ast.nodeKinds.schema) {
1599
+ if (exclude.some((filter) => matchesSchema(node, filter) === true)) return null;
1600
+ if (include) {
1601
+ const applicable = include.map((filter) => matchesSchema(node, filter)).filter((result) => result !== null);
1602
+ if (applicable.length > 0 && !applicable.includes(true)) return null;
1603
+ }
1604
+ return {
1605
+ ...options,
1606
+ ...override.find((filter) => matchesSchema(node, filter) === true)?.options
1607
+ };
1608
+ }
1609
+ return options;
1610
+ }
1611
+ /**
1612
+ * Returns `null` for filtered nodes. Caches merged options per options object and node.
1613
+ */
1614
+ function resolveOptions(node, context) {
1615
+ const { options } = context;
1616
+ if (typeof options !== "object" || options === null) return computeOptions(node, context);
1617
+ let byOptions = optionsCache.get(options);
1618
+ if (!byOptions) {
1619
+ byOptions = /* @__PURE__ */ new WeakMap();
1620
+ optionsCache.set(options, byOptions);
1621
+ }
1622
+ const cached = byOptions.get(node);
1623
+ if (cached) return cached.value;
1624
+ const result = computeOptions(node, context);
1625
+ byOptions.set(node, { value: result });
1626
+ return result;
1627
+ }
1628
+ //#endregion
1557
1629
  //#region src/Resolver.ts
1558
1630
  function isNamespace(value) {
1559
1631
  return typeof value === "object" && value !== null && !Array.isArray(value);
@@ -1624,12 +1696,12 @@ function toBaseName({ name, extname }) {
1624
1696
  * ```
1625
1697
  */
1626
1698
  var Resolver = class Resolver {
1627
- static #patternCache = /* @__PURE__ */ new Map();
1628
- static #optionsCache = /* @__PURE__ */ new WeakMap();
1629
1699
  pluginName;
1630
1700
  /** The built-in machinery, untouched by `name`/`file` overrides and built once since the driver reads it per node. */
1631
1701
  default;
1632
1702
  #options;
1703
+ #usedNames = /* @__PURE__ */ new Set();
1704
+ #resolvedNames = /* @__PURE__ */ new Map();
1633
1705
  #baseName;
1634
1706
  #filePath;
1635
1707
  constructor(options) {
@@ -1639,7 +1711,7 @@ var Resolver = class Resolver {
1639
1711
  this.#filePath = options.file?.path ? options.file.path.bind(this) : void 0;
1640
1712
  this.default = {
1641
1713
  name: camelCase,
1642
- options: this.#resolveOptions.bind(this),
1714
+ options: resolveOptions,
1643
1715
  path: this.#resolvePath.bind(this),
1644
1716
  file: this.#resolveFile.bind(this),
1645
1717
  banner: this.#resolveBanner.bind(this),
@@ -1654,6 +1726,50 @@ var Resolver = class Resolver {
1654
1726
  name(name) {
1655
1727
  return this.default.name(name);
1656
1728
  }
1729
+ /**
1730
+ * Reserves emitted identifiers before allocating other declarations. The driver reserves component
1731
+ * names. Reserve extra identifiers, such as enum type aliases, before calling `resolveName`.
1732
+ *
1733
+ * @example Reserve an enum's type and value names
1734
+ * ```ts
1735
+ * resolver.reserveNames(['petStatusEnum', 'PetStatusEnumKey'])
1736
+ * ```
1737
+ */
1738
+ reserveNames(names) {
1739
+ for (const name of names) this.#usedNames.add(name);
1740
+ }
1741
+ /**
1742
+ * Returns the same name for repeated declaration keys. Adds numeric suffixes starting at `2`
1743
+ * when the preferred name is taken. Array segment order and boundaries matter. String and array
1744
+ * keys are distinct. Pass an already-cased name or use `normalize` to compare emitted identifiers
1745
+ * while returning raw names. `Resolver.merge` creates a resolver with no reservations.
1746
+ *
1747
+ * @example Resolve a request body that collides with a component
1748
+ * ```ts
1749
+ * resolver.reserveNames(['CreateContainerBody'])
1750
+ * resolver.resolveName({ key: ['operation', 'POST', '/containers', 'body'], name: 'CreateContainerBody' })
1751
+ * // 'CreateContainerBody2'
1752
+ * ```
1753
+ *
1754
+ * @example Resolve a name that has to be free as both an enum value and a type alias
1755
+ * ```ts
1756
+ * resolver.resolveName({ key: ['enum', 'Pet', 'status'], name: 'Status', normalize: (name) => [camelCase(name), `${name}Key`] })
1757
+ * ```
1758
+ */
1759
+ resolveName({ key, name, normalize = (name) => name }) {
1760
+ const cacheKey = JSON.stringify(key);
1761
+ const previous = this.#resolvedNames.get(cacheKey);
1762
+ if (previous !== void 0) return previous;
1763
+ const emitted = (value) => {
1764
+ const names = normalize(value);
1765
+ return typeof names === "string" ? [names] : names;
1766
+ };
1767
+ let result = name;
1768
+ for (let suffix = 2; emitted(result).some((emittedName) => this.#usedNames.has(emittedName)); suffix++) result = `${name}${suffix}`;
1769
+ this.reserveNames(emitted(result));
1770
+ this.#resolvedNames.set(cacheKey, result);
1771
+ return result;
1772
+ }
1657
1773
  file(options) {
1658
1774
  return this.#resolveFile(options);
1659
1775
  }
@@ -1724,76 +1840,6 @@ var Resolver = class Resolver {
1724
1840
  root[key] = isNamespace(value) ? Object.fromEntries(Object.entries(value).map(([method, member]) => [method, bind(member)])) : bind(value);
1725
1841
  }
1726
1842
  }
1727
- static #testPattern(value, pattern) {
1728
- if (typeof pattern === "string") {
1729
- let regex = Resolver.#patternCache.get(pattern);
1730
- regex ??= new RegExp(pattern);
1731
- Resolver.#patternCache.set(pattern, regex);
1732
- return regex.test(value);
1733
- }
1734
- return value.match(pattern) !== null;
1735
- }
1736
- static #matchesOperation(node, filter) {
1737
- if (filter.type === "extension") {
1738
- const { extensions } = node;
1739
- return extensions !== void 0 && Object.hasOwn(extensions, filter.key) && (filter.value === void 0 || extensions[filter.key] === filter.value);
1740
- }
1741
- const { type, pattern } = filter;
1742
- if (type === "tag") return node.tags.some((tag) => Resolver.#testPattern(tag, pattern));
1743
- if (type === "operationId") return Resolver.#testPattern(node.operationId, pattern);
1744
- if (type === "path") return node.path !== void 0 && Resolver.#testPattern(node.path, pattern);
1745
- if (type === "method") return node.method !== void 0 && Resolver.#testPattern(node.method.toLowerCase(), pattern);
1746
- if (type === "contentType") return node.requestBody?.content?.some((c) => Resolver.#testPattern(c.contentType, pattern)) ?? false;
1747
- return false;
1748
- }
1749
- /**
1750
- * Returns `null` when the filter type doesn't apply to schemas, so include rules built
1751
- * from operation filters (e.g. `tag`) don't exclude every schema.
1752
- */
1753
- static #matchesSchema(node, filter) {
1754
- if (filter.type === "schemaName") return node.name ? Resolver.#testPattern(node.name, filter.pattern) : false;
1755
- return null;
1756
- }
1757
- static #computeOptions(node, { options, exclude = [], include, override = [] }) {
1758
- if (node.kind === ast.nodeKinds.operation) {
1759
- if (exclude.some((filter) => Resolver.#matchesOperation(node, filter))) return null;
1760
- if (include && !include.some((filter) => Resolver.#matchesOperation(node, filter))) return null;
1761
- return {
1762
- ...options,
1763
- ...override.find((filter) => Resolver.#matchesOperation(node, filter))?.options
1764
- };
1765
- }
1766
- if (node.kind === ast.nodeKinds.schema) {
1767
- if (exclude.some((filter) => Resolver.#matchesSchema(node, filter) === true)) return null;
1768
- if (include) {
1769
- const applicable = include.map((filter) => Resolver.#matchesSchema(node, filter)).filter((result) => result !== null);
1770
- if (applicable.length > 0 && !applicable.includes(true)) return null;
1771
- }
1772
- return {
1773
- ...options,
1774
- ...override.find((filter) => Resolver.#matchesSchema(node, filter) === true)?.options
1775
- };
1776
- }
1777
- return options;
1778
- }
1779
- /**
1780
- * Applies include/exclude filters and merges matching override options, caching the result
1781
- * per `(options, node)` pair. Returns `null` when the node is filtered out.
1782
- */
1783
- #resolveOptions(node, context) {
1784
- const { options } = context;
1785
- if (typeof options !== "object" || options === null) return Resolver.#computeOptions(node, context);
1786
- let byOptions = Resolver.#optionsCache.get(options);
1787
- if (!byOptions) {
1788
- byOptions = /* @__PURE__ */ new WeakMap();
1789
- Resolver.#optionsCache.set(options, byOptions);
1790
- }
1791
- const cached = byOptions.get(node);
1792
- if (cached) return cached.value;
1793
- const result = Resolver.#computeOptions(node, context);
1794
- byOptions.set(node, { value: result });
1795
- return result;
1796
- }
1797
1843
  /**
1798
1844
  * A custom `group.name` wins; otherwise `tag` groups use the camelCased tag and `path`
1799
1845
  * groups use the first non-traversal segment (`''` when none remain, placing the file in
@@ -2823,6 +2869,7 @@ var KubbDriver = class {
2823
2869
  optionsAreStatic: !exclude?.length && !include?.length && !override?.length,
2824
2870
  allowedSchemaNames: allowedSchemaNamesByPlugin.get(plugin.name) ?? null,
2825
2871
  schemaGenerators: generators.filter((generator) => generator.schema),
2872
+ schemas: /* @__PURE__ */ new Map(),
2826
2873
  operationGenerators: generators.filter((generator) => generator.operation),
2827
2874
  operationsGenerators: generators.filter((generator) => generator.operations),
2828
2875
  pluginOperations: [],
@@ -2848,15 +2895,29 @@ var KubbDriver = class {
2848
2895
  options
2849
2896
  };
2850
2897
  };
2898
+ for (const state of states) {
2899
+ if (!state.schemaGenerators.length) continue;
2900
+ for (const node of schemas) try {
2901
+ const resolved = resolveForPlugin(state, node);
2902
+ if (!resolved) continue;
2903
+ const name = resolved.transformedNode.name;
2904
+ if (state.allowedSchemaNames !== null && name && !state.allowedSchemaNames.has(name)) continue;
2905
+ state.schemas.set(node, resolved);
2906
+ if (name) state.generatorContext.resolver.reserveNames([state.generatorContext.resolver.name(name)]);
2907
+ } catch (caughtError) {
2908
+ state.schemas.set(node, toError(caughtError));
2909
+ break;
2910
+ }
2911
+ }
2851
2912
  for (const node of schemas) {
2852
2913
  const cache = createNodeCache();
2853
2914
  for (const state of states) {
2854
2915
  if (state.error || !state.schemaGenerators.length) continue;
2855
2916
  try {
2856
- const resolved = resolveForPlugin(state, node);
2917
+ const resolved = state.schemas.get(node);
2918
+ if (resolved instanceof Error) throw resolved;
2857
2919
  if (!resolved) continue;
2858
2920
  const { transformedNode, options } = resolved;
2859
- if (state.allowedSchemaNames !== null && transformedNode.name && !state.allowedSchemaNames.has(transformedNode.name)) continue;
2860
2921
  const ctx = {
2861
2922
  ...state.generatorContext,
2862
2923
  options,
@@ -3189,4 +3250,4 @@ var Hookable = class {
3189
3250
  //#endregion
3190
3251
  export { formatMs as A, writeRaw as B, useConfig as C, defineExtension as D, checkCompatibility as E, isPathInside as F, camelCase as H, listFiles as I, read as L, inParallel as M, exists as N, resolveMeta as O, existsSync$1 as P, rm$1 as R, tryUseKubb as S, useOptions as T, __name as U, toError as V, kubbContext as _, FileManager as a, setOptions as b, Resolver as c, isPublicAddress as d, assertPluginOption as f, injectFile as g, addMacro as h, createNodeCache as i, getElapsedMs as j, Diagnostics as k, getInputKind as l, addGenerator as m, KubbDriver as n, _usingCtx as o, definePlugin as p, pluginNotFoundError as r, createResolver as s, Hookable as t, checkoutGitInput as u, requirePlugin as v, useKubb as w, setResolver as x, setMacros as y, write as z };
3191
3252
 
3192
- //# sourceMappingURL=Hookable-a3f_W95w.js.map
3253
+ //# sourceMappingURL=Hookable-BLHdV93L.js.map