@kubb/core 5.0.0-alpha.9 → 5.0.0-beta.1
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 +23 -20
- package/dist/PluginDriver-BXibeQk-.cjs +1036 -0
- package/dist/PluginDriver-BXibeQk-.cjs.map +1 -0
- package/dist/PluginDriver-DV3p2Hky.js +945 -0
- package/dist/PluginDriver-DV3p2Hky.js.map +1 -0
- package/dist/index.cjs +729 -1641
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +271 -225
- package/dist/index.js +713 -1609
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +145 -0
- package/dist/mocks.cjs.map +1 -0
- package/dist/mocks.d.ts +80 -0
- package/dist/mocks.js +140 -0
- package/dist/mocks.js.map +1 -0
- package/dist/types-CuNocrbJ.d.ts +2148 -0
- package/package.json +51 -57
- package/src/FileManager.ts +115 -0
- package/src/FileProcessor.ts +86 -0
- package/src/Kubb.ts +207 -131
- package/src/PluginDriver.ts +325 -564
- package/src/constants.ts +20 -47
- package/src/createAdapter.ts +13 -6
- package/src/createKubb.ts +548 -0
- package/src/createRenderer.ts +57 -0
- package/src/createStorage.ts +13 -1
- package/src/defineGenerator.ts +77 -124
- package/src/defineLogger.ts +4 -2
- package/src/defineMiddleware.ts +62 -0
- package/src/defineParser.ts +44 -0
- package/src/definePlugin.ts +83 -0
- package/src/defineResolver.ts +418 -28
- package/src/devtools.ts +14 -14
- package/src/index.ts +13 -15
- package/src/mocks.ts +178 -0
- package/src/renderNode.ts +35 -0
- package/src/storages/fsStorage.ts +41 -11
- package/src/storages/memoryStorage.ts +4 -2
- package/src/types.ts +1031 -283
- package/src/utils/diagnostics.ts +4 -1
- package/src/utils/isInputPath.ts +10 -0
- package/src/utils/packageJSON.ts +50 -12
- package/dist/PluginDriver-BkFepPdm.d.ts +0 -1054
- package/dist/chunk-ByKO4r7w.cjs +0 -38
- package/dist/hooks.cjs +0 -103
- package/dist/hooks.cjs.map +0 -1
- package/dist/hooks.d.ts +0 -77
- package/dist/hooks.js +0 -98
- package/dist/hooks.js.map +0 -1
- package/src/build.ts +0 -418
- package/src/config.ts +0 -56
- package/src/createPlugin.ts +0 -28
- package/src/hooks/index.ts +0 -4
- package/src/hooks/useKubb.ts +0 -143
- package/src/hooks/useMode.ts +0 -11
- package/src/hooks/usePlugin.ts +0 -11
- package/src/hooks/usePluginDriver.ts +0 -11
- package/src/utils/FunctionParams.ts +0 -155
- package/src/utils/TreeNode.ts +0 -215
- package/src/utils/executeStrategies.ts +0 -81
- package/src/utils/formatters.ts +0 -56
- package/src/utils/getBarrelFiles.ts +0 -141
- package/src/utils/getConfigs.ts +0 -12
- package/src/utils/linters.ts +0 -25
|
@@ -0,0 +1,945 @@
|
|
|
1
|
+
import "./chunk--u3MIqq1.js";
|
|
2
|
+
import path, { extname, resolve } from "node:path";
|
|
3
|
+
import { createFile, isOperationNode, isSchemaNode } from "@kubb/ast";
|
|
4
|
+
import { deflateSync } from "fflate";
|
|
5
|
+
import { x } from "tinyexec";
|
|
6
|
+
//#region ../../internals/utils/src/casing.ts
|
|
7
|
+
/**
|
|
8
|
+
* Shared implementation for camelCase and PascalCase conversion.
|
|
9
|
+
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
|
|
10
|
+
* and capitalizes each word according to `pascal`.
|
|
11
|
+
*
|
|
12
|
+
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
|
|
13
|
+
*/
|
|
14
|
+
function toCamelOrPascal(text, pascal) {
|
|
15
|
+
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) => {
|
|
16
|
+
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
17
|
+
if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
|
|
18
|
+
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
19
|
+
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Splits `text` on `.` and applies `transformPart` to each segment.
|
|
23
|
+
* The last segment receives `isLast = true`, all earlier segments receive `false`.
|
|
24
|
+
* Segments are joined with `/` to form a file path.
|
|
25
|
+
*
|
|
26
|
+
* Only splits on dots followed by a letter so that version numbers
|
|
27
|
+
* embedded in operationIds (e.g. `v2025.0`) are kept intact.
|
|
28
|
+
*
|
|
29
|
+
* Empty segments are filtered before joining. They arise when the text starts with
|
|
30
|
+
* a dot followed immediately by a letter (e.g. `..Schema` splits into `['..', 'Schema']`
|
|
31
|
+
* and `'..'` transforms to an empty string). Without this filter the join would produce
|
|
32
|
+
* a leading `/`, which `path.resolve` would interpret as an absolute path, allowing
|
|
33
|
+
* generated files to escape the configured output directory.
|
|
34
|
+
*/
|
|
35
|
+
function applyToFileParts(text, transformPart) {
|
|
36
|
+
const parts = text.split(/\.(?=[a-zA-Z])/);
|
|
37
|
+
return parts.map((part, i) => transformPart(part, i === parts.length - 1)).filter(Boolean).join("/");
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Converts `text` to camelCase.
|
|
41
|
+
* When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* camelCase('hello-world') // 'helloWorld'
|
|
45
|
+
* camelCase('pet.petId', { isFile: true }) // 'pet/petId'
|
|
46
|
+
*/
|
|
47
|
+
function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
48
|
+
if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
|
|
49
|
+
prefix,
|
|
50
|
+
suffix
|
|
51
|
+
} : {}));
|
|
52
|
+
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Converts `text` to PascalCase.
|
|
56
|
+
* When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* pascalCase('hello-world') // 'HelloWorld'
|
|
60
|
+
* pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
|
|
61
|
+
*/
|
|
62
|
+
function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
63
|
+
if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
|
|
64
|
+
prefix,
|
|
65
|
+
suffix
|
|
66
|
+
}) : camelCase(part));
|
|
67
|
+
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/constants.ts
|
|
71
|
+
/**
|
|
72
|
+
* Base URL for the Kubb Studio web app.
|
|
73
|
+
*/
|
|
74
|
+
const DEFAULT_STUDIO_URL = "https://studio.kubb.dev";
|
|
75
|
+
/**
|
|
76
|
+
* Default banner style written at the top of every generated file.
|
|
77
|
+
*/
|
|
78
|
+
const DEFAULT_BANNER = "simple";
|
|
79
|
+
/**
|
|
80
|
+
* Default file-extension mapping used when no explicit mapping is configured.
|
|
81
|
+
*/
|
|
82
|
+
const DEFAULT_EXTENSION = { ".ts": ".ts" };
|
|
83
|
+
/**
|
|
84
|
+
* Numeric log-level thresholds used internally to compare verbosity.
|
|
85
|
+
*
|
|
86
|
+
* Higher numbers are more verbose.
|
|
87
|
+
*/
|
|
88
|
+
const logLevel = {
|
|
89
|
+
silent: Number.NEGATIVE_INFINITY,
|
|
90
|
+
error: 0,
|
|
91
|
+
warn: 1,
|
|
92
|
+
info: 3,
|
|
93
|
+
verbose: 4,
|
|
94
|
+
debug: 5
|
|
95
|
+
};
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/defineResolver.ts
|
|
98
|
+
const stringPatternCache = /* @__PURE__ */ new Map();
|
|
99
|
+
function testPattern(value, pattern) {
|
|
100
|
+
if (typeof pattern === "string") {
|
|
101
|
+
let regex = stringPatternCache.get(pattern);
|
|
102
|
+
if (!regex) {
|
|
103
|
+
regex = new RegExp(pattern);
|
|
104
|
+
stringPatternCache.set(pattern, regex);
|
|
105
|
+
}
|
|
106
|
+
return regex.test(value);
|
|
107
|
+
}
|
|
108
|
+
return value.match(pattern) !== null;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
|
|
112
|
+
*/
|
|
113
|
+
function matchesOperationPattern(node, type, pattern) {
|
|
114
|
+
switch (type) {
|
|
115
|
+
case "tag": return node.tags.some((tag) => testPattern(tag, pattern));
|
|
116
|
+
case "operationId": return testPattern(node.operationId, pattern);
|
|
117
|
+
case "path": return testPattern(node.path, pattern);
|
|
118
|
+
case "method": return testPattern(node.method.toLowerCase(), pattern);
|
|
119
|
+
case "contentType": return node.requestBody?.content?.some((c) => testPattern(c.contentType, pattern)) ?? false;
|
|
120
|
+
default: return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Checks if a schema matches a pattern for a given filter type (`schemaName`).
|
|
125
|
+
*
|
|
126
|
+
* Returns `null` when the filter type doesn't apply to schemas.
|
|
127
|
+
*/
|
|
128
|
+
function matchesSchemaPattern(node, type, pattern) {
|
|
129
|
+
switch (type) {
|
|
130
|
+
case "schemaName": return node.name ? testPattern(node.name, pattern) : false;
|
|
131
|
+
default: return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Default name resolver used by `defineResolver`.
|
|
136
|
+
*
|
|
137
|
+
* - `camelCase` for `function` and `file` types.
|
|
138
|
+
* - `PascalCase` for `type`.
|
|
139
|
+
* - `camelCase` for everything else.
|
|
140
|
+
*/
|
|
141
|
+
function defaultResolver(name, type) {
|
|
142
|
+
let resolvedName = camelCase(name);
|
|
143
|
+
if (type === "file" || type === "function") resolvedName = camelCase(name, { isFile: type === "file" });
|
|
144
|
+
if (type === "type") resolvedName = pascalCase(name);
|
|
145
|
+
return resolvedName;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Default option resolver — applies include/exclude filters and merges matching override options.
|
|
149
|
+
*
|
|
150
|
+
* Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
|
|
151
|
+
*
|
|
152
|
+
* @example Include/exclude filtering
|
|
153
|
+
* ```ts
|
|
154
|
+
* const options = defaultResolveOptions(operationNode, {
|
|
155
|
+
* options: { output: 'types' },
|
|
156
|
+
* exclude: [{ type: 'tag', pattern: 'internal' }],
|
|
157
|
+
* })
|
|
158
|
+
* // → null when node has tag 'internal'
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @example Override merging
|
|
162
|
+
* ```ts
|
|
163
|
+
* const options = defaultResolveOptions(operationNode, {
|
|
164
|
+
* options: { enumType: 'asConst' },
|
|
165
|
+
* override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
|
|
166
|
+
* })
|
|
167
|
+
* // → { enumType: 'enum' } when operationId matches
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
function defaultResolveOptions(node, { options, exclude = [], include, override = [] }) {
|
|
171
|
+
if (isOperationNode(node)) {
|
|
172
|
+
if (exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
|
|
173
|
+
if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
|
|
174
|
+
const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options;
|
|
175
|
+
return {
|
|
176
|
+
...options,
|
|
177
|
+
...overrideOptions
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
if (isSchemaNode(node)) {
|
|
181
|
+
if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) return null;
|
|
182
|
+
if (include) {
|
|
183
|
+
const applicable = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern)).filter((r) => r !== null);
|
|
184
|
+
if (applicable.length > 0 && !applicable.includes(true)) return null;
|
|
185
|
+
}
|
|
186
|
+
const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options;
|
|
187
|
+
return {
|
|
188
|
+
...options,
|
|
189
|
+
...overrideOptions
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
return options;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Default path resolver used by `defineResolver`.
|
|
196
|
+
*
|
|
197
|
+
* - Returns the output directory in `single` mode.
|
|
198
|
+
* - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
|
|
199
|
+
* - Falls back to a flat `output/baseName` path otherwise.
|
|
200
|
+
*
|
|
201
|
+
* A custom `group.name` function overrides the default subdirectory naming.
|
|
202
|
+
* For `tag` groups the default is `${camelCase(tag)}Controller`.
|
|
203
|
+
* For `path` groups the default is the first path segment after `/`.
|
|
204
|
+
*
|
|
205
|
+
* @example Flat output
|
|
206
|
+
* ```ts
|
|
207
|
+
* defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
|
|
208
|
+
* // → '/src/types/petTypes.ts'
|
|
209
|
+
* ```
|
|
210
|
+
*
|
|
211
|
+
* @example Tag-based grouping
|
|
212
|
+
* ```ts
|
|
213
|
+
* defaultResolvePath(
|
|
214
|
+
* { baseName: 'petTypes.ts', tag: 'pets' },
|
|
215
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
216
|
+
* )
|
|
217
|
+
* // → '/src/types/petsController/petTypes.ts'
|
|
218
|
+
* ```
|
|
219
|
+
*
|
|
220
|
+
* @example Path-based grouping
|
|
221
|
+
* ```ts
|
|
222
|
+
* defaultResolvePath(
|
|
223
|
+
* { baseName: 'petTypes.ts', path: '/pets/list' },
|
|
224
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
|
|
225
|
+
* )
|
|
226
|
+
* // → '/src/types/pets/petTypes.ts'
|
|
227
|
+
* ```
|
|
228
|
+
*
|
|
229
|
+
* @example Single-file mode
|
|
230
|
+
* ```ts
|
|
231
|
+
* defaultResolvePath(
|
|
232
|
+
* { baseName: 'petTypes.ts', pathMode: 'single' },
|
|
233
|
+
* { root: '/src', output: { path: 'types' } },
|
|
234
|
+
* )
|
|
235
|
+
* // → '/src/types'
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }, { root, output, group }) {
|
|
239
|
+
if ((pathMode ?? PluginDriver.getMode(path.resolve(root, output.path))) === "single") return path.resolve(root, output.path);
|
|
240
|
+
let result;
|
|
241
|
+
if (group && (groupPath || tag)) {
|
|
242
|
+
const groupValue = group.type === "path" ? groupPath : tag;
|
|
243
|
+
const defaultName = group.type === "tag" ? ({ group: g }) => `${camelCase(g)}Controller` : ({ group: g }) => {
|
|
244
|
+
const segment = g.split("/").filter((s) => s !== "" && s !== "." && s !== "..")[0];
|
|
245
|
+
return segment ? camelCase(segment) : "";
|
|
246
|
+
};
|
|
247
|
+
const resolveName = group.name ?? defaultName;
|
|
248
|
+
result = path.resolve(root, output.path, resolveName({ group: groupValue }), baseName);
|
|
249
|
+
} else result = path.resolve(root, output.path, baseName);
|
|
250
|
+
const outputDir = path.resolve(root, output.path);
|
|
251
|
+
const outputDirWithSep = outputDir.endsWith(path.sep) ? outputDir : `${outputDir}${path.sep}`;
|
|
252
|
+
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.`);
|
|
253
|
+
return result;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Default file resolver used by `defineResolver`.
|
|
257
|
+
*
|
|
258
|
+
* Resolves a `FileNode` by combining name resolution (`resolver.default`) with
|
|
259
|
+
* path resolution (`resolver.resolvePath`). The resolved file always has empty
|
|
260
|
+
* `sources`, `imports`, and `exports` arrays — consumers populate those separately.
|
|
261
|
+
*
|
|
262
|
+
* In `single` mode the name is omitted and the file sits directly in the output directory.
|
|
263
|
+
*
|
|
264
|
+
* @example Resolve a schema file
|
|
265
|
+
* ```ts
|
|
266
|
+
* const file = defaultResolveFile(
|
|
267
|
+
* { name: 'pet', extname: '.ts' },
|
|
268
|
+
* { root: '/src', output: { path: 'types' } },
|
|
269
|
+
* resolver,
|
|
270
|
+
* )
|
|
271
|
+
* // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
|
|
272
|
+
* ```
|
|
273
|
+
*
|
|
274
|
+
* @example Resolve an operation file with tag grouping
|
|
275
|
+
* ```ts
|
|
276
|
+
* const file = defaultResolveFile(
|
|
277
|
+
* { name: 'listPets', extname: '.ts', tag: 'pets' },
|
|
278
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
279
|
+
* resolver,
|
|
280
|
+
* )
|
|
281
|
+
* // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
function defaultResolveFile({ name, extname, tag, path: groupPath }, context, ctx) {
|
|
285
|
+
const pathMode = PluginDriver.getMode(path.resolve(context.root, context.output.path));
|
|
286
|
+
const baseName = `${pathMode === "single" ? "" : ctx.default(name, "file")}${extname}`;
|
|
287
|
+
const filePath = ctx.resolvePath({
|
|
288
|
+
baseName,
|
|
289
|
+
pathMode,
|
|
290
|
+
tag,
|
|
291
|
+
path: groupPath
|
|
292
|
+
}, context);
|
|
293
|
+
return createFile({
|
|
294
|
+
path: filePath,
|
|
295
|
+
baseName: path.basename(filePath),
|
|
296
|
+
meta: { pluginName: ctx.pluginName },
|
|
297
|
+
sources: [],
|
|
298
|
+
imports: [],
|
|
299
|
+
exports: []
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Generates the default "Generated by Kubb" banner from config and optional node metadata.
|
|
304
|
+
*/
|
|
305
|
+
function buildDefaultBanner({ title, description, version, config }) {
|
|
306
|
+
try {
|
|
307
|
+
let source = "";
|
|
308
|
+
if (Array.isArray(config.input)) {
|
|
309
|
+
const first = config.input[0];
|
|
310
|
+
if (first && "path" in first) source = path.basename(first.path);
|
|
311
|
+
} else if ("path" in config.input) source = path.basename(config.input.path);
|
|
312
|
+
else if ("data" in config.input) source = "text content";
|
|
313
|
+
let banner = "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n";
|
|
314
|
+
if (config.output.defaultBanner === "simple") {
|
|
315
|
+
banner += "*/\n";
|
|
316
|
+
return banner;
|
|
317
|
+
}
|
|
318
|
+
if (source) banner += `* Source: ${source}\n`;
|
|
319
|
+
if (title) banner += `* Title: ${title}\n`;
|
|
320
|
+
if (description) {
|
|
321
|
+
const formattedDescription = description.replace(/\n/gm, "\n* ");
|
|
322
|
+
banner += `* Description: ${formattedDescription}\n`;
|
|
323
|
+
}
|
|
324
|
+
if (version) banner += `* OpenAPI spec version: ${version}\n`;
|
|
325
|
+
banner += "*/\n";
|
|
326
|
+
return banner;
|
|
327
|
+
} catch (_error) {
|
|
328
|
+
return "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/";
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Default banner resolver — returns the banner string for a generated file.
|
|
333
|
+
*
|
|
334
|
+
* A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
|
|
335
|
+
* When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
|
|
336
|
+
* from the OAS spec when a `node` is provided).
|
|
337
|
+
*
|
|
338
|
+
* - When `output.banner` is a function and `node` is provided, returns `output.banner(node)`.
|
|
339
|
+
* - When `output.banner` is a function and `node` is absent, falls back to the Kubb notice.
|
|
340
|
+
* - When `output.banner` is a string, returns it directly.
|
|
341
|
+
* - When `config.output.defaultBanner` is `false`, returns `undefined`.
|
|
342
|
+
* - Otherwise returns the Kubb "Generated by Kubb" notice.
|
|
343
|
+
*
|
|
344
|
+
* @example String banner overrides default
|
|
345
|
+
* ```ts
|
|
346
|
+
* defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
|
|
347
|
+
* // → '// my banner'
|
|
348
|
+
* ```
|
|
349
|
+
*
|
|
350
|
+
* @example Function banner with node
|
|
351
|
+
* ```ts
|
|
352
|
+
* defaultResolveBanner(inputNode, { output: { banner: (node) => `// v${node.version}` }, config })
|
|
353
|
+
* // → '// v3.0.0'
|
|
354
|
+
* ```
|
|
355
|
+
*
|
|
356
|
+
* @example No user banner — Kubb notice with OAS metadata
|
|
357
|
+
* ```ts
|
|
358
|
+
* defaultResolveBanner(inputNode, { config })
|
|
359
|
+
* // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
|
|
360
|
+
* ```
|
|
361
|
+
*
|
|
362
|
+
* @example Disabled default banner
|
|
363
|
+
* ```ts
|
|
364
|
+
* defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
|
|
365
|
+
* // → undefined
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
368
|
+
function defaultResolveBanner(node, { output, config }) {
|
|
369
|
+
if (typeof output?.banner === "function") return output.banner(node);
|
|
370
|
+
if (typeof output?.banner === "string") return output.banner;
|
|
371
|
+
if (config.output.defaultBanner === false) return;
|
|
372
|
+
return buildDefaultBanner({
|
|
373
|
+
title: node?.meta?.title,
|
|
374
|
+
version: node?.meta?.version,
|
|
375
|
+
config
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Default footer resolver — returns the footer string for a generated file.
|
|
380
|
+
*
|
|
381
|
+
* - When `output.footer` is a function and `node` is provided, calls it with the node.
|
|
382
|
+
* - When `output.footer` is a function and `node` is absent, returns `undefined`.
|
|
383
|
+
* - When `output.footer` is a string, returns it directly.
|
|
384
|
+
* - Otherwise returns `undefined`.
|
|
385
|
+
*
|
|
386
|
+
* @example String footer
|
|
387
|
+
* ```ts
|
|
388
|
+
* defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
|
|
389
|
+
* // → '// end of file'
|
|
390
|
+
* ```
|
|
391
|
+
*
|
|
392
|
+
* @example Function footer with node
|
|
393
|
+
* ```ts
|
|
394
|
+
* defaultResolveFooter(inputNode, { output: { footer: (node) => `// ${node.title}` }, config })
|
|
395
|
+
* // → '// Pet Store'
|
|
396
|
+
* ```
|
|
397
|
+
*/
|
|
398
|
+
function defaultResolveFooter(node, { output }) {
|
|
399
|
+
if (typeof output?.footer === "function") return node ? output.footer(node) : void 0;
|
|
400
|
+
if (typeof output?.footer === "string") return output.footer;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Defines a resolver for a plugin, injecting built-in defaults for name casing,
|
|
404
|
+
* include/exclude/override filtering, path resolution, and file construction.
|
|
405
|
+
*
|
|
406
|
+
* All four defaults can be overridden by providing them in the builder function:
|
|
407
|
+
* - `default` — name casing strategy (camelCase / PascalCase)
|
|
408
|
+
* - `resolveOptions` — include/exclude/override filtering
|
|
409
|
+
* - `resolvePath` — output path computation
|
|
410
|
+
* - `resolveFile` — full `FileNode` construction
|
|
411
|
+
*
|
|
412
|
+
* The builder receives `ctx` — a reference to the assembled resolver — so methods can
|
|
413
|
+
* call sibling resolver methods using `ctx` instead of `this`.
|
|
414
|
+
*
|
|
415
|
+
* @example Basic resolver with naming helpers
|
|
416
|
+
* ```ts
|
|
417
|
+
* export const resolver = defineResolver<PluginTs>((ctx) => ({
|
|
418
|
+
* name: 'default',
|
|
419
|
+
* resolveName(node) {
|
|
420
|
+
* return ctx.default(node.name, 'function')
|
|
421
|
+
* },
|
|
422
|
+
* resolveTypedName(node) {
|
|
423
|
+
* return ctx.default(node.name, 'type')
|
|
424
|
+
* },
|
|
425
|
+
* }))
|
|
426
|
+
* ```
|
|
427
|
+
*
|
|
428
|
+
* @example Override resolvePath for a custom output structure
|
|
429
|
+
* ```ts
|
|
430
|
+
* export const resolver = defineResolver<PluginTs>((_ctx) => ({
|
|
431
|
+
* name: 'custom',
|
|
432
|
+
* resolvePath({ baseName }, { root, output }) {
|
|
433
|
+
* return path.resolve(root, output.path, 'generated', baseName)
|
|
434
|
+
* },
|
|
435
|
+
* }))
|
|
436
|
+
* ```
|
|
437
|
+
*
|
|
438
|
+
* @example Use ctx.default inside a helper
|
|
439
|
+
* ```ts
|
|
440
|
+
* export const resolver = defineResolver<PluginTs>((ctx) => ({
|
|
441
|
+
* name: 'default',
|
|
442
|
+
* resolveParamName(node, param) {
|
|
443
|
+
* return ctx.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
|
|
444
|
+
* },
|
|
445
|
+
* }))
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
448
|
+
function defineResolver(build) {
|
|
449
|
+
const resolver = {};
|
|
450
|
+
Object.assign(resolver, {
|
|
451
|
+
default: defaultResolver,
|
|
452
|
+
resolveOptions: defaultResolveOptions,
|
|
453
|
+
resolvePath: defaultResolvePath,
|
|
454
|
+
resolveFile: (params, context) => defaultResolveFile(params, context, resolver),
|
|
455
|
+
resolveBanner: defaultResolveBanner,
|
|
456
|
+
resolveFooter: defaultResolveFooter,
|
|
457
|
+
...build(resolver)
|
|
458
|
+
});
|
|
459
|
+
return resolver;
|
|
460
|
+
}
|
|
461
|
+
//#endregion
|
|
462
|
+
//#region src/devtools.ts
|
|
463
|
+
/**
|
|
464
|
+
* Encodes an `InputNode` as a compressed, URL-safe string.
|
|
465
|
+
*
|
|
466
|
+
* The JSON representation is deflate-compressed with {@link deflateSync} before
|
|
467
|
+
* base64url encoding, which typically reduces payload size by 70–80 % and
|
|
468
|
+
* keeps URLs well within browser and server path-length limits.
|
|
469
|
+
*
|
|
470
|
+
* Use {@link decodeAst} to reverse.
|
|
471
|
+
*/
|
|
472
|
+
function encodeAst(input) {
|
|
473
|
+
const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(input)));
|
|
474
|
+
return Buffer.from(compressed).toString("base64url");
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Constructs the Kubb Studio URL for the given `InputNode`.
|
|
478
|
+
* When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
|
|
479
|
+
* The `input` is encoded and attached as the `?root=` query parameter so Studio
|
|
480
|
+
* can decode and render it without a round-trip to any server.
|
|
481
|
+
*/
|
|
482
|
+
function getStudioUrl(input, studioUrl, options = {}) {
|
|
483
|
+
return `${studioUrl.replace(/\/$/, "")}${options.ast ? "/ast" : ""}?root=${encodeAst(input)}`;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Opens the Kubb Studio URL for the given `InputNode` in the default browser —
|
|
487
|
+
*
|
|
488
|
+
* Falls back to printing the URL if the browser cannot be launched.
|
|
489
|
+
*/
|
|
490
|
+
async function openInStudio(input, studioUrl, options = {}) {
|
|
491
|
+
const url = getStudioUrl(input, studioUrl, options);
|
|
492
|
+
const cmd = process.platform === "win32" ? "cmd" : process.platform === "darwin" ? "open" : "xdg-open";
|
|
493
|
+
const args = process.platform === "win32" ? [
|
|
494
|
+
"/c",
|
|
495
|
+
"start",
|
|
496
|
+
"",
|
|
497
|
+
url
|
|
498
|
+
] : [url];
|
|
499
|
+
try {
|
|
500
|
+
await x(cmd, args);
|
|
501
|
+
} catch {
|
|
502
|
+
console.log(`\n ${url}\n`);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
//#endregion
|
|
506
|
+
//#region src/FileManager.ts
|
|
507
|
+
function mergeFile(a, b) {
|
|
508
|
+
return {
|
|
509
|
+
...a,
|
|
510
|
+
banner: b.banner,
|
|
511
|
+
footer: b.footer,
|
|
512
|
+
sources: [...a.sources || [], ...b.sources || []],
|
|
513
|
+
imports: [...a.imports || [], ...b.imports || []],
|
|
514
|
+
exports: [...a.exports || [], ...b.exports || []]
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Collapses a list of files so that duplicates sharing the same `path` are merged
|
|
519
|
+
* in arrival order. Keeps the original order of first occurrence.
|
|
520
|
+
*/
|
|
521
|
+
function mergeFilesByPath(files) {
|
|
522
|
+
const merged = /* @__PURE__ */ new Map();
|
|
523
|
+
for (const file of files) {
|
|
524
|
+
const existing = merged.get(file.path);
|
|
525
|
+
merged.set(file.path, existing ? mergeFile(existing, file) : file);
|
|
526
|
+
}
|
|
527
|
+
return merged;
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* In-memory file store for generated files.
|
|
531
|
+
*
|
|
532
|
+
* Files with the same `path` are merged — sources, imports, and exports are concatenated.
|
|
533
|
+
* The `files` getter returns all stored files sorted by path length (shortest first).
|
|
534
|
+
*
|
|
535
|
+
* @example
|
|
536
|
+
* ```ts
|
|
537
|
+
* import { FileManager } from '@kubb/core'
|
|
538
|
+
*
|
|
539
|
+
* const manager = new FileManager()
|
|
540
|
+
* manager.upsert(myFile)
|
|
541
|
+
* console.log(manager.files) // all stored files
|
|
542
|
+
* ```
|
|
543
|
+
*/
|
|
544
|
+
var FileManager = class {
|
|
545
|
+
#cache = /* @__PURE__ */ new Map();
|
|
546
|
+
#filesCache = null;
|
|
547
|
+
/**
|
|
548
|
+
* Adds one or more files. Incoming files with the same path are merged
|
|
549
|
+
* (sources/imports/exports concatenated), but existing cache entries are
|
|
550
|
+
* replaced — use {@link upsert} when you want to merge into the cache too.
|
|
551
|
+
*/
|
|
552
|
+
add(...files) {
|
|
553
|
+
return this.#store(files, false);
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Adds or merges one or more files.
|
|
557
|
+
* If a file with the same path already exists in the cache, its
|
|
558
|
+
* sources/imports/exports are merged into the incoming file.
|
|
559
|
+
*/
|
|
560
|
+
upsert(...files) {
|
|
561
|
+
return this.#store(files, true);
|
|
562
|
+
}
|
|
563
|
+
#store(files, mergeExisting) {
|
|
564
|
+
const resolvedFiles = [];
|
|
565
|
+
for (const file of mergeFilesByPath(files).values()) {
|
|
566
|
+
const existing = mergeExisting ? this.#cache.get(file.path) : void 0;
|
|
567
|
+
const resolvedFile = createFile(existing ? mergeFile(existing, file) : file);
|
|
568
|
+
this.#cache.set(resolvedFile.path, resolvedFile);
|
|
569
|
+
resolvedFiles.push(resolvedFile);
|
|
570
|
+
}
|
|
571
|
+
this.#filesCache = null;
|
|
572
|
+
return resolvedFiles;
|
|
573
|
+
}
|
|
574
|
+
getByPath(path) {
|
|
575
|
+
return this.#cache.get(path) ?? null;
|
|
576
|
+
}
|
|
577
|
+
deleteByPath(path) {
|
|
578
|
+
this.#cache.delete(path);
|
|
579
|
+
this.#filesCache = null;
|
|
580
|
+
}
|
|
581
|
+
clear() {
|
|
582
|
+
this.#cache.clear();
|
|
583
|
+
this.#filesCache = null;
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* All stored files, sorted by path length (shorter paths first).
|
|
587
|
+
*/
|
|
588
|
+
get files() {
|
|
589
|
+
if (this.#filesCache) return this.#filesCache;
|
|
590
|
+
this.#filesCache = [...this.#cache.values()].sort((a, b) => {
|
|
591
|
+
const lenDiff = a.path.length - b.path.length;
|
|
592
|
+
if (lenDiff !== 0) return lenDiff;
|
|
593
|
+
const aIsIndex = a.path.endsWith("/index.ts") || a.path === "index.ts";
|
|
594
|
+
const bIsIndex = b.path.endsWith("/index.ts") || b.path === "index.ts";
|
|
595
|
+
if (aIsIndex && !bIsIndex) return 1;
|
|
596
|
+
if (!aIsIndex && bIsIndex) return -1;
|
|
597
|
+
return 0;
|
|
598
|
+
});
|
|
599
|
+
return this.#filesCache;
|
|
600
|
+
}
|
|
601
|
+
};
|
|
602
|
+
//#endregion
|
|
603
|
+
//#region src/renderNode.ts
|
|
604
|
+
/**
|
|
605
|
+
* Handles the return value of a plugin AST hook or generator method.
|
|
606
|
+
*
|
|
607
|
+
* - Renderer output → rendered via the provided `rendererFactory` (e.g. JSX), files stored in `driver.fileManager`
|
|
608
|
+
* - `Array<FileNode>` → added directly into `driver.fileManager`
|
|
609
|
+
* - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
|
|
610
|
+
*
|
|
611
|
+
* Pass a `rendererFactory` (e.g. `jsxRenderer` from `@kubb/renderer-jsx`) when the result
|
|
612
|
+
* may be a renderer element. Generators that only return `Array<FileNode>` do not need one.
|
|
613
|
+
*/
|
|
614
|
+
async function applyHookResult(result, driver, rendererFactory) {
|
|
615
|
+
if (!result) return;
|
|
616
|
+
if (Array.isArray(result)) {
|
|
617
|
+
driver.fileManager.upsert(...result);
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
if (!rendererFactory) return;
|
|
621
|
+
const renderer = rendererFactory();
|
|
622
|
+
await renderer.render(result);
|
|
623
|
+
driver.fileManager.upsert(...renderer.files);
|
|
624
|
+
renderer.unmount();
|
|
625
|
+
}
|
|
626
|
+
//#endregion
|
|
627
|
+
//#region src/PluginDriver.ts
|
|
628
|
+
function enforceOrder(enforce) {
|
|
629
|
+
return enforce === "pre" ? -1 : enforce === "post" ? 1 : 0;
|
|
630
|
+
}
|
|
631
|
+
var PluginDriver = class PluginDriver {
|
|
632
|
+
config;
|
|
633
|
+
options;
|
|
634
|
+
/**
|
|
635
|
+
* Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
|
|
636
|
+
*
|
|
637
|
+
* @example
|
|
638
|
+
* ```ts
|
|
639
|
+
* PluginDriver.getMode('src/gen/types.ts') // 'single'
|
|
640
|
+
* PluginDriver.getMode('src/gen/types') // 'split'
|
|
641
|
+
* ```
|
|
642
|
+
*/
|
|
643
|
+
static getMode(fileOrFolder) {
|
|
644
|
+
if (!fileOrFolder) return "split";
|
|
645
|
+
return extname(fileOrFolder) ? "single" : "split";
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* The universal `@kubb/ast` `InputNode` produced by the adapter, set by
|
|
649
|
+
* the build pipeline after the adapter's `parse()` resolves.
|
|
650
|
+
*/
|
|
651
|
+
inputNode = void 0;
|
|
652
|
+
adapter = void 0;
|
|
653
|
+
#studioIsOpen = false;
|
|
654
|
+
/**
|
|
655
|
+
* Central file store for all generated files.
|
|
656
|
+
* Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
|
|
657
|
+
* add files; this property gives direct read/write access when needed.
|
|
658
|
+
*/
|
|
659
|
+
fileManager = new FileManager();
|
|
660
|
+
plugins = /* @__PURE__ */ new Map();
|
|
661
|
+
/**
|
|
662
|
+
* Tracks which plugins have generators registered via `addGenerator()` (event-based path).
|
|
663
|
+
* Used by the build loop to decide whether to emit generator events for a given plugin.
|
|
664
|
+
*/
|
|
665
|
+
#pluginsWithEventGenerators = /* @__PURE__ */ new Set();
|
|
666
|
+
#resolvers = /* @__PURE__ */ new Map();
|
|
667
|
+
#defaultResolvers = /* @__PURE__ */ new Map();
|
|
668
|
+
#hookListeners = /* @__PURE__ */ new Map();
|
|
669
|
+
constructor(config, options) {
|
|
670
|
+
this.config = config;
|
|
671
|
+
this.options = options;
|
|
672
|
+
config.plugins.map((rawPlugin) => this.#normalizePlugin(rawPlugin)).filter((plugin) => {
|
|
673
|
+
if (typeof plugin.apply === "function") return plugin.apply(config);
|
|
674
|
+
return true;
|
|
675
|
+
}).sort((a, b) => {
|
|
676
|
+
if (b.dependencies?.includes(a.name)) return -1;
|
|
677
|
+
if (a.dependencies?.includes(b.name)) return 1;
|
|
678
|
+
return enforceOrder(a.enforce) - enforceOrder(b.enforce);
|
|
679
|
+
}).forEach((plugin) => {
|
|
680
|
+
this.plugins.set(plugin.name, plugin);
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
get hooks() {
|
|
684
|
+
return this.options.hooks;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Creates an `NormalizedPlugin` from a hook-style plugin and registers
|
|
688
|
+
* its lifecycle handlers on the `AsyncEventEmitter`.
|
|
689
|
+
*/
|
|
690
|
+
#normalizePlugin(hookPlugin) {
|
|
691
|
+
const normalizedPlugin = {
|
|
692
|
+
name: hookPlugin.name,
|
|
693
|
+
dependencies: hookPlugin.dependencies,
|
|
694
|
+
enforce: hookPlugin.enforce,
|
|
695
|
+
options: {
|
|
696
|
+
output: { path: "." },
|
|
697
|
+
exclude: [],
|
|
698
|
+
override: []
|
|
699
|
+
}
|
|
700
|
+
};
|
|
701
|
+
this.registerPluginHooks(hookPlugin, normalizedPlugin);
|
|
702
|
+
return normalizedPlugin;
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Registers a hook-style plugin's lifecycle handlers on the shared `AsyncEventEmitter`.
|
|
706
|
+
*
|
|
707
|
+
* For `kubb:plugin:setup`, the registered listener wraps the globally emitted context with a
|
|
708
|
+
* plugin-specific one so that `addGenerator`, `setResolver`, `setTransformer`, and
|
|
709
|
+
* `setRenderer` all target the correct `normalizedPlugin` entry in the plugins map.
|
|
710
|
+
*
|
|
711
|
+
* All other hooks are iterated and registered directly as pass-through listeners.
|
|
712
|
+
* Any event key present in the global `KubbHooks` interface can be subscribed to.
|
|
713
|
+
*
|
|
714
|
+
* External tooling can subscribe to any of these events via `hooks.on(...)` to observe
|
|
715
|
+
* the plugin lifecycle without modifying plugin behavior.
|
|
716
|
+
*
|
|
717
|
+
* @internal
|
|
718
|
+
*/
|
|
719
|
+
registerPluginHooks(hookPlugin, normalizedPlugin) {
|
|
720
|
+
const { hooks } = hookPlugin;
|
|
721
|
+
if (hooks["kubb:plugin:setup"]) {
|
|
722
|
+
const setupHandler = (globalCtx) => {
|
|
723
|
+
const pluginCtx = {
|
|
724
|
+
...globalCtx,
|
|
725
|
+
options: hookPlugin.options ?? {},
|
|
726
|
+
addGenerator: (gen) => {
|
|
727
|
+
this.registerGenerator(normalizedPlugin.name, gen);
|
|
728
|
+
},
|
|
729
|
+
setResolver: (resolver) => {
|
|
730
|
+
this.setPluginResolver(normalizedPlugin.name, resolver);
|
|
731
|
+
},
|
|
732
|
+
setTransformer: (visitor) => {
|
|
733
|
+
normalizedPlugin.transformer = visitor;
|
|
734
|
+
},
|
|
735
|
+
setRenderer: (renderer) => {
|
|
736
|
+
normalizedPlugin.renderer = renderer;
|
|
737
|
+
},
|
|
738
|
+
setOptions: (opts) => {
|
|
739
|
+
normalizedPlugin.options = {
|
|
740
|
+
...normalizedPlugin.options,
|
|
741
|
+
...opts
|
|
742
|
+
};
|
|
743
|
+
},
|
|
744
|
+
injectFile: (userFileNode) => {
|
|
745
|
+
this.fileManager.add(createFile(userFileNode));
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
return hooks["kubb:plugin:setup"](pluginCtx);
|
|
749
|
+
};
|
|
750
|
+
this.hooks.on("kubb:plugin:setup", setupHandler);
|
|
751
|
+
this.#trackHookListener("kubb:plugin:setup", setupHandler);
|
|
752
|
+
}
|
|
753
|
+
for (const [event, handler] of Object.entries(hooks)) {
|
|
754
|
+
if (event === "kubb:plugin:setup" || !handler) continue;
|
|
755
|
+
this.hooks.on(event, handler);
|
|
756
|
+
this.#trackHookListener(event, handler);
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
|
|
761
|
+
* can configure generators, resolvers, transformers and renderers before `buildStart` runs.
|
|
762
|
+
*
|
|
763
|
+
* Call this once from `safeBuild` before the plugin execution loop begins.
|
|
764
|
+
*/
|
|
765
|
+
async emitSetupHooks() {
|
|
766
|
+
const noop = () => {};
|
|
767
|
+
await this.hooks.emit("kubb:plugin:setup", {
|
|
768
|
+
config: this.config,
|
|
769
|
+
options: {},
|
|
770
|
+
addGenerator: noop,
|
|
771
|
+
setResolver: noop,
|
|
772
|
+
setTransformer: noop,
|
|
773
|
+
setRenderer: noop,
|
|
774
|
+
setOptions: noop,
|
|
775
|
+
injectFile: noop,
|
|
776
|
+
updateConfig: noop
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
* Registers a generator for the given plugin on the shared event emitter.
|
|
781
|
+
*
|
|
782
|
+
* The generator's `schema`, `operation`, and `operations` methods are registered as
|
|
783
|
+
* listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
|
|
784
|
+
* respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
|
|
785
|
+
* so that generators from different plugins do not cross-fire.
|
|
786
|
+
*
|
|
787
|
+
* The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
|
|
788
|
+
* Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
|
|
789
|
+
* declares a renderer.
|
|
790
|
+
*
|
|
791
|
+
* Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
|
|
792
|
+
*/
|
|
793
|
+
registerGenerator(pluginName, gen) {
|
|
794
|
+
const resolveRenderer = () => {
|
|
795
|
+
const plugin = this.plugins.get(pluginName);
|
|
796
|
+
return gen.renderer === null ? void 0 : gen.renderer ?? plugin?.renderer ?? this.config.renderer;
|
|
797
|
+
};
|
|
798
|
+
if (gen.schema) {
|
|
799
|
+
const schemaHandler = async (node, ctx) => {
|
|
800
|
+
if (ctx.plugin.name !== pluginName) return;
|
|
801
|
+
await applyHookResult(await gen.schema(node, ctx), this, resolveRenderer());
|
|
802
|
+
};
|
|
803
|
+
this.hooks.on("kubb:generate:schema", schemaHandler);
|
|
804
|
+
this.#trackHookListener("kubb:generate:schema", schemaHandler);
|
|
805
|
+
}
|
|
806
|
+
if (gen.operation) {
|
|
807
|
+
const operationHandler = async (node, ctx) => {
|
|
808
|
+
if (ctx.plugin.name !== pluginName) return;
|
|
809
|
+
await applyHookResult(await gen.operation(node, ctx), this, resolveRenderer());
|
|
810
|
+
};
|
|
811
|
+
this.hooks.on("kubb:generate:operation", operationHandler);
|
|
812
|
+
this.#trackHookListener("kubb:generate:operation", operationHandler);
|
|
813
|
+
}
|
|
814
|
+
if (gen.operations) {
|
|
815
|
+
const operationsHandler = async (nodes, ctx) => {
|
|
816
|
+
if (ctx.plugin.name !== pluginName) return;
|
|
817
|
+
await applyHookResult(await gen.operations(nodes, ctx), this, resolveRenderer());
|
|
818
|
+
};
|
|
819
|
+
this.hooks.on("kubb:generate:operations", operationsHandler);
|
|
820
|
+
this.#trackHookListener("kubb:generate:operations", operationsHandler);
|
|
821
|
+
}
|
|
822
|
+
this.#pluginsWithEventGenerators.add(pluginName);
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* Returns `true` when at least one generator was registered for the given plugin
|
|
826
|
+
* via `addGenerator()` in `kubb:plugin:setup` (event-based path).
|
|
827
|
+
*
|
|
828
|
+
* Used by the build loop to decide whether to walk the AST and emit generator events
|
|
829
|
+
* for a plugin that has no static `plugin.generators`.
|
|
830
|
+
*/
|
|
831
|
+
hasRegisteredGenerators(pluginName) {
|
|
832
|
+
return this.#pluginsWithEventGenerators.has(pluginName);
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* Unregisters all plugin lifecycle listeners from the shared event emitter.
|
|
836
|
+
* Called at the end of a build to prevent listener leaks across repeated builds.
|
|
837
|
+
*
|
|
838
|
+
* @internal
|
|
839
|
+
*/
|
|
840
|
+
dispose() {
|
|
841
|
+
for (const [event, handlers] of this.#hookListeners) for (const handler of handlers) this.hooks.off(event, handler);
|
|
842
|
+
this.#hookListeners.clear();
|
|
843
|
+
this.#pluginsWithEventGenerators.clear();
|
|
844
|
+
}
|
|
845
|
+
#trackHookListener(event, handler) {
|
|
846
|
+
let handlers = this.#hookListeners.get(event);
|
|
847
|
+
if (!handlers) {
|
|
848
|
+
handlers = /* @__PURE__ */ new Set();
|
|
849
|
+
this.#hookListeners.set(event, handlers);
|
|
850
|
+
}
|
|
851
|
+
handlers.add(handler);
|
|
852
|
+
}
|
|
853
|
+
#createDefaultResolver(pluginName) {
|
|
854
|
+
const existingResolver = this.#defaultResolvers.get(pluginName);
|
|
855
|
+
if (existingResolver) return existingResolver;
|
|
856
|
+
const resolver = defineResolver((_ctx) => ({
|
|
857
|
+
name: "default",
|
|
858
|
+
pluginName
|
|
859
|
+
}));
|
|
860
|
+
this.#defaultResolvers.set(pluginName, resolver);
|
|
861
|
+
return resolver;
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* Merges `partial` with the plugin's default resolver and stores the result.
|
|
865
|
+
* Also mirrors it onto `plugin.resolver` so callers using `getPlugin(name).resolver`
|
|
866
|
+
* get the up-to-date resolver without going through `getResolver()`.
|
|
867
|
+
*/
|
|
868
|
+
setPluginResolver(pluginName, partial) {
|
|
869
|
+
const merged = {
|
|
870
|
+
...this.#createDefaultResolver(pluginName),
|
|
871
|
+
...partial
|
|
872
|
+
};
|
|
873
|
+
this.#resolvers.set(pluginName, merged);
|
|
874
|
+
const plugin = this.plugins.get(pluginName);
|
|
875
|
+
if (plugin) plugin.resolver = merged;
|
|
876
|
+
}
|
|
877
|
+
getResolver(pluginName) {
|
|
878
|
+
return this.#resolvers.get(pluginName) ?? this.plugins.get(pluginName)?.resolver ?? this.#createDefaultResolver(pluginName);
|
|
879
|
+
}
|
|
880
|
+
getContext(plugin) {
|
|
881
|
+
const driver = this;
|
|
882
|
+
return {
|
|
883
|
+
config: driver.config,
|
|
884
|
+
get root() {
|
|
885
|
+
return resolve(driver.config.root, driver.config.output.path);
|
|
886
|
+
},
|
|
887
|
+
getMode(output) {
|
|
888
|
+
return PluginDriver.getMode(resolve(driver.config.root, driver.config.output.path, output.path));
|
|
889
|
+
},
|
|
890
|
+
hooks: driver.hooks,
|
|
891
|
+
plugin,
|
|
892
|
+
getPlugin: driver.getPlugin.bind(driver),
|
|
893
|
+
requirePlugin: driver.requirePlugin.bind(driver),
|
|
894
|
+
getResolver: driver.getResolver.bind(driver),
|
|
895
|
+
driver,
|
|
896
|
+
addFile: async (...files) => {
|
|
897
|
+
driver.fileManager.add(...files);
|
|
898
|
+
},
|
|
899
|
+
upsertFile: async (...files) => {
|
|
900
|
+
driver.fileManager.upsert(...files);
|
|
901
|
+
},
|
|
902
|
+
get inputNode() {
|
|
903
|
+
return driver.inputNode;
|
|
904
|
+
},
|
|
905
|
+
get adapter() {
|
|
906
|
+
return driver.adapter;
|
|
907
|
+
},
|
|
908
|
+
get resolver() {
|
|
909
|
+
return driver.getResolver(plugin.name);
|
|
910
|
+
},
|
|
911
|
+
get transformer() {
|
|
912
|
+
return plugin.transformer;
|
|
913
|
+
},
|
|
914
|
+
warn(message) {
|
|
915
|
+
driver.hooks.emit("kubb:warn", { message });
|
|
916
|
+
},
|
|
917
|
+
error(error) {
|
|
918
|
+
driver.hooks.emit("kubb:error", { error: typeof error === "string" ? new Error(error) : error });
|
|
919
|
+
},
|
|
920
|
+
info(message) {
|
|
921
|
+
driver.hooks.emit("kubb:info", { message });
|
|
922
|
+
},
|
|
923
|
+
openInStudio(options) {
|
|
924
|
+
if (!driver.config.devtools || driver.#studioIsOpen) return;
|
|
925
|
+
if (typeof driver.config.devtools !== "object") throw new Error("Devtools must be an object");
|
|
926
|
+
if (!driver.inputNode || !driver.adapter) throw new Error("adapter is not defined, make sure you have set the parser in kubb.config.ts");
|
|
927
|
+
driver.#studioIsOpen = true;
|
|
928
|
+
const studioUrl = driver.config.devtools?.studioUrl ?? "https://studio.kubb.dev";
|
|
929
|
+
return openInStudio(driver.inputNode, studioUrl, options);
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
getPlugin(pluginName) {
|
|
934
|
+
return this.plugins.get(pluginName);
|
|
935
|
+
}
|
|
936
|
+
requirePlugin(pluginName) {
|
|
937
|
+
const plugin = this.plugins.get(pluginName);
|
|
938
|
+
if (!plugin) throw new Error(`[kubb] Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`);
|
|
939
|
+
return plugin;
|
|
940
|
+
}
|
|
941
|
+
};
|
|
942
|
+
//#endregion
|
|
943
|
+
export { DEFAULT_BANNER as a, logLevel as c, defineResolver as i, camelCase as l, applyHookResult as n, DEFAULT_EXTENSION as o, FileManager as r, DEFAULT_STUDIO_URL as s, PluginDriver as t };
|
|
944
|
+
|
|
945
|
+
//# sourceMappingURL=PluginDriver-DV3p2Hky.js.map
|