@kubb/core 5.0.0-beta.4 → 5.0.0-beta.40
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/README.md +15 -148
- package/dist/diagnostics-DhfW8YpT.d.ts +2963 -0
- package/dist/index.cjs +775 -1193
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +165 -171
- package/dist/index.js +760 -1188
- package/dist/index.js.map +1 -1
- package/dist/memoryStorage-DTv1Kub1.js +2852 -0
- package/dist/memoryStorage-DTv1Kub1.js.map +1 -0
- package/dist/memoryStorage-Dkxnid2K.cjs +2985 -0
- package/dist/memoryStorage-Dkxnid2K.cjs.map +1 -0
- package/dist/mocks.cjs +80 -18
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.ts +35 -8
- package/dist/mocks.js +81 -21
- package/dist/mocks.js.map +1 -1
- package/package.json +8 -19
- package/src/FileManager.ts +85 -63
- package/src/FileProcessor.ts +171 -43
- package/src/HookRegistry.ts +45 -0
- package/src/KubbDriver.ts +897 -0
- package/src/Telemetry.ts +278 -0
- package/src/Transform.ts +58 -0
- package/src/constants.ts +111 -19
- package/src/createAdapter.ts +113 -17
- package/src/createKubb.ts +921 -493
- package/src/createRenderer.ts +58 -27
- package/src/createReporter.ts +147 -0
- package/src/createStorage.ts +36 -23
- package/src/defineGenerator.ts +129 -17
- package/src/defineLogger.ts +78 -7
- package/src/defineMiddleware.ts +19 -17
- package/src/defineParser.ts +30 -13
- package/src/definePlugin.ts +320 -17
- package/src/defineResolver.ts +381 -179
- package/src/diagnostics.ts +660 -0
- package/src/index.ts +8 -6
- package/src/mocks.ts +92 -19
- package/src/reporters/cliReporter.ts +90 -0
- package/src/reporters/fileReporter.ts +103 -0
- package/src/reporters/jsonReporter.ts +20 -0
- package/src/reporters/report.ts +85 -0
- package/src/storages/fsStorage.ts +13 -37
- package/src/types.ts +59 -1297
- package/dist/PluginDriver-Ds-Us-e4.cjs +0 -1036
- package/dist/PluginDriver-Ds-Us-e4.cjs.map +0 -1
- package/dist/PluginDriver-Wi34Pegx.js +0 -945
- package/dist/PluginDriver-Wi34Pegx.js.map +0 -1
- package/dist/types-Cd0jhNmx.d.ts +0 -2153
- package/src/Kubb.ts +0 -300
- package/src/PluginDriver.ts +0 -424
- package/src/devtools.ts +0 -59
- package/src/renderNode.ts +0 -35
- package/src/utils/diagnostics.ts +0 -18
- package/src/utils/isInputPath.ts +0 -10
- package/src/utils/packageJSON.ts +0 -99
- /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
package/src/createRenderer.ts
CHANGED
|
@@ -3,53 +3,84 @@ import type { FileNode } from '@kubb/ast'
|
|
|
3
3
|
/**
|
|
4
4
|
* Minimal interface any Kubb renderer must satisfy.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* This allows core to drive rendering without a hard dependency on
|
|
12
|
-
* `@kubb/renderer-jsx` or any specific renderer implementation.
|
|
6
|
+
* `TElement` is the type the renderer accepts, for example `KubbReactElement`
|
|
7
|
+
* for `@kubb/renderer-jsx` or a custom type for your own renderer. Defaults to
|
|
8
|
+
* `unknown` so generators that don't care about the element type work without
|
|
9
|
+
* specifying it.
|
|
13
10
|
*/
|
|
14
11
|
export type Renderer<TElement = unknown> = {
|
|
12
|
+
/**
|
|
13
|
+
* Renders `element` and populates {@link files} with the resulting {@link FileNode} objects.
|
|
14
|
+
* Called once per render cycle. Must resolve before {@link files} is read.
|
|
15
|
+
*/
|
|
15
16
|
render(element: TElement): Promise<void>
|
|
17
|
+
/**
|
|
18
|
+
* Tears down the renderer and releases any held resources.
|
|
19
|
+
* Pass an `Error` to signal a failure, a number for an exit code, or omit for a clean shutdown.
|
|
20
|
+
*/
|
|
16
21
|
unmount(error?: Error | number | null): void
|
|
22
|
+
/**
|
|
23
|
+
* Releases any held resources. `[Symbol.dispose]` delegates here.
|
|
24
|
+
*/
|
|
25
|
+
dispose(): void
|
|
26
|
+
/**
|
|
27
|
+
* Accumulated {@link FileNode} results produced by the last {@link render} call.
|
|
28
|
+
* Not populated when {@link stream} is implemented.
|
|
29
|
+
*/
|
|
17
30
|
readonly files: Array<FileNode>
|
|
31
|
+
/**
|
|
32
|
+
* When present, core calls this instead of {@link render} and {@link files},
|
|
33
|
+
* forwarding each file to `FileManager` as soon as it is ready.
|
|
34
|
+
*/
|
|
35
|
+
stream?(element: TElement): Iterable<FileNode>
|
|
36
|
+
/**
|
|
37
|
+
* Disposer hook so renderers participate in `using` blocks: `using r = rendererFactory()`
|
|
38
|
+
* guarantees {@link dispose} runs on every exit path, including thrown errors.
|
|
39
|
+
*/
|
|
40
|
+
[Symbol.dispose](): void
|
|
18
41
|
}
|
|
19
42
|
|
|
20
43
|
/**
|
|
21
|
-
* A factory function that produces a fresh {@link Renderer} per render.
|
|
44
|
+
* A factory function that produces a fresh {@link Renderer} per render cycle.
|
|
22
45
|
*
|
|
23
46
|
* Generators use this to declare which renderer handles their output.
|
|
24
47
|
*/
|
|
25
48
|
export type RendererFactory<TElement = unknown> = () => Renderer<TElement>
|
|
26
49
|
|
|
27
50
|
/**
|
|
28
|
-
*
|
|
51
|
+
* Defines a renderer factory. Renderers turn the generator's return value
|
|
52
|
+
* (JSX, a template string, a tree of any shape) into `FileNode`s that get
|
|
53
|
+
* written to disk.
|
|
29
54
|
*
|
|
30
|
-
*
|
|
31
|
-
* renderer
|
|
32
|
-
* to
|
|
55
|
+
* Use this to support output formats beyond JSX, for instance, a Handlebars
|
|
56
|
+
* renderer, a string-template renderer, or a renderer that writes binary
|
|
57
|
+
* files. Plugins and generators pick the renderer to use via the `renderer`
|
|
58
|
+
* field on `defineGenerator`.
|
|
33
59
|
*
|
|
34
|
-
* @example
|
|
60
|
+
* @example A minimal renderer that wraps a custom runtime
|
|
35
61
|
* ```ts
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
62
|
+
* import { createRenderer } from '@kubb/core'
|
|
63
|
+
*
|
|
64
|
+
* export const myRenderer = createRenderer(() => {
|
|
65
|
+
* const runtime = new MyRuntime()
|
|
39
66
|
* return {
|
|
40
|
-
* async render(element) {
|
|
41
|
-
*
|
|
42
|
-
*
|
|
67
|
+
* async render(element) {
|
|
68
|
+
* await runtime.render(element)
|
|
69
|
+
* },
|
|
70
|
+
* get files() {
|
|
71
|
+
* return runtime.files
|
|
72
|
+
* },
|
|
73
|
+
* dispose() {
|
|
74
|
+
* runtime.dispose()
|
|
75
|
+
* },
|
|
76
|
+
* unmount(error) {
|
|
77
|
+
* runtime.dispose(error)
|
|
78
|
+
* },
|
|
79
|
+
* [Symbol.dispose]() {
|
|
80
|
+
* this.dispose()
|
|
81
|
+
* },
|
|
43
82
|
* }
|
|
44
83
|
* })
|
|
45
|
-
*
|
|
46
|
-
* // packages/plugin-zod/src/generators/zodGenerator.tsx
|
|
47
|
-
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
48
|
-
* export const zodGenerator = defineGenerator<PluginZod>({
|
|
49
|
-
* name: 'zod',
|
|
50
|
-
* renderer: jsxRenderer,
|
|
51
|
-
* schema(node, options) { return <File ...>...</File> },
|
|
52
|
-
* })
|
|
53
84
|
* ```
|
|
54
85
|
*/
|
|
55
86
|
export function createRenderer<TElement = unknown>(factory: RendererFactory<TElement>): RendererFactory<TElement> {
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type { logLevel } from './defineLogger.ts'
|
|
2
|
+
import type { Config } from './createKubb.ts'
|
|
3
|
+
import type { Diagnostic } from './diagnostics.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A built-in reporter that renders a run's output, independent of the live logger view.
|
|
7
|
+
*
|
|
8
|
+
* - `cli` renders the per-config summary to the terminal (the default).
|
|
9
|
+
* - `json` writes a machine-readable report to stdout, for CI.
|
|
10
|
+
* - `file` writes a config's diagnostics to `.kubb/kubb-<name>-<timestamp>.log`.
|
|
11
|
+
*/
|
|
12
|
+
export type ReporterName = 'cli' | 'json' | 'file'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* One config's outcome within a run, as handed to a {@link Reporter}.
|
|
16
|
+
*/
|
|
17
|
+
export type GenerationResult = {
|
|
18
|
+
config: Config
|
|
19
|
+
/**
|
|
20
|
+
* Diagnostics collected while generating this config.
|
|
21
|
+
*/
|
|
22
|
+
diagnostics: Array<Diagnostic>
|
|
23
|
+
/**
|
|
24
|
+
* Number of files written for this config.
|
|
25
|
+
*/
|
|
26
|
+
filesCreated: number
|
|
27
|
+
status: 'success' | 'failed'
|
|
28
|
+
/**
|
|
29
|
+
* `process.hrtime()` snapshot taken when this config started generating.
|
|
30
|
+
*/
|
|
31
|
+
hrStart: [number, number]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Render context passed alongside the {@link GenerationResult}, carrying knobs a reporter needs
|
|
36
|
+
* but that are not part of the run data (e.g. verbosity).
|
|
37
|
+
*/
|
|
38
|
+
export type ReporterContext = {
|
|
39
|
+
/**
|
|
40
|
+
* Output verbosity. Use the `logLevel` constants exported from `@kubb/core`
|
|
41
|
+
* (`silent`, `error`, `warn`, `info`, `verbose`, `debug`).
|
|
42
|
+
*/
|
|
43
|
+
logLevel: (typeof logLevel)[keyof typeof logLevel]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Host-facing reporter, as installed onto a run. Unlike a Logger (the live TUI view), a reporter
|
|
48
|
+
* never sees the event emitter. `report` runs once per config; `drain`, when present, runs once
|
|
49
|
+
* after the last config.
|
|
50
|
+
*/
|
|
51
|
+
export type Reporter = {
|
|
52
|
+
/**
|
|
53
|
+
* Display name, matching a {@link ReporterName} for the built-ins.
|
|
54
|
+
*/
|
|
55
|
+
name: string
|
|
56
|
+
/**
|
|
57
|
+
* Called once per config with that config's result and the render context.
|
|
58
|
+
*/
|
|
59
|
+
report: (result: GenerationResult, context: ReporterContext) => void | Promise<void>
|
|
60
|
+
/**
|
|
61
|
+
* Optional finalizer called once after the run's last config. The host wires it to
|
|
62
|
+
* `kubb:lifecycle:end`. {@link createReporter} closes it over the reports `report` returned.
|
|
63
|
+
*/
|
|
64
|
+
drain?: (context: ReporterContext) => void | Promise<void>
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Reporter definition passed to {@link createReporter}. `report` returns the value to collect for
|
|
69
|
+
* this config (e.g. a built report), and the optional `drain` receives the collected reports to
|
|
70
|
+
* emit as one document. `T` is inferred from `report`'s return type.
|
|
71
|
+
*/
|
|
72
|
+
export type UserReporter<T = void> = {
|
|
73
|
+
name: string
|
|
74
|
+
report: (result: GenerationResult, context: ReporterContext) => T | Promise<T>
|
|
75
|
+
drain?: (context: ReporterContext, reports: Array<T>) => void | Promise<void>
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Defines a reporter. When the definition has a `drain`, the returned reporter buffers each value
|
|
80
|
+
* `report` returns and hands the array to `drain` once, then clears it. Without a `drain`, nothing
|
|
81
|
+
* is buffered. Wiring the reporter onto the run's events is the host's job, so the reporter only
|
|
82
|
+
* ever deals with a {@link GenerationResult}.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* import { createReporter, Diagnostics } from '@kubb/core'
|
|
87
|
+
*
|
|
88
|
+
* export const jsonReporter = createReporter({
|
|
89
|
+
* name: 'json',
|
|
90
|
+
* report(result) {
|
|
91
|
+
* return { status: Diagnostics.hasError(result.diagnostics) ? 'failed' : 'success', diagnostics: result.diagnostics }
|
|
92
|
+
* },
|
|
93
|
+
* drain(context, reports) {
|
|
94
|
+
* process.stdout.write(`${JSON.stringify(reports, null, 2)}\n`)
|
|
95
|
+
* },
|
|
96
|
+
* })
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export function createReporter<T = void>(reporter: UserReporter<T>): Reporter {
|
|
100
|
+
const drain = reporter.drain
|
|
101
|
+
if (!drain) {
|
|
102
|
+
return {
|
|
103
|
+
name: reporter.name,
|
|
104
|
+
async report(result, context) {
|
|
105
|
+
await reporter.report(result, context)
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const reports: Array<T> = []
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
name: reporter.name,
|
|
114
|
+
async report(result, context) {
|
|
115
|
+
reports.push(await reporter.report(result, context))
|
|
116
|
+
},
|
|
117
|
+
async drain(context) {
|
|
118
|
+
await drain(context, reports)
|
|
119
|
+
reports.length = 0
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Picks the reporters whose `name` matches one of `names`, in the order the names are given.
|
|
126
|
+
* The config carries every available reporter, and the host selects which to activate by name
|
|
127
|
+
* (the CLI maps `--reporter` to this). Duplicate names and names without a matching reporter are
|
|
128
|
+
* skipped.
|
|
129
|
+
*/
|
|
130
|
+
export function selectReporters(reporters: ReadonlyArray<Reporter>, names: ReadonlyArray<string>): Array<Reporter> {
|
|
131
|
+
const seen = new Set<string>()
|
|
132
|
+
const selected: Array<Reporter> = []
|
|
133
|
+
|
|
134
|
+
for (const name of names) {
|
|
135
|
+
if (seen.has(name)) {
|
|
136
|
+
continue
|
|
137
|
+
}
|
|
138
|
+
seen.add(name)
|
|
139
|
+
|
|
140
|
+
const reporter = reporters.find((candidate) => candidate.name === name)
|
|
141
|
+
if (reporter) {
|
|
142
|
+
selected.push(reporter)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return selected
|
|
147
|
+
}
|
package/src/createStorage.ts
CHANGED
|
@@ -1,68 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backend that persists generated files. Kubb ships with `fsStorage` (writes
|
|
3
|
+
* to disk) and `memoryStorage` (keeps everything in RAM). Implement this
|
|
4
|
+
* interface to write to S3, a database, or any other target.
|
|
5
|
+
*/
|
|
1
6
|
export type Storage = {
|
|
2
7
|
/**
|
|
3
|
-
* Identifier used
|
|
8
|
+
* Identifier used in logs and diagnostics (`'fs'`, `'memory'`, `'s3'`).
|
|
4
9
|
*/
|
|
5
10
|
readonly name: string
|
|
6
11
|
/**
|
|
7
|
-
* Returns `true` when an entry for `key` exists
|
|
12
|
+
* Returns `true` when an entry for `key` exists.
|
|
8
13
|
*/
|
|
9
14
|
hasItem(key: string): Promise<boolean>
|
|
10
15
|
/**
|
|
11
|
-
*
|
|
16
|
+
* Reads the stored string. Returns `null` when the key is missing.
|
|
12
17
|
*/
|
|
13
18
|
getItem(key: string): Promise<string | null>
|
|
14
19
|
/**
|
|
15
|
-
*
|
|
20
|
+
* Stores `value` under `key`, creating any required structure (directories,
|
|
21
|
+
* buckets, ...).
|
|
16
22
|
*/
|
|
17
23
|
setItem(key: string, value: string): Promise<void>
|
|
18
24
|
/**
|
|
19
|
-
*
|
|
25
|
+
* Deletes the entry for `key`. No-op when the key does not exist.
|
|
20
26
|
*/
|
|
21
27
|
removeItem(key: string): Promise<void>
|
|
22
28
|
/**
|
|
23
|
-
* Returns
|
|
29
|
+
* Returns every key. Pass `base` to filter to keys starting with that prefix.
|
|
24
30
|
*/
|
|
25
31
|
getKeys(base?: string): Promise<Array<string>>
|
|
26
32
|
/**
|
|
27
|
-
* Removes
|
|
33
|
+
* Removes every entry. Pass `base` to scope the wipe to a key prefix.
|
|
28
34
|
*/
|
|
29
35
|
clear(base?: string): Promise<void>
|
|
30
36
|
/**
|
|
31
|
-
* Optional teardown hook called after the build completes.
|
|
37
|
+
* Optional teardown hook called after the build completes. Use to flush
|
|
38
|
+
* buffers, close connections, or release file locks.
|
|
32
39
|
*/
|
|
33
40
|
dispose?(): Promise<void>
|
|
34
41
|
}
|
|
35
42
|
|
|
36
43
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
44
|
+
* Defines a custom storage backend. The builder receives user options and
|
|
45
|
+
* returns a `Storage` implementation. Kubb ships with filesystem and
|
|
46
|
+
* in-memory storages, reach for this when you need to write generated files
|
|
47
|
+
* elsewhere (cloud storage, a database, a remote API).
|
|
41
48
|
*
|
|
42
|
-
* @
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
49
|
+
* @example In-memory storage (the built-in implementation)
|
|
45
50
|
* ```ts
|
|
46
51
|
* import { createStorage } from '@kubb/core'
|
|
47
52
|
*
|
|
48
53
|
* export const memoryStorage = createStorage(() => {
|
|
49
54
|
* const store = new Map<string, string>()
|
|
55
|
+
*
|
|
50
56
|
* return {
|
|
51
57
|
* name: 'memory',
|
|
52
|
-
* async hasItem(key) {
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* async
|
|
58
|
+
* async hasItem(key) {
|
|
59
|
+
* return store.has(key)
|
|
60
|
+
* },
|
|
61
|
+
* async getItem(key) {
|
|
62
|
+
* return store.get(key) ?? null
|
|
63
|
+
* },
|
|
64
|
+
* async setItem(key, value) {
|
|
65
|
+
* store.set(key, value)
|
|
66
|
+
* },
|
|
67
|
+
* async removeItem(key) {
|
|
68
|
+
* store.delete(key)
|
|
69
|
+
* },
|
|
56
70
|
* async getKeys(base) {
|
|
57
71
|
* const keys = [...store.keys()]
|
|
58
72
|
* return base ? keys.filter((k) => k.startsWith(base)) : keys
|
|
59
73
|
* },
|
|
60
|
-
* async clear(base) {
|
|
74
|
+
* async clear(base) {
|
|
75
|
+
* if (!base) store.clear()
|
|
76
|
+
* },
|
|
61
77
|
* }
|
|
62
78
|
* })
|
|
63
|
-
*
|
|
64
|
-
* // Instantiate:
|
|
65
|
-
* const storage = memoryStorage()
|
|
66
79
|
* ```
|
|
67
80
|
*/
|
|
68
81
|
export function createStorage<TOptions = Record<string, never>>(build: (options: TOptions) => Storage): (options?: TOptions) => Storage {
|
package/src/defineGenerator.ts
CHANGED
|
@@ -1,16 +1,106 @@
|
|
|
1
|
-
import type { PossiblePromise } from '@internals/utils'
|
|
2
|
-
import type { FileNode, OperationNode, SchemaNode } from '@kubb/ast'
|
|
1
|
+
import type { AsyncEventEmitter, PossiblePromise } from '@internals/utils'
|
|
2
|
+
import type { FileNode, InputMeta, OperationNode, SchemaNode, Visitor } from '@kubb/ast'
|
|
3
|
+
import type { Adapter } from './createAdapter.ts'
|
|
3
4
|
import type { RendererFactory } from './createRenderer.ts'
|
|
4
|
-
import type {
|
|
5
|
+
import type { KubbHooks } from './types.ts'
|
|
6
|
+
import type { KubbDriver } from './KubbDriver.ts'
|
|
7
|
+
import type { Plugin, PluginFactoryOptions } from './definePlugin.ts'
|
|
8
|
+
import type { Resolver } from './defineResolver.ts'
|
|
9
|
+
import type { Config } from './types.ts'
|
|
5
10
|
|
|
6
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Context object passed to generator `schema`, `operation`, and `operations` methods.
|
|
13
|
+
*
|
|
14
|
+
* The adapter is always defined (guaranteed by `runPluginAstHooks`) so no runtime checks
|
|
15
|
+
* are needed. `ctx.options` carries resolved per-node options after exclude/include/override
|
|
16
|
+
* filtering for individual schema/operation calls, or plugin-level options for operations.
|
|
17
|
+
*/
|
|
18
|
+
export type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
19
|
+
config: Config
|
|
20
|
+
/**
|
|
21
|
+
* Absolute path to the current plugin's output directory.
|
|
22
|
+
*/
|
|
23
|
+
root: string
|
|
24
|
+
/**
|
|
25
|
+
* Determine output mode based on the output config.
|
|
26
|
+
* Returns `'single'` when `output.path` is a file, `'split'` for a directory.
|
|
27
|
+
*/
|
|
28
|
+
getMode: (output: { path: string }) => 'single' | 'split'
|
|
29
|
+
driver: KubbDriver
|
|
30
|
+
/**
|
|
31
|
+
* Get a plugin by name, typed via `Kubb.PluginRegistry` when registered.
|
|
32
|
+
*/
|
|
33
|
+
getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
|
|
34
|
+
getPlugin(name: string): Plugin | undefined
|
|
35
|
+
/**
|
|
36
|
+
* Get a plugin by name, throws an error if not found.
|
|
37
|
+
*/
|
|
38
|
+
requirePlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]>
|
|
39
|
+
requirePlugin(name: string): Plugin
|
|
40
|
+
/**
|
|
41
|
+
* Get a resolver by plugin name, typed via `Kubb.PluginRegistry` when registered.
|
|
42
|
+
*/
|
|
43
|
+
getResolver<TName extends keyof Kubb.PluginRegistry>(name: TName): Kubb.PluginRegistry[TName]['resolver']
|
|
44
|
+
getResolver(name: string): Resolver
|
|
45
|
+
/**
|
|
46
|
+
* Add files only if they don't exist.
|
|
47
|
+
*/
|
|
48
|
+
addFile: (...file: Array<FileNode>) => Promise<void>
|
|
49
|
+
/**
|
|
50
|
+
* Merge sources into the same output file.
|
|
51
|
+
*/
|
|
52
|
+
upsertFile: (...file: Array<FileNode>) => Promise<void>
|
|
53
|
+
hooks: AsyncEventEmitter<KubbHooks>
|
|
54
|
+
/**
|
|
55
|
+
* The current plugin instance.
|
|
56
|
+
*/
|
|
57
|
+
plugin: Plugin<TOptions>
|
|
58
|
+
/**
|
|
59
|
+
* The current plugin's resolver.
|
|
60
|
+
*/
|
|
61
|
+
resolver: TOptions['resolver']
|
|
62
|
+
/**
|
|
63
|
+
* The current plugin's transformer.
|
|
64
|
+
*/
|
|
65
|
+
transformer: Visitor | undefined
|
|
66
|
+
/**
|
|
67
|
+
* Report a warning. Collected as a `warning` diagnostic attributed to the current
|
|
68
|
+
* plugin. It surfaces in the run summary but does not fail the build. For a structured
|
|
69
|
+
* diagnostic with a code and source location, use `Diagnostics.report` or throw a
|
|
70
|
+
* `Diagnostics.Error` directly.
|
|
71
|
+
*/
|
|
72
|
+
warn: (message: string) => void
|
|
73
|
+
/**
|
|
74
|
+
* Report an error. Collected as an `error` diagnostic attributed to the current
|
|
75
|
+
* plugin, which fails the build.
|
|
76
|
+
*/
|
|
77
|
+
error: (error: string | Error) => void
|
|
78
|
+
/**
|
|
79
|
+
* Report an informational message. Collected as an `info` diagnostic attributed to
|
|
80
|
+
* the current plugin.
|
|
81
|
+
*/
|
|
82
|
+
info: (message: string) => void
|
|
83
|
+
/**
|
|
84
|
+
* The configured adapter instance.
|
|
85
|
+
*/
|
|
86
|
+
adapter: Adapter
|
|
87
|
+
/**
|
|
88
|
+
* Document metadata from the adapter: title, version, base URL, and pre-computed
|
|
89
|
+
* schema index fields (`circularNames`, `enumNames`).
|
|
90
|
+
*/
|
|
91
|
+
meta: InputMeta
|
|
92
|
+
/**
|
|
93
|
+
* Resolved options after exclude/include/override filtering.
|
|
94
|
+
*/
|
|
95
|
+
options: TOptions['resolvedOptions']
|
|
96
|
+
}
|
|
7
97
|
|
|
8
98
|
/**
|
|
9
99
|
* Declares a named generator unit that walks the AST and emits files.
|
|
10
100
|
*
|
|
11
101
|
* Each method (`schema`, `operation`, `operations`) is called for the matching node type.
|
|
12
|
-
* Each method returns `TElement | Array<FileNode> |
|
|
13
|
-
* Return `Array<FileNode>` directly or call `ctx.upsertFile()` manually and return `
|
|
102
|
+
* Each method returns `TElement | Array<FileNode> | undefined | null`. JSX-based generators require a `renderer` factory.
|
|
103
|
+
* Return `Array<FileNode>` directly or call `ctx.upsertFile()` manually and return `undefined` or `null` to bypass rendering.
|
|
14
104
|
*
|
|
15
105
|
* @note Generators are consumed by plugins and registered via `ctx.addGenerator()` in `kubb:plugin:setup`.
|
|
16
106
|
*
|
|
@@ -42,8 +132,7 @@ export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptio
|
|
|
42
132
|
*
|
|
43
133
|
* Generators that only return `Array<FileNode>` or `void` do not need to set this.
|
|
44
134
|
*
|
|
45
|
-
*
|
|
46
|
-
* declares a `renderer` (overrides the plugin-level fallback).
|
|
135
|
+
* Leave it unset or set `renderer: null` to opt out of rendering.
|
|
47
136
|
*
|
|
48
137
|
* @example
|
|
49
138
|
* ```ts
|
|
@@ -57,28 +146,51 @@ export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptio
|
|
|
57
146
|
renderer?: RendererFactory<TElement> | null
|
|
58
147
|
/**
|
|
59
148
|
* Called for each schema node in the AST walk.
|
|
60
|
-
* `ctx` carries the plugin context with `adapter` and `
|
|
149
|
+
* `ctx` carries the plugin context with `adapter` and `meta` (document metadata),
|
|
61
150
|
* plus `ctx.options` with the per-node resolved options (after exclude/include/override).
|
|
62
151
|
*/
|
|
63
|
-
schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> |
|
|
152
|
+
schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | undefined | null>
|
|
64
153
|
/**
|
|
65
154
|
* Called for each operation node in the AST walk.
|
|
66
|
-
* `ctx` carries the plugin context with `adapter` and `
|
|
155
|
+
* `ctx` carries the plugin context with `adapter` and `meta` (document metadata),
|
|
67
156
|
* plus `ctx.options` with the per-node resolved options (after exclude/include/override).
|
|
68
157
|
*/
|
|
69
|
-
operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> |
|
|
158
|
+
operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | undefined | null>
|
|
70
159
|
/**
|
|
71
160
|
* Called once after all operations have been walked.
|
|
72
|
-
* `ctx` carries the plugin context with `adapter` and `
|
|
161
|
+
* `ctx` carries the plugin context with `adapter` and `meta` (document metadata),
|
|
73
162
|
* plus `ctx.options` with the plugin-level options for the batch call.
|
|
74
163
|
*/
|
|
75
|
-
operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> |
|
|
164
|
+
operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | undefined | null>
|
|
76
165
|
}
|
|
77
166
|
|
|
78
167
|
/**
|
|
79
|
-
* Defines a generator
|
|
80
|
-
*
|
|
81
|
-
*
|
|
168
|
+
* Defines a generator: a unit of work that runs during the plugin's AST walk
|
|
169
|
+
* and produces files. Plugins register generators via `ctx.addGenerator()`
|
|
170
|
+
* inside `kubb:plugin:setup`.
|
|
171
|
+
*
|
|
172
|
+
* The returned object is the input as-is, but with `this` types preserved so
|
|
173
|
+
* `schema`/`operation`/`operations` methods are correctly typed against the
|
|
174
|
+
* plugin's `PluginFactoryOptions`. Renderer elements and `FileNode[]` returns
|
|
175
|
+
* are both handled by the runtime, so pick whichever style fits.
|
|
176
|
+
*
|
|
177
|
+
* @example JSX-based schema generator
|
|
178
|
+
* ```tsx
|
|
179
|
+
* import { defineGenerator } from '@kubb/core'
|
|
180
|
+
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
181
|
+
*
|
|
182
|
+
* export const typeGenerator = defineGenerator({
|
|
183
|
+
* name: 'typescript',
|
|
184
|
+
* renderer: jsxRenderer,
|
|
185
|
+
* schema(node, ctx) {
|
|
186
|
+
* return (
|
|
187
|
+
* <File path={`${ctx.root}/${node.name}.ts`}>
|
|
188
|
+
* <Type node={node} resolver={ctx.resolver} />
|
|
189
|
+
* </File>
|
|
190
|
+
* )
|
|
191
|
+
* },
|
|
192
|
+
* })
|
|
193
|
+
* ```
|
|
82
194
|
*/
|
|
83
195
|
export function defineGenerator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown>(
|
|
84
196
|
generator: Generator<TOptions, TElement>,
|
package/src/defineLogger.ts
CHANGED
|
@@ -1,19 +1,90 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AsyncEventEmitter } from '@internals/utils'
|
|
2
|
+
import type { KubbHooks } from './types.ts'
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
+
* Numeric log-level thresholds used internally to compare verbosity.
|
|
5
6
|
*
|
|
6
|
-
*
|
|
7
|
+
* Higher numbers are more verbose.
|
|
8
|
+
*/
|
|
9
|
+
export const logLevel = {
|
|
10
|
+
silent: Number.NEGATIVE_INFINITY,
|
|
11
|
+
error: 0,
|
|
12
|
+
warn: 1,
|
|
13
|
+
info: 3,
|
|
14
|
+
verbose: 4,
|
|
15
|
+
} as const
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Options accepted by a logger's `install` callback.
|
|
19
|
+
*/
|
|
20
|
+
export type LoggerOptions = {
|
|
21
|
+
/**
|
|
22
|
+
* Output verbosity. Use the `logLevel` constants exported from `@kubb/core`
|
|
23
|
+
* (`silent`, `error`, `warn`, `info`, `verbose`, `debug`).
|
|
24
|
+
*/
|
|
25
|
+
logLevel: (typeof logLevel)[keyof typeof logLevel]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Event emitter handed to `Logger.install`. Use `.on('kubb:info', ...)` and
|
|
30
|
+
* friends to subscribe to build events.
|
|
31
|
+
*/
|
|
32
|
+
export type LoggerContext = AsyncEventEmitter<KubbHooks>
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Logger contract. A logger receives the build's event emitter and subscribes
|
|
36
|
+
* to whichever lifecycle events it wants to forward to its destination
|
|
37
|
+
* (console, file, remote sink).
|
|
38
|
+
*/
|
|
39
|
+
export type Logger<TOptions extends LoggerOptions = LoggerOptions, TInstallReturn = void> = {
|
|
40
|
+
/**
|
|
41
|
+
* Display name used in diagnostics.
|
|
42
|
+
*/
|
|
43
|
+
name: string
|
|
44
|
+
/**
|
|
45
|
+
* Called once per build with the shared event emitter. Subscribe to events
|
|
46
|
+
* here. The return value (if any) is forwarded to whoever installed the
|
|
47
|
+
* logger, which is handy for sink factories.
|
|
48
|
+
*/
|
|
49
|
+
install: (context: LoggerContext, options?: TOptions) => TInstallReturn | Promise<TInstallReturn>
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type UserLogger<TOptions extends LoggerOptions = LoggerOptions, TInstallReturn = void> = Logger<TOptions, TInstallReturn>
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Defines a typed logger. Use the second type parameter to declare a return
|
|
56
|
+
* value from `install`, which is handy when the logger exposes a sink factory
|
|
57
|
+
* or cleanup callback to the caller.
|
|
58
|
+
*
|
|
59
|
+
* @example Basic logger
|
|
7
60
|
* ```ts
|
|
61
|
+
* import { defineLogger } from '@kubb/core'
|
|
62
|
+
*
|
|
8
63
|
* export const myLogger = defineLogger({
|
|
9
64
|
* name: 'my-logger',
|
|
10
|
-
* install(context
|
|
11
|
-
* context.on('kubb:info', (message) => console.log('ℹ', message))
|
|
12
|
-
* context.on('kubb:error', (error) => console.error('✗', error.message))
|
|
65
|
+
* install(context) {
|
|
66
|
+
* context.on('kubb:info', ({ message }) => console.log('ℹ', message))
|
|
67
|
+
* context.on('kubb:error', ({ error }) => console.error('✗', error.message))
|
|
68
|
+
* },
|
|
69
|
+
* })
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @example Logger that returns a hook sink factory
|
|
73
|
+
* ```ts
|
|
74
|
+
* import { defineLogger, type LoggerOptions } from '@kubb/core'
|
|
75
|
+
* import type { HookSinkFactory } from './sinks'
|
|
76
|
+
*
|
|
77
|
+
* export const myLogger = defineLogger<LoggerOptions, HookSinkFactory>({
|
|
78
|
+
* name: 'my-logger',
|
|
79
|
+
* install(context) {
|
|
80
|
+
* // … register event handlers …
|
|
81
|
+
* return () => ({ onStdout: console.log })
|
|
13
82
|
* },
|
|
14
83
|
* })
|
|
15
84
|
* ```
|
|
16
85
|
*/
|
|
17
|
-
export function defineLogger<Options extends LoggerOptions = LoggerOptions
|
|
86
|
+
export function defineLogger<Options extends LoggerOptions = LoggerOptions, TInstallReturn = void>(
|
|
87
|
+
logger: UserLogger<Options, TInstallReturn>,
|
|
88
|
+
): Logger<Options, TInstallReturn> {
|
|
18
89
|
return logger
|
|
19
90
|
}
|