@kubb/core 5.14.2 → 5.14.4

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.2";
22
+ var version = "5.14.4";
23
23
  //#endregion
24
24
  //#region ../../internals/utils/src/casing.ts
25
25
  /**
@@ -1554,6 +1554,75 @@ function inputToAdapterSource(config) {
1554
1554
  };
1555
1555
  }
1556
1556
  //#endregion
1557
+ //#region src/resolveOptions.ts
1558
+ const patternCache = /* @__PURE__ */ new Map();
1559
+ const optionsCache = /* @__PURE__ */ new WeakMap();
1560
+ function testPattern(value, pattern) {
1561
+ if (typeof pattern === "string") {
1562
+ let regex = patternCache.get(pattern);
1563
+ regex ??= new RegExp(pattern);
1564
+ patternCache.set(pattern, regex);
1565
+ return regex.test(value);
1566
+ }
1567
+ return value.match(pattern) !== null;
1568
+ }
1569
+ function matchesOperation(node, filter) {
1570
+ if (filter.type === "extension") {
1571
+ const { extensions } = node;
1572
+ return extensions !== void 0 && Object.hasOwn(extensions, filter.key) && (filter.value === void 0 || extensions[filter.key] === filter.value);
1573
+ }
1574
+ const { type, pattern } = filter;
1575
+ if (type === "tag") return node.tags.some((tag) => testPattern(tag, pattern));
1576
+ if (type === "operationId") return testPattern(node.operationId, pattern);
1577
+ if (type === "path") return node.path !== void 0 && testPattern(node.path, pattern);
1578
+ if (type === "method") return node.method !== void 0 && testPattern(node.method.toLowerCase(), pattern);
1579
+ if (type === "contentType") return node.requestBody?.content?.some((c) => testPattern(c.contentType, pattern)) ?? false;
1580
+ return false;
1581
+ }
1582
+ function matchesSchema(node, filter) {
1583
+ if (filter.type === "schemaName") return node.name ? testPattern(node.name, filter.pattern) : false;
1584
+ return null;
1585
+ }
1586
+ function computeOptions(node, { options, exclude = [], include, override = [] }) {
1587
+ if (node.kind === ast.nodeKinds.operation) {
1588
+ if (exclude.some((filter) => matchesOperation(node, filter))) return null;
1589
+ if (include && !include.some((filter) => matchesOperation(node, filter))) return null;
1590
+ return {
1591
+ ...options,
1592
+ ...override.find((filter) => matchesOperation(node, filter))?.options
1593
+ };
1594
+ }
1595
+ if (node.kind === ast.nodeKinds.schema) {
1596
+ if (exclude.some((filter) => matchesSchema(node, filter) === true)) return null;
1597
+ if (include) {
1598
+ const applicable = include.map((filter) => matchesSchema(node, filter)).filter((result) => result !== null);
1599
+ if (applicable.length > 0 && !applicable.includes(true)) return null;
1600
+ }
1601
+ return {
1602
+ ...options,
1603
+ ...override.find((filter) => matchesSchema(node, filter) === true)?.options
1604
+ };
1605
+ }
1606
+ return options;
1607
+ }
1608
+ /**
1609
+ * Returns `null` for filtered nodes. Caches merged options per options object and node.
1610
+ */
1611
+ function resolveOptions(node, context) {
1612
+ const { options } = context;
1613
+ if (typeof options !== "object" || options === null) return computeOptions(node, context);
1614
+ let byOptions = optionsCache.get(options);
1615
+ if (!byOptions) {
1616
+ byOptions = /* @__PURE__ */ new WeakMap();
1617
+ optionsCache.set(options, byOptions);
1618
+ }
1619
+ const cached = byOptions.get(node);
1620
+ if (cached) return cached.value;
1621
+ const result = computeOptions(node, context);
1622
+ byOptions.set(node, { value: result });
1623
+ return result;
1624
+ }
1625
+ //#endregion
1557
1626
  //#region src/Resolver.ts
1558
1627
  function isNamespace(value) {
1559
1628
  return typeof value === "object" && value !== null && !Array.isArray(value);
@@ -1624,12 +1693,12 @@ function toBaseName({ name, extname }) {
1624
1693
  * ```
1625
1694
  */
1626
1695
  var Resolver = class Resolver {
1627
- static #patternCache = /* @__PURE__ */ new Map();
1628
- static #optionsCache = /* @__PURE__ */ new WeakMap();
1629
1696
  pluginName;
1630
1697
  /** The built-in machinery, untouched by `name`/`file` overrides and built once since the driver reads it per node. */
1631
1698
  default;
1632
1699
  #options;
1700
+ #usedNames = /* @__PURE__ */ new Set();
1701
+ #resolvedNames = /* @__PURE__ */ new Map();
1633
1702
  #baseName;
1634
1703
  #filePath;
1635
1704
  constructor(options) {
@@ -1639,7 +1708,7 @@ var Resolver = class Resolver {
1639
1708
  this.#filePath = options.file?.path ? options.file.path.bind(this) : void 0;
1640
1709
  this.default = {
1641
1710
  name: camelCase,
1642
- options: this.#resolveOptions.bind(this),
1711
+ options: resolveOptions,
1643
1712
  path: this.#resolvePath.bind(this),
1644
1713
  file: this.#resolveFile.bind(this),
1645
1714
  banner: this.#resolveBanner.bind(this),
@@ -1654,6 +1723,41 @@ var Resolver = class Resolver {
1654
1723
  name(name) {
1655
1724
  return this.default.name(name);
1656
1725
  }
1726
+ /**
1727
+ * Reserves emitted identifiers before allocating other declarations. The driver reserves component
1728
+ * names. Reserve extra identifiers, such as enum type aliases, before calling `resolveName`.
1729
+ *
1730
+ * @example Reserve an enum's type and value names
1731
+ * ```ts
1732
+ * resolver.reserveNames(['petStatusEnum', 'PetStatusEnumKey'])
1733
+ * ```
1734
+ */
1735
+ reserveNames(names) {
1736
+ for (const name of names) this.#usedNames.add(name);
1737
+ }
1738
+ /**
1739
+ * Returns the same name for repeated declaration keys. Adds numeric suffixes starting at `2`
1740
+ * when the preferred name is taken. Array segment order and boundaries matter. String and array
1741
+ * keys are distinct. Pass an already-cased name or use `normalize` to compare emitted identifiers
1742
+ * while returning raw names. `Resolver.merge` creates a resolver with no reservations.
1743
+ *
1744
+ * @example Resolve a request body that collides with a component
1745
+ * ```ts
1746
+ * resolver.reserveNames(['CreateContainerBody'])
1747
+ * resolver.resolveName({ key: ['operation', 'POST', '/containers', 'body'], name: 'CreateContainerBody' })
1748
+ * // 'CreateContainerBody2'
1749
+ * ```
1750
+ */
1751
+ resolveName({ key, name, normalize = (name) => name }) {
1752
+ const cacheKey = JSON.stringify(key);
1753
+ const previous = this.#resolvedNames.get(cacheKey);
1754
+ if (previous !== void 0) return previous;
1755
+ let result = name;
1756
+ for (let suffix = 2; this.#usedNames.has(normalize(result)); suffix++) result = `${name}${suffix}`;
1757
+ this.#usedNames.add(normalize(result));
1758
+ this.#resolvedNames.set(cacheKey, result);
1759
+ return result;
1760
+ }
1657
1761
  file(options) {
1658
1762
  return this.#resolveFile(options);
1659
1763
  }
@@ -1724,76 +1828,6 @@ var Resolver = class Resolver {
1724
1828
  root[key] = isNamespace(value) ? Object.fromEntries(Object.entries(value).map(([method, member]) => [method, bind(member)])) : bind(value);
1725
1829
  }
1726
1830
  }
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
1831
  /**
1798
1832
  * A custom `group.name` wins; otherwise `tag` groups use the camelCased tag and `path`
1799
1833
  * groups use the first non-traversal segment (`''` when none remain, placing the file in
@@ -2823,6 +2857,7 @@ var KubbDriver = class {
2823
2857
  optionsAreStatic: !exclude?.length && !include?.length && !override?.length,
2824
2858
  allowedSchemaNames: allowedSchemaNamesByPlugin.get(plugin.name) ?? null,
2825
2859
  schemaGenerators: generators.filter((generator) => generator.schema),
2860
+ schemas: /* @__PURE__ */ new Map(),
2826
2861
  operationGenerators: generators.filter((generator) => generator.operation),
2827
2862
  operationsGenerators: generators.filter((generator) => generator.operations),
2828
2863
  pluginOperations: [],
@@ -2848,15 +2883,29 @@ var KubbDriver = class {
2848
2883
  options
2849
2884
  };
2850
2885
  };
2886
+ for (const state of states) {
2887
+ if (!state.schemaGenerators.length) continue;
2888
+ for (const node of schemas) try {
2889
+ const resolved = resolveForPlugin(state, node);
2890
+ if (!resolved) continue;
2891
+ const name = resolved.transformedNode.name;
2892
+ if (state.allowedSchemaNames !== null && name && !state.allowedSchemaNames.has(name)) continue;
2893
+ state.schemas.set(node, resolved);
2894
+ if (name) state.generatorContext.resolver.reserveNames([state.generatorContext.resolver.name(name)]);
2895
+ } catch (caughtError) {
2896
+ state.schemas.set(node, toError(caughtError));
2897
+ break;
2898
+ }
2899
+ }
2851
2900
  for (const node of schemas) {
2852
2901
  const cache = createNodeCache();
2853
2902
  for (const state of states) {
2854
2903
  if (state.error || !state.schemaGenerators.length) continue;
2855
2904
  try {
2856
- const resolved = resolveForPlugin(state, node);
2905
+ const resolved = state.schemas.get(node);
2906
+ if (resolved instanceof Error) throw resolved;
2857
2907
  if (!resolved) continue;
2858
2908
  const { transformedNode, options } = resolved;
2859
- if (state.allowedSchemaNames !== null && transformedNode.name && !state.allowedSchemaNames.has(transformedNode.name)) continue;
2860
2909
  const ctx = {
2861
2910
  ...state.generatorContext,
2862
2911
  options,
@@ -3189,4 +3238,4 @@ var Hookable = class {
3189
3238
  //#endregion
3190
3239
  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
3240
 
3192
- //# sourceMappingURL=Hookable-DjPMSKYr.js.map
3241
+ //# sourceMappingURL=Hookable-CPbYCCpI.js.map