@kubb/core 5.0.0-beta.22 → 5.0.0-beta.23

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,5 +1,5 @@
1
1
  import "./chunk--u3MIqq1.js";
2
- import { a as FileManager, c as DEFAULT_BANNER, d as logLevel, f as URLPath, i as FileProcessor, l as DEFAULT_EXTENSION, m as BuildError, o as defineResolver, p as AsyncEventEmitter, r as _usingCtx, s as definePlugin, t as KubbDriver, u as DEFAULT_STUDIO_URL } from "./KubbDriver-l31wllgN.js";
2
+ import { a as FileManager, c as DEFAULT_BANNER, d as logLevel, f as URLPath, i as FileProcessor, l as DEFAULT_EXTENSION, m as BuildError, o as defineResolver, p as AsyncEventEmitter, r as _usingCtx, s as definePlugin, t as KubbDriver, u as DEFAULT_STUDIO_URL } from "./KubbDriver-BkwOamHp.js";
3
3
  import { access, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
4
4
  import { dirname, join, resolve } from "node:path";
5
5
  import * as ast from "@kubb/ast";
@@ -72,28 +72,33 @@ async function clean(path) {
72
72
  //#endregion
73
73
  //#region src/createAdapter.ts
74
74
  /**
75
- * Factory for implementing custom adapters that translate non-OpenAPI specs into Kubb's AST.
75
+ * Defines a custom adapter that translates a spec format into Kubb's universal
76
+ * AST. Use this when you need to consume GraphQL, gRPC, AsyncAPI, or another
77
+ * domain-specific schema. Built-in adapters: `@kubb/adapter-oas` for
78
+ * OpenAPI/Swagger documents.
76
79
  *
77
- * Use this to support GraphQL schemas, gRPC definitions, AsyncAPI, or custom domain-specific languages.
78
- * Built-in adapters include `@kubb/adapter-oas` for OpenAPI and Swagger documents.
79
- *
80
- * @note Adapters must parse their input format to Kubb's `InputNode` structure.
80
+ * Adapters must return an `InputNode` from `parse`. That node is what every
81
+ * plugin in the build consumes.
81
82
  *
82
83
  * @example
83
84
  * ```ts
84
- * export const myAdapter = createAdapter<MyAdapter>((options) => {
85
- * return {
86
- * name: 'my-adapter',
87
- * options,
88
- * async parse(source) {
89
- * // Transform source format to InputNode
90
- * return { ... }
91
- * },
92
- * }
93
- * })
85
+ * import { createAdapter, ast, type AdapterFactoryOptions } from '@kubb/core'
86
+ *
87
+ * type MyAdapter = AdapterFactoryOptions<'my-adapter', { validate?: boolean }>
94
88
  *
95
- * // Instantiate:
96
- * const adapter = myAdapter({ validate: true })
89
+ * export const myAdapter = createAdapter<MyAdapter>((options) => ({
90
+ * name: 'my-adapter',
91
+ * options,
92
+ * document: null,
93
+ * async parse(_source) {
94
+ * // Convert `source` (path or inline data) into an InputNode.
95
+ * return ast.createInput()
96
+ * },
97
+ * getImports: () => [],
98
+ * async validate() {
99
+ * // Throw or call ctx.error here when the spec is invalid.
100
+ * },
101
+ * }))
97
102
  * ```
98
103
  */
99
104
  function createAdapter(build) {
@@ -101,39 +106,45 @@ function createAdapter(build) {
101
106
  }
102
107
  //#endregion
103
108
  //#region package.json
104
- var version$1 = "5.0.0-beta.22";
109
+ var version$1 = "5.0.0-beta.23";
105
110
  //#endregion
106
111
  //#region src/createStorage.ts
107
112
  /**
108
- * Factory for implementing custom storage backends that control where generated files are written.
113
+ * Defines a custom storage backend. The builder receives user options and
114
+ * returns a `Storage` implementation. Kubb ships with filesystem and
115
+ * in-memory storages — reach for this when you need to write generated files
116
+ * elsewhere (cloud storage, a database, a remote API).
109
117
  *
110
- * Takes a builder function `(options: TOptions) => Storage` and returns a factory `(options?: TOptions) => Storage`.
111
- * Kubb provides filesystem and in-memory implementations out of the box.
112
- *
113
- * @note Call the returned factory with optional options to instantiate the storage adapter.
114
- *
115
- * @example
118
+ * @example In-memory storage (the built-in implementation)
116
119
  * ```ts
117
120
  * import { createStorage } from '@kubb/core'
118
121
  *
119
122
  * export const memoryStorage = createStorage(() => {
120
123
  * const store = new Map<string, string>()
124
+ *
121
125
  * return {
122
126
  * name: 'memory',
123
- * async hasItem(key) { return store.has(key) },
124
- * async getItem(key) { return store.get(key) ?? null },
125
- * async setItem(key, value) { store.set(key, value) },
126
- * async removeItem(key) { store.delete(key) },
127
+ * async hasItem(key) {
128
+ * return store.has(key)
129
+ * },
130
+ * async getItem(key) {
131
+ * return store.get(key) ?? null
132
+ * },
133
+ * async setItem(key, value) {
134
+ * store.set(key, value)
135
+ * },
136
+ * async removeItem(key) {
137
+ * store.delete(key)
138
+ * },
127
139
  * async getKeys(base) {
128
140
  * const keys = [...store.keys()]
129
141
  * return base ? keys.filter((k) => k.startsWith(base)) : keys
130
142
  * },
131
- * async clear(base) { if (!base) store.clear() },
143
+ * async clear(base) {
144
+ * if (!base) store.clear()
145
+ * },
132
146
  * }
133
147
  * })
134
- *
135
- * // Instantiate:
136
- * const storage = memoryStorage()
137
148
  * ```
138
149
  */
139
150
  function createStorage(build) {
@@ -422,8 +433,24 @@ var Kubb = class {
422
433
  }
423
434
  };
424
435
  /**
425
- * Factory for {@link Kubb}. Equivalent to `new Kubb(userConfig, options)` and kept
426
- * as the canonical public entry point.
436
+ * Constructs a {@link Kubb} build orchestrator from a user config. Equivalent
437
+ * to `new Kubb(userConfig, options)` and the canonical public entry point.
438
+ *
439
+ * @example
440
+ * ```ts
441
+ * import { createKubb } from '@kubb/core'
442
+ * import { adapterOas } from '@kubb/adapter-oas'
443
+ * import { pluginTs } from '@kubb/plugin-ts'
444
+ *
445
+ * const kubb = createKubb({
446
+ * input: { path: './petStore.yaml' },
447
+ * output: { path: './src/gen' },
448
+ * adapter: adapterOas(),
449
+ * plugins: [pluginTs()],
450
+ * })
451
+ *
452
+ * await kubb.build()
453
+ * ```
427
454
  */
428
455
  function createKubb(userConfig, options = {}) {
429
456
  return new Kubb(userConfig, options);
@@ -431,17 +458,37 @@ function createKubb(userConfig, options = {}) {
431
458
  //#endregion
432
459
  //#region src/createRenderer.ts
433
460
  /**
434
- * Wraps a renderer factory for use in generator definitions.
461
+ * Defines a renderer factory. Renderers turn the generator's return value
462
+ * (JSX, a template string, a tree of any shape) into `FileNode`s that get
463
+ * written to disk.
435
464
  *
436
- * @example
465
+ * Use this to support output formats beyond JSX — for instance, a Handlebars
466
+ * renderer, a string-template renderer, or a renderer that writes binary
467
+ * files. Plugins and generators pick the renderer to use via the `renderer`
468
+ * field on `defineGenerator`.
469
+ *
470
+ * @example A minimal renderer that wraps a custom runtime
437
471
  * ```ts
438
- * export const jsxRenderer = createRenderer(() => {
439
- * const runtime = new Runtime()
472
+ * import { createRenderer } from '@kubb/core'
473
+ *
474
+ * export const myRenderer = createRenderer(() => {
475
+ * const runtime = new MyRuntime()
440
476
  * return {
441
- * async render(element) { await runtime.render(element) },
442
- * get files() { return runtime.nodes },
443
- * dispose() { runtime.unmount() },
444
- * unmount(error) { runtime.unmount(error) },
477
+ * async render(element) {
478
+ * await runtime.render(element)
479
+ * },
480
+ * get files() {
481
+ * return runtime.files
482
+ * },
483
+ * dispose() {
484
+ * runtime.dispose()
485
+ * },
486
+ * unmount(error) {
487
+ * runtime.dispose(error)
488
+ * },
489
+ * [Symbol.dispose]() {
490
+ * this.dispose()
491
+ * },
445
492
  * }
446
493
  * })
447
494
  * ```
@@ -452,9 +499,32 @@ function createRenderer(factory) {
452
499
  //#endregion
453
500
  //#region src/defineGenerator.ts
454
501
  /**
455
- * Defines a generator. Returns the object as-is with correct `this` typings.
456
- * `applyHookResult` handles renderer elements and `File[]` uniformly using
457
- * the generator's declared `renderer` factory.
502
+ * Defines a generator: a unit of work that runs during the plugin's AST walk
503
+ * and produces files. Plugins register generators via `ctx.addGenerator()`
504
+ * inside `kubb:plugin:setup`.
505
+ *
506
+ * The returned object is the input as-is, but with `this` types preserved so
507
+ * `schema`/`operation`/`operations` methods are correctly typed against the
508
+ * plugin's `PluginFactoryOptions`. Renderer elements and `FileNode[]` returns
509
+ * are both handled by the runtime — pick whichever style fits.
510
+ *
511
+ * @example JSX-based schema generator
512
+ * ```tsx
513
+ * import { defineGenerator } from '@kubb/core'
514
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
515
+ *
516
+ * export const typeGenerator = defineGenerator({
517
+ * name: 'typescript',
518
+ * renderer: jsxRenderer,
519
+ * schema(node, ctx) {
520
+ * return (
521
+ * <File path={`${ctx.root}/${node.name}.ts`}>
522
+ * <Type node={node} resolver={ctx.resolver} />
523
+ * </File>
524
+ * )
525
+ * },
526
+ * })
527
+ * ```
458
528
  */
459
529
  function defineGenerator(generator) {
460
530
  return generator;
@@ -462,30 +532,33 @@ function defineGenerator(generator) {
462
532
  //#endregion
463
533
  //#region src/defineLogger.ts
464
534
  /**
465
- * Wraps a logger definition into a typed {@link Logger}.
466
- *
467
- * The optional second type parameter `TInstallReturn` allows loggers to return
468
- * a value from `install` — for example, a sink factory that the caller can
469
- * forward to hook execution.
535
+ * Defines a typed logger. Use the second type parameter to declare a return
536
+ * value from `install`, which is handy when the logger exposes a sink factory
537
+ * or cleanup callback to the caller.
470
538
  *
471
539
  * @example Basic logger
472
540
  * ```ts
541
+ * import { defineLogger } from '@kubb/core'
542
+ *
473
543
  * export const myLogger = defineLogger({
474
544
  * name: 'my-logger',
475
- * install(context, options) {
476
- * context.on('kubb:info', (message) => console.log('ℹ', message))
477
- * context.on('kubb:error', (error) => console.error('✗', error.message))
545
+ * install(context) {
546
+ * context.on('kubb:info', ({ message }) => console.log('ℹ', message))
547
+ * context.on('kubb:error', ({ error }) => console.error('✗', error.message))
478
548
  * },
479
549
  * })
480
550
  * ```
481
551
  *
482
552
  * @example Logger that returns a hook sink factory
483
553
  * ```ts
554
+ * import { defineLogger, type LoggerOptions } from '@kubb/core'
555
+ * import type { HookSinkFactory } from './sinks'
556
+ *
484
557
  * export const myLogger = defineLogger<LoggerOptions, HookSinkFactory>({
485
558
  * name: 'my-logger',
486
- * install(context, options) {
559
+ * install(context) {
487
560
  * // … register event handlers …
488
- * return (commandWithArgs) => ({ onStdout: console.log })
561
+ * return () => ({ onStdout: console.log })
489
562
  * },
490
563
  * })
491
564
  * ```
@@ -496,18 +569,17 @@ function defineLogger(logger) {
496
569
  //#endregion
497
570
  //#region src/defineMiddleware.ts
498
571
  /**
499
- * Creates a middleware factory using the hook-style `hooks` API.
500
- *
501
- * Middleware handlers fire after all plugin handlers for any given event, making them ideal for post-processing, logging, and auditing.
502
- * Per-build state (such as accumulators) belongs inside the factory closure so each `createKubb` invocation gets its own isolated instance.
572
+ * Creates a middleware factory. Middleware fires after every plugin handler
573
+ * for the same event, which makes it the natural place for post-processing
574
+ * (barrel files, lint runs, audit logs).
503
575
  *
504
- * @note The factory can accept typed options. See examples for using options and per-build state patterns.
576
+ * Per-build state belongs inside the factory closure so each `createKubb`
577
+ * invocation gets its own isolated instance.
505
578
  *
506
- * @example
579
+ * @example Stateless middleware
507
580
  * ```ts
508
581
  * import { defineMiddleware } from '@kubb/core'
509
582
  *
510
- * // Stateless middleware
511
583
  * export const logMiddleware = defineMiddleware(() => ({
512
584
  * name: 'log-middleware',
513
585
  * hooks: {
@@ -516,8 +588,12 @@ function defineLogger(logger) {
516
588
  * },
517
589
  * },
518
590
  * }))
591
+ * ```
592
+ *
593
+ * @example Middleware with options and per-build state
594
+ * ```ts
595
+ * import { defineMiddleware } from '@kubb/core'
519
596
  *
520
- * // Middleware with options and per-build state
521
597
  * export const prefixMiddleware = defineMiddleware((options: { prefix: string } = { prefix: '' }) => {
522
598
  * const seen = new Set<string>()
523
599
  * return {
@@ -537,20 +613,20 @@ function defineMiddleware(factory) {
537
613
  //#endregion
538
614
  //#region src/defineParser.ts
539
615
  /**
540
- * Defines a parser with type safety. Creates parsers that transform generated files to strings based on their extension.
541
- *
542
- * @note Call the returned factory with optional options to instantiate the parser.
616
+ * Defines a parser with type-safe `this`. Used to register handlers for new
617
+ * file extensions or to plug a non-TypeScript output into the build.
543
618
  *
544
619
  * @example
545
620
  * ```ts
546
- * import { defineParser } from '@kubb/core'
621
+ * import { defineParser, ast } from '@kubb/core'
547
622
  *
548
623
  * export const jsonParser = defineParser({
549
624
  * name: 'json',
550
625
  * extNames: ['.json'],
551
626
  * parse(file) {
552
- * const { extractStringsFromNodes } = await import('@kubb/ast')
553
- * return file.sources.map((s) => extractStringsFromNodes(s.nodes ?? [])).join('\n')
627
+ * return file.sources
628
+ * .map((source) => ast.extractStringsFromNodes(source.nodes ?? []))
629
+ * .join('\n')
554
630
  * },
555
631
  * })
556
632
  * ```