@kubb/core 5.0.0-beta.36 → 5.0.0-beta.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,58 @@
1
+ import type { OperationNode, SchemaNode, Visitor } from '@kubb/ast'
2
+ import { transform } from '@kubb/ast'
3
+
4
+ /**
5
+ * Holds one `Visitor` per plugin, keyed by plugin name. Each plugin's transformer runs in
6
+ * isolation on the original adapter node. `applyTo` is a lookup, not a chain, so plugin A's
7
+ * visitor never sees plugin B's output. When no transformer is registered, `applyTo` returns
8
+ * the original node reference, and the `@kubb/ast` `transform` primitive does the same when
9
+ * its visitor leaves the tree untouched. Callers can compare by identity to detect a no-op.
10
+ *
11
+ * Registration order matches the order setup hooks fire, which the driver has already sorted
12
+ * by `enforce` and dependency edges. The registry does not re-order anything.
13
+ */
14
+ export class Transform {
15
+ readonly #visitors = new Map<string, Visitor>()
16
+
17
+ /**
18
+ * Number of plugins with a registered transformer.
19
+ */
20
+ get size(): number {
21
+ return this.#visitors.size
22
+ }
23
+
24
+ /**
25
+ * Records `visitor` as the transformer for `pluginName`. A second call for the same plugin
26
+ * replaces the first.
27
+ */
28
+ register(pluginName: string, visitor: Visitor): void {
29
+ this.#visitors.set(pluginName, visitor)
30
+ }
31
+
32
+ /**
33
+ * Looks up the transformer for `pluginName`. The generator context uses this so plugins can
34
+ * read their own visitor through `ctx.transformer`.
35
+ */
36
+ get(pluginName: string): Visitor | undefined {
37
+ return this.#visitors.get(pluginName)
38
+ }
39
+
40
+ /**
41
+ * Runs the plugin's transformer on `node`. Returns the original node reference when the
42
+ * plugin has no transformer, so callers can compare by identity to detect a no-op.
43
+ */
44
+ applyTo<TNode extends SchemaNode | OperationNode>(pluginName: string, node: TNode): TNode {
45
+ const visitor = this.#visitors.get(pluginName)
46
+ if (!visitor) return node
47
+
48
+ return transform(node, visitor) as TNode
49
+ }
50
+
51
+ /**
52
+ * Clears every registration. Called from the driver's `dispose()` so visitors do not leak
53
+ * across builds.
54
+ */
55
+ dispose(): void {
56
+ this.#visitors.clear()
57
+ }
58
+ }
package/src/constants.ts CHANGED
@@ -1,29 +1,31 @@
1
- import type { FileNode } from '@kubb/ast'
2
-
3
1
  /**
4
2
  * Base URL for the Kubb Studio web app.
5
3
  */
6
4
  export const DEFAULT_STUDIO_URL = 'https://kubb.studio' as const
7
5
 
8
6
  /**
9
- * Default banner style written at the top of every generated file.
7
+ * Number of file writes to batch in parallel during `flushPendingFiles`.
10
8
  */
11
- export const DEFAULT_BANNER = 'simple' as const
9
+ export const STREAM_FLUSH_EVERY = 50
12
10
 
13
11
  /**
14
- * Default file-extension mapping used when no explicit mapping is configured.
12
+ * Number of schema/operation nodes to dispatch concurrently during generation.
15
13
  */
16
- export const DEFAULT_EXTENSION: Record<FileNode['extname'], FileNode['extname'] | ''> = { '.ts': '.ts' }
14
+ export const SCHEMA_PARALLEL = 8
17
15
 
18
16
  /**
19
- * Number of file writes to batch in parallel during `flushPendingFiles`.
17
+ * Upper bound of hook listeners a single plugin can add to one event (its schema, operation,
18
+ * and operations generators, plus lifecycle hooks). Used to size the hooks emitter's
19
+ * max-listener ceiling so a multi-generator plugin set does not trip Node's leak warning.
20
20
  */
21
- export const STREAM_FLUSH_EVERY = 50
21
+ export const HOOK_LISTENERS_PER_PLUGIN = 4
22
22
 
23
23
  /**
24
- * Number of schema/operation nodes to dispatch concurrently during generation.
24
+ * Plugin `include` filter types that select operations directly. When one of these is set
25
+ * without a `schemaName` include, the generate phase pre-scans operations to compute the set
26
+ * of schemas they reach, so unreachable schemas can be pruned for that plugin.
25
27
  */
26
- export const SCHEMA_PARALLEL = 8
28
+ export const OPERATION_FILTER_TYPES: ReadonlySet<string> = new Set(['tag', 'operationId', 'path', 'method', 'contentType'])
27
29
 
28
30
  /**
29
31
  * Numeric log-level thresholds used internally to compare verbosity.
@@ -36,5 +38,97 @@ export const logLevel = {
36
38
  warn: 1,
37
39
  info: 3,
38
40
  verbose: 4,
39
- debug: 5,
40
41
  } as const
42
+
43
+ /**
44
+ * Stable codes Kubb attaches to a `Diagnostic`. Each maps to a known failure mode
45
+ * and stays stable so it can be referenced in tooling and (later) docs. Reference
46
+ * these instead of inlining the string at a throw site.
47
+ */
48
+ export const diagnosticCode = {
49
+ /**
50
+ * Fallback for an unstructured error with no specific code.
51
+ */
52
+ unknown: 'KUBB_UNKNOWN',
53
+ /**
54
+ * The `input.path` file or URL could not be read.
55
+ */
56
+ inputNotFound: 'KUBB_INPUT_NOT_FOUND',
57
+ /**
58
+ * An adapter was configured without an `input`.
59
+ */
60
+ inputRequired: 'KUBB_INPUT_REQUIRED',
61
+ /**
62
+ * A `$ref` (or equivalent reference) could not be resolved in the source document.
63
+ */
64
+ refNotFound: 'KUBB_REF_NOT_FOUND',
65
+ /**
66
+ * A server variable value is not allowed by its `enum`.
67
+ */
68
+ invalidServerVariable: 'KUBB_INVALID_SERVER_VARIABLE',
69
+ /**
70
+ * A required plugin is missing from the config.
71
+ */
72
+ pluginNotFound: 'KUBB_PLUGIN_NOT_FOUND',
73
+ /**
74
+ * A plugin threw while generating.
75
+ */
76
+ pluginFailed: 'KUBB_PLUGIN_FAILED',
77
+ /**
78
+ * A plugin reported a non-fatal warning through `ctx.warn`.
79
+ */
80
+ pluginWarning: 'KUBB_PLUGIN_WARNING',
81
+ /**
82
+ * A plugin reported an informational message through `ctx.info`.
83
+ */
84
+ pluginInfo: 'KUBB_PLUGIN_INFO',
85
+ /**
86
+ * A schema uses a `format` Kubb does not map to a specific type. Reserved for
87
+ * adapters to emit as a `warning`.
88
+ */
89
+ unsupportedFormat: 'KUBB_UNSUPPORTED_FORMAT',
90
+ /**
91
+ * A referenced schema or operation is marked `deprecated`. Reserved for adapters
92
+ * to emit as an `info`.
93
+ */
94
+ deprecated: 'KUBB_DEPRECATED',
95
+ /**
96
+ * An adapter is required but the config has none. The build cannot read the input
97
+ * without one.
98
+ */
99
+ adapterRequired: 'KUBB_ADAPTER_REQUIRED',
100
+ /**
101
+ * The `devtools` config is set to something other than an object.
102
+ */
103
+ devtoolsInvalid: 'KUBB_DEVTOOLS_INVALID',
104
+ /**
105
+ * A resolved output path escapes the output directory, which can stem from a path
106
+ * traversal in the spec or a misconfigured `group.name`.
107
+ */
108
+ pathTraversal: 'KUBB_PATH_TRAVERSAL',
109
+ /**
110
+ * A post-generate shell hook (`hooks.done`) exited with a failure.
111
+ */
112
+ hookFailed: 'KUBB_HOOK_FAILED',
113
+ /**
114
+ * The formatter pass over the generated files failed.
115
+ */
116
+ formatFailed: 'KUBB_FORMAT_FAILED',
117
+ /**
118
+ * The linter pass over the generated files failed.
119
+ */
120
+ lintFailed: 'KUBB_LINT_FAILED',
121
+ /**
122
+ * Not a failure. Carries a plugin's elapsed time, summed into the run total.
123
+ */
124
+ performance: 'KUBB_PERFORMANCE',
125
+ /**
126
+ * Not a failure. A newer Kubb version is available on npm.
127
+ */
128
+ updateAvailable: 'KUBB_UPDATE_AVAILABLE',
129
+ } as const
130
+
131
+ /**
132
+ * Union of the stable {@link diagnosticCode} values.
133
+ */
134
+ export type DiagnosticCode = (typeof diagnosticCode)[keyof typeof diagnosticCode]