@kubb/core 5.0.0-alpha.35 → 5.0.0-alpha.38

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.js CHANGED
@@ -1,16 +1,11 @@
1
- import { t as __name } from "./chunk--u3MIqq1.js";
1
+ import "./chunk--u3MIqq1.js";
2
+ import { a as definePlugin, c as DEFAULT_BANNER, d as logLevel, f as isValidVarName, i as defineResolver, l as DEFAULT_EXTENSION, n as applyHookResult, o as pLimit, p as camelCase, r as FileManager, s as BARREL_FILENAME, t as PluginDriver, u as DEFAULT_STUDIO_URL } from "./PluginDriver-CgXFtmNP.js";
2
3
  import { EventEmitter } from "node:events";
3
- import { existsSync, readFileSync } from "node:fs";
4
4
  import { access, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
5
- import path, { basename, dirname, extname, join, posix, resolve } from "node:path";
5
+ import path, { dirname, join, posix, resolve } from "node:path";
6
6
  import * as ast from "@kubb/ast";
7
- import { composeTransformers, createExport, createFile, createSource, definePrinter, extractStringsFromNodes, isOperationNode, isSchemaNode, transform, walk } from "@kubb/ast";
8
- import { performance } from "node:perf_hooks";
9
- import { deflateSync } from "fflate";
10
- import { x } from "tinyexec";
7
+ import { createExport, createFile, createSource, extractStringsFromNodes, transform, walk } from "@kubb/ast";
11
8
  import { version } from "node:process";
12
- import { sortBy } from "remeda";
13
- import { coerce, satisfies } from "semver";
14
9
  //#region ../../internals/utils/src/errors.ts
15
10
  /**
16
11
  * Thrown when one or more errors occur during a Kubb build.
@@ -151,64 +146,6 @@ var AsyncEventEmitter = class {
151
146
  }
152
147
  };
153
148
  //#endregion
154
- //#region ../../internals/utils/src/casing.ts
155
- /**
156
- * Shared implementation for camelCase and PascalCase conversion.
157
- * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
158
- * and capitalizes each word according to `pascal`.
159
- *
160
- * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
161
- */
162
- function toCamelOrPascal(text, pascal) {
163
- return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
164
- if (word.length > 1 && word === word.toUpperCase()) return word;
165
- if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
166
- return word.charAt(0).toUpperCase() + word.slice(1);
167
- }).join("").replace(/[^a-zA-Z0-9]/g, "");
168
- }
169
- /**
170
- * Splits `text` on `.` and applies `transformPart` to each segment.
171
- * The last segment receives `isLast = true`, all earlier segments receive `false`.
172
- * Segments are joined with `/` to form a file path.
173
- *
174
- * Only splits on dots followed by a letter so that version numbers
175
- * embedded in operationIds (e.g. `v2025.0`) are kept intact.
176
- */
177
- function applyToFileParts(text, transformPart) {
178
- const parts = text.split(/\.(?=[a-zA-Z])/);
179
- return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
180
- }
181
- /**
182
- * Converts `text` to camelCase.
183
- * When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
184
- *
185
- * @example
186
- * camelCase('hello-world') // 'helloWorld'
187
- * camelCase('pet.petId', { isFile: true }) // 'pet/petId'
188
- */
189
- function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
190
- if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
191
- prefix,
192
- suffix
193
- } : {}));
194
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
195
- }
196
- /**
197
- * Converts `text` to PascalCase.
198
- * When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
199
- *
200
- * @example
201
- * pascalCase('hello-world') // 'HelloWorld'
202
- * pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
203
- */
204
- function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
205
- if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
206
- prefix,
207
- suffix
208
- }) : camelCase(part));
209
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
210
- }
211
- //#endregion
212
149
  //#region ../../internals/utils/src/time.ts
213
150
  /**
214
151
  * Calculates elapsed time in milliseconds from a high-resolution `process.hrtime` start time.
@@ -244,26 +181,6 @@ function formatMs(ms) {
244
181
  //#endregion
245
182
  //#region ../../internals/utils/src/fs.ts
246
183
  /**
247
- * Walks up the directory tree from `cwd` (defaults to `process.cwd()`) and
248
- * returns the absolute path of the nearest `package.json`, or `null` when none
249
- * is found before reaching the filesystem root.
250
- *
251
- * @example
252
- * ```ts
253
- * const pkgPath = findPackageJSON('/home/user/project/src') // '/home/user/project/package.json'
254
- * ```
255
- */
256
- function findPackageJSON(cwd) {
257
- let dir = cwd ? resolve(cwd) : process.cwd();
258
- while (true) {
259
- const pkgPath = join(dir, "package.json");
260
- if (existsSync(pkgPath)) return pkgPath;
261
- const parent = dirname(dir);
262
- if (parent === dir) return null;
263
- dir = parent;
264
- }
265
- }
266
- /**
267
184
  * Converts all backslashes to forward slashes.
268
185
  * Extended-length Windows paths (`\\?\...`) are left unchanged.
269
186
  */
@@ -302,17 +219,6 @@ async function exists(path) {
302
219
  return access(path).then(() => true, () => false);
303
220
  }
304
221
  /**
305
- * Synchronous counterpart of `read`.
306
- *
307
- * @example
308
- * ```ts
309
- * const source = readSync('./src/Pet.ts')
310
- * ```
311
- */
312
- function readSync(path) {
313
- return readFileSync(path, { encoding: "utf8" });
314
- }
315
- /**
316
222
  * Writes `data` to `path`, trimming leading/trailing whitespace before saving.
317
223
  * Skips the write when the trimmed content is empty or identical to what is already on disk.
318
224
  * Creates any missing parent directories automatically.
@@ -362,159 +268,6 @@ async function clean(path) {
362
268
  });
363
269
  }
364
270
  //#endregion
365
- //#region ../../internals/utils/src/string.ts
366
- /**
367
- * Strips the file extension from a path or file name.
368
- * Only removes the last `.ext` segment when the dot is not part of a directory name.
369
- *
370
- * @example
371
- * trimExtName('petStore.ts') // 'petStore'
372
- * trimExtName('/src/models/pet.ts') // '/src/models/pet'
373
- * trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
374
- * trimExtName('noExtension') // 'noExtension'
375
- */
376
- function trimExtName$1(text) {
377
- const dotIndex = text.lastIndexOf(".");
378
- if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
379
- return text;
380
- }
381
- __name(trimExtName$1, "trimExtName");
382
- //#endregion
383
- //#region ../../internals/utils/src/promise.ts
384
- /** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.
385
- *
386
- * @example
387
- * ```ts
388
- * const results = await Promise.allSettled([p1, p2])
389
- * results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)
390
- * ```
391
- */
392
- function isPromiseRejectedResult(result) {
393
- return result.status === "rejected";
394
- }
395
- //#endregion
396
- //#region ../../internals/utils/src/reserved.ts
397
- /**
398
- * JavaScript and Java reserved words.
399
- * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
400
- */
401
- const reservedWords = new Set([
402
- "abstract",
403
- "arguments",
404
- "boolean",
405
- "break",
406
- "byte",
407
- "case",
408
- "catch",
409
- "char",
410
- "class",
411
- "const",
412
- "continue",
413
- "debugger",
414
- "default",
415
- "delete",
416
- "do",
417
- "double",
418
- "else",
419
- "enum",
420
- "eval",
421
- "export",
422
- "extends",
423
- "false",
424
- "final",
425
- "finally",
426
- "float",
427
- "for",
428
- "function",
429
- "goto",
430
- "if",
431
- "implements",
432
- "import",
433
- "in",
434
- "instanceof",
435
- "int",
436
- "interface",
437
- "let",
438
- "long",
439
- "native",
440
- "new",
441
- "null",
442
- "package",
443
- "private",
444
- "protected",
445
- "public",
446
- "return",
447
- "short",
448
- "static",
449
- "super",
450
- "switch",
451
- "synchronized",
452
- "this",
453
- "throw",
454
- "throws",
455
- "transient",
456
- "true",
457
- "try",
458
- "typeof",
459
- "var",
460
- "void",
461
- "volatile",
462
- "while",
463
- "with",
464
- "yield",
465
- "Array",
466
- "Date",
467
- "hasOwnProperty",
468
- "Infinity",
469
- "isFinite",
470
- "isNaN",
471
- "isPrototypeOf",
472
- "length",
473
- "Math",
474
- "name",
475
- "NaN",
476
- "Number",
477
- "Object",
478
- "prototype",
479
- "String",
480
- "toString",
481
- "undefined",
482
- "valueOf"
483
- ]);
484
- /**
485
- * Prefixes `word` with `_` when it is a reserved JavaScript/Java identifier or starts with a digit.
486
- *
487
- * @example
488
- * ```ts
489
- * transformReservedWord('class') // '_class'
490
- * transformReservedWord('42foo') // '_42foo'
491
- * transformReservedWord('status') // 'status'
492
- * ```
493
- */
494
- function transformReservedWord(word) {
495
- const firstChar = word.charCodeAt(0);
496
- if (word && (reservedWords.has(word) || firstChar >= 48 && firstChar <= 57)) return `_${word}`;
497
- return word;
498
- }
499
- /**
500
- * Returns `true` when `name` is a syntactically valid JavaScript variable name.
501
- *
502
- * @example
503
- * ```ts
504
- * isValidVarName('status') // true
505
- * isValidVarName('class') // false (reserved word)
506
- * isValidVarName('42foo') // false (starts with digit)
507
- * ```
508
- */
509
- function isValidVarName(name) {
510
- try {
511
- new Function(`var ${name}`);
512
- } catch {
513
- return false;
514
- }
515
- return true;
516
- }
517
- //#endregion
518
271
  //#region ../../internals/utils/src/urlPath.ts
519
272
  /**
520
273
  * Parses and transforms an OpenAPI/Swagger path string into various URL formats.
@@ -566,1619 +319,161 @@ var URLPath = class {
566
319
  * new URLPath('/pet/{petId}').template // '`/pet/${petId}`'
567
320
  * new URLPath('/account/monetary-accountID').template // '`/account/${monetaryAccountId}`'
568
321
  */
569
- get template() {
570
- return this.toTemplateString();
571
- }
572
- /** Returns the path and its extracted params as a structured `URLObject`, or as a stringified expression when `stringify` is set.
573
- *
574
- * @example
575
- * ```ts
576
- * new URLPath('/pet/{petId}').object
577
- * // { url: '/pet/:petId', params: { petId: 'petId' } }
578
- * ```
579
- */
580
- get object() {
581
- return this.toObject();
582
- }
583
- /** Returns a map of path parameter names, or `undefined` when the path has no parameters.
584
- *
585
- * @example
586
- * ```ts
587
- * new URLPath('/pet/{petId}').params // { petId: 'petId' }
588
- * new URLPath('/pet').params // undefined
589
- * ```
590
- */
591
- get params() {
592
- return this.getParams();
593
- }
594
- #transformParam(raw) {
595
- const param = isValidVarName(raw) ? raw : camelCase(raw);
596
- return this.#options.casing === "camelcase" ? camelCase(param) : param;
597
- }
598
- /**
599
- * Iterates over every `{param}` token in `path`, calling `fn` with the raw token and transformed name.
600
- */
601
- #eachParam(fn) {
602
- for (const match of this.path.matchAll(/\{([^}]+)\}/g)) {
603
- const raw = match[1];
604
- fn(raw, this.#transformParam(raw));
605
- }
606
- }
607
- toObject({ type = "path", replacer, stringify } = {}) {
608
- const object = {
609
- url: type === "path" ? this.toURLPath() : this.toTemplateString({ replacer }),
610
- params: this.getParams()
611
- };
612
- if (stringify) {
613
- if (type === "template") return JSON.stringify(object).replaceAll("'", "").replaceAll(`"`, "");
614
- if (object.params) return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", "").replaceAll(`"`, "")} }`;
615
- return `{ url: '${object.url}' }`;
616
- }
617
- return object;
618
- }
619
- /**
620
- * Converts the OpenAPI path to a TypeScript template literal string.
621
- * An optional `replacer` can transform each extracted parameter name before interpolation.
622
- *
623
- * @example
624
- * new URLPath('/pet/{petId}').toTemplateString() // '`/pet/${petId}`'
625
- */
626
- toTemplateString({ prefix = "", replacer } = {}) {
627
- return `\`${prefix}${this.path.split(/\{([^}]+)\}/).map((part, i) => {
628
- if (i % 2 === 0) return part;
629
- const param = this.#transformParam(part);
630
- return `\${${replacer ? replacer(param) : param}}`;
631
- }).join("")}\``;
632
- }
633
- /**
634
- * Extracts all `{param}` segments from the path and returns them as a key-value map.
635
- * An optional `replacer` transforms each parameter name in both key and value positions.
636
- * Returns `undefined` when no path parameters are found.
637
- *
638
- * @example
639
- * ```ts
640
- * new URLPath('/pet/{petId}/tag/{tagId}').getParams()
641
- * // { petId: 'petId', tagId: 'tagId' }
642
- * ```
643
- */
644
- getParams(replacer) {
645
- const params = {};
646
- this.#eachParam((_raw, param) => {
647
- const key = replacer ? replacer(param) : param;
648
- params[key] = key;
649
- });
650
- return Object.keys(params).length > 0 ? params : void 0;
651
- }
652
- /** Converts the OpenAPI path to Express-style colon syntax.
653
- *
654
- * @example
655
- * ```ts
656
- * new URLPath('/pet/{petId}').toURLPath() // '/pet/:petId'
657
- * ```
658
- */
659
- toURLPath() {
660
- return this.path.replace(/\{([^}]+)\}/g, ":$1");
661
- }
662
- };
663
- //#endregion
664
- //#region src/constants.ts
665
- /**
666
- * Base URL for the Kubb Studio web app.
667
- */
668
- const DEFAULT_STUDIO_URL = "https://studio.kubb.dev";
669
- /**
670
- * File name used for generated barrel (index) files.
671
- */
672
- const BARREL_FILENAME = "index.ts";
673
- /**
674
- * Default banner style written at the top of every generated file.
675
- */
676
- const DEFAULT_BANNER = "simple";
677
- /**
678
- * Default file-extension mapping used when no explicit mapping is configured.
679
- */
680
- const DEFAULT_EXTENSION = { ".ts": ".ts" };
681
- /**
682
- * Numeric log-level thresholds used internally to compare verbosity.
683
- *
684
- * Higher numbers are more verbose.
685
- */
686
- const logLevel = {
687
- silent: Number.NEGATIVE_INFINITY,
688
- error: 0,
689
- warn: 1,
690
- info: 3,
691
- verbose: 4,
692
- debug: 5
693
- };
694
- /**
695
- * CLI command descriptors for each supported linter.
696
- *
697
- * Each entry contains the executable `command`, an `args` factory that maps an
698
- * output path to the correct argument list, and an `errorMessage` shown when
699
- * the linter is not found.
700
- */
701
- const linters = {
702
- eslint: {
703
- command: "eslint",
704
- args: (outputPath) => [outputPath, "--fix"],
705
- errorMessage: "Eslint not found"
706
- },
707
- biome: {
708
- command: "biome",
709
- args: (outputPath) => [
710
- "lint",
711
- "--fix",
712
- outputPath
713
- ],
714
- errorMessage: "Biome not found"
715
- },
716
- oxlint: {
717
- command: "oxlint",
718
- args: (outputPath) => ["--fix", outputPath],
719
- errorMessage: "Oxlint not found"
720
- }
721
- };
722
- /**
723
- * CLI command descriptors for each supported code formatter.
724
- *
725
- * Each entry contains the executable `command`, an `args` factory that maps an
726
- * output path to the correct argument list, and an `errorMessage` shown when
727
- * the formatter is not found.
728
- */
729
- const formatters = {
730
- prettier: {
731
- command: "prettier",
732
- args: (outputPath) => [
733
- "--ignore-unknown",
734
- "--write",
735
- outputPath
736
- ],
737
- errorMessage: "Prettier not found"
738
- },
739
- biome: {
740
- command: "biome",
741
- args: (outputPath) => [
742
- "format",
743
- "--write",
744
- outputPath
745
- ],
746
- errorMessage: "Biome not found"
747
- },
748
- oxfmt: {
749
- command: "oxfmt",
750
- args: (outputPath) => [outputPath],
751
- errorMessage: "Oxfmt not found"
752
- }
753
- };
754
- //#endregion
755
- //#region src/createAdapter.ts
756
- /**
757
- * Creates an adapter factory. Call the returned function with optional options to get the adapter instance.
758
- *
759
- * @example
760
- * export const myAdapter = createAdapter<MyAdapter>((options) => {
761
- * return {
762
- * name: 'my-adapter',
763
- * options,
764
- * async parse(source) { ... },
765
- * }
766
- * })
767
- *
768
- * // instantiate
769
- * const adapter = myAdapter({ validate: true })
770
- */
771
- function createAdapter(build) {
772
- return (options) => build(options ?? {});
773
- }
774
- //#endregion
775
- //#region ../../node_modules/.pnpm/yocto-queue@1.2.2/node_modules/yocto-queue/index.js
776
- var Node$1 = class {
777
- static {
778
- __name(this, "Node");
779
- }
780
- value;
781
- next;
782
- constructor(value) {
783
- this.value = value;
784
- }
785
- };
786
- var Queue = class {
787
- #head;
788
- #tail;
789
- #size;
790
- constructor() {
791
- this.clear();
792
- }
793
- enqueue(value) {
794
- const node = new Node$1(value);
795
- if (this.#head) {
796
- this.#tail.next = node;
797
- this.#tail = node;
798
- } else {
799
- this.#head = node;
800
- this.#tail = node;
801
- }
802
- this.#size++;
803
- }
804
- dequeue() {
805
- const current = this.#head;
806
- if (!current) return;
807
- this.#head = this.#head.next;
808
- this.#size--;
809
- if (!this.#head) this.#tail = void 0;
810
- return current.value;
811
- }
812
- peek() {
813
- if (!this.#head) return;
814
- return this.#head.value;
815
- }
816
- clear() {
817
- this.#head = void 0;
818
- this.#tail = void 0;
819
- this.#size = 0;
820
- }
821
- get size() {
822
- return this.#size;
823
- }
824
- *[Symbol.iterator]() {
825
- let current = this.#head;
826
- while (current) {
827
- yield current.value;
828
- current = current.next;
829
- }
830
- }
831
- *drain() {
832
- while (this.#head) yield this.dequeue();
833
- }
834
- };
835
- //#endregion
836
- //#region ../../node_modules/.pnpm/p-limit@7.3.0/node_modules/p-limit/index.js
837
- function pLimit(concurrency) {
838
- let rejectOnClear = false;
839
- if (typeof concurrency === "object") ({concurrency, rejectOnClear = false} = concurrency);
840
- validateConcurrency(concurrency);
841
- if (typeof rejectOnClear !== "boolean") throw new TypeError("Expected `rejectOnClear` to be a boolean");
842
- const queue = new Queue();
843
- let activeCount = 0;
844
- const resumeNext = () => {
845
- if (activeCount < concurrency && queue.size > 0) {
846
- activeCount++;
847
- queue.dequeue().run();
848
- }
849
- };
850
- const next = () => {
851
- activeCount--;
852
- resumeNext();
853
- };
854
- const run = async (function_, resolve, arguments_) => {
855
- const result = (async () => function_(...arguments_))();
856
- resolve(result);
857
- try {
858
- await result;
859
- } catch {}
860
- next();
861
- };
862
- const enqueue = (function_, resolve, reject, arguments_) => {
863
- const queueItem = { reject };
864
- new Promise((internalResolve) => {
865
- queueItem.run = internalResolve;
866
- queue.enqueue(queueItem);
867
- }).then(run.bind(void 0, function_, resolve, arguments_));
868
- if (activeCount < concurrency) resumeNext();
869
- };
870
- const generator = (function_, ...arguments_) => new Promise((resolve, reject) => {
871
- enqueue(function_, resolve, reject, arguments_);
872
- });
873
- Object.defineProperties(generator, {
874
- activeCount: { get: () => activeCount },
875
- pendingCount: { get: () => queue.size },
876
- clearQueue: { value() {
877
- if (!rejectOnClear) {
878
- queue.clear();
879
- return;
880
- }
881
- const abortError = AbortSignal.abort().reason;
882
- while (queue.size > 0) queue.dequeue().reject(abortError);
883
- } },
884
- concurrency: {
885
- get: () => concurrency,
886
- set(newConcurrency) {
887
- validateConcurrency(newConcurrency);
888
- concurrency = newConcurrency;
889
- queueMicrotask(() => {
890
- while (activeCount < concurrency && queue.size > 0) resumeNext();
891
- });
892
- }
893
- },
894
- map: { async value(iterable, function_) {
895
- const promises = Array.from(iterable, (value, index) => this(function_, value, index));
896
- return Promise.all(promises);
897
- } }
898
- });
899
- return generator;
900
- }
901
- function validateConcurrency(concurrency) {
902
- if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) throw new TypeError("Expected `concurrency` to be a number from 1 and up");
903
- }
904
- //#endregion
905
- //#region src/FileProcessor.ts
906
- function joinSources(file) {
907
- return file.sources.map((item) => extractStringsFromNodes(item.nodes)).filter(Boolean).join("\n\n");
908
- }
909
- /**
910
- * Converts a single file to a string using the registered parsers.
911
- * Falls back to joining source values when no matching parser is found.
912
- */
913
- var FileProcessor = class {
914
- #limit = pLimit(100);
915
- async parse(file, { parsers, extension } = {}) {
916
- const parseExtName = extension?.[file.extname] || void 0;
917
- if (!parsers || !file.extname) return joinSources(file);
918
- const parser = parsers.get(file.extname);
919
- if (!parser) return joinSources(file);
920
- return parser.parse(file, { extname: parseExtName });
921
- }
922
- async run(files, { parsers, mode = "sequential", extension, onStart, onEnd, onUpdate } = {}) {
923
- await onStart?.(files);
924
- const total = files.length;
925
- let processed = 0;
926
- const processOne = async (file) => {
927
- const source = await this.parse(file, {
928
- extension,
929
- parsers
930
- });
931
- const currentProcessed = ++processed;
932
- const percentage = currentProcessed / total * 100;
933
- await onUpdate?.({
934
- file,
935
- source,
936
- processed: currentProcessed,
937
- percentage,
938
- total
939
- });
940
- };
941
- if (mode === "sequential") for (const file of files) await processOne(file);
942
- else await Promise.all(files.map((file) => this.#limit(() => processOne(file))));
943
- await onEnd?.(files);
944
- return files;
945
- }
946
- };
947
- //#endregion
948
- //#region src/definePlugin.ts
949
- /**
950
- * Returns `true` when `plugin` is a hook-style plugin created with `definePlugin`.
951
- *
952
- * Used by `PluginDriver` to distinguish hook-style plugins from legacy `createPlugin` plugins
953
- * so it can normalize them and register their handlers on the `AsyncEventEmitter`.
954
- */
955
- function isHookStylePlugin(plugin) {
956
- return typeof plugin === "object" && plugin !== null && "hooks" in plugin;
957
- }
958
- /**
959
- * Creates a plugin factory using the new hook-style (`hooks:`) API.
960
- *
961
- * The returned factory is called with optional options and produces a `HookStylePlugin`
962
- * that coexists with plugins created via the legacy `createPlugin` API in the same
963
- * `kubb.config.ts`.
964
- *
965
- * Lifecycle handlers are registered on the `PluginDriver`'s `AsyncEventEmitter`, enabling
966
- * both the plugin's own handlers and external tooling (CLI, devtools) to observe every event.
967
- *
968
- * @example
969
- * ```ts
970
- * // With PluginFactoryOptions (recommended for real plugins)
971
- * export const pluginTs = definePlugin<PluginTs>((options) => ({
972
- * name: 'plugin-ts',
973
- * hooks: {
974
- * 'kubb:plugin:setup'(ctx) {
975
- * ctx.setResolver(resolverTs) // typed as Partial<ResolverTs>
976
- * },
977
- * },
978
- * }))
979
- * ```
980
- */
981
- function definePlugin(factory) {
982
- return (options) => factory(options ?? {});
983
- }
984
- //#endregion
985
- //#region src/defineResolver.ts
986
- /**
987
- * Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
988
- */
989
- function matchesOperationPattern(node, type, pattern) {
990
- switch (type) {
991
- case "tag": return node.tags.some((tag) => !!tag.match(pattern));
992
- case "operationId": return !!node.operationId.match(pattern);
993
- case "path": return !!node.path.match(pattern);
994
- case "method": return !!node.method.toLowerCase().match(pattern);
995
- case "contentType": return !!node.requestBody?.contentType?.match(pattern);
996
- default: return false;
997
- }
998
- }
999
- /**
1000
- * Checks if a schema matches a pattern for a given filter type (`schemaName`).
1001
- *
1002
- * Returns `null` when the filter type doesn't apply to schemas.
1003
- */
1004
- function matchesSchemaPattern(node, type, pattern) {
1005
- switch (type) {
1006
- case "schemaName": return node.name ? !!node.name.match(pattern) : false;
1007
- default: return null;
1008
- }
1009
- }
1010
- /**
1011
- * Default name resolver used by `defineResolver`.
1012
- *
1013
- * - `camelCase` for `function` and `file` types.
1014
- * - `PascalCase` for `type`.
1015
- * - `camelCase` for everything else.
1016
- */
1017
- function defaultResolver(name, type) {
1018
- let resolvedName = camelCase(name);
1019
- if (type === "file" || type === "function") resolvedName = camelCase(name, { isFile: type === "file" });
1020
- if (type === "type") resolvedName = pascalCase(name);
1021
- return resolvedName;
1022
- }
1023
- /**
1024
- * Default option resolver — applies include/exclude filters and merges matching override options.
1025
- *
1026
- * Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
1027
- *
1028
- * @example Include/exclude filtering
1029
- * ```ts
1030
- * const options = defaultResolveOptions(operationNode, {
1031
- * options: { output: 'types' },
1032
- * exclude: [{ type: 'tag', pattern: 'internal' }],
1033
- * })
1034
- * // → null when node has tag 'internal'
1035
- * ```
1036
- *
1037
- * @example Override merging
1038
- * ```ts
1039
- * const options = defaultResolveOptions(operationNode, {
1040
- * options: { enumType: 'asConst' },
1041
- * override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
1042
- * })
1043
- * // → { enumType: 'enum' } when operationId matches
1044
- * ```
1045
- */
1046
- function defaultResolveOptions(node, { options, exclude = [], include, override = [] }) {
1047
- if (isOperationNode(node)) {
1048
- if (exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
1049
- if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
1050
- const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options;
1051
- return {
1052
- ...options,
1053
- ...overrideOptions
1054
- };
1055
- }
1056
- if (isSchemaNode(node)) {
1057
- if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) return null;
1058
- if (include) {
1059
- const applicable = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern)).filter((r) => r !== null);
1060
- if (applicable.length > 0 && !applicable.includes(true)) return null;
1061
- }
1062
- const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options;
1063
- return {
1064
- ...options,
1065
- ...overrideOptions
1066
- };
1067
- }
1068
- return options;
1069
- }
1070
- /**
1071
- * Default path resolver used by `defineResolver`.
1072
- *
1073
- * - Returns the output directory in `single` mode.
1074
- * - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
1075
- * - Falls back to a flat `output/baseName` path otherwise.
1076
- *
1077
- * A custom `group.name` function overrides the default subdirectory naming.
1078
- * For `tag` groups the default is `${camelCase(tag)}Controller`.
1079
- * For `path` groups the default is the first path segment after `/`.
1080
- *
1081
- * @example Flat output
1082
- * ```ts
1083
- * defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
1084
- * // → '/src/types/petTypes.ts'
1085
- * ```
1086
- *
1087
- * @example Tag-based grouping
1088
- * ```ts
1089
- * defaultResolvePath(
1090
- * { baseName: 'petTypes.ts', tag: 'pets' },
1091
- * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
1092
- * )
1093
- * // → '/src/types/petsController/petTypes.ts'
1094
- * ```
1095
- *
1096
- * @example Path-based grouping
1097
- * ```ts
1098
- * defaultResolvePath(
1099
- * { baseName: 'petTypes.ts', path: '/pets/list' },
1100
- * { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
1101
- * )
1102
- * // → '/src/types/pets/petTypes.ts'
1103
- * ```
1104
- *
1105
- * @example Single-file mode
1106
- * ```ts
1107
- * defaultResolvePath(
1108
- * { baseName: 'petTypes.ts', pathMode: 'single' },
1109
- * { root: '/src', output: { path: 'types' } },
1110
- * )
1111
- * // → '/src/types'
1112
- * ```
1113
- */
1114
- function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }, { root, output, group }) {
1115
- if ((pathMode ?? getMode(path.resolve(root, output.path))) === "single") return path.resolve(root, output.path);
1116
- if (group && (groupPath || tag)) return path.resolve(root, output.path, group.name({ group: group.type === "path" ? groupPath : tag }), baseName);
1117
- return path.resolve(root, output.path, baseName);
1118
- }
1119
- /**
1120
- * Default file resolver used by `defineResolver`.
1121
- *
1122
- * Resolves a `FileNode` by combining name resolution (`resolver.default`) with
1123
- * path resolution (`resolver.resolvePath`). The resolved file always has empty
1124
- * `sources`, `imports`, and `exports` arrays — consumers populate those separately.
1125
- *
1126
- * In `single` mode the name is omitted and the file sits directly in the output directory.
1127
- *
1128
- * @example Resolve a schema file
1129
- * ```ts
1130
- * const file = defaultResolveFile.call(resolver,
1131
- * { name: 'pet', extname: '.ts' },
1132
- * { root: '/src', output: { path: 'types' } },
1133
- * )
1134
- * // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
1135
- * ```
1136
- *
1137
- * @example Resolve an operation file with tag grouping
1138
- * ```ts
1139
- * const file = defaultResolveFile.call(resolver,
1140
- * { name: 'listPets', extname: '.ts', tag: 'pets' },
1141
- * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
1142
- * )
1143
- * // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
1144
- * ```
1145
- */
1146
- function defaultResolveFile({ name, extname, tag, path: groupPath }, context) {
1147
- const pathMode = getMode(path.resolve(context.root, context.output.path));
1148
- const baseName = `${pathMode === "single" ? "" : this.default(name, "file")}${extname}`;
1149
- const filePath = this.resolvePath({
1150
- baseName,
1151
- pathMode,
1152
- tag,
1153
- path: groupPath
1154
- }, context);
1155
- return createFile({
1156
- path: filePath,
1157
- baseName: path.basename(filePath),
1158
- meta: { pluginName: this.pluginName },
1159
- sources: [],
1160
- imports: [],
1161
- exports: []
1162
- });
1163
- }
1164
- /**
1165
- * Generates the default "Generated by Kubb" banner from config and optional node metadata.
1166
- */
1167
- function buildDefaultBanner({ title, description, version, config }) {
1168
- try {
1169
- let source = "";
1170
- if (Array.isArray(config.input)) {
1171
- const first = config.input[0];
1172
- if (first && "path" in first) source = path.basename(first.path);
1173
- } else if ("path" in config.input) source = path.basename(config.input.path);
1174
- else if ("data" in config.input) source = "text content";
1175
- let banner = "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n";
1176
- if (config.output.defaultBanner === "simple") {
1177
- banner += "*/\n";
1178
- return banner;
1179
- }
1180
- if (source) banner += `* Source: ${source}\n`;
1181
- if (title) banner += `* Title: ${title}\n`;
1182
- if (description) {
1183
- const formattedDescription = description.replace(/\n/gm, "\n* ");
1184
- banner += `* Description: ${formattedDescription}\n`;
1185
- }
1186
- if (version) banner += `* OpenAPI spec version: ${version}\n`;
1187
- banner += "*/\n";
1188
- return banner;
1189
- } catch (_error) {
1190
- return "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/";
1191
- }
1192
- }
1193
- /**
1194
- * Default banner resolver — returns the banner string for a generated file.
1195
- *
1196
- * A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
1197
- * When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
1198
- * from the OAS spec when a `node` is provided).
1199
- *
1200
- * - When `output.banner` is a function and `node` is provided, returns `output.banner(node)`.
1201
- * - When `output.banner` is a function and `node` is absent, falls back to the Kubb notice.
1202
- * - When `output.banner` is a string, returns it directly.
1203
- * - When `config.output.defaultBanner` is `false`, returns `undefined`.
1204
- * - Otherwise returns the Kubb "Generated by Kubb" notice.
1205
- *
1206
- * @example String banner overrides default
1207
- * ```ts
1208
- * defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
1209
- * // → '// my banner'
1210
- * ```
1211
- *
1212
- * @example Function banner with node
1213
- * ```ts
1214
- * defaultResolveBanner(inputNode, { output: { banner: (node) => `// v${node.version}` }, config })
1215
- * // → '// v3.0.0'
1216
- * ```
1217
- *
1218
- * @example No user banner — Kubb notice with OAS metadata
1219
- * ```ts
1220
- * defaultResolveBanner(inputNode, { config })
1221
- * // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
1222
- * ```
1223
- *
1224
- * @example Disabled default banner
1225
- * ```ts
1226
- * defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
1227
- * // → undefined
1228
- * ```
1229
- */
1230
- function defaultResolveBanner(node, { output, config }) {
1231
- if (typeof output?.banner === "function") return output.banner(node);
1232
- if (typeof output?.banner === "string") return output.banner;
1233
- if (config.output.defaultBanner === false) return;
1234
- return buildDefaultBanner({
1235
- title: node?.meta?.title,
1236
- version: node?.meta?.version,
1237
- config
1238
- });
1239
- }
1240
- /**
1241
- * Default footer resolver — returns the footer string for a generated file.
1242
- *
1243
- * - When `output.footer` is a function and `node` is provided, calls it with the node.
1244
- * - When `output.footer` is a function and `node` is absent, returns `undefined`.
1245
- * - When `output.footer` is a string, returns it directly.
1246
- * - Otherwise returns `undefined`.
1247
- *
1248
- * @example String footer
1249
- * ```ts
1250
- * defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
1251
- * // → '// end of file'
1252
- * ```
1253
- *
1254
- * @example Function footer with node
1255
- * ```ts
1256
- * defaultResolveFooter(inputNode, { output: { footer: (node) => `// ${node.title}` }, config })
1257
- * // → '// Pet Store'
1258
- * ```
1259
- */
1260
- function defaultResolveFooter(node, { output }) {
1261
- if (typeof output?.footer === "function") return node ? output.footer(node) : void 0;
1262
- if (typeof output?.footer === "string") return output.footer;
1263
- }
1264
- /**
1265
- * Defines a resolver for a plugin, injecting built-in defaults for name casing,
1266
- * include/exclude/override filtering, path resolution, and file construction.
1267
- *
1268
- * All four defaults can be overridden by providing them in the builder function:
1269
- * - `default` — name casing strategy (camelCase / PascalCase)
1270
- * - `resolveOptions` — include/exclude/override filtering
1271
- * - `resolvePath` — output path computation
1272
- * - `resolveFile` — full `FileNode` construction
1273
- *
1274
- * Methods in the builder have access to `this` (the full resolver object), so they
1275
- * can call other resolver methods without circular imports.
1276
- *
1277
- * @example Basic resolver with naming helpers
1278
- * ```ts
1279
- * export const resolver = defineResolver<PluginTs>(() => ({
1280
- * name: 'default',
1281
- * resolveName(node) {
1282
- * return this.default(node.name, 'function')
1283
- * },
1284
- * resolveTypedName(node) {
1285
- * return this.default(node.name, 'type')
1286
- * },
1287
- * }))
1288
- * ```
1289
- *
1290
- * @example Override resolvePath for a custom output structure
1291
- * ```ts
1292
- * export const resolver = defineResolver<PluginTs>(() => ({
1293
- * name: 'custom',
1294
- * resolvePath({ baseName }, { root, output }) {
1295
- * return path.resolve(root, output.path, 'generated', baseName)
1296
- * },
1297
- * }))
1298
- * ```
1299
- *
1300
- * @example Use this.default inside a helper
1301
- * ```ts
1302
- * export const resolver = defineResolver<PluginTs>(() => ({
1303
- * name: 'default',
1304
- * resolveParamName(node, param) {
1305
- * return this.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
1306
- * },
1307
- * }))
1308
- * ```
1309
- */
1310
- function defineResolver(build) {
1311
- return {
1312
- default: defaultResolver,
1313
- resolveOptions: defaultResolveOptions,
1314
- resolvePath: defaultResolvePath,
1315
- resolveFile: defaultResolveFile,
1316
- resolveBanner: defaultResolveBanner,
1317
- resolveFooter: defaultResolveFooter,
1318
- ...build()
1319
- };
1320
- }
1321
- //#endregion
1322
- //#region src/devtools.ts
1323
- /**
1324
- * Encodes an `InputNode` as a compressed, URL-safe string.
1325
- *
1326
- * The JSON representation is deflate-compressed with {@link deflateSync} before
1327
- * base64url encoding, which typically reduces payload size by 70–80 % and
1328
- * keeps URLs well within browser and server path-length limits.
1329
- *
1330
- * Use {@link decodeAst} to reverse.
1331
- */
1332
- function encodeAst(input) {
1333
- const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(input)));
1334
- return Buffer.from(compressed).toString("base64url");
1335
- }
1336
- /**
1337
- * Constructs the Kubb Studio URL for the given `InputNode`.
1338
- * When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
1339
- * The `input` is encoded and attached as the `?root=` query parameter so Studio
1340
- * can decode and render it without a round-trip to any server.
1341
- */
1342
- function getStudioUrl(input, studioUrl, options = {}) {
1343
- return `${studioUrl.replace(/\/$/, "")}${options.ast ? "/ast" : ""}?root=${encodeAst(input)}`;
1344
- }
1345
- /**
1346
- * Opens the Kubb Studio URL for the given `InputNode` in the default browser —
1347
- *
1348
- * Falls back to printing the URL if the browser cannot be launched.
1349
- */
1350
- async function openInStudio(input, studioUrl, options = {}) {
1351
- const url = getStudioUrl(input, studioUrl, options);
1352
- const cmd = process.platform === "win32" ? "cmd" : process.platform === "darwin" ? "open" : "xdg-open";
1353
- const args = process.platform === "win32" ? [
1354
- "/c",
1355
- "start",
1356
- "",
1357
- url
1358
- ] : [url];
1359
- try {
1360
- await x(cmd, args);
1361
- } catch {
1362
- console.log(`\n ${url}\n`);
1363
- }
1364
- }
1365
- //#endregion
1366
- //#region src/FileManager.ts
1367
- function mergeFile(a, b) {
1368
- return {
1369
- ...a,
1370
- sources: [...a.sources || [], ...b.sources || []],
1371
- imports: [...a.imports || [], ...b.imports || []],
1372
- exports: [...a.exports || [], ...b.exports || []]
1373
- };
1374
- }
1375
- /**
1376
- * In-memory file store for generated files.
1377
- *
1378
- * Files with the same `path` are merged — sources, imports, and exports are concatenated.
1379
- * The `files` getter returns all stored files sorted by path length (shortest first).
1380
- *
1381
- * @example
1382
- * ```ts
1383
- * import { FileManager } from '@kubb/core'
1384
- *
1385
- * const manager = new FileManager()
1386
- * manager.upsert(myFile)
1387
- * console.log(manager.files) // all stored files
1388
- * ```
1389
- */
1390
- var FileManager = class {
1391
- #cache = /* @__PURE__ */ new Map();
1392
- #filesCache = null;
1393
- /**
1394
- * Adds one or more files. Files with the same path are merged — sources, imports,
1395
- * and exports from all calls with the same path are concatenated together.
1396
- */
1397
- add(...files) {
1398
- const resolvedFiles = [];
1399
- const mergedFiles = /* @__PURE__ */ new Map();
1400
- files.forEach((file) => {
1401
- const existing = mergedFiles.get(file.path);
1402
- if (existing) mergedFiles.set(file.path, mergeFile(existing, file));
1403
- else mergedFiles.set(file.path, file);
1404
- });
1405
- for (const file of mergedFiles.values()) {
1406
- const resolvedFile = createFile(file);
1407
- this.#cache.set(resolvedFile.path, resolvedFile);
1408
- this.#filesCache = null;
1409
- resolvedFiles.push(resolvedFile);
1410
- }
1411
- return resolvedFiles;
1412
- }
1413
- /**
1414
- * Adds or merges one or more files.
1415
- * If a file with the same path already exists, its sources/imports/exports are merged together.
1416
- */
1417
- upsert(...files) {
1418
- const resolvedFiles = [];
1419
- const mergedFiles = /* @__PURE__ */ new Map();
1420
- files.forEach((file) => {
1421
- const existing = mergedFiles.get(file.path);
1422
- if (existing) mergedFiles.set(file.path, mergeFile(existing, file));
1423
- else mergedFiles.set(file.path, file);
1424
- });
1425
- for (const file of mergedFiles.values()) {
1426
- const existing = this.#cache.get(file.path);
1427
- const resolvedFile = createFile(existing ? mergeFile(existing, file) : file);
1428
- this.#cache.set(resolvedFile.path, resolvedFile);
1429
- this.#filesCache = null;
1430
- resolvedFiles.push(resolvedFile);
1431
- }
1432
- return resolvedFiles;
1433
- }
1434
- getByPath(path) {
1435
- return this.#cache.get(path) ?? null;
1436
- }
1437
- deleteByPath(path) {
1438
- this.#cache.delete(path);
1439
- this.#filesCache = null;
1440
- }
1441
- clear() {
1442
- this.#cache.clear();
1443
- this.#filesCache = null;
1444
- }
1445
- /**
1446
- * All stored files, sorted by path length (shorter paths first).
1447
- * Barrel/index files (e.g. index.ts) are sorted last within each length bucket.
1448
- */
1449
- get files() {
1450
- if (this.#filesCache) return this.#filesCache;
1451
- const keys = [...this.#cache.keys()].sort((a, b) => {
1452
- if (a.length !== b.length) return a.length - b.length;
1453
- const aIsIndex = trimExtName$1(a).endsWith("index");
1454
- if (aIsIndex !== trimExtName$1(b).endsWith("index")) return aIsIndex ? 1 : -1;
1455
- return 0;
1456
- });
1457
- const files = [];
1458
- for (const key of keys) {
1459
- const file = this.#cache.get(key);
1460
- if (file) files.push(file);
1461
- }
1462
- this.#filesCache = files;
1463
- return files;
1464
- }
1465
- };
1466
- //#endregion
1467
- //#region src/renderNode.ts
1468
- /**
1469
- * Handles the return value of a plugin AST hook or generator method.
1470
- *
1471
- * - Renderer output → rendered via the provided `rendererFactory` (e.g. JSX), files stored in `driver.fileManager`
1472
- * - `Array<FileNode>` → added directly into `driver.fileManager`
1473
- * - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
1474
- *
1475
- * Pass a `rendererFactory` (e.g. `jsxRenderer` from `@kubb/renderer-jsx`) when the result
1476
- * may be a renderer element. Generators that only return `Array<FileNode>` do not need one.
1477
- */
1478
- async function applyHookResult(result, driver, rendererFactory) {
1479
- if (!result) return;
1480
- if (Array.isArray(result)) {
1481
- driver.fileManager.upsert(...result);
1482
- return;
1483
- }
1484
- if (!rendererFactory) return;
1485
- const renderer = rendererFactory();
1486
- await renderer.render(result);
1487
- driver.fileManager.upsert(...renderer.files);
1488
- renderer.unmount();
1489
- }
1490
- //#endregion
1491
- //#region src/utils/executeStrategies.ts
1492
- /**
1493
- * Runs promise functions in sequence, threading each result into the next call.
1494
- *
1495
- * - Each function receives the accumulated state from the previous call.
1496
- * - Skips functions that return a falsy value (acts as a no-op for that step).
1497
- * - Returns an array of all individual results.
1498
- * @deprecated
1499
- */
1500
- function hookSeq(promises) {
1501
- return promises.filter(Boolean).reduce((promise, func) => {
1502
- if (typeof func !== "function") throw new Error("HookSeq needs a function that returns a promise `() => Promise<unknown>`");
1503
- return promise.then((state) => {
1504
- const calledFunc = func(state);
1505
- if (calledFunc) return calledFunc.then(Array.prototype.concat.bind(state));
1506
- return state;
1507
- });
1508
- }, Promise.resolve([]));
1509
- }
1510
- /**
1511
- * Runs promise functions in sequence and returns the first non-null result.
1512
- *
1513
- * - Stops as soon as `nullCheck` passes for a result (default: `!== null`).
1514
- * - Subsequent functions are skipped once a match is found.
1515
- * @deprecated
1516
- */
1517
- function hookFirst(promises, nullCheck = (state) => state !== null) {
1518
- let promise = Promise.resolve(null);
1519
- for (const func of promises.filter(Boolean)) promise = promise.then((state) => {
1520
- if (nullCheck(state)) return state;
1521
- return func(state);
1522
- });
1523
- return promise;
1524
- }
1525
- /**
1526
- * Runs promise functions concurrently and returns all settled results.
1527
- *
1528
- * - Limits simultaneous executions to `concurrency` (default: unlimited).
1529
- * - Uses `Promise.allSettled` so individual failures do not cancel other tasks.
1530
- * @deprecated
1531
- */
1532
- function hookParallel(promises, concurrency = Number.POSITIVE_INFINITY) {
1533
- const limit = pLimit(concurrency);
1534
- const tasks = promises.filter(Boolean).map((promise) => limit(() => promise()));
1535
- return Promise.allSettled(tasks);
1536
- }
1537
- //#endregion
1538
- //#region src/PluginDriver.ts
1539
- /**
1540
- * Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
1541
- *
1542
- * @example
1543
- * ```ts
1544
- * getMode('src/gen/types.ts') // 'single'
1545
- * getMode('src/gen/types') // 'split'
1546
- * ```
1547
- */
1548
- function getMode(fileOrFolder) {
1549
- if (!fileOrFolder) return "split";
1550
- return extname(fileOrFolder) ? "single" : "split";
1551
- }
1552
- const hookFirstNullCheck = (state) => !!state?.result;
1553
- var PluginDriver = class {
1554
- config;
1555
- options;
1556
- /**
1557
- * The universal `@kubb/ast` `InputNode` produced by the adapter, set by
1558
- * the build pipeline after the adapter's `parse()` resolves.
1559
- */
1560
- inputNode = void 0;
1561
- adapter = void 0;
1562
- #studioIsOpen = false;
1563
- /**
1564
- * Central file store for all generated files.
1565
- * Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
1566
- * add files; this property gives direct read/write access when needed.
1567
- */
1568
- fileManager = new FileManager();
1569
- plugins = /* @__PURE__ */ new Map();
1570
- /**
1571
- * Tracks which plugins have generators registered via `addGenerator()` (event-based path).
1572
- * Used by the build loop to decide whether to emit generator events for a given plugin.
1573
- */
1574
- #pluginsWithEventGenerators = /* @__PURE__ */ new Set();
1575
- #resolvers = /* @__PURE__ */ new Map();
1576
- #defaultResolvers = /* @__PURE__ */ new Map();
1577
- #hookListeners = /* @__PURE__ */ new Map();
1578
- constructor(config, options) {
1579
- this.config = config;
1580
- this.options = {
1581
- ...options,
1582
- hooks: options.hooks
1583
- };
1584
- config.plugins.map((rawPlugin) => {
1585
- if (isHookStylePlugin(rawPlugin)) return this.#normalizeHookStylePlugin(rawPlugin);
1586
- return {
1587
- ...rawPlugin,
1588
- buildStart: rawPlugin.buildStart ?? (() => {}),
1589
- buildEnd: rawPlugin.buildEnd ?? (() => {})
1590
- };
1591
- }).filter((plugin) => {
1592
- if (typeof plugin.apply === "function") return plugin.apply(config);
1593
- return true;
1594
- }).sort((a, b) => {
1595
- if (b.dependencies?.includes(a.name)) return -1;
1596
- if (a.dependencies?.includes(b.name)) return 1;
1597
- return 0;
1598
- }).forEach((plugin) => {
1599
- this.plugins.set(plugin.name, plugin);
1600
- });
1601
- }
1602
- get hooks() {
1603
- if (!this.options.hooks) throw new Error("hooks are not defined");
1604
- return this.options.hooks;
1605
- }
1606
- /**
1607
- * Creates a `Plugin`-compatible object from a hook-style plugin and registers
1608
- * its lifecycle handlers on the `AsyncEventEmitter`.
1609
- *
1610
- * The normalized plugin has an empty `buildStart` — generators registered via
1611
- * `addGenerator()` in `kubb:plugin:setup` are stored on `normalizedPlugin.generators`
1612
- * and used by `runPluginAstHooks` during the build.
1613
- */
1614
- #normalizeHookStylePlugin(hookPlugin) {
1615
- const generators = [];
1616
- const driver = this;
1617
- const normalizedPlugin = {
1618
- name: hookPlugin.name,
1619
- dependencies: hookPlugin.dependencies,
1620
- options: {
1621
- output: { path: "." },
1622
- exclude: [],
1623
- override: []
1624
- },
1625
- generators,
1626
- inject: () => void 0,
1627
- resolveName(name, type) {
1628
- return driver.getResolver(hookPlugin.name).default(name, type);
1629
- },
1630
- resolvePath(baseName, pathMode, resolveOptions) {
1631
- const resolver = driver.getResolver(hookPlugin.name);
1632
- const opts = normalizedPlugin.options;
1633
- const group = resolveOptions?.group;
1634
- return resolver.resolvePath({
1635
- baseName,
1636
- pathMode,
1637
- tag: group?.tag,
1638
- path: group?.path
1639
- }, {
1640
- root: resolve(driver.config.root, driver.config.output.path),
1641
- output: opts.output,
1642
- group: opts.group
1643
- });
1644
- },
1645
- buildStart() {},
1646
- buildEnd() {}
1647
- };
1648
- this.registerPluginHooks(hookPlugin, normalizedPlugin);
1649
- return normalizedPlugin;
1650
- }
1651
- /**
1652
- * Registers a hook-style plugin's lifecycle handlers on the shared `AsyncEventEmitter`.
1653
- *
1654
- * For `kubb:plugin:setup`, the registered listener wraps the globally emitted context with a
1655
- * plugin-specific one so that `addGenerator`, `setResolver`, `setTransformer`, and
1656
- * `setRenderer` all target the correct `normalizedPlugin` entry in the plugins map.
1657
- *
1658
- * All other hooks are iterated and registered directly as pass-through listeners.
1659
- * Any event key present in the global `KubbHooks` interface can be subscribed to.
1660
- *
1661
- * External tooling can subscribe to any of these events via `hooks.on(...)` to observe
1662
- * the plugin lifecycle without modifying plugin behavior.
1663
- */
1664
- registerPluginHooks(hookPlugin, normalizedPlugin) {
1665
- const { hooks } = hookPlugin;
1666
- if (hooks["kubb:plugin:setup"]) {
1667
- const setupHandler = (globalCtx) => {
1668
- const pluginCtx = {
1669
- ...globalCtx,
1670
- options: hookPlugin.options ?? {},
1671
- addGenerator: (gen) => {
1672
- this.registerGenerator(normalizedPlugin.name, gen);
1673
- },
1674
- setResolver: (resolver) => {
1675
- this.setPluginResolver(normalizedPlugin.name, resolver);
1676
- },
1677
- setTransformer: (visitor) => {
1678
- normalizedPlugin.transformer = visitor;
1679
- },
1680
- setRenderer: (renderer) => {
1681
- normalizedPlugin.renderer = renderer;
1682
- },
1683
- setOptions: (opts) => {
1684
- normalizedPlugin.options = {
1685
- ...normalizedPlugin.options,
1686
- ...opts
1687
- };
1688
- },
1689
- injectFile: (file) => {
1690
- const fileNode = createFile({
1691
- baseName: file.baseName,
1692
- path: file.path,
1693
- sources: file.sources ?? [],
1694
- imports: [],
1695
- exports: []
1696
- });
1697
- this.fileManager.add(fileNode);
1698
- }
1699
- };
1700
- return hooks["kubb:plugin:setup"](pluginCtx);
1701
- };
1702
- this.hooks.on("kubb:plugin:setup", setupHandler);
1703
- this.#trackHookListener("kubb:plugin:setup", setupHandler);
1704
- }
1705
- for (const [event, handler] of Object.entries(hooks)) {
1706
- if (event === "kubb:plugin:setup" || !handler) continue;
1707
- this.hooks.on(event, handler);
1708
- this.#trackHookListener(event, handler);
1709
- }
1710
- }
1711
- /**
1712
- * Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
1713
- * can configure generators, resolvers, transformers and renderers before `buildStart` runs.
1714
- *
1715
- * Call this once from `safeBuild` before the plugin execution loop begins.
1716
- */
1717
- async emitSetupHooks() {
1718
- await this.hooks.emit("kubb:plugin:setup", {
1719
- config: this.config,
1720
- addGenerator: () => {},
1721
- setResolver: () => {},
1722
- setTransformer: () => {},
1723
- setRenderer: () => {},
1724
- setOptions: () => {},
1725
- injectFile: () => {},
1726
- updateConfig: () => {},
1727
- options: {}
1728
- });
1729
- }
1730
- /**
1731
- * Registers a generator for the given plugin on the shared event emitter.
1732
- *
1733
- * The generator's `schema`, `operation`, and `operations` methods are registered as
1734
- * listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
1735
- * respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
1736
- * so that generators from different plugins do not cross-fire.
1737
- *
1738
- * The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
1739
- * Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
1740
- * declares a renderer.
1741
- *
1742
- * Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
1743
- */
1744
- registerGenerator(pluginName, gen) {
1745
- const resolveRenderer = () => {
1746
- const plugin = this.plugins.get(pluginName);
1747
- return gen.renderer === null ? void 0 : gen.renderer ?? plugin?.renderer ?? this.config.renderer;
1748
- };
1749
- if (gen.schema) {
1750
- const schemaHandler = async (node, ctx) => {
1751
- if (ctx.plugin.name !== pluginName) return;
1752
- await applyHookResult(await gen.schema(node, ctx), this, resolveRenderer());
1753
- };
1754
- this.hooks.on("kubb:generate:schema", schemaHandler);
1755
- this.#trackHookListener("kubb:generate:schema", schemaHandler);
1756
- }
1757
- if (gen.operation) {
1758
- const operationHandler = async (node, ctx) => {
1759
- if (ctx.plugin.name !== pluginName) return;
1760
- await applyHookResult(await gen.operation(node, ctx), this, resolveRenderer());
1761
- };
1762
- this.hooks.on("kubb:generate:operation", operationHandler);
1763
- this.#trackHookListener("kubb:generate:operation", operationHandler);
1764
- }
1765
- if (gen.operations) {
1766
- const operationsHandler = async (nodes, ctx) => {
1767
- if (ctx.plugin.name !== pluginName) return;
1768
- await applyHookResult(await gen.operations(nodes, ctx), this, resolveRenderer());
1769
- };
1770
- this.hooks.on("kubb:generate:operations", operationsHandler);
1771
- this.#trackHookListener("kubb:generate:operations", operationsHandler);
1772
- }
1773
- this.#pluginsWithEventGenerators.add(pluginName);
1774
- }
1775
- /**
1776
- * Returns `true` when at least one generator was registered for the given plugin
1777
- * via `addGenerator()` in `kubb:plugin:setup` (event-based path).
1778
- *
1779
- * Used by the build loop to decide whether to walk the AST and emit generator events
1780
- * for a plugin that has no static `plugin.generators`.
1781
- */
1782
- hasRegisteredGenerators(pluginName) {
1783
- return this.#pluginsWithEventGenerators.has(pluginName);
1784
- }
1785
- dispose() {
1786
- for (const [event, handlers] of this.#hookListeners) for (const handler of handlers) this.hooks.off(event, handler);
1787
- this.#hookListeners.clear();
1788
- this.#pluginsWithEventGenerators.clear();
1789
- }
1790
- #trackHookListener(event, handler) {
1791
- let handlers = this.#hookListeners.get(event);
1792
- if (!handlers) {
1793
- handlers = /* @__PURE__ */ new Set();
1794
- this.#hookListeners.set(event, handlers);
1795
- }
1796
- handlers.add(handler);
1797
- }
1798
- #createDefaultResolver(pluginName) {
1799
- const existingResolver = this.#defaultResolvers.get(pluginName);
1800
- if (existingResolver) return existingResolver;
1801
- const resolver = defineResolver(() => ({
1802
- name: "default",
1803
- pluginName
1804
- }));
1805
- this.#defaultResolvers.set(pluginName, resolver);
1806
- return resolver;
1807
- }
1808
- setPluginResolver(pluginName, partial) {
1809
- const merged = {
1810
- ...this.#createDefaultResolver(pluginName),
1811
- ...partial
1812
- };
1813
- this.#resolvers.set(pluginName, merged);
1814
- const plugin = this.plugins.get(pluginName);
1815
- if (plugin) plugin.resolver = merged;
1816
- }
1817
- getResolver(pluginName) {
1818
- const dynamicResolver = this.#resolvers.get(pluginName);
1819
- if (dynamicResolver) return dynamicResolver;
1820
- const pluginResolver = this.plugins.get(pluginName)?.resolver;
1821
- if (pluginResolver) return pluginResolver;
1822
- return this.#createDefaultResolver(pluginName);
1823
- }
1824
- getContext(plugin) {
1825
- const driver = this;
1826
- const baseContext = {
1827
- config: driver.config,
1828
- get root() {
1829
- return resolve(driver.config.root, driver.config.output.path);
1830
- },
1831
- getMode(output) {
1832
- return getMode(resolve(driver.config.root, driver.config.output.path, output.path));
1833
- },
1834
- hooks: driver.hooks,
1835
- plugin,
1836
- getPlugin: driver.getPlugin.bind(driver),
1837
- requirePlugin: driver.requirePlugin.bind(driver),
1838
- driver,
1839
- addFile: async (...files) => {
1840
- driver.fileManager.add(...files);
1841
- },
1842
- upsertFile: async (...files) => {
1843
- driver.fileManager.upsert(...files);
1844
- },
1845
- get inputNode() {
1846
- return driver.inputNode;
1847
- },
1848
- get adapter() {
1849
- return driver.adapter;
1850
- },
1851
- get resolver() {
1852
- return driver.getResolver(plugin.name);
1853
- },
1854
- get transformer() {
1855
- return plugin.transformer;
1856
- },
1857
- warn(message) {
1858
- driver.hooks.emit("kubb:warn", message);
1859
- },
1860
- error(error) {
1861
- driver.hooks.emit("kubb:error", typeof error === "string" ? new Error(error) : error);
1862
- },
1863
- info(message) {
1864
- driver.hooks.emit("kubb:info", message);
1865
- },
1866
- openInStudio(options) {
1867
- if (!driver.config.devtools || driver.#studioIsOpen) return;
1868
- if (typeof driver.config.devtools !== "object") throw new Error("Devtools must be an object");
1869
- if (!driver.inputNode || !driver.adapter) throw new Error("adapter is not defined, make sure you have set the parser in kubb.config.ts");
1870
- driver.#studioIsOpen = true;
1871
- const studioUrl = driver.config.devtools?.studioUrl ?? "https://studio.kubb.dev";
1872
- return openInStudio(driver.inputNode, studioUrl, options);
1873
- }
1874
- };
1875
- let mergedExtras = {};
1876
- for (const p of this.plugins.values()) if (typeof p.inject === "function") {
1877
- const result = p.inject.call(baseContext);
1878
- if (result !== null && typeof result === "object") mergedExtras = {
1879
- ...mergedExtras,
1880
- ...result
1881
- };
1882
- }
1883
- return {
1884
- ...baseContext,
1885
- ...mergedExtras
1886
- };
1887
- }
1888
- /**
1889
- * @deprecated use resolvers context instead
1890
- */
1891
- getFile({ name, mode, extname, pluginName, options }) {
1892
- const resolvedName = mode ? mode === "single" ? "" : this.resolveName({
1893
- name,
1894
- pluginName,
1895
- type: "file"
1896
- }) : name;
1897
- const path = this.resolvePath({
1898
- baseName: `${resolvedName}${extname}`,
1899
- mode,
1900
- pluginName,
1901
- options
1902
- });
1903
- if (!path) throw new Error(`Filepath should be defined for resolvedName "${resolvedName}" and pluginName "${pluginName}"`);
1904
- return createFile({
1905
- path,
1906
- baseName: basename(path),
1907
- meta: { pluginName },
1908
- sources: [],
1909
- imports: [],
1910
- exports: []
1911
- });
1912
- }
1913
- /**
1914
- * @deprecated use resolvers context instead
1915
- */
1916
- resolvePath = (params) => {
1917
- const defaultPath = resolve(resolve(this.config.root, this.config.output.path), params.baseName);
1918
- if (params.pluginName) return this.hookForPluginSync({
1919
- pluginName: params.pluginName,
1920
- hookName: "resolvePath",
1921
- parameters: [
1922
- params.baseName,
1923
- params.mode,
1924
- params.options
1925
- ]
1926
- })?.at(0) || defaultPath;
1927
- return this.hookFirstSync({
1928
- hookName: "resolvePath",
1929
- parameters: [
1930
- params.baseName,
1931
- params.mode,
1932
- params.options
1933
- ]
1934
- })?.result || defaultPath;
1935
- };
1936
- /**
1937
- * @deprecated use resolvers context instead
1938
- */
1939
- resolveName = (params) => {
1940
- if (params.pluginName) return transformReservedWord(this.hookForPluginSync({
1941
- pluginName: params.pluginName,
1942
- hookName: "resolveName",
1943
- parameters: [params.name.trim(), params.type]
1944
- })?.at(0) ?? params.name);
1945
- const name = this.hookFirstSync({
1946
- hookName: "resolveName",
1947
- parameters: [params.name.trim(), params.type]
1948
- })?.result;
1949
- return transformReservedWord(name ?? params.name);
1950
- };
1951
- /**
1952
- * Run a specific hookName for plugin x.
1953
- */
1954
- async hookForPlugin({ pluginName, hookName, parameters }) {
1955
- const plugin = this.plugins.get(pluginName);
1956
- if (!plugin) return [null];
1957
- this.hooks.emit("kubb:plugins:hook:progress:start", {
1958
- hookName,
1959
- plugins: [plugin]
1960
- });
1961
- const result = await this.#execute({
1962
- strategy: "hookFirst",
1963
- hookName,
1964
- parameters,
1965
- plugin
1966
- });
1967
- this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
1968
- return [result];
322
+ get template() {
323
+ return this.toTemplateString();
1969
324
  }
1970
- /**
1971
- * Run a specific hookName for plugin x.
325
+ /** Returns the path and its extracted params as a structured `URLObject`, or as a stringified expression when `stringify` is set.
326
+ *
327
+ * @example
328
+ * ```ts
329
+ * new URLPath('/pet/{petId}').object
330
+ * // { url: '/pet/:petId', params: { petId: 'petId' } }
331
+ * ```
1972
332
  */
1973
- hookForPluginSync({ pluginName, hookName, parameters }) {
1974
- const plugin = this.plugins.get(pluginName);
1975
- if (!plugin) return null;
1976
- const result = this.#executeSync({
1977
- strategy: "hookFirst",
1978
- hookName,
1979
- parameters,
1980
- plugin
1981
- });
1982
- return result !== null ? [result] : [];
333
+ get object() {
334
+ return this.toObject();
1983
335
  }
1984
- /**
1985
- * Returns the first non-null result.
336
+ /** Returns a map of path parameter names, or `undefined` when the path has no parameters.
337
+ *
338
+ * @example
339
+ * ```ts
340
+ * new URLPath('/pet/{petId}').params // { petId: 'petId' }
341
+ * new URLPath('/pet').params // undefined
342
+ * ```
1986
343
  */
1987
- async hookFirst({ hookName, parameters, skipped }) {
1988
- const plugins = [];
1989
- for (const plugin of this.plugins.values()) if (hookName in plugin && (skipped ? !skipped.has(plugin) : true)) plugins.push(plugin);
1990
- this.hooks.emit("kubb:plugins:hook:progress:start", {
1991
- hookName,
1992
- plugins
1993
- });
1994
- const result = await hookFirst(plugins.map((plugin) => {
1995
- return async () => {
1996
- const value = await this.#execute({
1997
- strategy: "hookFirst",
1998
- hookName,
1999
- parameters,
2000
- plugin
2001
- });
2002
- return Promise.resolve({
2003
- plugin,
2004
- result: value
2005
- });
2006
- };
2007
- }), hookFirstNullCheck);
2008
- this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
2009
- return result;
344
+ get params() {
345
+ return this.getParams();
346
+ }
347
+ #transformParam(raw) {
348
+ const param = isValidVarName(raw) ? raw : camelCase(raw);
349
+ return this.#options.casing === "camelcase" ? camelCase(param) : param;
2010
350
  }
2011
351
  /**
2012
- * Returns the first non-null result.
352
+ * Iterates over every `{param}` token in `path`, calling `fn` with the raw token and transformed name.
2013
353
  */
2014
- hookFirstSync({ hookName, parameters, skipped }) {
2015
- let parseResult = null;
2016
- for (const plugin of this.plugins.values()) {
2017
- if (!(hookName in plugin)) continue;
2018
- if (skipped?.has(plugin)) continue;
2019
- parseResult = {
2020
- result: this.#executeSync({
2021
- strategy: "hookFirst",
2022
- hookName,
2023
- parameters,
2024
- plugin
2025
- }),
2026
- plugin
2027
- };
2028
- if (parseResult.result != null) break;
354
+ #eachParam(fn) {
355
+ for (const match of this.path.matchAll(/\{([^}]+)\}/g)) {
356
+ const raw = match[1];
357
+ fn(raw, this.#transformParam(raw));
358
+ }
359
+ }
360
+ toObject({ type = "path", replacer, stringify } = {}) {
361
+ const object = {
362
+ url: type === "path" ? this.toURLPath() : this.toTemplateString({ replacer }),
363
+ params: this.getParams()
364
+ };
365
+ if (stringify) {
366
+ if (type === "template") return JSON.stringify(object).replaceAll("'", "").replaceAll(`"`, "");
367
+ if (object.params) return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", "").replaceAll(`"`, "")} }`;
368
+ return `{ url: '${object.url}' }`;
2029
369
  }
2030
- return parseResult;
370
+ return object;
2031
371
  }
2032
372
  /**
2033
- * Runs all plugins in parallel based on `this.plugin` order and `dependencies` settings.
373
+ * Converts the OpenAPI path to a TypeScript template literal string.
374
+ * An optional `replacer` can transform each extracted parameter name before interpolation.
375
+ *
376
+ * @example
377
+ * new URLPath('/pet/{petId}').toTemplateString() // '`/pet/${petId}`'
2034
378
  */
2035
- async hookParallel({ hookName, parameters }) {
2036
- const plugins = [];
2037
- for (const plugin of this.plugins.values()) if (hookName in plugin) plugins.push(plugin);
2038
- this.hooks.emit("kubb:plugins:hook:progress:start", {
2039
- hookName,
2040
- plugins
2041
- });
2042
- const pluginStartTimes = /* @__PURE__ */ new Map();
2043
- const results = await hookParallel(plugins.map((plugin) => {
2044
- return () => {
2045
- pluginStartTimes.set(plugin, performance.now());
2046
- return this.#execute({
2047
- strategy: "hookParallel",
2048
- hookName,
2049
- parameters,
2050
- plugin
2051
- });
2052
- };
2053
- }), this.options.concurrency);
2054
- results.forEach((result, index) => {
2055
- if (isPromiseRejectedResult(result)) {
2056
- const plugin = plugins[index];
2057
- if (plugin) {
2058
- const startTime = pluginStartTimes.get(plugin) ?? performance.now();
2059
- this.hooks.emit("kubb:error", result.reason, {
2060
- plugin,
2061
- hookName,
2062
- strategy: "hookParallel",
2063
- duration: Math.round(performance.now() - startTime),
2064
- parameters
2065
- });
2066
- }
2067
- }
2068
- });
2069
- this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
2070
- return results.reduce((acc, result) => {
2071
- if (result.status === "fulfilled") acc.push(result.value);
2072
- return acc;
2073
- }, []);
379
+ toTemplateString({ prefix = "", replacer } = {}) {
380
+ return `\`${prefix}${this.path.split(/\{([^}]+)\}/).map((part, i) => {
381
+ if (i % 2 === 0) return part;
382
+ const param = this.#transformParam(part);
383
+ return `\${${replacer ? replacer(param) : param}}`;
384
+ }).join("")}\``;
2074
385
  }
2075
386
  /**
2076
- * Execute a lifecycle hook sequentially for all plugins that implement it.
387
+ * Extracts all `{param}` segments from the path and returns them as a key-value map.
388
+ * An optional `replacer` transforms each parameter name in both key and value positions.
389
+ * Returns `undefined` when no path parameters are found.
390
+ *
391
+ * @example
392
+ * ```ts
393
+ * new URLPath('/pet/{petId}/tag/{tagId}').getParams()
394
+ * // { petId: 'petId', tagId: 'tagId' }
395
+ * ```
2077
396
  */
2078
- async hookSeq({ hookName, parameters }) {
2079
- const plugins = [];
2080
- for (const plugin of this.plugins.values()) if (hookName in plugin) plugins.push(plugin);
2081
- this.hooks.emit("kubb:plugins:hook:progress:start", {
2082
- hookName,
2083
- plugins
397
+ getParams(replacer) {
398
+ const params = {};
399
+ this.#eachParam((_raw, param) => {
400
+ const key = replacer ? replacer(param) : param;
401
+ params[key] = key;
2084
402
  });
2085
- await hookSeq(plugins.map((plugin) => {
2086
- return () => this.#execute({
2087
- strategy: "hookSeq",
2088
- hookName,
2089
- parameters,
2090
- plugin
2091
- });
2092
- }));
2093
- this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
2094
- }
2095
- getPlugin(pluginName) {
2096
- return this.plugins.get(pluginName);
2097
- }
2098
- requirePlugin(pluginName) {
2099
- const plugin = this.plugins.get(pluginName);
2100
- if (!plugin) throw new Error(`[kubb] Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`);
2101
- return plugin;
403
+ return Object.keys(params).length > 0 ? params : void 0;
2102
404
  }
2103
- /**
2104
- * Emit hook-processing completion metadata after a plugin hook resolves.
405
+ /** Converts the OpenAPI path to Express-style colon syntax.
406
+ *
407
+ * @example
408
+ * ```ts
409
+ * new URLPath('/pet/{petId}').toURLPath() // '/pet/:petId'
410
+ * ```
2105
411
  */
2106
- #emitProcessingEnd({ startTime, output, strategy, hookName, plugin, parameters }) {
2107
- this.hooks.emit("kubb:plugins:hook:processing:end", {
2108
- duration: Math.round(performance.now() - startTime),
2109
- parameters,
2110
- output,
2111
- strategy,
2112
- hookName,
2113
- plugin
2114
- });
412
+ toURLPath() {
413
+ return this.path.replace(/\{([^}]+)\}/g, ":$1");
2115
414
  }
2116
- #execute({ strategy, hookName, parameters, plugin }) {
2117
- const hook = plugin[hookName];
2118
- if (!hook) return null;
2119
- this.hooks.emit("kubb:plugins:hook:processing:start", {
2120
- strategy,
2121
- hookName,
2122
- parameters,
2123
- plugin
2124
- });
2125
- const startTime = performance.now();
2126
- return (async () => {
2127
- try {
2128
- const output = typeof hook === "function" ? await Promise.resolve(hook.apply(this.getContext(plugin), parameters ?? [])) : hook;
2129
- this.#emitProcessingEnd({
2130
- startTime,
2131
- output,
2132
- strategy,
2133
- hookName,
2134
- plugin,
2135
- parameters
2136
- });
2137
- return output;
2138
- } catch (error) {
2139
- this.hooks.emit("kubb:error", error, {
2140
- plugin,
2141
- hookName,
2142
- strategy,
2143
- duration: Math.round(performance.now() - startTime)
2144
- });
2145
- return null;
2146
- }
2147
- })();
415
+ };
416
+ //#endregion
417
+ //#region src/createAdapter.ts
418
+ /**
419
+ * Creates an adapter factory. Call the returned function with optional options to get the adapter instance.
420
+ *
421
+ * @example
422
+ * export const myAdapter = createAdapter<MyAdapter>((options) => {
423
+ * return {
424
+ * name: 'my-adapter',
425
+ * options,
426
+ * async parse(source) { ... },
427
+ * }
428
+ * })
429
+ *
430
+ * // instantiate
431
+ * const adapter = myAdapter({ validate: true })
432
+ */
433
+ function createAdapter(build) {
434
+ return (options) => build(options ?? {});
435
+ }
436
+ //#endregion
437
+ //#region src/FileProcessor.ts
438
+ function joinSources(file) {
439
+ return file.sources.map((item) => extractStringsFromNodes(item.nodes)).filter(Boolean).join("\n\n");
440
+ }
441
+ /**
442
+ * Converts a single file to a string using the registered parsers.
443
+ * Falls back to joining source values when no matching parser is found.
444
+ */
445
+ var FileProcessor = class {
446
+ #limit = pLimit(100);
447
+ async parse(file, { parsers, extension } = {}) {
448
+ const parseExtName = extension?.[file.extname] || void 0;
449
+ if (!parsers || !file.extname) return joinSources(file);
450
+ const parser = parsers.get(file.extname);
451
+ if (!parser) return joinSources(file);
452
+ return parser.parse(file, { extname: parseExtName });
2148
453
  }
2149
- /**
2150
- * Execute a plugin lifecycle hook synchronously and return its output.
2151
- */
2152
- #executeSync({ strategy, hookName, parameters, plugin }) {
2153
- const hook = plugin[hookName];
2154
- if (!hook) return null;
2155
- this.hooks.emit("kubb:plugins:hook:processing:start", {
2156
- strategy,
2157
- hookName,
2158
- parameters,
2159
- plugin
2160
- });
2161
- const startTime = performance.now();
2162
- try {
2163
- const output = typeof hook === "function" ? hook.apply(this.getContext(plugin), parameters) : hook;
2164
- this.#emitProcessingEnd({
2165
- startTime,
2166
- output,
2167
- strategy,
2168
- hookName,
2169
- plugin,
2170
- parameters
454
+ async run(files, { parsers, mode = "sequential", extension, onStart, onEnd, onUpdate } = {}) {
455
+ await onStart?.(files);
456
+ const total = files.length;
457
+ let processed = 0;
458
+ const processOne = async (file) => {
459
+ const source = await this.parse(file, {
460
+ extension,
461
+ parsers
2171
462
  });
2172
- return output;
2173
- } catch (error) {
2174
- this.hooks.emit("kubb:error", error, {
2175
- plugin,
2176
- hookName,
2177
- strategy,
2178
- duration: Math.round(performance.now() - startTime)
463
+ const currentProcessed = ++processed;
464
+ const percentage = currentProcessed / total * 100;
465
+ await onUpdate?.({
466
+ file,
467
+ source,
468
+ processed: currentProcessed,
469
+ percentage,
470
+ total
2179
471
  });
2180
- return null;
2181
- }
472
+ };
473
+ if (mode === "sequential") for (const file of files) await processOne(file);
474
+ else await Promise.all(files.map((file) => this.#limit(() => processOne(file))));
475
+ await onEnd?.(files);
476
+ return files;
2182
477
  }
2183
478
  };
2184
479
  //#endregion
@@ -2279,7 +574,7 @@ const fsStorage = createStorage(() => ({
2279
574
  }));
2280
575
  //#endregion
2281
576
  //#region package.json
2282
- var version$1 = "5.0.0-alpha.35";
577
+ var version$1 = "5.0.0-alpha.38";
2283
578
  //#endregion
2284
579
  //#region src/utils/diagnostics.ts
2285
580
  /**
@@ -2394,14 +689,14 @@ var TreeNode = class TreeNode {
2394
689
  name: filteredTree.name,
2395
690
  path: filteredTree.path,
2396
691
  file: filteredTree.file,
2397
- type: getMode(filteredTree.path)
692
+ type: PluginDriver.getMode(filteredTree.path)
2398
693
  });
2399
694
  const recurse = (node, item) => {
2400
695
  const subNode = node.addChild({
2401
696
  name: item.name,
2402
697
  path: item.path,
2403
698
  file: item.file,
2404
- type: getMode(item.path)
699
+ type: PluginDriver.getMode(item.path)
2405
700
  });
2406
701
  if (item.children?.length) item.children?.forEach((child) => {
2407
702
  recurse(subNode, child);
@@ -2986,30 +1281,6 @@ function createKubb(options) {
2986
1281
  return instance;
2987
1282
  }
2988
1283
  //#endregion
2989
- //#region src/createPlugin.ts
2990
- /**
2991
- * Creates a plugin factory. Call the returned function with optional options to get the plugin instance.
2992
- *
2993
- * @example
2994
- * ```ts
2995
- * export const myPlugin = createPlugin<MyPlugin>((options) => {
2996
- * return {
2997
- * name: 'my-plugin',
2998
- * get options() { return options },
2999
- * resolvePath(baseName) { ... },
3000
- * resolveName(name, type) { ... },
3001
- * }
3002
- * })
3003
- *
3004
- * // instantiate
3005
- * const plugin = myPlugin({ output: { path: 'src/gen' } })
3006
- * ```
3007
- * @deprecated use definePlugin instead
3008
- */
3009
- function createPlugin(build) {
3010
- return (options) => build(options ?? {});
3011
- }
3012
- //#endregion
3013
1284
  //#region src/createRenderer.ts
3014
1285
  /**
3015
1286
  * Creates a renderer factory for use in generator definitions.
@@ -3143,245 +1414,6 @@ const memoryStorage = createStorage(() => {
3143
1414
  };
3144
1415
  });
3145
1416
  //#endregion
3146
- //#region src/utils/formatters.ts
3147
- /**
3148
- * Returns `true` when the given formatter is installed and callable.
3149
- *
3150
- * Availability is detected by running `<formatter> --version` and checking
3151
- * that the process exits without error.
3152
- */
3153
- async function isFormatterAvailable(formatter) {
3154
- try {
3155
- await x(formatter, ["--version"], { nodeOptions: { stdio: "ignore" } });
3156
- return true;
3157
- } catch {
3158
- return false;
3159
- }
3160
- }
3161
- /**
3162
- * Detects the first available code formatter on the current system.
3163
- *
3164
- * - Checks in preference order: `biome`, `oxfmt`, `prettier`.
3165
- * - Returns `null` when none are found.
3166
- *
3167
- * @example
3168
- * ```ts
3169
- * const formatter = await detectFormatter()
3170
- * if (formatter) {
3171
- * console.log(`Using ${formatter} for formatting`)
3172
- * }
3173
- * ```
3174
- */
3175
- async function detectFormatter() {
3176
- const formatterNames = new Set([
3177
- "biome",
3178
- "oxfmt",
3179
- "prettier"
3180
- ]);
3181
- for (const formatter of formatterNames) if (await isFormatterAvailable(formatter)) return formatter;
3182
- return null;
3183
- }
3184
- //#endregion
3185
- //#region src/utils/getFunctionParams.ts
3186
- function order(items) {
3187
- return sortBy(items.filter(Boolean), ([_key, item]) => {
3188
- if (item?.children) return 0;
3189
- if (item?.optional) return 1;
3190
- if (item?.default) return 2;
3191
- return 0;
3192
- });
3193
- }
3194
- function parseChild(key, item, options) {
3195
- const entries = order(Object.entries(item.children));
3196
- const types = [];
3197
- const names = [];
3198
- const optional = entries.every(([_key, item]) => item?.optional || !!item?.default);
3199
- entries.forEach(([key, entryItem]) => {
3200
- if (entryItem) {
3201
- const name = parseItem(key, {
3202
- ...entryItem,
3203
- type: void 0
3204
- }, options);
3205
- if (entryItem.children) {
3206
- const subTypes = Object.entries(entryItem.children).map(([key]) => {
3207
- return key;
3208
- }).join(", ");
3209
- if (subTypes) names.push(`${name}: { ${subTypes} }`);
3210
- else names.push(name);
3211
- } else if (options.type === "call" && options.transformName) names.push(`${key}: ${name}`);
3212
- else names.push(name);
3213
- if (entries.some(([_key, item]) => item?.type)) types.push(parseItem(key, {
3214
- ...entryItem,
3215
- default: void 0
3216
- }, options));
3217
- }
3218
- });
3219
- const name = item.mode === "inline" ? key : names.length ? `{ ${names.join(", ")} }` : void 0;
3220
- const type = item.type ? item.type : types.length ? `{ ${types.join("; ")} }` : void 0;
3221
- if (!name) return null;
3222
- return parseItem(name, {
3223
- type,
3224
- default: item.default,
3225
- optional: !item.default ? optional : void 0
3226
- }, options);
3227
- }
3228
- function parseItem(name, item, options) {
3229
- const acc = [];
3230
- const transformedName = options.transformName ? options.transformName(name) : name;
3231
- const transformedType = options.transformType && item.type ? options.transformType(item.type) : item.type;
3232
- if (options.type === "object") return transformedName;
3233
- if (options.type === "objectValue") return item.value ? `${transformedName}: ${item.value}` : transformedName;
3234
- if (item.type && options.type === "constructor") if (item.optional) if (transformedName.startsWith("{")) acc.push(`${transformedName}: ${transformedType} = {}`);
3235
- else acc.push(`${transformedName}?: ${transformedType}`);
3236
- else acc.push(`${transformedName}: ${transformedType}${item.default ? ` = ${item.default}` : ""}`);
3237
- else if (item.default && options.type === "constructor") acc.push(`${transformedName} = ${item.default}`);
3238
- else if (item.value) acc.push(`${transformedName} : ${item.value}`);
3239
- else if (item.mode === "inlineSpread") acc.push(`... ${transformedName}`);
3240
- else acc.push(transformedName);
3241
- return acc[0];
3242
- }
3243
- function getFunctionParams(params, options) {
3244
- return order(Object.entries(params)).reduce((acc, [key, item]) => {
3245
- if (!item) return acc;
3246
- if (item.children) {
3247
- if (Object.keys(item.children).length === 0) return acc;
3248
- if (item.mode === "inlineSpread") return [...acc, getFunctionParams(item.children, options)];
3249
- const parsedItem = parseChild(key, item, options);
3250
- if (!parsedItem) return acc;
3251
- return [...acc, parsedItem];
3252
- }
3253
- const parsedItem = parseItem(key, item, options);
3254
- return [...acc, parsedItem];
3255
- }, []).join(", ");
3256
- }
3257
- /**
3258
- * @deprecated use @kubb/ast
3259
- */
3260
- function createFunctionParams(params) {
3261
- return params;
3262
- }
3263
- /**
3264
- * @deprecated use @kubb/ast
3265
- */
3266
- var FunctionParams = class FunctionParams {
3267
- #params;
3268
- static factory(params) {
3269
- return new FunctionParams(params);
3270
- }
3271
- constructor(params) {
3272
- this.#params = params;
3273
- }
3274
- get params() {
3275
- return this.#params;
3276
- }
3277
- get flatParams() {
3278
- const flatter = (acc, [key, item]) => {
3279
- if (item?.children) return Object.entries(item.children).reduce(flatter, acc);
3280
- if (item) acc[key] = item;
3281
- return acc;
3282
- };
3283
- return Object.entries(this.#params).reduce(flatter, {});
3284
- }
3285
- toCall({ transformName, transformType } = {}) {
3286
- return getFunctionParams(this.#params, {
3287
- type: "call",
3288
- transformName,
3289
- transformType
3290
- });
3291
- }
3292
- toObject() {
3293
- return getFunctionParams(this.#params, { type: "object" });
3294
- }
3295
- toObjectValue() {
3296
- return getFunctionParams(this.#params, { type: "objectValue" });
3297
- }
3298
- toConstructor() {
3299
- return getFunctionParams(this.#params, { type: "constructor" });
3300
- }
3301
- };
3302
- //#endregion
3303
- //#region src/utils/linters.ts
3304
- /**
3305
- * Returns `true` when the given linter is installed and callable.
3306
- *
3307
- * Availability is detected by running `<linter> --version` and checking
3308
- * that the process exits without error.
3309
- */
3310
- async function isLinterAvailable(linter) {
3311
- try {
3312
- await x(linter, ["--version"], { nodeOptions: { stdio: "ignore" } });
3313
- return true;
3314
- } catch {
3315
- return false;
3316
- }
3317
- }
3318
- /**
3319
- * Detects the first available linter on the current system.
3320
- *
3321
- * - Checks in preference order: `biome`, `oxlint`, `eslint`.
3322
- * - Returns `null` when none are found.
3323
- *
3324
- * @example
3325
- * ```ts
3326
- * const linter = await detectLinter()
3327
- * if (linter) {
3328
- * console.log(`Using ${linter} for linting`)
3329
- * }
3330
- * ```
3331
- */
3332
- async function detectLinter() {
3333
- const linterNames = new Set([
3334
- "biome",
3335
- "oxlint",
3336
- "eslint"
3337
- ]);
3338
- for (const linter of linterNames) if (await isLinterAvailable(linter)) return linter;
3339
- return null;
3340
- }
3341
- //#endregion
3342
- //#region src/utils/packageJSON.ts
3343
- function getPackageJSONSync(cwd) {
3344
- const pkgPath = findPackageJSON(cwd);
3345
- if (!pkgPath) return null;
3346
- return JSON.parse(readSync(pkgPath));
3347
- }
3348
- function match(packageJSON, dependency) {
3349
- const dependencies = {
3350
- ...packageJSON.dependencies || {},
3351
- ...packageJSON.devDependencies || {}
3352
- };
3353
- if (typeof dependency === "string" && dependencies[dependency]) return dependencies[dependency];
3354
- const matched = Object.keys(dependencies).find((dep) => dep.match(dependency));
3355
- return matched ? dependencies[matched] ?? null : null;
3356
- }
3357
- function getVersionSync(dependency, cwd) {
3358
- const packageJSON = getPackageJSONSync(cwd);
3359
- return packageJSON ? match(packageJSON, dependency) : null;
3360
- }
3361
- /**
3362
- * Returns `true` when the nearest `package.json` declares a dependency that
3363
- * satisfies the given semver range.
3364
- *
3365
- * - Searches both `dependencies` and `devDependencies`.
3366
- * - Accepts a string package name or a `RegExp` to match scoped/pattern packages.
3367
- * - Uses `semver.satisfies` for range comparison; returns `false` when the
3368
- * version string cannot be coerced into a valid semver.
3369
- *
3370
- * @example
3371
- * ```ts
3372
- * satisfiesDependency('react', '>=18') // true when react@18.x is installed
3373
- * satisfiesDependency(/^@tanstack\//, '>=5') // true when any @tanstack/* >=5 is found
3374
- * ```
3375
- */
3376
- function satisfiesDependency(dependency, version, cwd) {
3377
- const packageVersion = getVersionSync(dependency, cwd);
3378
- if (!packageVersion) return false;
3379
- if (packageVersion === version) return true;
3380
- const semVer = coerce(packageVersion);
3381
- if (!semVer) return false;
3382
- return satisfies(semVer, version);
3383
- }
3384
- //#endregion
3385
- export { AsyncEventEmitter, FunctionParams, PluginDriver, URLPath, ast, buildDefaultBanner, composeTransformers, createAdapter, createFunctionParams, createKubb, createPlugin, createRenderer, createStorage, defaultResolveBanner, defaultResolveFile, defaultResolveFooter, defaultResolveOptions, defaultResolvePath, defineGenerator, defineLogger, defineParser, definePlugin, definePrinter, defineResolver, detectFormatter, detectLinter, formatters, fsStorage, getBarrelFiles, getFunctionParams, getMode, isInputPath, linters, logLevel, memoryStorage, satisfiesDependency };
1417
+ export { AsyncEventEmitter, FileManager, FileProcessor, PluginDriver, URLPath, ast, createAdapter, createKubb, createRenderer, createStorage, defineGenerator, defineLogger, defineParser, definePlugin, defineResolver, fsStorage, isInputPath, logLevel, memoryStorage };
3386
1418
 
3387
1419
  //# sourceMappingURL=index.js.map