@kubb/core 5.0.0-beta.3 → 5.0.0-beta.31

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.
Files changed (46) hide show
  1. package/README.md +8 -38
  2. package/dist/KubbDriver-CFx2DdhF.js +2131 -0
  3. package/dist/KubbDriver-CFx2DdhF.js.map +1 -0
  4. package/dist/KubbDriver-vyD7F0Ip.cjs +2252 -0
  5. package/dist/KubbDriver-vyD7F0Ip.cjs.map +1 -0
  6. package/dist/{types-CC09VtBt.d.ts → createKubb-6zii1jo-.d.ts} +1610 -1257
  7. package/dist/index.cjs +351 -1125
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +3 -186
  10. package/dist/index.js +341 -1119
  11. package/dist/index.js.map +1 -1
  12. package/dist/mocks.cjs +30 -21
  13. package/dist/mocks.cjs.map +1 -1
  14. package/dist/mocks.d.ts +5 -5
  15. package/dist/mocks.js +29 -20
  16. package/dist/mocks.js.map +1 -1
  17. package/package.json +6 -18
  18. package/src/FileManager.ts +78 -61
  19. package/src/FileProcessor.ts +48 -38
  20. package/src/KubbDriver.ts +930 -0
  21. package/src/constants.ts +11 -6
  22. package/src/createAdapter.ts +113 -17
  23. package/src/createKubb.ts +1039 -478
  24. package/src/createRenderer.ts +58 -27
  25. package/src/createStorage.ts +36 -23
  26. package/src/defineGenerator.ts +127 -15
  27. package/src/defineLogger.ts +66 -7
  28. package/src/defineMiddleware.ts +19 -17
  29. package/src/defineParser.ts +30 -13
  30. package/src/definePlugin.ts +329 -14
  31. package/src/defineResolver.ts +365 -167
  32. package/src/devtools.ts +8 -1
  33. package/src/index.ts +2 -2
  34. package/src/mocks.ts +11 -14
  35. package/src/storages/fsStorage.ts +13 -37
  36. package/src/types.ts +48 -1292
  37. package/dist/PluginDriver-BXibeQk-.cjs +0 -1036
  38. package/dist/PluginDriver-BXibeQk-.cjs.map +0 -1
  39. package/dist/PluginDriver-DV3p2Hky.js +0 -945
  40. package/dist/PluginDriver-DV3p2Hky.js.map +0 -1
  41. package/src/Kubb.ts +0 -300
  42. package/src/PluginDriver.ts +0 -424
  43. package/src/renderNode.ts +0 -35
  44. package/src/utils/diagnostics.ts +0 -18
  45. package/src/utils/isInputPath.ts +0 -10
  46. package/src/utils/packageJSON.ts +0 -99
@@ -0,0 +1,2252 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", {
5
+ value,
6
+ configurable: true
7
+ });
8
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
9
+ var __getOwnPropNames = Object.getOwnPropertyNames;
10
+ var __getProtoOf = Object.getPrototypeOf;
11
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
14
+ key = keys[i];
15
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
16
+ get: ((k) => from[k]).bind(null, key),
17
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
18
+ });
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+ //#endregion
27
+ let node_events = require("node:events");
28
+ let node_path = require("node:path");
29
+ node_path = __toESM(node_path, 1);
30
+ let _kubb_ast = require("@kubb/ast");
31
+ let fflate = require("fflate");
32
+ let tinyexec = require("tinyexec");
33
+ //#region ../../internals/utils/src/errors.ts
34
+ /**
35
+ * Thrown when one or more errors occur during a Kubb build.
36
+ * Carries the full list of underlying errors on `errors`.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * throw new BuildError('Build failed', { errors: [err1, err2] })
41
+ * ```
42
+ */
43
+ var BuildError = class extends Error {
44
+ errors;
45
+ constructor(message, options) {
46
+ super(message, { cause: options.cause });
47
+ this.name = "BuildError";
48
+ this.errors = options.errors;
49
+ }
50
+ };
51
+ /**
52
+ * Coerces an unknown thrown value to an `Error` instance.
53
+ * Returns the value as-is when it is already an `Error`; otherwise wraps it with `String(value)`.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * try { ... } catch(err) {
58
+ * throw new BuildError('Build failed', { cause: toError(err), errors: [] })
59
+ * }
60
+ * ```
61
+ */
62
+ function toError(value) {
63
+ return value instanceof Error ? value : new Error(String(value));
64
+ }
65
+ //#endregion
66
+ //#region ../../internals/utils/src/asyncEventEmitter.ts
67
+ /**
68
+ * Typed `EventEmitter` that awaits all async listeners before resolving.
69
+ * Wraps Node's `EventEmitter` with full TypeScript event-map inference.
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * const emitter = new AsyncEventEmitter<{ build: [name: string] }>()
74
+ * emitter.on('build', async (name) => { console.log(name) })
75
+ * await emitter.emit('build', 'petstore') // all listeners awaited
76
+ * ```
77
+ */
78
+ var AsyncEventEmitter = class {
79
+ /**
80
+ * Maximum number of listeners per event before Node emits a memory-leak warning.
81
+ * @default 10
82
+ */
83
+ constructor(maxListener = 10) {
84
+ this.#emitter.setMaxListeners(maxListener);
85
+ }
86
+ #emitter = new node_events.EventEmitter();
87
+ /**
88
+ * Emits `eventName` and awaits all registered listeners sequentially.
89
+ * Throws if any listener rejects, wrapping the cause with the event name and serialized arguments.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * await emitter.emit('build', 'petstore')
94
+ * ```
95
+ */
96
+ emit(eventName, ...eventArgs) {
97
+ const listeners = this.#emitter.listeners(eventName);
98
+ if (listeners.length === 0) return;
99
+ return this.#emitAll(eventName, listeners, eventArgs);
100
+ }
101
+ async #emitAll(eventName, listeners, eventArgs) {
102
+ for (const listener of listeners) try {
103
+ await listener(...eventArgs);
104
+ } catch (err) {
105
+ let serializedArgs;
106
+ try {
107
+ serializedArgs = JSON.stringify(eventArgs);
108
+ } catch {
109
+ serializedArgs = String(eventArgs);
110
+ }
111
+ throw new Error(`Error in async listener for "${eventName}" with eventArgs ${serializedArgs}`, { cause: toError(err) });
112
+ }
113
+ }
114
+ /**
115
+ * Registers a persistent listener for `eventName`.
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * emitter.on('build', async (name) => { console.log(name) })
120
+ * ```
121
+ */
122
+ on(eventName, handler) {
123
+ this.#emitter.on(eventName, handler);
124
+ }
125
+ /**
126
+ * Registers a one-shot listener that removes itself after the first invocation.
127
+ *
128
+ * @example
129
+ * ```ts
130
+ * emitter.onOnce('build', async (name) => { console.log(name) })
131
+ * ```
132
+ */
133
+ onOnce(eventName, handler) {
134
+ const wrapper = (...args) => {
135
+ this.off(eventName, wrapper);
136
+ return handler(...args);
137
+ };
138
+ this.on(eventName, wrapper);
139
+ }
140
+ /**
141
+ * Removes a previously registered listener.
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * emitter.off('build', handler)
146
+ * ```
147
+ */
148
+ off(eventName, handler) {
149
+ this.#emitter.off(eventName, handler);
150
+ }
151
+ /**
152
+ * Returns the number of listeners registered for `eventName`.
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * emitter.on('build', handler)
157
+ * emitter.listenerCount('build') // 1
158
+ * ```
159
+ */
160
+ listenerCount(eventName) {
161
+ return this.#emitter.listenerCount(eventName);
162
+ }
163
+ /**
164
+ * Removes all listeners from every event channel.
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * emitter.removeAll()
169
+ * ```
170
+ */
171
+ removeAll() {
172
+ this.#emitter.removeAllListeners();
173
+ }
174
+ };
175
+ //#endregion
176
+ //#region ../../internals/utils/src/casing.ts
177
+ /**
178
+ * Shared implementation for camelCase and PascalCase conversion.
179
+ * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
180
+ * and capitalizes each word according to `pascal`.
181
+ *
182
+ * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
183
+ */
184
+ function toCamelOrPascal(text, pascal) {
185
+ 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) => {
186
+ if (word.length > 1 && word === word.toUpperCase()) return word;
187
+ if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
188
+ return word.charAt(0).toUpperCase() + word.slice(1);
189
+ }).join("").replace(/[^a-zA-Z0-9]/g, "");
190
+ }
191
+ /**
192
+ * Splits `text` on `.` and applies `transformPart` to each segment.
193
+ * The last segment receives `isLast = true`, all earlier segments receive `false`.
194
+ * Segments are joined with `/` to form a file path.
195
+ *
196
+ * Only splits on dots followed by a letter so that version numbers
197
+ * embedded in operationIds (e.g. `v2025.0`) are kept intact.
198
+ *
199
+ * Empty segments are filtered before joining. They arise when the text starts with
200
+ * a dot followed immediately by a letter (e.g. `..Schema` splits into `['..', 'Schema']`
201
+ * and `'..'` transforms to an empty string). Without this filter the join would produce
202
+ * a leading `/`, which `path.resolve` would interpret as an absolute path, allowing
203
+ * generated files to escape the configured output directory.
204
+ */
205
+ function applyToFileParts(text, transformPart) {
206
+ const parts = text.split(/\.(?=[a-zA-Z])/);
207
+ return parts.map((part, i) => transformPart(part, i === parts.length - 1)).filter(Boolean).join("/");
208
+ }
209
+ /**
210
+ * Converts `text` to camelCase.
211
+ * When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
212
+ *
213
+ * @example
214
+ * camelCase('hello-world') // 'helloWorld'
215
+ * camelCase('pet.petId', { isFile: true }) // 'pet/petId'
216
+ */
217
+ function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
218
+ if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
219
+ prefix,
220
+ suffix
221
+ } : {}));
222
+ return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
223
+ }
224
+ /**
225
+ * Converts `text` to PascalCase.
226
+ * When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
227
+ *
228
+ * @example
229
+ * pascalCase('hello-world') // 'HelloWorld'
230
+ * pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
231
+ */
232
+ function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
233
+ if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
234
+ prefix,
235
+ suffix
236
+ }) : camelCase(part));
237
+ return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
238
+ }
239
+ //#endregion
240
+ //#region ../../internals/utils/src/time.ts
241
+ /**
242
+ * Calculates elapsed time in milliseconds from a high-resolution `process.hrtime` start time.
243
+ * Rounds to 2 decimal places for sub-millisecond precision without noise.
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * const start = process.hrtime()
248
+ * doWork()
249
+ * getElapsedMs(start) // 42.35
250
+ * ```
251
+ */
252
+ function getElapsedMs(hrStart) {
253
+ const [seconds, nanoseconds] = process.hrtime(hrStart);
254
+ const ms = seconds * 1e3 + nanoseconds / 1e6;
255
+ return Math.round(ms * 100) / 100;
256
+ }
257
+ /**
258
+ * Converts a millisecond duration into a human-readable string (`ms`, `s`, or `m s`).
259
+ *
260
+ * @example
261
+ * ```ts
262
+ * formatMs(250) // '250ms'
263
+ * formatMs(1500) // '1.50s'
264
+ * formatMs(90000) // '1m 30.0s'
265
+ * ```
266
+ */
267
+ function formatMs(ms) {
268
+ if (ms >= 6e4) return `${Math.floor(ms / 6e4)}m ${(ms % 6e4 / 1e3).toFixed(1)}s`;
269
+ if (ms >= 1e3) return `${(ms / 1e3).toFixed(2)}s`;
270
+ return `${Math.round(ms)}ms`;
271
+ }
272
+ //#endregion
273
+ //#region ../../internals/utils/src/promise.ts
274
+ function* chunks(arr, size) {
275
+ for (let i = 0; i < arr.length; i += size) yield arr.slice(i, i + size);
276
+ }
277
+ /**
278
+ * Slices `source` into batches of `concurrency` items and awaits `process` for each batch.
279
+ * Accepts both plain arrays (sync) and `AsyncIterable` (streaming).
280
+ *
281
+ * `process` controls whether items inside a batch run in parallel; this helper only
282
+ * controls batch size and per-batch flushing.
283
+ *
284
+ * @example
285
+ * ```ts
286
+ * // parallel dispatch inside each batch
287
+ * await forBatches(schemas, (batch) => Promise.all(batch.map(process)), { concurrency: 8 })
288
+ *
289
+ * // async iterable with a flush after every batch
290
+ * await forBatches(stream.schemas, (batch) => dispatch(batch), { concurrency: 8, flush })
291
+ * ```
292
+ */
293
+ async function forBatches(source, process, options) {
294
+ const { concurrency, flush } = options;
295
+ if (Array.isArray(source)) {
296
+ for (const batch of chunks(source, concurrency)) {
297
+ await process(batch);
298
+ if (flush) await flush();
299
+ }
300
+ return;
301
+ }
302
+ const batch = [];
303
+ for await (const item of source) {
304
+ batch.push(item);
305
+ if (batch.length >= concurrency) {
306
+ await process(batch.splice(0));
307
+ if (flush) await flush();
308
+ }
309
+ }
310
+ if (batch.length > 0) {
311
+ await process(batch.splice(0));
312
+ if (flush) await flush();
313
+ }
314
+ }
315
+ /** Returns `true` when `result` is a thenable `Promise`.
316
+ *
317
+ * @example
318
+ * ```ts
319
+ * isPromise(Promise.resolve(1)) // true
320
+ * isPromise(42) // false
321
+ * ```
322
+ */
323
+ function isPromise(result) {
324
+ return result !== null && result !== void 0 && typeof result["then"] === "function";
325
+ }
326
+ /**
327
+ * Wraps `factory` with a keyed cache backed by the provided store.
328
+ *
329
+ * Pass a `WeakMap` for object keys (results are GC-eligible when the key is
330
+ * collected) or a `Map` for primitive keys. For multi-argument functions,
331
+ * nest two `memoize` calls — the outer keyed by the first argument, the
332
+ * inner (created once per outer miss) keyed by the second.
333
+ *
334
+ * Because the cache is owned by the caller, it can be shared, inspected, or
335
+ * cleared independently of the memoized function.
336
+ *
337
+ * @example Single WeakMap key
338
+ * ```ts
339
+ * const cache = new WeakMap<SchemaNode, Set<string>>()
340
+ * const getRefs = memoize(cache, (node) => collectRefs(node))
341
+ * ```
342
+ *
343
+ * @example Single Map key (primitive)
344
+ * ```ts
345
+ * const cache = new Map<string, Resolver>()
346
+ * const getResolver = memoize(cache, (name) => buildResolver(name))
347
+ * ```
348
+ *
349
+ * @example Two-level (object + primitive)
350
+ * ```ts
351
+ * const outer = new WeakMap<Params[], Map<string, Params[]>>()
352
+ * const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))
353
+ * fn(params)('camelcase')
354
+ * ```
355
+ */
356
+ function memoize(store, factory) {
357
+ return (key) => {
358
+ if (store.has(key)) return store.get(key);
359
+ const value = factory(key);
360
+ store.set(key, value);
361
+ return value;
362
+ };
363
+ }
364
+ /**
365
+ * Wraps a plain array in a reusable `AsyncIterable`.
366
+ * Each `[Symbol.asyncIterator]()` call returns a fresh generator so the
367
+ * iterable can be consumed multiple times (e.g. once per plugin pre-scan).
368
+ *
369
+ * @example
370
+ * ```ts
371
+ * const stream = arrayToAsyncIterable([1, 2, 3])
372
+ * for await (const n of stream) console.log(n) // 1, 2, 3
373
+ * ```
374
+ */
375
+ function arrayToAsyncIterable(arr) {
376
+ return { [Symbol.asyncIterator]() {
377
+ return (async function* () {
378
+ yield* arr;
379
+ })();
380
+ } };
381
+ }
382
+ //#endregion
383
+ //#region ../../internals/utils/src/reserved.ts
384
+ /**
385
+ * JavaScript and Java reserved words.
386
+ * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
387
+ */
388
+ const reservedWords = new Set([
389
+ "abstract",
390
+ "arguments",
391
+ "boolean",
392
+ "break",
393
+ "byte",
394
+ "case",
395
+ "catch",
396
+ "char",
397
+ "class",
398
+ "const",
399
+ "continue",
400
+ "debugger",
401
+ "default",
402
+ "delete",
403
+ "do",
404
+ "double",
405
+ "else",
406
+ "enum",
407
+ "eval",
408
+ "export",
409
+ "extends",
410
+ "false",
411
+ "final",
412
+ "finally",
413
+ "float",
414
+ "for",
415
+ "function",
416
+ "goto",
417
+ "if",
418
+ "implements",
419
+ "import",
420
+ "in",
421
+ "instanceof",
422
+ "int",
423
+ "interface",
424
+ "let",
425
+ "long",
426
+ "native",
427
+ "new",
428
+ "null",
429
+ "package",
430
+ "private",
431
+ "protected",
432
+ "public",
433
+ "return",
434
+ "short",
435
+ "static",
436
+ "super",
437
+ "switch",
438
+ "synchronized",
439
+ "this",
440
+ "throw",
441
+ "throws",
442
+ "transient",
443
+ "true",
444
+ "try",
445
+ "typeof",
446
+ "var",
447
+ "void",
448
+ "volatile",
449
+ "while",
450
+ "with",
451
+ "yield",
452
+ "Array",
453
+ "Date",
454
+ "hasOwnProperty",
455
+ "Infinity",
456
+ "isFinite",
457
+ "isNaN",
458
+ "isPrototypeOf",
459
+ "length",
460
+ "Math",
461
+ "name",
462
+ "NaN",
463
+ "Number",
464
+ "Object",
465
+ "prototype",
466
+ "String",
467
+ "toString",
468
+ "undefined",
469
+ "valueOf"
470
+ ]);
471
+ /**
472
+ * Returns `true` when `name` is a syntactically valid JavaScript variable name.
473
+ *
474
+ * @example
475
+ * ```ts
476
+ * isValidVarName('status') // true
477
+ * isValidVarName('class') // false (reserved word)
478
+ * isValidVarName('42foo') // false (starts with digit)
479
+ * ```
480
+ */
481
+ function isValidVarName(name) {
482
+ if (!name || reservedWords.has(name)) return false;
483
+ return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
484
+ }
485
+ //#endregion
486
+ //#region ../../internals/utils/src/urlPath.ts
487
+ /**
488
+ * Parses and transforms an OpenAPI/Swagger path string into various URL formats.
489
+ *
490
+ * @example
491
+ * const p = new URLPath('/pet/{petId}')
492
+ * p.URL // '/pet/:petId'
493
+ * p.template // '`/pet/${petId}`'
494
+ */
495
+ var URLPath = class {
496
+ /**
497
+ * The raw OpenAPI/Swagger path string, e.g. `/pet/{petId}`.
498
+ */
499
+ path;
500
+ #options;
501
+ constructor(path, options = {}) {
502
+ this.path = path;
503
+ this.#options = options;
504
+ }
505
+ /** Converts the OpenAPI path to Express-style colon syntax, e.g. `/pet/{petId}` → `/pet/:petId`.
506
+ *
507
+ * @example
508
+ * ```ts
509
+ * new URLPath('/pet/{petId}').URL // '/pet/:petId'
510
+ * ```
511
+ */
512
+ get URL() {
513
+ return this.toURLPath();
514
+ }
515
+ /** Returns `true` when `path` is a fully-qualified URL (e.g. starts with `https://`).
516
+ *
517
+ * @example
518
+ * ```ts
519
+ * new URLPath('https://petstore.swagger.io/v2/pet').isURL // true
520
+ * new URLPath('/pet/{petId}').isURL // false
521
+ * ```
522
+ */
523
+ get isURL() {
524
+ try {
525
+ return !!new URL(this.path).href;
526
+ } catch {
527
+ return false;
528
+ }
529
+ }
530
+ /**
531
+ * Converts the OpenAPI path to a TypeScript template literal string.
532
+ *
533
+ * @example
534
+ * new URLPath('/pet/{petId}').template // '`/pet/${petId}`'
535
+ * new URLPath('/account/monetary-accountID').template // '`/account/${monetaryAccountId}`'
536
+ */
537
+ get template() {
538
+ return this.toTemplateString();
539
+ }
540
+ /** Returns the path and its extracted params as a structured `URLObject`, or as a stringified expression when `stringify` is set.
541
+ *
542
+ * @example
543
+ * ```ts
544
+ * new URLPath('/pet/{petId}').object
545
+ * // { url: '/pet/:petId', params: { petId: 'petId' } }
546
+ * ```
547
+ */
548
+ get object() {
549
+ return this.toObject();
550
+ }
551
+ /** Returns a map of path parameter names, or `undefined` when the path has no parameters.
552
+ *
553
+ * @example
554
+ * ```ts
555
+ * new URLPath('/pet/{petId}').params // { petId: 'petId' }
556
+ * new URLPath('/pet').params // undefined
557
+ * ```
558
+ */
559
+ get params() {
560
+ return this.getParams();
561
+ }
562
+ #transformParam(raw) {
563
+ const param = isValidVarName(raw) ? raw : camelCase(raw);
564
+ return this.#options.casing === "camelcase" ? camelCase(param) : param;
565
+ }
566
+ /**
567
+ * Iterates over every `{param}` token in `path`, calling `fn` with the raw token and transformed name.
568
+ */
569
+ #eachParam(fn) {
570
+ for (const match of this.path.matchAll(/\{([^}]+)\}/g)) {
571
+ const raw = match[1];
572
+ fn(raw, this.#transformParam(raw));
573
+ }
574
+ }
575
+ toObject({ type = "path", replacer, stringify } = {}) {
576
+ const object = {
577
+ url: type === "path" ? this.toURLPath() : this.toTemplateString({ replacer }),
578
+ params: this.getParams()
579
+ };
580
+ if (stringify) {
581
+ if (type === "template") return JSON.stringify(object).replaceAll("'", "").replaceAll(`"`, "");
582
+ if (object.params) return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", "").replaceAll(`"`, "")} }`;
583
+ return `{ url: '${object.url}' }`;
584
+ }
585
+ return object;
586
+ }
587
+ /**
588
+ * Converts the OpenAPI path to a TypeScript template literal string.
589
+ * An optional `replacer` can transform each extracted parameter name before interpolation.
590
+ *
591
+ * @example
592
+ * new URLPath('/pet/{petId}').toTemplateString() // '`/pet/${petId}`'
593
+ */
594
+ toTemplateString({ prefix, replacer } = {}) {
595
+ const result = this.path.split(/\{([^}]+)\}/).map((part, i) => {
596
+ if (i % 2 === 0) return part;
597
+ const param = this.#transformParam(part);
598
+ return `\${${replacer ? replacer(param) : param}}`;
599
+ }).join("");
600
+ return `\`${prefix ?? ""}${result}\``;
601
+ }
602
+ /**
603
+ * Extracts all `{param}` segments from the path and returns them as a key-value map.
604
+ * An optional `replacer` transforms each parameter name in both key and value positions.
605
+ * Returns `undefined` when no path parameters are found.
606
+ *
607
+ * @example
608
+ * ```ts
609
+ * new URLPath('/pet/{petId}/tag/{tagId}').getParams()
610
+ * // { petId: 'petId', tagId: 'tagId' }
611
+ * ```
612
+ */
613
+ getParams(replacer) {
614
+ const params = {};
615
+ this.#eachParam((_raw, param) => {
616
+ const key = replacer ? replacer(param) : param;
617
+ params[key] = key;
618
+ });
619
+ return Object.keys(params).length > 0 ? params : void 0;
620
+ }
621
+ /** Converts the OpenAPI path to Express-style colon syntax.
622
+ *
623
+ * @example
624
+ * ```ts
625
+ * new URLPath('/pet/{petId}').toURLPath() // '/pet/:petId'
626
+ * ```
627
+ */
628
+ toURLPath() {
629
+ return this.path.replace(/\{([^}]+)\}/g, ":$1");
630
+ }
631
+ };
632
+ //#endregion
633
+ //#region src/constants.ts
634
+ /**
635
+ * Base URL for the Kubb Studio web app.
636
+ */
637
+ const DEFAULT_STUDIO_URL = "https://kubb.studio";
638
+ /**
639
+ * Default banner style written at the top of every generated file.
640
+ */
641
+ const DEFAULT_BANNER = "simple";
642
+ /**
643
+ * Default file-extension mapping used when no explicit mapping is configured.
644
+ */
645
+ const DEFAULT_EXTENSION = { ".ts": ".ts" };
646
+ /**
647
+ * Numeric log-level thresholds used internally to compare verbosity.
648
+ *
649
+ * Higher numbers are more verbose.
650
+ */
651
+ const logLevel = {
652
+ silent: Number.NEGATIVE_INFINITY,
653
+ error: 0,
654
+ warn: 1,
655
+ info: 3,
656
+ verbose: 4,
657
+ debug: 5
658
+ };
659
+ //#endregion
660
+ //#region src/definePlugin.ts
661
+ /**
662
+ * Wraps a plugin factory and returns a function that accepts user options and
663
+ * yields a fully typed `Plugin`. Lifecycle handlers go inside a single
664
+ * `hooks` object (inspired by Astro integrations).
665
+ *
666
+ * Pass a `PluginFactoryOptions` type parameter to get a typed `ctx` inside
667
+ * `kubb:plugin:setup`. Plugin names should follow the `plugin-<feature>`
668
+ * convention (`plugin-react-query`, `plugin-zod`, ...).
669
+ *
670
+ * @example
671
+ * ```ts
672
+ * import { definePlugin } from '@kubb/core'
673
+ *
674
+ * export const pluginTs = definePlugin((options: { prefix?: string } = {}) => ({
675
+ * name: 'plugin-ts',
676
+ * hooks: {
677
+ * 'kubb:plugin:setup'(ctx) {
678
+ * ctx.setResolver(resolverTs)
679
+ * },
680
+ * },
681
+ * }))
682
+ * ```
683
+ */
684
+ function definePlugin(factory) {
685
+ return (options) => factory(options ?? {});
686
+ }
687
+ /**
688
+ * Detects whether an output path points at a single file (`'single'`) or a
689
+ * directory (`'split'`). Decided purely from the presence of a file extension.
690
+ *
691
+ * @example Directory
692
+ * `getMode('./types') // 'split'`
693
+ *
694
+ * @example Single file
695
+ * `getMode('./api.ts') // 'single'`
696
+ */
697
+ function getMode(fileOrFolder) {
698
+ if (!fileOrFolder) return "split";
699
+ return (0, node_path.extname)(fileOrFolder) ? "single" : "split";
700
+ }
701
+ //#endregion
702
+ //#region src/defineResolver.ts
703
+ /**
704
+ * Merges document `meta` with per-file `file` context into the `BannerMeta` passed to a
705
+ * `banner`/`footer` function. Missing fields default to empty/`false` so the object shape
706
+ * is stable even when a caller (e.g. the barrel middleware) has no document metadata.
707
+ */
708
+ function buildBannerMeta({ meta, file }) {
709
+ return {
710
+ title: meta?.title,
711
+ description: meta?.description,
712
+ version: meta?.version,
713
+ baseURL: meta?.baseURL,
714
+ circularNames: meta?.circularNames ?? [],
715
+ enumNames: meta?.enumNames ?? [],
716
+ filePath: file?.path ?? "",
717
+ baseName: file?.baseName ?? "",
718
+ isBarrel: file?.isBarrel ?? false,
719
+ isAggregation: file?.isAggregation ?? false
720
+ };
721
+ }
722
+ const stringPatternCache = /* @__PURE__ */ new Map();
723
+ function testPattern(value, pattern) {
724
+ if (typeof pattern === "string") {
725
+ let regex = stringPatternCache.get(pattern);
726
+ if (!regex) {
727
+ regex = new RegExp(pattern);
728
+ stringPatternCache.set(pattern, regex);
729
+ }
730
+ return regex.test(value);
731
+ }
732
+ return value.match(pattern) !== null;
733
+ }
734
+ /**
735
+ * Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
736
+ */
737
+ function matchesOperationPattern(node, type, pattern) {
738
+ if (type === "tag") return node.tags.some((tag) => testPattern(tag, pattern));
739
+ if (type === "operationId") return testPattern(node.operationId, pattern);
740
+ if (type === "path") return node.path !== void 0 && testPattern(node.path, pattern);
741
+ if (type === "method") return node.method !== void 0 && testPattern(node.method.toLowerCase(), pattern);
742
+ if (type === "contentType") return node.requestBody?.content?.some((c) => testPattern(c.contentType, pattern)) ?? false;
743
+ return false;
744
+ }
745
+ /**
746
+ * Checks if a schema matches a pattern for a given filter type (`schemaName`).
747
+ *
748
+ * Returns `null` when the filter type doesn't apply to schemas.
749
+ */
750
+ function matchesSchemaPattern(node, type, pattern) {
751
+ if (type === "schemaName") return node.name ? testPattern(node.name, pattern) : false;
752
+ return null;
753
+ }
754
+ /**
755
+ * Default name resolver used by `defineResolver`.
756
+ *
757
+ * - `camelCase` for `function` and `file` types.
758
+ * - `PascalCase` for `type`.
759
+ * - `camelCase` for everything else.
760
+ */
761
+ function defaultResolver(name, type) {
762
+ if (type === "file" || type === "function") return camelCase(name, { isFile: type === "file" });
763
+ if (type === "type") return pascalCase(name);
764
+ return camelCase(name);
765
+ }
766
+ /**
767
+ * Default option resolver — applies include/exclude filters and merges matching override options.
768
+ *
769
+ * Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
770
+ *
771
+ * @example Include/exclude filtering
772
+ * ```ts
773
+ * const options = defaultResolveOptions(operationNode, {
774
+ * options: { output: 'types' },
775
+ * exclude: [{ type: 'tag', pattern: 'internal' }],
776
+ * })
777
+ * // → null when node has tag 'internal'
778
+ * ```
779
+ *
780
+ * @example Override merging
781
+ * ```ts
782
+ * const options = defaultResolveOptions(operationNode, {
783
+ * options: { enumType: 'asConst' },
784
+ * override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
785
+ * })
786
+ * // → { enumType: 'enum' } when operationId matches
787
+ * ```
788
+ */
789
+ const resolveOptionsCache = /* @__PURE__ */ new WeakMap();
790
+ function computeOptions(node, options, exclude, include, override) {
791
+ if ((0, _kubb_ast.isOperationNode)(node)) {
792
+ if (exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
793
+ if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
794
+ const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options;
795
+ return {
796
+ ...options,
797
+ ...overrideOptions
798
+ };
799
+ }
800
+ if ((0, _kubb_ast.isSchemaNode)(node)) {
801
+ if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) return null;
802
+ if (include) {
803
+ const applicable = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern)).filter((r) => r !== null);
804
+ if (applicable.length > 0 && !applicable.includes(true)) return null;
805
+ }
806
+ const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options;
807
+ return {
808
+ ...options,
809
+ ...overrideOptions
810
+ };
811
+ }
812
+ return options;
813
+ }
814
+ function defaultResolveOptions(node, { options, exclude = [], include, override = [] }) {
815
+ const optionsKey = options;
816
+ let byOptions = resolveOptionsCache.get(optionsKey);
817
+ if (!byOptions) {
818
+ byOptions = /* @__PURE__ */ new WeakMap();
819
+ resolveOptionsCache.set(optionsKey, byOptions);
820
+ }
821
+ const cached = byOptions.get(node);
822
+ if (cached !== void 0) return cached.value;
823
+ const result = computeOptions(node, options, exclude, include, override);
824
+ byOptions.set(node, { value: result });
825
+ return result;
826
+ }
827
+ /**
828
+ * Default path resolver used by `defineResolver`.
829
+ *
830
+ * - Returns the output directory in `single` mode.
831
+ * - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
832
+ * - Falls back to a flat `output/baseName` path otherwise.
833
+ *
834
+ * A custom `group.name` function overrides the default subdirectory naming.
835
+ * For `tag` groups the default is `${camelCase(tag)}Controller`.
836
+ * For `path` groups the default is the first path segment after `/`.
837
+ *
838
+ * @example Flat output
839
+ * ```ts
840
+ * defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
841
+ * // → '/src/types/petTypes.ts'
842
+ * ```
843
+ *
844
+ * @example Tag-based grouping
845
+ * ```ts
846
+ * defaultResolvePath(
847
+ * { baseName: 'petTypes.ts', tag: 'pets' },
848
+ * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
849
+ * )
850
+ * // → '/src/types/petsController/petTypes.ts'
851
+ * ```
852
+ *
853
+ * @example Path-based grouping
854
+ * ```ts
855
+ * defaultResolvePath(
856
+ * { baseName: 'petTypes.ts', path: '/pets/list' },
857
+ * { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
858
+ * )
859
+ * // → '/src/types/pets/petTypes.ts'
860
+ * ```
861
+ *
862
+ * @example Single-file mode
863
+ * ```ts
864
+ * defaultResolvePath(
865
+ * { baseName: 'petTypes.ts', pathMode: 'single' },
866
+ * { root: '/src', output: { path: 'types' } },
867
+ * )
868
+ * // → '/src/types'
869
+ * ```
870
+ */
871
+ function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }, { root, output, group }) {
872
+ if ((pathMode ?? getMode(node_path.default.resolve(root, output.path))) === "single") return node_path.default.resolve(root, output.path);
873
+ const result = (() => {
874
+ if (group && (groupPath || tag)) {
875
+ const groupValue = group.type === "path" ? groupPath : tag;
876
+ const defaultName = group.type === "tag" ? ({ group: g }) => `${camelCase(g)}Controller` : ({ group: g }) => {
877
+ const segment = g.split("/").filter((s) => s !== "" && s !== "." && s !== "..")[0];
878
+ return segment ? camelCase(segment) : "";
879
+ };
880
+ const resolveName = group.name ?? defaultName;
881
+ return node_path.default.resolve(root, output.path, resolveName({ group: groupValue }), baseName);
882
+ }
883
+ return node_path.default.resolve(root, output.path, baseName);
884
+ })();
885
+ const outputDir = node_path.default.resolve(root, output.path);
886
+ const outputDirWithSep = outputDir.endsWith(node_path.default.sep) ? outputDir : `${outputDir}${node_path.default.sep}`;
887
+ if (result !== outputDir && !result.startsWith(outputDirWithSep)) throw new Error(`[Kubb] Resolved path "${result}" is outside the output directory "${outputDir}". This may indicate a path traversal attempt in the OpenAPI specification or a misconfigured group.name function.`);
888
+ return result;
889
+ }
890
+ /**
891
+ * Default file resolver used by `defineResolver`.
892
+ *
893
+ * Resolves a `FileNode` by combining name resolution (`resolver.default`) with
894
+ * path resolution (`resolver.resolvePath`). The resolved file always has empty
895
+ * `sources`, `imports`, and `exports` arrays — consumers populate those separately.
896
+ *
897
+ * In `single` mode the name is omitted and the file sits directly in the output directory.
898
+ *
899
+ * @example Resolve a schema file
900
+ * ```ts
901
+ * const file = defaultResolveFile.call(
902
+ * resolver,
903
+ * { name: 'pet', extname: '.ts' },
904
+ * { root: '/src', output: { path: 'types' } },
905
+ * )
906
+ * // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
907
+ * ```
908
+ *
909
+ * @example Resolve an operation file with tag grouping
910
+ * ```ts
911
+ * const file = defaultResolveFile.call(
912
+ * resolver,
913
+ * { name: 'listPets', extname: '.ts', tag: 'pets' },
914
+ * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
915
+ * )
916
+ * // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
917
+ * ```
918
+ */
919
+ function defaultResolveFile({ name, extname, tag, path: groupPath }, context) {
920
+ const pathMode = getMode(node_path.default.resolve(context.root, context.output.path));
921
+ const baseName = `${pathMode === "single" ? "" : this.default(name, "file")}${extname}`;
922
+ const filePath = this.resolvePath({
923
+ baseName,
924
+ pathMode,
925
+ tag,
926
+ path: groupPath
927
+ }, context);
928
+ return (0, _kubb_ast.createFile)({
929
+ path: filePath,
930
+ baseName: node_path.default.basename(filePath),
931
+ meta: { pluginName: this.pluginName },
932
+ sources: [],
933
+ imports: [],
934
+ exports: []
935
+ });
936
+ }
937
+ /**
938
+ * Generates the default "Generated by Kubb" banner from config and optional node metadata.
939
+ */
940
+ function buildDefaultBanner({ title, description, version, config }) {
941
+ try {
942
+ const source = (() => {
943
+ if (Array.isArray(config.input)) {
944
+ const first = config.input[0];
945
+ if (first && "path" in first) return node_path.default.basename(first.path);
946
+ return "";
947
+ }
948
+ if (config.input && "path" in config.input) return node_path.default.basename(config.input.path);
949
+ if (config.input && "data" in config.input) return "text content";
950
+ return "";
951
+ })();
952
+ let banner = "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n";
953
+ if (config.output.defaultBanner === "simple") {
954
+ banner += "*/\n";
955
+ return banner;
956
+ }
957
+ if (source) banner += `* Source: ${source}\n`;
958
+ if (title) banner += `* Title: ${title}\n`;
959
+ if (description) {
960
+ const formattedDescription = description.replace(/\n/gm, "\n* ");
961
+ banner += `* Description: ${formattedDescription}\n`;
962
+ }
963
+ if (version) banner += `* OpenAPI spec version: ${version}\n`;
964
+ banner += "*/\n";
965
+ return banner;
966
+ } catch (_error) {
967
+ return "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/";
968
+ }
969
+ }
970
+ /**
971
+ * Default banner resolver — returns the banner string for a generated file.
972
+ *
973
+ * A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
974
+ * When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
975
+ * from the document metadata when `meta` is provided).
976
+ *
977
+ * - When `output.banner` is a function, calls it with the file's `BannerMeta` and returns the result.
978
+ * - When `output.banner` is a string, returns it directly.
979
+ * - When `config.output.defaultBanner` is `false`, returns `undefined`.
980
+ * - Otherwise returns the Kubb "Generated by Kubb" notice.
981
+ *
982
+ * @example String banner overrides default
983
+ * ```ts
984
+ * defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
985
+ * // → '// my banner'
986
+ * ```
987
+ *
988
+ * @example Function banner with metadata
989
+ * ```ts
990
+ * defaultResolveBanner(meta, { output: { banner: (m) => `// v${m.version}` }, config })
991
+ * // → '// v3.0.0'
992
+ * ```
993
+ *
994
+ * @example Function banner skips re-export files
995
+ * ```ts
996
+ * defaultResolveBanner(meta, { output: { banner: (m) => (m.isBarrel ? '' : "'use server'") }, config, file: { path, baseName, isBarrel: true } })
997
+ * // → ''
998
+ * ```
999
+ *
1000
+ * @example No user banner — Kubb notice with OAS metadata
1001
+ * ```ts
1002
+ * defaultResolveBanner(meta, { config })
1003
+ * // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
1004
+ * ```
1005
+ *
1006
+ * @example Disabled default banner
1007
+ * ```ts
1008
+ * defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
1009
+ * // → null
1010
+ * ```
1011
+ */
1012
+ function defaultResolveBanner(meta, { output, config, file }) {
1013
+ if (typeof output?.banner === "function") return output.banner(buildBannerMeta({
1014
+ meta,
1015
+ file
1016
+ }));
1017
+ if (typeof output?.banner === "string") return output.banner;
1018
+ if (config.output.defaultBanner === false) return null;
1019
+ return buildDefaultBanner({
1020
+ title: meta?.title,
1021
+ version: meta?.version,
1022
+ config
1023
+ });
1024
+ }
1025
+ /**
1026
+ * Default footer resolver — returns the footer string for a generated file.
1027
+ *
1028
+ * - When `output.footer` is a function, calls it with the file's `BannerMeta` and returns the result.
1029
+ * - When `output.footer` is a string, returns it directly.
1030
+ * - Otherwise returns `undefined`.
1031
+ *
1032
+ * @example String footer
1033
+ * ```ts
1034
+ * defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
1035
+ * // → '// end of file'
1036
+ * ```
1037
+ *
1038
+ * @example Function footer with metadata
1039
+ * ```ts
1040
+ * defaultResolveFooter(meta, { output: { footer: (m) => `// ${m.title}` }, config })
1041
+ * // → '// Pet Store'
1042
+ * ```
1043
+ */
1044
+ function defaultResolveFooter(meta, { output, file }) {
1045
+ if (typeof output?.footer === "function") return output.footer(buildBannerMeta({
1046
+ meta,
1047
+ file
1048
+ }));
1049
+ if (typeof output?.footer === "string") return output.footer;
1050
+ return null;
1051
+ }
1052
+ /**
1053
+ * Defines a plugin resolver. The resolver is the object that decides what
1054
+ * every generated symbol and file path is called. Built-in defaults handle
1055
+ * name casing, include/exclude/override filtering, output path computation,
1056
+ * and file construction. Supply your own to override any of them:
1057
+ *
1058
+ * - `default` — name casing strategy (camelCase / PascalCase).
1059
+ * - `resolveOptions` — include/exclude/override filtering.
1060
+ * - `resolvePath` — output path computation.
1061
+ * - `resolveFile` — full `FileNode` construction.
1062
+ * - `resolveBanner` / `resolveFooter` — top/bottom-of-file text.
1063
+ *
1064
+ * Methods in the returned object can call sibling resolver methods via `this`,
1065
+ * which keeps custom rules small (`this.default(name, 'type')` to delegate).
1066
+ *
1067
+ * @example Basic resolver with naming helpers
1068
+ * ```ts
1069
+ * export const resolverTs = defineResolver<PluginTs>(() => ({
1070
+ * name: 'default',
1071
+ * resolveName(name) {
1072
+ * return this.default(name, 'function')
1073
+ * },
1074
+ * resolveTypeName(name) {
1075
+ * return this.default(name, 'type')
1076
+ * },
1077
+ * }))
1078
+ * ```
1079
+ *
1080
+ * @example Custom output path
1081
+ * ```ts
1082
+ * import path from 'node:path'
1083
+ *
1084
+ * export const resolverTs = defineResolver<PluginTs>(() => ({
1085
+ * name: 'custom',
1086
+ * resolvePath({ baseName }, { root, output }) {
1087
+ * return path.resolve(root, output.path, 'generated', baseName)
1088
+ * },
1089
+ * }))
1090
+ * ```
1091
+ */
1092
+ function defineResolver(build) {
1093
+ let resolver;
1094
+ resolver = {
1095
+ default: defaultResolver,
1096
+ resolveOptions: defaultResolveOptions,
1097
+ resolvePath: defaultResolvePath,
1098
+ resolveFile: (params, context) => defaultResolveFile.call(resolver, params, context),
1099
+ resolveBanner: defaultResolveBanner,
1100
+ resolveFooter: defaultResolveFooter,
1101
+ ...build()
1102
+ };
1103
+ return resolver;
1104
+ }
1105
+ //#endregion
1106
+ //#region src/devtools.ts
1107
+ /**
1108
+ * Encodes an `InputNode` as a compressed, URL-safe string.
1109
+ *
1110
+ * The JSON representation is deflate-compressed with {@link deflateSync} before
1111
+ * base64url encoding, which typically reduces payload size by 70–80 % and
1112
+ * keeps URLs well within browser and server path-length limits.
1113
+ *
1114
+ * Use {@link decodeAst} to reverse.
1115
+ */
1116
+ function encodeAst(input) {
1117
+ const compressed = (0, fflate.deflateSync)(new TextEncoder().encode(JSON.stringify(input)));
1118
+ return Buffer.from(compressed).toString("base64url");
1119
+ }
1120
+ /**
1121
+ * Constructs the Kubb Studio URL for the given `InputNode`.
1122
+ * When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
1123
+ * The `input` is encoded and attached as the `?root=` query parameter so Studio
1124
+ * can decode and render it without a round-trip to any server.
1125
+ */
1126
+ function getStudioUrl(input, studioUrl, options = {}) {
1127
+ return `${studioUrl.replace(/\/$/, "")}${options.ast ? "/ast" : ""}?root=${encodeAst(input)}`;
1128
+ }
1129
+ /**
1130
+ * Opens the Kubb Studio URL for the given `InputNode` in the default browser —
1131
+ *
1132
+ * Falls back to printing the URL if the browser cannot be launched.
1133
+ */
1134
+ async function openInStudio(input, studioUrl, options = {}) {
1135
+ const url = getStudioUrl(input, studioUrl, options);
1136
+ const cmd = process.platform === "win32" ? "cmd" : process.platform === "darwin" ? "open" : "xdg-open";
1137
+ const args = process.platform === "win32" ? [
1138
+ "/c",
1139
+ "start",
1140
+ "",
1141
+ url
1142
+ ] : [url];
1143
+ try {
1144
+ await (0, tinyexec.x)(cmd, args);
1145
+ } catch {
1146
+ console.log(`\n ${url}\n`);
1147
+ }
1148
+ }
1149
+ //#endregion
1150
+ //#region src/FileManager.ts
1151
+ function mergeFile(a, b) {
1152
+ return {
1153
+ ...a,
1154
+ banner: b.banner,
1155
+ footer: b.footer,
1156
+ sources: a.sources.length ? b.sources.length ? [...a.sources, ...b.sources] : a.sources : b.sources,
1157
+ imports: a.imports.length ? b.imports.length ? [...a.imports, ...b.imports] : a.imports : b.imports,
1158
+ exports: a.exports.length ? b.exports.length ? [...a.exports, ...b.exports] : a.exports : b.exports
1159
+ };
1160
+ }
1161
+ function isIndexPath(path) {
1162
+ return path.endsWith("/index.ts") || path === "index.ts";
1163
+ }
1164
+ function compareFiles(a, b) {
1165
+ const lenDiff = a.path.length - b.path.length;
1166
+ if (lenDiff !== 0) return lenDiff;
1167
+ const aIsIndex = isIndexPath(a.path);
1168
+ const bIsIndex = isIndexPath(b.path);
1169
+ if (aIsIndex && !bIsIndex) return 1;
1170
+ if (!aIsIndex && bIsIndex) return -1;
1171
+ return 0;
1172
+ }
1173
+ /**
1174
+ * In-memory file store for generated files. Files sharing a `path` are merged
1175
+ * (sources/imports/exports concatenated). The `files` getter is sorted by
1176
+ * path length (barrel `index.ts` last within a bucket).
1177
+ *
1178
+ * @example
1179
+ * ```ts
1180
+ * const manager = new FileManager()
1181
+ * manager.upsert(myFile)
1182
+ * manager.files // sorted view
1183
+ * ```
1184
+ */
1185
+ var FileManager = class {
1186
+ #cache = /* @__PURE__ */ new Map();
1187
+ #sorted = null;
1188
+ #onUpsert = null;
1189
+ /**
1190
+ * Registers a callback invoked with the resolved {@link FileNode} on every
1191
+ * `add` / `upsert`. Used by the build loop to track newly written files
1192
+ * without keeping its own scan-based diff. Single subscriber by design —
1193
+ * setting again replaces the previous callback. Pass `null` to detach.
1194
+ */
1195
+ setOnUpsert(callback) {
1196
+ this.#onUpsert = callback;
1197
+ }
1198
+ add(...files) {
1199
+ return this.#store(files, false);
1200
+ }
1201
+ upsert(...files) {
1202
+ return this.#store(files, true);
1203
+ }
1204
+ #store(files, mergeExisting) {
1205
+ const batch = files.length > 1 ? this.#dedupe(files) : files;
1206
+ const resolved = [];
1207
+ for (const file of batch) {
1208
+ const existing = this.#cache.get(file.path);
1209
+ const merged = existing && mergeExisting ? (0, _kubb_ast.createFile)(mergeFile(existing, file)) : (0, _kubb_ast.createFile)(file);
1210
+ this.#cache.set(merged.path, merged);
1211
+ resolved.push(merged);
1212
+ this.#onUpsert?.(merged);
1213
+ }
1214
+ if (resolved.length > 0) this.#sorted = null;
1215
+ return resolved;
1216
+ }
1217
+ #dedupe(files) {
1218
+ const seen = /* @__PURE__ */ new Map();
1219
+ for (const file of files) {
1220
+ const prev = seen.get(file.path);
1221
+ seen.set(file.path, prev ? mergeFile(prev, file) : file);
1222
+ }
1223
+ return [...seen.values()];
1224
+ }
1225
+ getByPath(path) {
1226
+ return this.#cache.get(path) ?? null;
1227
+ }
1228
+ deleteByPath(path) {
1229
+ if (!this.#cache.delete(path)) return;
1230
+ this.#sorted = null;
1231
+ }
1232
+ clear() {
1233
+ this.#cache.clear();
1234
+ this.#sorted = null;
1235
+ }
1236
+ /**
1237
+ * Releases all stored files. Called by the core after `kubb:build:end`.
1238
+ */
1239
+ dispose() {
1240
+ this.clear();
1241
+ this.#onUpsert = null;
1242
+ }
1243
+ [Symbol.dispose]() {
1244
+ this.dispose();
1245
+ }
1246
+ /**
1247
+ * All stored files in stable sort order (shortest path first, barrel files
1248
+ * last within a length bucket). Returns a cached view — do not mutate.
1249
+ */
1250
+ get files() {
1251
+ return this.#sorted ??= [...this.#cache.values()].sort(compareFiles);
1252
+ }
1253
+ };
1254
+ //#endregion
1255
+ //#region src/FileProcessor.ts
1256
+ function joinSources(file) {
1257
+ const sources = file.sources;
1258
+ if (sources.length === 0) return "";
1259
+ const parts = [];
1260
+ for (const source of sources) {
1261
+ const s = (0, _kubb_ast.extractStringsFromNodes)(source.nodes);
1262
+ if (s) parts.push(s);
1263
+ }
1264
+ return parts.join("\n\n");
1265
+ }
1266
+ /**
1267
+ * Converts a single file to a string using the registered parsers.
1268
+ * Falls back to joining source values when no matching parser is found.
1269
+ *
1270
+ * @internal
1271
+ */
1272
+ var FileProcessor = class {
1273
+ events = new AsyncEventEmitter();
1274
+ parse(file, { parsers, extension } = {}) {
1275
+ const parseExtName = extension?.[file.extname] || void 0;
1276
+ if (!parsers || !file.extname) return joinSources(file);
1277
+ const parser = parsers.get(file.extname);
1278
+ if (!parser) return joinSources(file);
1279
+ return parser.parse(file, { extname: parseExtName });
1280
+ }
1281
+ *stream(files, options = {}) {
1282
+ const total = files.length;
1283
+ if (total === 0) return;
1284
+ let processed = 0;
1285
+ for (const file of files) {
1286
+ const source = this.parse(file, options);
1287
+ processed++;
1288
+ yield {
1289
+ file,
1290
+ source,
1291
+ processed,
1292
+ total,
1293
+ percentage: processed / total * 100
1294
+ };
1295
+ }
1296
+ }
1297
+ async run(files, options = {}) {
1298
+ await this.events.emit("start", files);
1299
+ for (const { file, source, processed, total, percentage } of this.stream(files, options)) await this.events.emit("update", {
1300
+ file,
1301
+ source,
1302
+ processed,
1303
+ percentage,
1304
+ total
1305
+ });
1306
+ await this.events.emit("end", files);
1307
+ return files;
1308
+ }
1309
+ /**
1310
+ * Clears all registered event listeners.
1311
+ */
1312
+ dispose() {
1313
+ this.events.removeAll();
1314
+ }
1315
+ [Symbol.dispose]() {
1316
+ this.dispose();
1317
+ }
1318
+ };
1319
+ //#endregion
1320
+ //#region \0@oxc-project+runtime@0.129.0/helpers/usingCtx.js
1321
+ function _usingCtx() {
1322
+ var r = "function" == typeof SuppressedError ? SuppressedError : function(r, e) {
1323
+ var n = Error();
1324
+ return n.name = "SuppressedError", n.error = r, n.suppressed = e, n;
1325
+ };
1326
+ var e = {};
1327
+ var n = [];
1328
+ function using(r, e) {
1329
+ if (null != e) {
1330
+ if (Object(e) !== e) throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");
1331
+ if (r) var o = e[Symbol.asyncDispose || Symbol["for"]("Symbol.asyncDispose")];
1332
+ if (void 0 === o && (o = e[Symbol.dispose || Symbol["for"]("Symbol.dispose")], r)) var t = o;
1333
+ if ("function" != typeof o) throw new TypeError("Object is not disposable.");
1334
+ t && (o = function o() {
1335
+ try {
1336
+ t.call(e);
1337
+ } catch (r) {
1338
+ return Promise.reject(r);
1339
+ }
1340
+ }), n.push({
1341
+ v: e,
1342
+ d: o,
1343
+ a: r
1344
+ });
1345
+ } else r && n.push({
1346
+ d: e,
1347
+ a: r
1348
+ });
1349
+ return e;
1350
+ }
1351
+ return {
1352
+ e,
1353
+ u: using.bind(null, !1),
1354
+ a: using.bind(null, !0),
1355
+ d: function d() {
1356
+ var o;
1357
+ var t = this.e;
1358
+ var s = 0;
1359
+ function next() {
1360
+ for (; o = n.pop();) try {
1361
+ if (!o.a && 1 === s) return s = 0, n.push(o), Promise.resolve().then(next);
1362
+ if (o.d) {
1363
+ var r = o.d.call(o.v);
1364
+ if (o.a) return s |= 2, Promise.resolve(r).then(next, err);
1365
+ } else s |= 1;
1366
+ } catch (r) {
1367
+ return err(r);
1368
+ }
1369
+ if (1 === s) return t !== e ? Promise.reject(t) : Promise.resolve();
1370
+ if (t !== e) throw t;
1371
+ }
1372
+ function err(n) {
1373
+ return t = t !== e ? new r(n, t) : n, next();
1374
+ }
1375
+ return next();
1376
+ }
1377
+ };
1378
+ }
1379
+ //#endregion
1380
+ //#region src/KubbDriver.ts
1381
+ function enforceOrder(enforce) {
1382
+ return enforce === "pre" ? -1 : enforce === "post" ? 1 : 0;
1383
+ }
1384
+ const OPERATION_FILTER_TYPES = new Set([
1385
+ "tag",
1386
+ "operationId",
1387
+ "path",
1388
+ "method",
1389
+ "contentType"
1390
+ ]);
1391
+ var KubbDriver = class KubbDriver {
1392
+ config;
1393
+ options;
1394
+ /**
1395
+ * Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
1396
+ *
1397
+ * @example
1398
+ * ```ts
1399
+ * KubbDriver.getMode('src/gen/types.ts') // 'single'
1400
+ * KubbDriver.getMode('src/gen/types') // 'split'
1401
+ * ```
1402
+ */
1403
+ static getMode(fileOrFolder) {
1404
+ return getMode(fileOrFolder);
1405
+ }
1406
+ /**
1407
+ * The streaming `InputStreamNode` produced by the adapter.
1408
+ * Always set after adapter setup — parse-only adapters are wrapped automatically.
1409
+ */
1410
+ inputNode = null;
1411
+ adapter = null;
1412
+ /**
1413
+ * Studio session state, kept together so `dispose()` can reset it atomically.
1414
+ *
1415
+ * - `source` holds the raw adapter source so `adapter.parse()` can be called lazily.
1416
+ * Intentionally outlives the build; cleared by `dispose()`.
1417
+ * - `isOpen` prevents opening the studio more than once per build.
1418
+ * - `inputNode` caches the parse promise so `adapter.parse()` is called at most once
1419
+ * per studio session, even when `openInStudio()` is called multiple times.
1420
+ */
1421
+ #studio = {
1422
+ source: null,
1423
+ isOpen: false,
1424
+ inputNode: null
1425
+ };
1426
+ #middlewareListeners = [];
1427
+ /**
1428
+ * Central file store for all generated files.
1429
+ * Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
1430
+ * add files; this property gives direct read/write access when needed.
1431
+ */
1432
+ fileManager = new FileManager();
1433
+ #fileProcessor = new FileProcessor();
1434
+ plugins = /* @__PURE__ */ new Map();
1435
+ /**
1436
+ * Tracks which plugins have generators registered via `addGenerator()` (event-based path).
1437
+ * Used by the build loop to decide whether to emit generator events for a given plugin.
1438
+ */
1439
+ #eventGeneratorPlugins = /* @__PURE__ */ new Set();
1440
+ #resolvers = /* @__PURE__ */ new Map();
1441
+ #defaultResolvers = /* @__PURE__ */ new Map();
1442
+ #hookListeners = /* @__PURE__ */ new Map();
1443
+ constructor(config, options) {
1444
+ this.config = config;
1445
+ this.options = options;
1446
+ this.adapter = config.adapter ?? null;
1447
+ }
1448
+ async setup() {
1449
+ const normalized = this.config.plugins.map((rawPlugin) => this.#normalizePlugin(rawPlugin));
1450
+ normalized.sort((a, b) => {
1451
+ if (b.dependencies?.includes(a.name)) return -1;
1452
+ if (a.dependencies?.includes(b.name)) return 1;
1453
+ return enforceOrder(a.enforce) - enforceOrder(b.enforce);
1454
+ });
1455
+ for (const plugin of normalized) {
1456
+ if (plugin.apply) plugin.apply(this.config);
1457
+ this.#registerPlugin(plugin);
1458
+ this.plugins.set(plugin.name, plugin);
1459
+ }
1460
+ if (this.config.middleware) for (const middleware of this.config.middleware) for (const event of Object.keys(middleware.hooks)) this.#registerMiddleware(event, middleware.hooks);
1461
+ if (this.config.adapter) await this.#registerAdapter(this.config.adapter);
1462
+ }
1463
+ get hooks() {
1464
+ return this.options.hooks;
1465
+ }
1466
+ /**
1467
+ * Creates an `NormalizedPlugin` from a hook-style plugin and registers
1468
+ * its lifecycle handlers on the `AsyncEventEmitter`.
1469
+ */
1470
+ #normalizePlugin(plugin) {
1471
+ const normalized = {
1472
+ name: plugin.name,
1473
+ dependencies: plugin.dependencies,
1474
+ enforce: plugin.enforce,
1475
+ hooks: plugin.hooks,
1476
+ options: plugin.options ?? {
1477
+ output: { path: "." },
1478
+ exclude: [],
1479
+ override: []
1480
+ }
1481
+ };
1482
+ if ("apply" in plugin && typeof plugin.apply === "function") normalized.apply = plugin.apply;
1483
+ return normalized;
1484
+ }
1485
+ async #registerAdapter(adapter) {
1486
+ const source = inputToAdapterSource(this.config);
1487
+ this.#studio.source = source;
1488
+ if (adapter.stream) {
1489
+ this.inputNode = await adapter.stream(source);
1490
+ await this.hooks.emit("kubb:debug", {
1491
+ date: /* @__PURE__ */ new Date(),
1492
+ logs: [`✓ Adapter '${adapter.name}' producing input stream`]
1493
+ });
1494
+ } else {
1495
+ const inputNode = await adapter.parse(source);
1496
+ this.inputNode = (0, _kubb_ast.createStreamInput)(arrayToAsyncIterable(inputNode.schemas), arrayToAsyncIterable(inputNode.operations), inputNode.meta);
1497
+ await this.hooks.emit("kubb:debug", {
1498
+ date: /* @__PURE__ */ new Date(),
1499
+ logs: [
1500
+ `✓ Adapter '${adapter.name}' resolved InputNode (wrapped as stream)`,
1501
+ ` • Schemas: ${inputNode.schemas.length}`,
1502
+ ` • Operations: ${inputNode.operations.length}`
1503
+ ]
1504
+ });
1505
+ }
1506
+ }
1507
+ #registerMiddleware(event, middlewareHooks) {
1508
+ const handler = middlewareHooks[event];
1509
+ if (!handler) return;
1510
+ this.hooks.on(event, handler);
1511
+ this.#middlewareListeners.push([event, handler]);
1512
+ }
1513
+ /**
1514
+ * Registers a hook-style plugin's lifecycle handlers on the shared `AsyncEventEmitter`.
1515
+ *
1516
+ * For `kubb:plugin:setup`, the registered listener wraps the globally emitted context with a
1517
+ * plugin-specific one so that `addGenerator`, `setResolver`, `setTransformer`, and
1518
+ * `setRenderer` all target the correct `normalizedPlugin` entry in the plugins map.
1519
+ *
1520
+ * All other hooks are iterated and registered directly as pass-through listeners.
1521
+ * Any event key present in the global `KubbHooks` interface can be subscribed to.
1522
+ *
1523
+ * External tooling can subscribe to any of these events via `hooks.on(...)` to observe
1524
+ * the plugin lifecycle without modifying plugin behavior.
1525
+ *
1526
+ * @internal
1527
+ */
1528
+ #registerPlugin(plugin) {
1529
+ const { hooks } = plugin;
1530
+ if (!hooks) return;
1531
+ if (hooks["kubb:plugin:setup"]) {
1532
+ const setupHandler = (globalCtx) => {
1533
+ const pluginCtx = {
1534
+ ...globalCtx,
1535
+ options: plugin.options ?? {},
1536
+ addGenerator: (gen) => {
1537
+ this.registerGenerator(plugin.name, gen);
1538
+ },
1539
+ setResolver: (resolver) => {
1540
+ this.setPluginResolver(plugin.name, resolver);
1541
+ },
1542
+ setTransformer: (visitor) => {
1543
+ plugin.transformer = visitor;
1544
+ },
1545
+ setRenderer: (renderer) => {
1546
+ plugin.renderer = renderer;
1547
+ },
1548
+ setOptions: (opts) => {
1549
+ plugin.options = {
1550
+ ...plugin.options,
1551
+ ...opts
1552
+ };
1553
+ },
1554
+ injectFile: (userFileNode) => {
1555
+ this.fileManager.add((0, _kubb_ast.createFile)(userFileNode));
1556
+ }
1557
+ };
1558
+ return hooks["kubb:plugin:setup"](pluginCtx);
1559
+ };
1560
+ this.hooks.on("kubb:plugin:setup", setupHandler);
1561
+ this.#trackHookListener("kubb:plugin:setup", setupHandler);
1562
+ }
1563
+ for (const [event, handler] of Object.entries(hooks)) {
1564
+ if (event === "kubb:plugin:setup" || !handler) continue;
1565
+ this.hooks.on(event, handler);
1566
+ this.#trackHookListener(event, handler);
1567
+ }
1568
+ }
1569
+ /**
1570
+ * Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
1571
+ * can configure generators, resolvers, transformers and renderers before `buildStart` runs.
1572
+ *
1573
+ * Call this once from `safeBuild` before the plugin execution loop begins.
1574
+ */
1575
+ async emitSetupHooks() {
1576
+ const noop = () => {};
1577
+ await this.hooks.emit("kubb:plugin:setup", {
1578
+ config: this.config,
1579
+ options: {},
1580
+ addGenerator: noop,
1581
+ setResolver: noop,
1582
+ setTransformer: noop,
1583
+ setRenderer: noop,
1584
+ setOptions: noop,
1585
+ injectFile: noop,
1586
+ updateConfig: noop
1587
+ });
1588
+ }
1589
+ /**
1590
+ * Registers a generator for the given plugin on the shared event emitter.
1591
+ *
1592
+ * The generator's `schema`, `operation`, and `operations` methods are registered as
1593
+ * listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
1594
+ * respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
1595
+ * so that generators from different plugins do not cross-fire.
1596
+ *
1597
+ * The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
1598
+ * Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
1599
+ * declares a renderer.
1600
+ *
1601
+ * Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
1602
+ */
1603
+ registerGenerator(pluginName, gen) {
1604
+ const resolveRenderer = () => {
1605
+ const plugin = this.plugins.get(pluginName);
1606
+ return gen.renderer === null ? void 0 : gen.renderer ?? plugin?.renderer ?? this.config.renderer;
1607
+ };
1608
+ if (gen.schema) {
1609
+ const schemaHandler = async (node, ctx) => {
1610
+ if (ctx.plugin.name !== pluginName) return;
1611
+ await applyHookResult({
1612
+ result: await gen.schema(node, ctx),
1613
+ driver: this,
1614
+ rendererFactory: resolveRenderer()
1615
+ });
1616
+ };
1617
+ this.hooks.on("kubb:generate:schema", schemaHandler);
1618
+ this.#trackHookListener("kubb:generate:schema", schemaHandler);
1619
+ }
1620
+ if (gen.operation) {
1621
+ const operationHandler = async (node, ctx) => {
1622
+ if (ctx.plugin.name !== pluginName) return;
1623
+ await applyHookResult({
1624
+ result: await gen.operation(node, ctx),
1625
+ driver: this,
1626
+ rendererFactory: resolveRenderer()
1627
+ });
1628
+ };
1629
+ this.hooks.on("kubb:generate:operation", operationHandler);
1630
+ this.#trackHookListener("kubb:generate:operation", operationHandler);
1631
+ }
1632
+ if (gen.operations) {
1633
+ const operationsHandler = async (nodes, ctx) => {
1634
+ if (ctx.plugin.name !== pluginName) return;
1635
+ await applyHookResult({
1636
+ result: await gen.operations(nodes, ctx),
1637
+ driver: this,
1638
+ rendererFactory: resolveRenderer()
1639
+ });
1640
+ };
1641
+ this.hooks.on("kubb:generate:operations", operationsHandler);
1642
+ this.#trackHookListener("kubb:generate:operations", operationsHandler);
1643
+ }
1644
+ this.#eventGeneratorPlugins.add(pluginName);
1645
+ }
1646
+ /**
1647
+ * Returns `true` when at least one generator was registered for the given plugin
1648
+ * via `addGenerator()` in `kubb:plugin:setup` (event-based path).
1649
+ *
1650
+ * Used by the build loop to decide whether to walk the AST and emit generator events
1651
+ * for a plugin that has no static `plugin.generators`.
1652
+ */
1653
+ hasEventGenerators(pluginName) {
1654
+ return this.#eventGeneratorPlugins.has(pluginName);
1655
+ }
1656
+ /**
1657
+ * Runs the full plugin pipeline. Returns timings/failures collected so far even
1658
+ * when an outer hook throws — the orchestrator preserves partial state by capturing
1659
+ * the error into `error` instead of propagating.
1660
+ */
1661
+ async run({ storage }) {
1662
+ const hooks = this.hooks;
1663
+ const config = this.config;
1664
+ const failedPlugins = /* @__PURE__ */ new Set();
1665
+ const pluginTimings = /* @__PURE__ */ new Map();
1666
+ const parsersMap = /* @__PURE__ */ new Map();
1667
+ for (const parser of config.parsers) if (parser.extNames) for (const ext of parser.extNames) parsersMap.set(ext, parser);
1668
+ const pendingFiles = /* @__PURE__ */ new Map();
1669
+ this.fileManager.setOnUpsert((file) => {
1670
+ pendingFiles.set(file.path, file);
1671
+ });
1672
+ try {
1673
+ const flushPending = async () => {
1674
+ if (pendingFiles.size === 0) return;
1675
+ const files = [...pendingFiles.values()];
1676
+ pendingFiles.clear();
1677
+ await hooks.emit("kubb:debug", {
1678
+ date: /* @__PURE__ */ new Date(),
1679
+ logs: [`Writing ${files.length} files...`]
1680
+ });
1681
+ await hooks.emit("kubb:files:processing:start", { files });
1682
+ const items = [...this.#fileProcessor.stream(files, {
1683
+ parsers: parsersMap,
1684
+ extension: config.output.extension
1685
+ })];
1686
+ await hooks.emit("kubb:files:processing:update", { files: items.map(({ file, source, processed, total, percentage }) => ({
1687
+ file,
1688
+ source,
1689
+ processed,
1690
+ total,
1691
+ percentage,
1692
+ config
1693
+ })) });
1694
+ const queue = [];
1695
+ for (const { file, source } of items) if (source) {
1696
+ queue.push(storage.setItem(file.path, source));
1697
+ if (queue.length >= 50) await Promise.all(queue.splice(0));
1698
+ }
1699
+ await Promise.all(queue);
1700
+ await hooks.emit("kubb:files:processing:end", { files });
1701
+ await hooks.emit("kubb:debug", {
1702
+ date: /* @__PURE__ */ new Date(),
1703
+ logs: [`✓ File write process completed for ${files.length} files`]
1704
+ });
1705
+ };
1706
+ await this.emitSetupHooks();
1707
+ if (this.adapter && this.inputNode) await hooks.emit("kubb:build:start", Object.assign({
1708
+ config,
1709
+ adapter: this.adapter,
1710
+ meta: this.inputNode.meta,
1711
+ getPlugin: this.getPlugin.bind(this)
1712
+ }, this.#filesPayload()));
1713
+ const generatorPlugins = [];
1714
+ for (const plugin of this.plugins.values()) {
1715
+ const context = this.getContext(plugin);
1716
+ const hrStart = process.hrtime();
1717
+ try {
1718
+ await hooks.emit("kubb:plugin:start", { plugin });
1719
+ await hooks.emit("kubb:debug", {
1720
+ date: /* @__PURE__ */ new Date(),
1721
+ logs: ["Starting plugin...", ` • Plugin Name: ${plugin.name}`]
1722
+ });
1723
+ } catch (caughtError) {
1724
+ const error = caughtError;
1725
+ const duration = getElapsedMs(hrStart);
1726
+ pluginTimings.set(plugin.name, duration);
1727
+ await this.#emitPluginEnd({
1728
+ plugin,
1729
+ duration,
1730
+ success: false,
1731
+ error
1732
+ });
1733
+ failedPlugins.add({
1734
+ plugin,
1735
+ error
1736
+ });
1737
+ continue;
1738
+ }
1739
+ if (plugin.generators?.length || this.hasEventGenerators(plugin.name)) {
1740
+ generatorPlugins.push({
1741
+ plugin,
1742
+ context,
1743
+ hrStart
1744
+ });
1745
+ continue;
1746
+ }
1747
+ const duration = getElapsedMs(hrStart);
1748
+ pluginTimings.set(plugin.name, duration);
1749
+ await this.#emitPluginEnd({
1750
+ plugin,
1751
+ duration,
1752
+ success: true
1753
+ });
1754
+ await hooks.emit("kubb:debug", {
1755
+ date: /* @__PURE__ */ new Date(),
1756
+ logs: [`✓ Plugin started successfully (${formatMs(duration)})`]
1757
+ });
1758
+ }
1759
+ if (generatorPlugins.length > 0) if (this.inputNode) {
1760
+ const { timings, failed } = await this.#runGenerators(generatorPlugins, flushPending);
1761
+ await flushPending();
1762
+ for (const [name, duration] of timings) pluginTimings.set(name, duration);
1763
+ for (const entry of failed) failedPlugins.add(entry);
1764
+ } else for (const { plugin, hrStart } of generatorPlugins) {
1765
+ const duration = getElapsedMs(hrStart);
1766
+ pluginTimings.set(plugin.name, duration);
1767
+ await this.#emitPluginEnd({
1768
+ plugin,
1769
+ duration,
1770
+ success: true
1771
+ });
1772
+ }
1773
+ await hooks.emit("kubb:plugins:end", Object.assign({ config }, this.#filesPayload()));
1774
+ await flushPending();
1775
+ const files = this.fileManager.files;
1776
+ await hooks.emit("kubb:build:end", {
1777
+ files,
1778
+ config,
1779
+ outputDir: (0, node_path.resolve)(config.root, config.output.path)
1780
+ });
1781
+ return {
1782
+ failedPlugins,
1783
+ pluginTimings
1784
+ };
1785
+ } catch (caughtError) {
1786
+ return {
1787
+ failedPlugins,
1788
+ pluginTimings,
1789
+ error: caughtError
1790
+ };
1791
+ } finally {
1792
+ this.fileManager.setOnUpsert(null);
1793
+ }
1794
+ }
1795
+ #filesPayload() {
1796
+ const driver = this;
1797
+ return {
1798
+ get files() {
1799
+ return driver.fileManager.files;
1800
+ },
1801
+ upsertFile: (...files) => driver.fileManager.upsert(...files)
1802
+ };
1803
+ }
1804
+ #emitPluginEnd({ plugin, duration, success, error }) {
1805
+ return this.hooks.emit("kubb:plugin:end", Object.assign({
1806
+ plugin,
1807
+ duration,
1808
+ success,
1809
+ ...error ? { error } : {},
1810
+ config: this.config
1811
+ }, this.#filesPayload()));
1812
+ }
1813
+ async #runGenerators(entries, flushPending) {
1814
+ const timings = /* @__PURE__ */ new Map();
1815
+ const failed = /* @__PURE__ */ new Set();
1816
+ const driver = this;
1817
+ const { schemas, operations } = this.inputNode;
1818
+ const states = entries.map(({ plugin, context, hrStart }) => {
1819
+ const { exclude, include, override } = plugin.options;
1820
+ const hasExclude = Array.isArray(exclude) && exclude.length > 0;
1821
+ const hasInclude = Array.isArray(include) && include.length > 0;
1822
+ const hasOverride = Array.isArray(override) && override.length > 0;
1823
+ return {
1824
+ plugin,
1825
+ generatorContext: {
1826
+ ...context,
1827
+ resolver: this.getResolver(plugin.name)
1828
+ },
1829
+ generators: plugin.generators ?? [],
1830
+ hrStart,
1831
+ failed: false,
1832
+ error: null,
1833
+ optionsAreStatic: !hasExclude && !hasInclude && !hasOverride,
1834
+ allowedSchemaNames: null
1835
+ };
1836
+ });
1837
+ const emitsSchemaHook = this.hooks.listenerCount("kubb:generate:schema") > 0;
1838
+ const emitsOperationHook = this.hooks.listenerCount("kubb:generate:operation") > 0;
1839
+ const pruningStates = states.filter(({ plugin }) => {
1840
+ const { include } = plugin.options;
1841
+ return (include?.some(({ type }) => OPERATION_FILTER_TYPES.has(type)) ?? false) && !(include?.some(({ type }) => type === "schemaName") ?? false);
1842
+ });
1843
+ if (pruningStates.length > 0) {
1844
+ const allSchemas = [];
1845
+ for await (const schema of schemas) allSchemas.push(schema);
1846
+ const includedOpsByState = new Map(pruningStates.map((s) => [s, []]));
1847
+ for await (const operation of operations) for (const state of pruningStates) {
1848
+ const { exclude, include, override } = state.plugin.options;
1849
+ if (state.generatorContext.resolver.resolveOptions(operation, {
1850
+ options: state.plugin.options,
1851
+ exclude,
1852
+ include,
1853
+ override
1854
+ }) !== null) includedOpsByState.get(state)?.push(operation);
1855
+ }
1856
+ for (const state of pruningStates) {
1857
+ state.allowedSchemaNames = (0, _kubb_ast.collectUsedSchemaNames)(includedOpsByState.get(state) ?? [], allSchemas);
1858
+ includedOpsByState.delete(state);
1859
+ }
1860
+ }
1861
+ const resolveRendererFor = (gen, state) => gen.renderer === null ? void 0 : gen.renderer ?? state.plugin.renderer ?? state.generatorContext.config.renderer;
1862
+ const dispatchNode = async (state, node, dispatch) => {
1863
+ if (state.failed) return;
1864
+ try {
1865
+ const { plugin, generatorContext, generators } = state;
1866
+ const transformedNode = plugin.transformer ? (0, _kubb_ast.transform)(node, plugin.transformer) : node;
1867
+ if (dispatch.checkAllowedNames && state.allowedSchemaNames !== null && "name" in transformedNode && transformedNode.name && !state.allowedSchemaNames.has(transformedNode.name)) return;
1868
+ const { exclude, include, override } = plugin.options;
1869
+ const options = state.optionsAreStatic ? plugin.options : generatorContext.resolver.resolveOptions(transformedNode, {
1870
+ options: plugin.options,
1871
+ exclude,
1872
+ include,
1873
+ override
1874
+ });
1875
+ if (options === null) return;
1876
+ const ctx = {
1877
+ ...generatorContext,
1878
+ options
1879
+ };
1880
+ for (const gen of generators) {
1881
+ const generate = gen[dispatch.method];
1882
+ if (!generate) continue;
1883
+ const raw = generate(transformedNode, ctx);
1884
+ const applied = applyHookResult({
1885
+ result: isPromise(raw) ? await raw : raw,
1886
+ driver,
1887
+ rendererFactory: resolveRendererFor(gen, state)
1888
+ });
1889
+ if (isPromise(applied)) await applied;
1890
+ }
1891
+ if (dispatch.emit) await dispatch.emit(transformedNode, ctx);
1892
+ } catch (caughtError) {
1893
+ state.failed = true;
1894
+ state.error = caughtError;
1895
+ }
1896
+ };
1897
+ const schemaDispatch = {
1898
+ method: "schema",
1899
+ checkAllowedNames: true,
1900
+ emit: emitsSchemaHook ? (node, ctx) => this.hooks.emit("kubb:generate:schema", node, ctx) : null
1901
+ };
1902
+ const operationDispatch = {
1903
+ method: "operation",
1904
+ checkAllowedNames: false,
1905
+ emit: emitsOperationHook ? (node, ctx) => this.hooks.emit("kubb:generate:operation", node, ctx) : null
1906
+ };
1907
+ const needsCollectedOperations = this.hooks.listenerCount("kubb:generate:operations") > 0 || states.some((s) => s.generators.some((g) => !!g.operations));
1908
+ const collectedOperations = needsCollectedOperations ? [] : void 0;
1909
+ await forBatches(schemas, (nodes) => Promise.all(nodes.flatMap((n) => states.map((state) => dispatchNode(state, n, schemaDispatch)))), {
1910
+ concurrency: 8,
1911
+ flush: flushPending
1912
+ });
1913
+ await forBatches(operations, (nodes) => {
1914
+ if (needsCollectedOperations) collectedOperations.push(...nodes);
1915
+ return Promise.all(nodes.flatMap((n) => states.map((state) => dispatchNode(state, n, operationDispatch))));
1916
+ }, {
1917
+ concurrency: 8,
1918
+ flush: flushPending
1919
+ });
1920
+ for (const state of states) {
1921
+ if (!state.failed && needsCollectedOperations) try {
1922
+ const { plugin, generatorContext, generators } = state;
1923
+ const ctx = {
1924
+ ...generatorContext,
1925
+ options: plugin.options
1926
+ };
1927
+ const pluginOperations = state.optionsAreStatic ? collectedOperations : collectedOperations.filter((node) => {
1928
+ const transformed = plugin.transformer ? (0, _kubb_ast.transform)(node, plugin.transformer) : node;
1929
+ const { exclude, include, override } = plugin.options;
1930
+ return generatorContext.resolver.resolveOptions(transformed, {
1931
+ options: plugin.options,
1932
+ exclude,
1933
+ include,
1934
+ override
1935
+ }) !== null;
1936
+ });
1937
+ for (const gen of generators) {
1938
+ if (!gen.operations) continue;
1939
+ await applyHookResult({
1940
+ result: await gen.operations(pluginOperations, ctx),
1941
+ driver,
1942
+ rendererFactory: resolveRendererFor(gen, state)
1943
+ });
1944
+ }
1945
+ await this.hooks.emit("kubb:generate:operations", pluginOperations, ctx);
1946
+ } catch (caughtError) {
1947
+ state.failed = true;
1948
+ state.error = caughtError;
1949
+ }
1950
+ const duration = getElapsedMs(state.hrStart);
1951
+ timings.set(state.plugin.name, duration);
1952
+ await this.#emitPluginEnd({
1953
+ plugin: state.plugin,
1954
+ duration,
1955
+ success: !state.failed,
1956
+ error: state.failed && state.error ? state.error : void 0
1957
+ });
1958
+ if (state.failed && state.error) failed.add({
1959
+ plugin: state.plugin,
1960
+ error: state.error
1961
+ });
1962
+ await this.hooks.emit("kubb:debug", {
1963
+ date: /* @__PURE__ */ new Date(),
1964
+ logs: [state.failed ? "✗ Plugin start failed" : `✓ Plugin started successfully (${formatMs(duration)})`]
1965
+ });
1966
+ }
1967
+ return {
1968
+ timings,
1969
+ failed
1970
+ };
1971
+ }
1972
+ /**
1973
+ * Unregisters all plugin lifecycle listeners from the shared event emitter.
1974
+ * Called at the end of a build to prevent listener leaks across repeated builds.
1975
+ *
1976
+ * @internal
1977
+ */
1978
+ dispose() {
1979
+ for (const [event, handlers] of this.#hookListeners) for (const handler of handlers) this.hooks.off(event, handler);
1980
+ this.#hookListeners.clear();
1981
+ this.#eventGeneratorPlugins.clear();
1982
+ this.#resolvers.clear();
1983
+ this.#defaultResolvers.clear();
1984
+ this.fileManager.dispose();
1985
+ this.#fileProcessor.dispose();
1986
+ this.inputNode = null;
1987
+ this.#studio = {
1988
+ source: null,
1989
+ isOpen: false,
1990
+ inputNode: null
1991
+ };
1992
+ for (const [event, handler] of this.#middlewareListeners) this.hooks.off(event, handler);
1993
+ }
1994
+ [Symbol.dispose]() {
1995
+ this.dispose();
1996
+ }
1997
+ #trackHookListener(event, handler) {
1998
+ let handlers = this.#hookListeners.get(event);
1999
+ if (!handlers) {
2000
+ handlers = /* @__PURE__ */ new Set();
2001
+ this.#hookListeners.set(event, handlers);
2002
+ }
2003
+ handlers.add(handler);
2004
+ }
2005
+ #getDefaultResolver = memoize(this.#defaultResolvers, (pluginName) => defineResolver(() => ({
2006
+ name: "default",
2007
+ pluginName
2008
+ })));
2009
+ /**
2010
+ * Merges `partial` with the plugin's default resolver and stores the result.
2011
+ * Also mirrors it onto `plugin.resolver` so callers using `getPlugin(name).resolver`
2012
+ * get the up-to-date resolver without going through `getResolver()`.
2013
+ */
2014
+ setPluginResolver(pluginName, partial) {
2015
+ const merged = {
2016
+ ...this.#getDefaultResolver(pluginName),
2017
+ ...partial
2018
+ };
2019
+ this.#resolvers.set(pluginName, merged);
2020
+ const plugin = this.plugins.get(pluginName);
2021
+ if (plugin) plugin.resolver = merged;
2022
+ }
2023
+ getResolver(pluginName) {
2024
+ return this.#resolvers.get(pluginName) ?? this.plugins.get(pluginName)?.resolver ?? this.#getDefaultResolver(pluginName);
2025
+ }
2026
+ getContext(plugin) {
2027
+ const driver = this;
2028
+ return {
2029
+ config: driver.config,
2030
+ get root() {
2031
+ return (0, node_path.resolve)(driver.config.root, driver.config.output.path);
2032
+ },
2033
+ getMode(output) {
2034
+ return KubbDriver.getMode((0, node_path.resolve)(driver.config.root, driver.config.output.path, output.path));
2035
+ },
2036
+ hooks: driver.hooks,
2037
+ plugin,
2038
+ getPlugin: driver.getPlugin.bind(driver),
2039
+ requirePlugin: driver.requirePlugin.bind(driver),
2040
+ getResolver: driver.getResolver.bind(driver),
2041
+ driver,
2042
+ addFile: async (...files) => {
2043
+ driver.fileManager.add(...files);
2044
+ },
2045
+ upsertFile: async (...files) => {
2046
+ driver.fileManager.upsert(...files);
2047
+ },
2048
+ get meta() {
2049
+ return driver.inputNode?.meta ?? {
2050
+ circularNames: [],
2051
+ enumNames: []
2052
+ };
2053
+ },
2054
+ get adapter() {
2055
+ return driver.adapter;
2056
+ },
2057
+ get resolver() {
2058
+ return driver.getResolver(plugin.name);
2059
+ },
2060
+ get transformer() {
2061
+ return plugin.transformer;
2062
+ },
2063
+ warn(message) {
2064
+ driver.hooks.emit("kubb:warn", { message });
2065
+ },
2066
+ error(error) {
2067
+ driver.hooks.emit("kubb:error", { error: typeof error === "string" ? new Error(error) : error });
2068
+ },
2069
+ info(message) {
2070
+ driver.hooks.emit("kubb:info", { message });
2071
+ },
2072
+ async openInStudio(options) {
2073
+ if (!driver.config.devtools || driver.#studio.isOpen) return;
2074
+ if (typeof driver.config.devtools !== "object") throw new Error("Devtools must be an object");
2075
+ if (!driver.adapter || !driver.#studio.source) throw new Error("adapter is not defined, make sure you have set the parser in kubb.config.ts");
2076
+ driver.#studio.isOpen = true;
2077
+ const studioUrl = driver.config.devtools?.studioUrl ?? "https://kubb.studio";
2078
+ driver.#studio.inputNode ??= Promise.resolve(driver.adapter.parse(driver.#studio.source));
2079
+ return openInStudio(await driver.#studio.inputNode, studioUrl, options);
2080
+ }
2081
+ };
2082
+ }
2083
+ getPlugin(pluginName) {
2084
+ return this.plugins.get(pluginName);
2085
+ }
2086
+ requirePlugin(pluginName) {
2087
+ const plugin = this.plugins.get(pluginName);
2088
+ if (!plugin) throw new Error(`[kubb] Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`);
2089
+ return plugin;
2090
+ }
2091
+ };
2092
+ /**
2093
+ * Handles the return value of a plugin AST hook or generator method.
2094
+ *
2095
+ * - Renderer output → rendered via the provided `rendererFactory` (e.g. JSX), files stored in `driver.fileManager`
2096
+ * - `Array<FileNode>` → added directly into `driver.fileManager`
2097
+ * - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
2098
+ *
2099
+ * Pass a `rendererFactory` (e.g. `jsxRenderer` from `@kubb/renderer-jsx`) when the result
2100
+ * may be a renderer element. Generators that only return `Array<FileNode>` do not need one.
2101
+ */
2102
+ function applyHookResult({ result, driver, rendererFactory }) {
2103
+ if (!result) return;
2104
+ if (Array.isArray(result)) {
2105
+ driver.fileManager.upsert(...result);
2106
+ return;
2107
+ }
2108
+ if (!rendererFactory) return;
2109
+ const renderer = rendererFactory();
2110
+ if (renderer.stream) try {
2111
+ var _usingCtx$1 = _usingCtx();
2112
+ const r = _usingCtx$1.u(renderer);
2113
+ for (const file of r.stream(result)) driver.fileManager.upsert(file);
2114
+ return;
2115
+ } catch (_) {
2116
+ _usingCtx$1.e = _;
2117
+ } finally {
2118
+ _usingCtx$1.d();
2119
+ }
2120
+ return applyAsyncRender({
2121
+ renderer,
2122
+ result,
2123
+ driver
2124
+ });
2125
+ }
2126
+ async function applyAsyncRender({ renderer, result, driver }) {
2127
+ try {
2128
+ var _usingCtx3 = _usingCtx();
2129
+ const r = _usingCtx3.u(renderer);
2130
+ await r.render(result);
2131
+ driver.fileManager.upsert(...r.files);
2132
+ } catch (_) {
2133
+ _usingCtx3.e = _;
2134
+ } finally {
2135
+ _usingCtx3.d();
2136
+ }
2137
+ }
2138
+ function inputToAdapterSource(config) {
2139
+ const input = config.input;
2140
+ if (!input) throw new Error("[kubb] input is required when using an adapter. Provide input.path or input.data in your config.");
2141
+ if ("data" in input) return {
2142
+ type: "data",
2143
+ data: input.data
2144
+ };
2145
+ if (new URLPath(input.path).isURL) return {
2146
+ type: "path",
2147
+ path: input.path
2148
+ };
2149
+ return {
2150
+ type: "path",
2151
+ path: (0, node_path.resolve)(config.root, input.path)
2152
+ };
2153
+ }
2154
+ //#endregion
2155
+ Object.defineProperty(exports, "AsyncEventEmitter", {
2156
+ enumerable: true,
2157
+ get: function() {
2158
+ return AsyncEventEmitter;
2159
+ }
2160
+ });
2161
+ Object.defineProperty(exports, "BuildError", {
2162
+ enumerable: true,
2163
+ get: function() {
2164
+ return BuildError;
2165
+ }
2166
+ });
2167
+ Object.defineProperty(exports, "DEFAULT_BANNER", {
2168
+ enumerable: true,
2169
+ get: function() {
2170
+ return DEFAULT_BANNER;
2171
+ }
2172
+ });
2173
+ Object.defineProperty(exports, "DEFAULT_EXTENSION", {
2174
+ enumerable: true,
2175
+ get: function() {
2176
+ return DEFAULT_EXTENSION;
2177
+ }
2178
+ });
2179
+ Object.defineProperty(exports, "DEFAULT_STUDIO_URL", {
2180
+ enumerable: true,
2181
+ get: function() {
2182
+ return DEFAULT_STUDIO_URL;
2183
+ }
2184
+ });
2185
+ Object.defineProperty(exports, "FileManager", {
2186
+ enumerable: true,
2187
+ get: function() {
2188
+ return FileManager;
2189
+ }
2190
+ });
2191
+ Object.defineProperty(exports, "FileProcessor", {
2192
+ enumerable: true,
2193
+ get: function() {
2194
+ return FileProcessor;
2195
+ }
2196
+ });
2197
+ Object.defineProperty(exports, "KubbDriver", {
2198
+ enumerable: true,
2199
+ get: function() {
2200
+ return KubbDriver;
2201
+ }
2202
+ });
2203
+ Object.defineProperty(exports, "URLPath", {
2204
+ enumerable: true,
2205
+ get: function() {
2206
+ return URLPath;
2207
+ }
2208
+ });
2209
+ Object.defineProperty(exports, "__name", {
2210
+ enumerable: true,
2211
+ get: function() {
2212
+ return __name;
2213
+ }
2214
+ });
2215
+ Object.defineProperty(exports, "__toESM", {
2216
+ enumerable: true,
2217
+ get: function() {
2218
+ return __toESM;
2219
+ }
2220
+ });
2221
+ Object.defineProperty(exports, "_usingCtx", {
2222
+ enumerable: true,
2223
+ get: function() {
2224
+ return _usingCtx;
2225
+ }
2226
+ });
2227
+ Object.defineProperty(exports, "applyHookResult", {
2228
+ enumerable: true,
2229
+ get: function() {
2230
+ return applyHookResult;
2231
+ }
2232
+ });
2233
+ Object.defineProperty(exports, "definePlugin", {
2234
+ enumerable: true,
2235
+ get: function() {
2236
+ return definePlugin;
2237
+ }
2238
+ });
2239
+ Object.defineProperty(exports, "defineResolver", {
2240
+ enumerable: true,
2241
+ get: function() {
2242
+ return defineResolver;
2243
+ }
2244
+ });
2245
+ Object.defineProperty(exports, "logLevel", {
2246
+ enumerable: true,
2247
+ get: function() {
2248
+ return logLevel;
2249
+ }
2250
+ });
2251
+
2252
+ //# sourceMappingURL=KubbDriver-vyD7F0Ip.cjs.map