@kubb/core 5.0.0-beta.94 → 5.0.0-beta.96

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
@@ -33,7 +33,6 @@ node_process = require_usingCtx.__toESM(node_process, 1);
33
33
  * // Convert the source (path or inline data) into an InputNode.
34
34
  * return ast.factory.createInput()
35
35
  * },
36
- * getImports: () => [],
37
36
  * async validate() {
38
37
  * // Throw here when the spec is invalid.
39
38
  * },
@@ -192,7 +191,7 @@ function memoize(store, factory) {
192
191
  }
193
192
  //#endregion
194
193
  //#region package.json
195
- var version = "5.0.0-beta.94";
194
+ var version = "5.0.0-beta.96";
196
195
  //#endregion
197
196
  //#region src/constants.ts
198
197
  /**
@@ -813,9 +812,9 @@ function toBaseName({ name, extname }) {
813
812
  /**
814
813
  * Base constraint for all plugin resolver objects.
815
814
  *
816
- * The built-in machinery lives under `default`. Generators call the top-level `name` and
817
- * `file`, and a plugin overrides them to set its conventions. Extend with top-level helpers
818
- * (`typeName`, …) and/or grouped namespaces (`query`, `schema`, …).
815
+ * The built-in machinery lives under `default`. Generators call the top-level `name`, `file`,
816
+ * and `imports`, and a plugin overrides `name` and `file` to set its conventions. Extend with
817
+ * top-level helpers (`typeName`, …) and/or grouped namespaces (`query`, `schema`, …).
819
818
  *
820
819
  * @example Top-level helper
821
820
  * ```ts
@@ -873,23 +872,61 @@ var Resolver = class Resolver {
873
872
  return this.#resolveFile(options);
874
873
  }
875
874
  /**
876
- * Merges `override` over `base` and returns a new resolver with helpers re-bound. Top-level
877
- * keys replace, and a namespace (or `file`) merges per method, so overriding `query.name`
878
- * keeps the base `query.keyName`. Used when applying `setResolver` partial overrides. Reads a
879
- * resolver's options through the shared brand rather than `instanceof`, so a `file` override
880
- * survives even when `base` and `override` come from different `@kubb/core` copies.
881
- */
882
- static merge(base, override) {
883
- const patch = resolverOptions in override ? override[resolverOptions] : override;
884
- const merged = { ...base[resolverOptions] };
885
- for (const [key, value] of Object.entries(patch)) {
886
- if (value === void 0) continue;
887
- const current = merged[key];
888
- merged[key] = isNamespace(value) && isNamespace(current) ? {
889
- ...current,
890
- ...value
891
- } : value;
892
- }
875
+ * Builds one `ImportNode` per unique schema referenced in the tree, in first-occurrence
876
+ * order. Each ref's target resolves through `resolveRefName`, so collision- or macro-renamed
877
+ * schemas (`targetName`) import the emitted name. Names and paths go through the top-level
878
+ * `name` and `file`, so import entries follow the plugin's conventions, and a per-call
879
+ * `name` override wins over both.
880
+ */
881
+ imports(options) {
882
+ const { node, root, output, group, extname = ".ts", name } = options;
883
+ const resolveName = name ?? ((schemaName) => this.name(schemaName));
884
+ const seen = /* @__PURE__ */ new Set();
885
+ return (0, _kubb_ast.collect)(node, { schema: (schemaNode) => {
886
+ const schemaRef = (0, _kubb_ast.narrowSchema)(schemaNode, "ref");
887
+ if (!schemaRef?.ref) return null;
888
+ const schemaName = (0, _kubb_ast.resolveRefName)(schemaRef);
889
+ if (!schemaName || seen.has(schemaName)) return null;
890
+ seen.add(schemaName);
891
+ return _kubb_ast.ast.factory.createImport({
892
+ name: [resolveName(schemaName)],
893
+ path: this.file({
894
+ name: schemaName,
895
+ extname,
896
+ root,
897
+ output,
898
+ group
899
+ }).path
900
+ });
901
+ } });
902
+ }
903
+ /**
904
+ * Folds each `override` over `base`, left to right, and returns a new resolver with helpers
905
+ * re-bound. Top-level keys replace, and a namespace (or `file`) merges per method, so overriding
906
+ * `query.name` keeps the base `query.keyName`. The last override wins per key. Used when applying
907
+ * `setResolver` partial overrides, and to compose shared resolver fragments without spreading each
908
+ * namespace by hand. Reads a resolver's options through the shared brand rather than `instanceof`,
909
+ * so a `file` override survives even when `base` and `override` come from different `@kubb/core`
910
+ * copies.
911
+ *
912
+ * @example Fold several partial overrides onto a resolver
913
+ * ```ts
914
+ * const resolver = Resolver.merge(defaultResolver, sharedNamingPatch, { name: (name) => name.toUpperCase() })
915
+ * ```
916
+ */
917
+ static merge(base, ...overrides) {
918
+ const merged = overrides.reduce((acc, override) => {
919
+ const patch = resolverOptions in override ? override[resolverOptions] : override;
920
+ for (const [key, value] of Object.entries(patch)) {
921
+ if (value === void 0) continue;
922
+ const current = acc[key];
923
+ acc[key] = isNamespace(value) && isNamespace(current) ? {
924
+ ...current,
925
+ ...value
926
+ } : value;
927
+ }
928
+ return acc;
929
+ }, { ...base[resolverOptions] });
893
930
  return new Resolver(merged);
894
931
  }
895
932
  /**