@kubb/core 5.0.0-alpha.35 → 5.0.0-alpha.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/PluginDriver-B_65W4fv.js +1677 -0
- package/dist/PluginDriver-B_65W4fv.js.map +1 -0
- package/dist/{PluginDriver-D8lWvtUg.d.ts → PluginDriver-C9iBgYbk.d.ts} +3 -4
- package/dist/PluginDriver-CCdkwR14.cjs +1806 -0
- package/dist/PluginDriver-CCdkwR14.cjs.map +1 -0
- package/dist/hooks.d.ts +1 -1
- package/dist/index.cjs +31 -1696
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +42 -2
- package/dist/index.js +6 -1676
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +165 -0
- package/dist/mocks.cjs.map +1 -0
- package/dist/mocks.d.ts +74 -0
- package/dist/mocks.js +159 -0
- package/dist/mocks.js.map +1 -0
- package/package.json +11 -4
- package/src/index.ts +2 -0
- package/src/mocks.ts +234 -0
- package/src/types.ts +1 -2
package/src/mocks.ts
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { resolve } from 'node:path'
|
|
2
|
+
import type { FileNode, OperationNode, SchemaNode, Visitor } from '@kubb/ast'
|
|
3
|
+
import { transform } from '@kubb/ast'
|
|
4
|
+
import { FileManager } from './FileManager.ts'
|
|
5
|
+
import { getMode, type PluginDriver } from './PluginDriver.ts'
|
|
6
|
+
import { applyHookResult } from './renderNode.ts'
|
|
7
|
+
import type {
|
|
8
|
+
Adapter,
|
|
9
|
+
AdapterFactoryOptions,
|
|
10
|
+
Config,
|
|
11
|
+
Generator,
|
|
12
|
+
GeneratorContext,
|
|
13
|
+
Plugin,
|
|
14
|
+
PluginFactoryOptions,
|
|
15
|
+
ResolveNameParams,
|
|
16
|
+
ResolvePathParams,
|
|
17
|
+
} from './types.ts'
|
|
18
|
+
|
|
19
|
+
function toCamelOrPascal(text: string, pascal: boolean): string {
|
|
20
|
+
const normalized = text
|
|
21
|
+
.trim()
|
|
22
|
+
.replace(/([a-z\d])([A-Z])/g, '$1 $2')
|
|
23
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
|
|
24
|
+
.replace(/(\d)([a-z])/g, '$1 $2')
|
|
25
|
+
|
|
26
|
+
const words = normalized.split(/[\s\-_./\\:]+/).filter(Boolean)
|
|
27
|
+
|
|
28
|
+
return words
|
|
29
|
+
.map((word, i) => {
|
|
30
|
+
const allUpper = word.length > 1 && word === word.toUpperCase()
|
|
31
|
+
if (allUpper) return word
|
|
32
|
+
if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1)
|
|
33
|
+
return word.charAt(0).toUpperCase() + word.slice(1)
|
|
34
|
+
})
|
|
35
|
+
.join('')
|
|
36
|
+
.replace(/[^a-zA-Z0-9]/g, '')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function camelCase(text: string): string {
|
|
40
|
+
return toCamelOrPascal(text, false)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function pascalCase(text: string): string {
|
|
44
|
+
return toCamelOrPascal(text, true)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Creates a minimal `PluginDriver` mock suitable for unit tests.
|
|
49
|
+
*/
|
|
50
|
+
export function createMockedPluginDriver(options: { name?: string; plugin?: Plugin<any>; config?: Config } = {}): PluginDriver {
|
|
51
|
+
return {
|
|
52
|
+
resolveName: (result: ResolveNameParams) => {
|
|
53
|
+
if (result.type === 'file') {
|
|
54
|
+
return camelCase(options?.name || result.name)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (result.type === 'type') {
|
|
58
|
+
return pascalCase(result.name)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (result.type === 'function') {
|
|
62
|
+
return camelCase(result.name)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return camelCase(result.name)
|
|
66
|
+
},
|
|
67
|
+
config: options?.config ?? {
|
|
68
|
+
root: '.',
|
|
69
|
+
output: {
|
|
70
|
+
path: './path',
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
resolvePath: ({ baseName }: ResolvePathParams) => baseName,
|
|
74
|
+
getFile: ({
|
|
75
|
+
name,
|
|
76
|
+
extname,
|
|
77
|
+
pluginName,
|
|
78
|
+
options: fileOptions,
|
|
79
|
+
}: {
|
|
80
|
+
name: string
|
|
81
|
+
extname: `.${string}`
|
|
82
|
+
pluginName: string
|
|
83
|
+
options?: { group?: { tag?: string; path?: string } }
|
|
84
|
+
}) => {
|
|
85
|
+
const baseName = `${name}${extname}`
|
|
86
|
+
const groupDir = fileOptions?.group?.tag ?? fileOptions?.group?.path?.split('/').filter(Boolean)[0]
|
|
87
|
+
const filePath = groupDir ? `${groupDir}/${baseName}` : baseName
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
path: filePath,
|
|
91
|
+
baseName,
|
|
92
|
+
meta: { pluginName },
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
getPlugin(_pluginName: Plugin['name']): Plugin | undefined {
|
|
96
|
+
return options?.plugin
|
|
97
|
+
},
|
|
98
|
+
fileManager: new FileManager(),
|
|
99
|
+
} as unknown as PluginDriver
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Creates a minimal `Adapter` mock suitable for unit tests.
|
|
104
|
+
*
|
|
105
|
+
* - `parse` returns an empty `InputNode` by default; override via `options.parse`.
|
|
106
|
+
* - `getImports` returns `[]` by default (single-file mode, no cross-file imports).
|
|
107
|
+
*/
|
|
108
|
+
export function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(
|
|
109
|
+
options: {
|
|
110
|
+
name?: TOptions['name']
|
|
111
|
+
resolvedOptions?: TOptions['resolvedOptions']
|
|
112
|
+
inputNode?: Adapter<TOptions>['inputNode']
|
|
113
|
+
parse?: Adapter<TOptions>['parse']
|
|
114
|
+
getImports?: Adapter<TOptions>['getImports']
|
|
115
|
+
} = {},
|
|
116
|
+
): Adapter<TOptions> {
|
|
117
|
+
return {
|
|
118
|
+
name: (options.name ?? 'oas') as TOptions['name'],
|
|
119
|
+
options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],
|
|
120
|
+
inputNode: options.inputNode ?? null,
|
|
121
|
+
parse: options.parse ?? (async () => ({ kind: 'Input' as const, schemas: [], operations: [] })),
|
|
122
|
+
getImports: options.getImports ?? ((_node: SchemaNode, _resolve: (schemaName: string) => { name: string; path: string }) => []),
|
|
123
|
+
} as Adapter<TOptions>
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Creates a minimal `Plugin` mock suitable for unit tests.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })
|
|
131
|
+
*/
|
|
132
|
+
export function createMockedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(params: {
|
|
133
|
+
name: TOptions['name']
|
|
134
|
+
options: TOptions['resolvedOptions']
|
|
135
|
+
resolver?: TOptions['resolver']
|
|
136
|
+
transformer?: Visitor
|
|
137
|
+
dependencies?: Array<string>
|
|
138
|
+
}): Plugin<TOptions> {
|
|
139
|
+
return {
|
|
140
|
+
name: params.name,
|
|
141
|
+
options: params.options,
|
|
142
|
+
resolver: params.resolver,
|
|
143
|
+
transformer: params.transformer,
|
|
144
|
+
dependencies: params.dependencies,
|
|
145
|
+
install: () => {},
|
|
146
|
+
inject: () => undefined as TOptions['context'],
|
|
147
|
+
} as unknown as Plugin<TOptions>
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
type RenderGeneratorOptions<TOptions extends PluginFactoryOptions> = {
|
|
151
|
+
config: Config
|
|
152
|
+
adapter: Adapter
|
|
153
|
+
driver: PluginDriver
|
|
154
|
+
plugin: Plugin<TOptions>
|
|
155
|
+
options: TOptions['resolvedOptions']
|
|
156
|
+
resolver: TOptions['resolver']
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function createMockedPluginContext<TOptions extends PluginFactoryOptions>(opts: RenderGeneratorOptions<TOptions>): Omit<GeneratorContext<TOptions>, 'options'> {
|
|
160
|
+
const root = resolve(opts.config.root, opts.config.output.path)
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
config: opts.config,
|
|
164
|
+
root,
|
|
165
|
+
getMode: (output: { path: string }) => getMode(resolve(root, output.path)),
|
|
166
|
+
adapter: opts.adapter,
|
|
167
|
+
resolver: opts.resolver,
|
|
168
|
+
plugin: opts.plugin,
|
|
169
|
+
driver: opts.driver,
|
|
170
|
+
inputNode: { kind: 'Input', schemas: [], operations: [] },
|
|
171
|
+
upsertFile: async (...files: Array<FileNode>) => opts.driver.fileManager.upsert(...files),
|
|
172
|
+
warn: (msg: string) => console.warn(msg),
|
|
173
|
+
error: (msg: string) => console.error(msg),
|
|
174
|
+
info: (msg: string) => console.info(msg),
|
|
175
|
+
openInStudio: async () => {},
|
|
176
|
+
} as unknown as Omit<GeneratorContext<TOptions>, 'options'>
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Renders a generator's `schema` method in a test context.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
|
|
184
|
+
* await matchFiles(driver.fileManager.files)
|
|
185
|
+
*/
|
|
186
|
+
export async function renderGeneratorSchema<TOptions extends PluginFactoryOptions>(
|
|
187
|
+
generator: Generator<TOptions>,
|
|
188
|
+
node: SchemaNode,
|
|
189
|
+
opts: RenderGeneratorOptions<TOptions>,
|
|
190
|
+
): Promise<void> {
|
|
191
|
+
if (!generator.schema) return
|
|
192
|
+
const context = createMockedPluginContext(opts)
|
|
193
|
+
const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node
|
|
194
|
+
const result = await generator.schema(transformedNode, { ...context, options: opts.options })
|
|
195
|
+
await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Renders a generator's `operation` method in a test context.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* await renderGeneratorOperation(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
|
|
203
|
+
* await matchFiles(driver.fileManager.files)
|
|
204
|
+
*/
|
|
205
|
+
export async function renderGeneratorOperation<TOptions extends PluginFactoryOptions>(
|
|
206
|
+
generator: Generator<TOptions>,
|
|
207
|
+
node: OperationNode,
|
|
208
|
+
opts: RenderGeneratorOptions<TOptions>,
|
|
209
|
+
): Promise<void> {
|
|
210
|
+
if (!generator.operation) return
|
|
211
|
+
const context = createMockedPluginContext(opts)
|
|
212
|
+
const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node
|
|
213
|
+
const result = await generator.operation(transformedNode, { ...context, options: opts.options })
|
|
214
|
+
await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Renders a generator's `operations` method in a test context.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* await renderGeneratorOperations(classClientGenerator, nodes, { config, adapter, driver, plugin, options, resolver })
|
|
222
|
+
* await matchFiles(driver.fileManager.files)
|
|
223
|
+
*/
|
|
224
|
+
export async function renderGeneratorOperations<TOptions extends PluginFactoryOptions>(
|
|
225
|
+
generator: Generator<TOptions>,
|
|
226
|
+
nodes: Array<OperationNode>,
|
|
227
|
+
opts: RenderGeneratorOptions<TOptions>,
|
|
228
|
+
): Promise<void> {
|
|
229
|
+
if (!generator.operations) return
|
|
230
|
+
const context = createMockedPluginContext(opts)
|
|
231
|
+
const transformedNodes = opts.plugin.transformer ? nodes.map((n) => transform(n, opts.plugin.transformer!)) : nodes
|
|
232
|
+
const result = await generator.operations(transformedNodes, { ...context, options: opts.options })
|
|
233
|
+
await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
|
|
234
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { AsyncEventEmitter, PossiblePromise } from '@internals/utils'
|
|
2
|
-
import type { FileNode, ImportNode, InputNode, Node, OperationNode, SchemaNode, Visitor } from '@kubb/ast'
|
|
3
|
-
import type { HttpMethod } from '@kubb/oas'
|
|
2
|
+
import type { FileNode, HttpMethod, ImportNode, InputNode, Node, OperationNode, SchemaNode, Visitor } from '@kubb/ast'
|
|
4
3
|
import type { DEFAULT_STUDIO_URL, logLevel } from './constants.ts'
|
|
5
4
|
import type { RendererFactory } from './createRenderer.ts'
|
|
6
5
|
import type { Storage } from './createStorage.ts'
|