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

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