@kubb/core 5.0.0-alpha.6 → 5.0.0-alpha.61
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 +3 -2
- package/dist/PluginDriver-Bc0HQM8V.js +948 -0
- package/dist/PluginDriver-Bc0HQM8V.js.map +1 -0
- package/dist/PluginDriver-Dyl2fwfQ.cjs +1039 -0
- package/dist/PluginDriver-Dyl2fwfQ.cjs.map +1 -0
- package/dist/index.cjs +691 -1798
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +279 -265
- package/dist/index.js +678 -1765
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +138 -0
- package/dist/mocks.cjs.map +1 -0
- package/dist/mocks.d.ts +74 -0
- package/dist/mocks.js +133 -0
- package/dist/mocks.js.map +1 -0
- package/dist/types-mW3-Ihuf.d.ts +1903 -0
- package/package.json +51 -57
- package/src/FileManager.ts +110 -0
- package/src/FileProcessor.ts +86 -0
- package/src/Kubb.ts +205 -130
- package/src/PluginDriver.ts +424 -0
- package/src/constants.ts +20 -47
- package/src/createAdapter.ts +25 -0
- package/src/createKubb.ts +527 -0
- package/src/createRenderer.ts +57 -0
- package/src/createStorage.ts +58 -0
- package/src/defineGenerator.ts +88 -100
- package/src/defineLogger.ts +13 -3
- package/src/defineMiddleware.ts +59 -0
- package/src/defineParser.ts +45 -0
- package/src/definePlugin.ts +78 -7
- package/src/defineResolver.ts +521 -0
- package/src/devtools.ts +14 -14
- package/src/index.ts +13 -17
- package/src/mocks.ts +171 -0
- package/src/renderNode.ts +35 -0
- package/src/storages/fsStorage.ts +40 -11
- package/src/storages/memoryStorage.ts +4 -3
- package/src/types.ts +738 -218
- package/src/utils/diagnostics.ts +4 -1
- package/src/utils/isInputPath.ts +10 -0
- package/src/utils/packageJSON.ts +99 -0
- package/dist/PluginManager-vZodFEMe.d.ts +0 -1056
- package/dist/chunk-ByKO4r7w.cjs +0 -38
- package/dist/hooks.cjs +0 -60
- package/dist/hooks.cjs.map +0 -1
- package/dist/hooks.d.ts +0 -64
- package/dist/hooks.js +0 -56
- package/dist/hooks.js.map +0 -1
- package/src/BarrelManager.ts +0 -74
- package/src/PackageManager.ts +0 -180
- package/src/PluginManager.ts +0 -667
- package/src/PromiseManager.ts +0 -40
- package/src/build.ts +0 -419
- package/src/config.ts +0 -56
- package/src/defineAdapter.ts +0 -22
- package/src/defineStorage.ts +0 -56
- package/src/errors.ts +0 -1
- package/src/hooks/index.ts +0 -4
- package/src/hooks/useKubb.ts +0 -55
- package/src/hooks/useMode.ts +0 -11
- package/src/hooks/usePlugin.ts +0 -11
- package/src/hooks/usePluginManager.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 -79
- package/src/utils/getConfigs.ts +0 -30
- package/src/utils/getPlugins.ts +0 -23
- package/src/utils/linters.ts +0 -25
- package/src/utils/resolveOptions.ts +0 -93
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { camelCase, pascalCase } from '@internals/utils'
|
|
3
|
+
import type { FileNode, InputNode, Node, OperationNode, SchemaNode } from '@kubb/ast'
|
|
4
|
+
import { createFile, isOperationNode, isSchemaNode } from '@kubb/ast'
|
|
5
|
+
import { PluginDriver } from './PluginDriver.ts'
|
|
6
|
+
import type {
|
|
7
|
+
Config,
|
|
8
|
+
PluginFactoryOptions,
|
|
9
|
+
ResolveBannerContext,
|
|
10
|
+
ResolveOptionsContext,
|
|
11
|
+
Resolver,
|
|
12
|
+
ResolverContext,
|
|
13
|
+
ResolverFileParams,
|
|
14
|
+
ResolverPathParams,
|
|
15
|
+
} from './types.ts'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Builder type for the plugin-specific resolver fields.
|
|
19
|
+
*
|
|
20
|
+
* `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
|
|
21
|
+
* are optional — built-in fallbacks are injected when omitted.
|
|
22
|
+
*
|
|
23
|
+
* The builder receives `ctx` — a reference to the fully assembled resolver — so methods can
|
|
24
|
+
* call sibling resolver methods without using `this`. Because `ctx` is captured by the closure
|
|
25
|
+
* and the resolver is populated after the builder runs, `ctx` correctly reflects any overrides
|
|
26
|
+
* that were applied by the builder itself.
|
|
27
|
+
*/
|
|
28
|
+
type ResolverBuilder<T extends PluginFactoryOptions> = (ctx: T['resolver']) => Omit<
|
|
29
|
+
T['resolver'],
|
|
30
|
+
'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter' | 'name' | 'pluginName'
|
|
31
|
+
> &
|
|
32
|
+
Partial<Pick<T['resolver'], 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter'>> & {
|
|
33
|
+
name: string
|
|
34
|
+
pluginName: T['name']
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// String patterns are compiled lazily and cached — the same filter is reused for every node.
|
|
38
|
+
const stringPatternCache = new Map<string, RegExp>()
|
|
39
|
+
|
|
40
|
+
function testPattern(value: string, pattern: string | RegExp): boolean {
|
|
41
|
+
if (typeof pattern === 'string') {
|
|
42
|
+
let regex = stringPatternCache.get(pattern)
|
|
43
|
+
if (!regex) {
|
|
44
|
+
regex = new RegExp(pattern)
|
|
45
|
+
stringPatternCache.set(pattern, regex)
|
|
46
|
+
}
|
|
47
|
+
return regex.test(value)
|
|
48
|
+
}
|
|
49
|
+
// Use .match() for user-supplied RegExp to preserve semantics regardless of `g`/`y` flags.
|
|
50
|
+
return value.match(pattern) !== null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
|
|
55
|
+
*/
|
|
56
|
+
function matchesOperationPattern(node: OperationNode, type: string, pattern: string | RegExp): boolean {
|
|
57
|
+
switch (type) {
|
|
58
|
+
case 'tag':
|
|
59
|
+
return node.tags.some((tag) => testPattern(tag, pattern))
|
|
60
|
+
case 'operationId':
|
|
61
|
+
return testPattern(node.operationId, pattern)
|
|
62
|
+
case 'path':
|
|
63
|
+
return testPattern(node.path, pattern)
|
|
64
|
+
case 'method':
|
|
65
|
+
return testPattern(node.method.toLowerCase(), pattern)
|
|
66
|
+
case 'contentType':
|
|
67
|
+
return node.requestBody?.content?.some((c) => testPattern(c.contentType, pattern)) ?? false
|
|
68
|
+
default:
|
|
69
|
+
return false
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Checks if a schema matches a pattern for a given filter type (`schemaName`).
|
|
75
|
+
*
|
|
76
|
+
* Returns `null` when the filter type doesn't apply to schemas.
|
|
77
|
+
*/
|
|
78
|
+
function matchesSchemaPattern(node: SchemaNode, type: string, pattern: string | RegExp): boolean | null {
|
|
79
|
+
switch (type) {
|
|
80
|
+
case 'schemaName':
|
|
81
|
+
return node.name ? testPattern(node.name, pattern) : false
|
|
82
|
+
default:
|
|
83
|
+
return null
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Default name resolver used by `defineResolver`.
|
|
89
|
+
*
|
|
90
|
+
* - `camelCase` for `function` and `file` types.
|
|
91
|
+
* - `PascalCase` for `type`.
|
|
92
|
+
* - `camelCase` for everything else.
|
|
93
|
+
*/
|
|
94
|
+
function defaultResolver(name: string, type?: 'file' | 'function' | 'type' | 'const'): string {
|
|
95
|
+
let resolvedName = camelCase(name)
|
|
96
|
+
|
|
97
|
+
if (type === 'file' || type === 'function') {
|
|
98
|
+
resolvedName = camelCase(name, {
|
|
99
|
+
isFile: type === 'file',
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (type === 'type') {
|
|
104
|
+
resolvedName = pascalCase(name)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return resolvedName
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Default option resolver — applies include/exclude filters and merges matching override options.
|
|
112
|
+
*
|
|
113
|
+
* Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
|
|
114
|
+
*
|
|
115
|
+
* @example Include/exclude filtering
|
|
116
|
+
* ```ts
|
|
117
|
+
* const options = defaultResolveOptions(operationNode, {
|
|
118
|
+
* options: { output: 'types' },
|
|
119
|
+
* exclude: [{ type: 'tag', pattern: 'internal' }],
|
|
120
|
+
* })
|
|
121
|
+
* // → null when node has tag 'internal'
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @example Override merging
|
|
125
|
+
* ```ts
|
|
126
|
+
* const options = defaultResolveOptions(operationNode, {
|
|
127
|
+
* options: { enumType: 'asConst' },
|
|
128
|
+
* override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
|
|
129
|
+
* })
|
|
130
|
+
* // → { enumType: 'enum' } when operationId matches
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
export function defaultResolveOptions<TOptions>(
|
|
134
|
+
node: Node,
|
|
135
|
+
{ options, exclude = [], include, override = [] }: ResolveOptionsContext<TOptions>,
|
|
136
|
+
): TOptions | null {
|
|
137
|
+
if (isOperationNode(node)) {
|
|
138
|
+
const isExcluded = exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))
|
|
139
|
+
if (isExcluded) {
|
|
140
|
+
return null
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) {
|
|
144
|
+
return null
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options
|
|
148
|
+
|
|
149
|
+
return { ...options, ...overrideOptions }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (isSchemaNode(node)) {
|
|
153
|
+
if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) {
|
|
154
|
+
return null
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (include) {
|
|
158
|
+
const results = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern))
|
|
159
|
+
const applicable = results.filter((r) => r !== null)
|
|
160
|
+
if (applicable.length > 0 && !applicable.includes(true)) {
|
|
161
|
+
return null
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options
|
|
166
|
+
|
|
167
|
+
return { ...options, ...overrideOptions }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return options
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Default path resolver used by `defineResolver`.
|
|
175
|
+
*
|
|
176
|
+
* - Returns the output directory in `single` mode.
|
|
177
|
+
* - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
|
|
178
|
+
* - Falls back to a flat `output/baseName` path otherwise.
|
|
179
|
+
*
|
|
180
|
+
* A custom `group.name` function overrides the default subdirectory naming.
|
|
181
|
+
* For `tag` groups the default is `${camelCase(tag)}Controller`.
|
|
182
|
+
* For `path` groups the default is the first path segment after `/`.
|
|
183
|
+
*
|
|
184
|
+
* @example Flat output
|
|
185
|
+
* ```ts
|
|
186
|
+
* defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
|
|
187
|
+
* // → '/src/types/petTypes.ts'
|
|
188
|
+
* ```
|
|
189
|
+
*
|
|
190
|
+
* @example Tag-based grouping
|
|
191
|
+
* ```ts
|
|
192
|
+
* defaultResolvePath(
|
|
193
|
+
* { baseName: 'petTypes.ts', tag: 'pets' },
|
|
194
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
195
|
+
* )
|
|
196
|
+
* // → '/src/types/petsController/petTypes.ts'
|
|
197
|
+
* ```
|
|
198
|
+
*
|
|
199
|
+
* @example Path-based grouping
|
|
200
|
+
* ```ts
|
|
201
|
+
* defaultResolvePath(
|
|
202
|
+
* { baseName: 'petTypes.ts', path: '/pets/list' },
|
|
203
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
|
|
204
|
+
* )
|
|
205
|
+
* // → '/src/types/pets/petTypes.ts'
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* @example Single-file mode
|
|
209
|
+
* ```ts
|
|
210
|
+
* defaultResolvePath(
|
|
211
|
+
* { baseName: 'petTypes.ts', pathMode: 'single' },
|
|
212
|
+
* { root: '/src', output: { path: 'types' } },
|
|
213
|
+
* )
|
|
214
|
+
* // → '/src/types'
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
export function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }: ResolverPathParams, { root, output, group }: ResolverContext): string {
|
|
218
|
+
const mode = pathMode ?? PluginDriver.getMode(path.resolve(root, output.path))
|
|
219
|
+
|
|
220
|
+
if (mode === 'single') {
|
|
221
|
+
return path.resolve(root, output.path)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
let result: string
|
|
225
|
+
|
|
226
|
+
if (group && (groupPath || tag)) {
|
|
227
|
+
const groupValue = group.type === 'path' ? groupPath! : tag!
|
|
228
|
+
const defaultName =
|
|
229
|
+
group.type === 'tag'
|
|
230
|
+
? ({ group: g }: { group: string }) => `${camelCase(g)}Controller`
|
|
231
|
+
: ({ group: g }: { group: string }) => {
|
|
232
|
+
// Strip traversal components (empty, '.', '..') before taking the first meaningful segment.
|
|
233
|
+
// When every segment is a traversal component (e.g. '../../') we fall back to '' so the
|
|
234
|
+
// file is placed directly in the output root — the boundary check below ensures safety.
|
|
235
|
+
const segment = g.split('/').filter((s) => s !== '' && s !== '.' && s !== '..')[0]
|
|
236
|
+
return segment ? camelCase(segment) : ''
|
|
237
|
+
}
|
|
238
|
+
const resolveName = group.name ?? defaultName
|
|
239
|
+
result = path.resolve(root, output.path, resolveName({ group: groupValue }), baseName)
|
|
240
|
+
} else {
|
|
241
|
+
result = path.resolve(root, output.path, baseName)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Ensure the resolved path stays within the configured output directory.
|
|
245
|
+
// This prevents path traversal from malicious OpenAPI specs or custom group.name functions.
|
|
246
|
+
// `result === outputDir` is intentionally permitted: it matches single-file mode paths and
|
|
247
|
+
// edge cases where baseName resolves to the output directory itself.
|
|
248
|
+
const outputDir = path.resolve(root, output.path)
|
|
249
|
+
const outputDirWithSep = outputDir.endsWith(path.sep) ? outputDir : `${outputDir}${path.sep}`
|
|
250
|
+
if (result !== outputDir && !result.startsWith(outputDirWithSep)) {
|
|
251
|
+
throw new Error(
|
|
252
|
+
`[Kubb] Resolved path "${result}" is outside the output directory "${outputDir}". ` +
|
|
253
|
+
'This may indicate a path traversal attempt in the OpenAPI specification or a misconfigured group.name function.',
|
|
254
|
+
)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return result
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Default file resolver used by `defineResolver`.
|
|
262
|
+
*
|
|
263
|
+
* Resolves a `FileNode` by combining name resolution (`resolver.default`) with
|
|
264
|
+
* path resolution (`resolver.resolvePath`). The resolved file always has empty
|
|
265
|
+
* `sources`, `imports`, and `exports` arrays — consumers populate those separately.
|
|
266
|
+
*
|
|
267
|
+
* In `single` mode the name is omitted and the file sits directly in the output directory.
|
|
268
|
+
*
|
|
269
|
+
* @example Resolve a schema file
|
|
270
|
+
* ```ts
|
|
271
|
+
* const file = defaultResolveFile(
|
|
272
|
+
* { name: 'pet', extname: '.ts' },
|
|
273
|
+
* { root: '/src', output: { path: 'types' } },
|
|
274
|
+
* resolver,
|
|
275
|
+
* )
|
|
276
|
+
* // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
|
|
277
|
+
* ```
|
|
278
|
+
*
|
|
279
|
+
* @example Resolve an operation file with tag grouping
|
|
280
|
+
* ```ts
|
|
281
|
+
* const file = defaultResolveFile(
|
|
282
|
+
* { name: 'listPets', extname: '.ts', tag: 'pets' },
|
|
283
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
284
|
+
* resolver,
|
|
285
|
+
* )
|
|
286
|
+
* // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
export function defaultResolveFile({ name, extname, tag, path: groupPath }: ResolverFileParams, context: ResolverContext, ctx: Resolver): FileNode {
|
|
290
|
+
const pathMode = PluginDriver.getMode(path.resolve(context.root, context.output.path))
|
|
291
|
+
const resolvedName = pathMode === 'single' ? '' : ctx.default(name, 'file')
|
|
292
|
+
const baseName = `${resolvedName}${extname}` as FileNode['baseName']
|
|
293
|
+
const filePath = ctx.resolvePath({ baseName, pathMode, tag, path: groupPath }, context)
|
|
294
|
+
|
|
295
|
+
return createFile({
|
|
296
|
+
path: filePath,
|
|
297
|
+
baseName: path.basename(filePath) as `${string}.${string}`,
|
|
298
|
+
meta: {
|
|
299
|
+
pluginName: ctx.pluginName,
|
|
300
|
+
},
|
|
301
|
+
sources: [],
|
|
302
|
+
imports: [],
|
|
303
|
+
exports: [],
|
|
304
|
+
})
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Generates the default "Generated by Kubb" banner from config and optional node metadata.
|
|
309
|
+
*/
|
|
310
|
+
export function buildDefaultBanner({
|
|
311
|
+
title,
|
|
312
|
+
description,
|
|
313
|
+
version,
|
|
314
|
+
config,
|
|
315
|
+
}: {
|
|
316
|
+
title?: string
|
|
317
|
+
description?: string
|
|
318
|
+
version?: string
|
|
319
|
+
config: Config
|
|
320
|
+
}): string {
|
|
321
|
+
try {
|
|
322
|
+
let source = ''
|
|
323
|
+
if (Array.isArray(config.input)) {
|
|
324
|
+
const first = config.input[0]
|
|
325
|
+
if (first && 'path' in first) {
|
|
326
|
+
source = path.basename(first.path)
|
|
327
|
+
}
|
|
328
|
+
} else if ('path' in config.input) {
|
|
329
|
+
source = path.basename(config.input.path)
|
|
330
|
+
} else if ('data' in config.input) {
|
|
331
|
+
source = 'text content'
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
let banner = '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n'
|
|
335
|
+
|
|
336
|
+
if (config.output.defaultBanner === 'simple') {
|
|
337
|
+
banner += '*/\n'
|
|
338
|
+
return banner
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (source) {
|
|
342
|
+
banner += `* Source: ${source}\n`
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (title) {
|
|
346
|
+
banner += `* Title: ${title}\n`
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if (description) {
|
|
350
|
+
const formattedDescription = description.replace(/\n/gm, '\n* ')
|
|
351
|
+
banner += `* Description: ${formattedDescription}\n`
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (version) {
|
|
355
|
+
banner += `* OpenAPI spec version: ${version}\n`
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
banner += '*/\n'
|
|
359
|
+
return banner
|
|
360
|
+
} catch (_error) {
|
|
361
|
+
return '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/'
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Default banner resolver — returns the banner string for a generated file.
|
|
367
|
+
*
|
|
368
|
+
* A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
|
|
369
|
+
* When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
|
|
370
|
+
* from the OAS spec when a `node` is provided).
|
|
371
|
+
*
|
|
372
|
+
* - When `output.banner` is a function and `node` is provided, returns `output.banner(node)`.
|
|
373
|
+
* - When `output.banner` is a function and `node` is absent, falls back to the Kubb notice.
|
|
374
|
+
* - When `output.banner` is a string, returns it directly.
|
|
375
|
+
* - When `config.output.defaultBanner` is `false`, returns `undefined`.
|
|
376
|
+
* - Otherwise returns the Kubb "Generated by Kubb" notice.
|
|
377
|
+
*
|
|
378
|
+
* @example String banner overrides default
|
|
379
|
+
* ```ts
|
|
380
|
+
* defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
|
|
381
|
+
* // → '// my banner'
|
|
382
|
+
* ```
|
|
383
|
+
*
|
|
384
|
+
* @example Function banner with node
|
|
385
|
+
* ```ts
|
|
386
|
+
* defaultResolveBanner(inputNode, { output: { banner: (node) => `// v${node.version}` }, config })
|
|
387
|
+
* // → '// v3.0.0'
|
|
388
|
+
* ```
|
|
389
|
+
*
|
|
390
|
+
* @example No user banner — Kubb notice with OAS metadata
|
|
391
|
+
* ```ts
|
|
392
|
+
* defaultResolveBanner(inputNode, { config })
|
|
393
|
+
* // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
|
|
394
|
+
* ```
|
|
395
|
+
*
|
|
396
|
+
* @example Disabled default banner
|
|
397
|
+
* ```ts
|
|
398
|
+
* defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
|
|
399
|
+
* // → undefined
|
|
400
|
+
* ```
|
|
401
|
+
*/
|
|
402
|
+
export function defaultResolveBanner(node: InputNode | undefined, { output, config }: ResolveBannerContext): string | undefined {
|
|
403
|
+
if (typeof output?.banner === 'function') {
|
|
404
|
+
return output.banner(node)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (typeof output?.banner === 'string') {
|
|
408
|
+
return output.banner
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if (config.output.defaultBanner === false) {
|
|
412
|
+
return undefined
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return buildDefaultBanner({
|
|
416
|
+
title: node?.meta?.title,
|
|
417
|
+
version: node?.meta?.version,
|
|
418
|
+
config,
|
|
419
|
+
})
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Default footer resolver — returns the footer string for a generated file.
|
|
424
|
+
*
|
|
425
|
+
* - When `output.footer` is a function and `node` is provided, calls it with the node.
|
|
426
|
+
* - When `output.footer` is a function and `node` is absent, returns `undefined`.
|
|
427
|
+
* - When `output.footer` is a string, returns it directly.
|
|
428
|
+
* - Otherwise returns `undefined`.
|
|
429
|
+
*
|
|
430
|
+
* @example String footer
|
|
431
|
+
* ```ts
|
|
432
|
+
* defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
|
|
433
|
+
* // → '// end of file'
|
|
434
|
+
* ```
|
|
435
|
+
*
|
|
436
|
+
* @example Function footer with node
|
|
437
|
+
* ```ts
|
|
438
|
+
* defaultResolveFooter(inputNode, { output: { footer: (node) => `// ${node.title}` }, config })
|
|
439
|
+
* // → '// Pet Store'
|
|
440
|
+
* ```
|
|
441
|
+
*/
|
|
442
|
+
export function defaultResolveFooter(node: InputNode | undefined, { output }: ResolveBannerContext): string | undefined {
|
|
443
|
+
if (typeof output?.footer === 'function') {
|
|
444
|
+
return node ? output.footer(node) : undefined
|
|
445
|
+
}
|
|
446
|
+
if (typeof output?.footer === 'string') {
|
|
447
|
+
return output.footer
|
|
448
|
+
}
|
|
449
|
+
return undefined
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Defines a resolver for a plugin, injecting built-in defaults for name casing,
|
|
454
|
+
* include/exclude/override filtering, path resolution, and file construction.
|
|
455
|
+
*
|
|
456
|
+
* All four defaults can be overridden by providing them in the builder function:
|
|
457
|
+
* - `default` — name casing strategy (camelCase / PascalCase)
|
|
458
|
+
* - `resolveOptions` — include/exclude/override filtering
|
|
459
|
+
* - `resolvePath` — output path computation
|
|
460
|
+
* - `resolveFile` — full `FileNode` construction
|
|
461
|
+
*
|
|
462
|
+
* The builder receives `ctx` — a reference to the assembled resolver — so methods can
|
|
463
|
+
* call sibling resolver methods using `ctx` instead of `this`.
|
|
464
|
+
*
|
|
465
|
+
* @example Basic resolver with naming helpers
|
|
466
|
+
* ```ts
|
|
467
|
+
* export const resolver = defineResolver<PluginTs>((ctx) => ({
|
|
468
|
+
* name: 'default',
|
|
469
|
+
* resolveName(node) {
|
|
470
|
+
* return ctx.default(node.name, 'function')
|
|
471
|
+
* },
|
|
472
|
+
* resolveTypedName(node) {
|
|
473
|
+
* return ctx.default(node.name, 'type')
|
|
474
|
+
* },
|
|
475
|
+
* }))
|
|
476
|
+
* ```
|
|
477
|
+
*
|
|
478
|
+
* @example Override resolvePath for a custom output structure
|
|
479
|
+
* ```ts
|
|
480
|
+
* export const resolver = defineResolver<PluginTs>((_ctx) => ({
|
|
481
|
+
* name: 'custom',
|
|
482
|
+
* resolvePath({ baseName }, { root, output }) {
|
|
483
|
+
* return path.resolve(root, output.path, 'generated', baseName)
|
|
484
|
+
* },
|
|
485
|
+
* }))
|
|
486
|
+
* ```
|
|
487
|
+
*
|
|
488
|
+
* @example Use ctx.default inside a helper
|
|
489
|
+
* ```ts
|
|
490
|
+
* export const resolver = defineResolver<PluginTs>((ctx) => ({
|
|
491
|
+
* name: 'default',
|
|
492
|
+
* resolveParamName(node, param) {
|
|
493
|
+
* return ctx.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
|
|
494
|
+
* },
|
|
495
|
+
* }))
|
|
496
|
+
* ```
|
|
497
|
+
*/
|
|
498
|
+
export function defineResolver<T extends PluginFactoryOptions>(build: ResolverBuilder<T>): T['resolver'] {
|
|
499
|
+
// Create the resolver shell first. When `build(resolver)` executes below, `resolver` is
|
|
500
|
+
// still empty, but methods returned by the builder capture it by reference. By the time
|
|
501
|
+
// those methods are actually called, `Object.assign` will have already populated all
|
|
502
|
+
// properties (including any overrides from the builder itself).
|
|
503
|
+
const resolver = {} as T['resolver']
|
|
504
|
+
|
|
505
|
+
Object.assign(resolver, {
|
|
506
|
+
default: defaultResolver,
|
|
507
|
+
resolveOptions: defaultResolveOptions,
|
|
508
|
+
resolvePath: defaultResolvePath,
|
|
509
|
+
// Wire the default resolveFile implementation with a wrapper that passes resolver as ctx.
|
|
510
|
+
// Unlike other defaults which can be assigned directly, defaultResolveFile requires the
|
|
511
|
+
// resolver as its third parameter.
|
|
512
|
+
resolveFile: (params: ResolverFileParams, context: ResolverContext) => defaultResolveFile(params, context, resolver as Resolver),
|
|
513
|
+
resolveBanner: defaultResolveBanner,
|
|
514
|
+
resolveFooter: defaultResolveFooter,
|
|
515
|
+
// Builder overrides are applied last. Any method in the builder can call
|
|
516
|
+
// ctx.xxx() and will see the fully merged resolver (including its own overrides).
|
|
517
|
+
...build(resolver),
|
|
518
|
+
})
|
|
519
|
+
|
|
520
|
+
return resolver
|
|
521
|
+
}
|
package/src/devtools.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { InputNode } from '@kubb/ast'
|
|
2
2
|
import { deflateSync, inflateSync } from 'fflate'
|
|
3
3
|
import { x } from 'tinyexec'
|
|
4
4
|
import type { DevtoolsOptions } from './types.ts'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Encodes
|
|
7
|
+
* Encodes an `InputNode` as a compressed, URL-safe string.
|
|
8
8
|
*
|
|
9
9
|
* The JSON representation is deflate-compressed with {@link deflateSync} before
|
|
10
10
|
* base64url encoding, which typically reduces payload size by 70–80 % and
|
|
@@ -12,41 +12,41 @@ import type { DevtoolsOptions } from './types.ts'
|
|
|
12
12
|
*
|
|
13
13
|
* Use {@link decodeAst} to reverse.
|
|
14
14
|
*/
|
|
15
|
-
export function encodeAst(
|
|
16
|
-
const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(
|
|
15
|
+
export function encodeAst(input: InputNode): string {
|
|
16
|
+
const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(input)))
|
|
17
17
|
return Buffer.from(compressed).toString('base64url')
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* Decodes
|
|
21
|
+
* Decodes an `InputNode` from a string produced by {@link encodeAst}.
|
|
22
22
|
*
|
|
23
23
|
* Works in both Node.js and the browser — no streaming APIs required.
|
|
24
24
|
*/
|
|
25
|
-
export function decodeAst(encoded: string):
|
|
25
|
+
export function decodeAst(encoded: string): InputNode {
|
|
26
26
|
const bytes = Buffer.from(encoded, 'base64url')
|
|
27
|
-
return JSON.parse(new TextDecoder().decode(inflateSync(bytes))) as
|
|
27
|
+
return JSON.parse(new TextDecoder().decode(inflateSync(bytes))) as InputNode
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
|
-
* Constructs the Kubb Studio URL for the given `
|
|
31
|
+
* Constructs the Kubb Studio URL for the given `InputNode`.
|
|
32
32
|
* When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
|
|
33
|
-
* The `
|
|
33
|
+
* The `input` is encoded and attached as the `?root=` query parameter so Studio
|
|
34
34
|
* can decode and render it without a round-trip to any server.
|
|
35
35
|
*/
|
|
36
|
-
export function getStudioUrl(
|
|
36
|
+
export function getStudioUrl(input: InputNode, studioUrl: string, options: DevtoolsOptions = {}): string {
|
|
37
37
|
const baseUrl = studioUrl.replace(/\/$/, '')
|
|
38
38
|
const path = options.ast ? '/ast' : ''
|
|
39
39
|
|
|
40
|
-
return `${baseUrl}${path}?root=${encodeAst(
|
|
40
|
+
return `${baseUrl}${path}?root=${encodeAst(input)}`
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
|
-
* Opens the Kubb Studio URL for the given `
|
|
44
|
+
* Opens the Kubb Studio URL for the given `InputNode` in the default browser —
|
|
45
45
|
*
|
|
46
46
|
* Falls back to printing the URL if the browser cannot be launched.
|
|
47
47
|
*/
|
|
48
|
-
export async function openInStudio(
|
|
49
|
-
const url = getStudioUrl(
|
|
48
|
+
export async function openInStudio(input: InputNode, studioUrl: string, options: DevtoolsOptions = {}): Promise<void> {
|
|
49
|
+
const url = getStudioUrl(input, studioUrl, options)
|
|
50
50
|
|
|
51
51
|
const cmd = process.platform === 'win32' ? 'cmd' : process.platform === 'darwin' ? 'open' : 'xdg-open'
|
|
52
52
|
const args = process.platform === 'win32' ? ['/c', 'start', '', url] : [url]
|
package/src/index.ts
CHANGED
|
@@ -1,24 +1,20 @@
|
|
|
1
1
|
export { AsyncEventEmitter, URLPath } from '@internals/utils'
|
|
2
|
-
export
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
2
|
+
export * as ast from '@kubb/ast'
|
|
3
|
+
export { logLevel } from './constants.ts'
|
|
4
|
+
export { createAdapter } from './createAdapter.ts'
|
|
5
|
+
export { createKubb } from './createKubb.ts'
|
|
6
|
+
export { createRenderer } from './createRenderer.ts'
|
|
7
|
+
export { createStorage } from './createStorage.ts'
|
|
7
8
|
export { defineGenerator } from './defineGenerator.ts'
|
|
8
9
|
export { defineLogger } from './defineLogger.ts'
|
|
10
|
+
export { defineMiddleware } from './defineMiddleware.ts'
|
|
11
|
+
export { defineParser } from './defineParser.ts'
|
|
9
12
|
export { definePlugin } from './definePlugin.ts'
|
|
10
|
-
export {
|
|
11
|
-
export {
|
|
12
|
-
export {
|
|
13
|
-
export {
|
|
13
|
+
export { defineResolver } from './defineResolver.ts'
|
|
14
|
+
export { FileManager } from './FileManager.ts'
|
|
15
|
+
export { FileProcessor } from './FileProcessor.ts'
|
|
16
|
+
export { PluginDriver } from './PluginDriver.ts'
|
|
14
17
|
export { fsStorage } from './storages/fsStorage.ts'
|
|
15
18
|
export { memoryStorage } from './storages/memoryStorage.ts'
|
|
16
19
|
export * from './types.ts'
|
|
17
|
-
export
|
|
18
|
-
export { FunctionParams } from './utils/FunctionParams.ts'
|
|
19
|
-
export { detectFormatter } from './utils/formatters.ts'
|
|
20
|
-
export type { FileMetaBase } from './utils/getBarrelFiles.ts'
|
|
21
|
-
export { getBarrelFiles } from './utils/getBarrelFiles.ts'
|
|
22
|
-
export { getConfigs } from './utils/getConfigs.ts'
|
|
23
|
-
export { detectLinter } from './utils/linters.ts'
|
|
24
|
-
export { resolveOptions } from './utils/resolveOptions.ts'
|
|
20
|
+
export { isInputPath } from './utils/isInputPath.ts'
|