@kubb/core 5.0.0-beta.7 → 5.0.0-beta.71

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.
Files changed (49) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +20 -123
  3. package/dist/diagnostics-CJtO1uSM.d.ts +2892 -0
  4. package/dist/index.cjs +2340 -1129
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.d.ts +80 -289
  7. package/dist/index.js +2330 -1124
  8. package/dist/index.js.map +1 -1
  9. package/dist/memoryStorage-B4VTTIpQ.js +905 -0
  10. package/dist/memoryStorage-B4VTTIpQ.js.map +1 -0
  11. package/dist/memoryStorage-CfycFGzX.cjs +1043 -0
  12. package/dist/memoryStorage-CfycFGzX.cjs.map +1 -0
  13. package/dist/mocks.cjs +84 -24
  14. package/dist/mocks.cjs.map +1 -1
  15. package/dist/mocks.d.ts +37 -11
  16. package/dist/mocks.js +86 -28
  17. package/dist/mocks.js.map +1 -1
  18. package/package.json +9 -23
  19. package/dist/PluginDriver-BkTRD2H2.js +0 -946
  20. package/dist/PluginDriver-BkTRD2H2.js.map +0 -1
  21. package/dist/PluginDriver-Cadu4ORh.cjs +0 -1037
  22. package/dist/PluginDriver-Cadu4ORh.cjs.map +0 -1
  23. package/dist/types-ChyWgIgi.d.ts +0 -2159
  24. package/src/FileManager.ts +0 -115
  25. package/src/FileProcessor.ts +0 -86
  26. package/src/Kubb.ts +0 -300
  27. package/src/PluginDriver.ts +0 -426
  28. package/src/constants.ts +0 -35
  29. package/src/createAdapter.ts +0 -32
  30. package/src/createKubb.ts +0 -573
  31. package/src/createRenderer.ts +0 -57
  32. package/src/createStorage.ts +0 -70
  33. package/src/defineGenerator.ts +0 -87
  34. package/src/defineLogger.ts +0 -36
  35. package/src/defineMiddleware.ts +0 -62
  36. package/src/defineParser.ts +0 -44
  37. package/src/definePlugin.ts +0 -83
  38. package/src/defineResolver.ts +0 -521
  39. package/src/devtools.ts +0 -59
  40. package/src/index.ts +0 -20
  41. package/src/mocks.ts +0 -178
  42. package/src/renderNode.ts +0 -35
  43. package/src/storages/fsStorage.ts +0 -114
  44. package/src/storages/memoryStorage.ts +0 -55
  45. package/src/types.ts +0 -1305
  46. package/src/utils/diagnostics.ts +0 -18
  47. package/src/utils/isInputPath.ts +0 -10
  48. package/src/utils/packageJSON.ts +0 -99
  49. /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/src/mocks.ts DELETED
@@ -1,178 +0,0 @@
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 { PluginDriver } from './PluginDriver.ts'
6
- import { applyHookResult } from './renderNode.ts'
7
- import type { Adapter, AdapterFactoryOptions, Config, Generator, GeneratorContext, NormalizedPlugin, PluginFactoryOptions } from './types.ts'
8
-
9
- /**
10
-
11
- * Creates a minimal `PluginDriver` mock for unit tests.
12
- */
13
- export function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): PluginDriver {
14
- return {
15
- config: options?.config ?? {
16
- root: '.',
17
- output: {
18
- path: './path',
19
- },
20
- },
21
- getPlugin(_pluginName: string): NormalizedPlugin | undefined {
22
- return options?.plugin
23
- },
24
- getResolver: (_pluginName: string) => options?.plugin?.resolver,
25
- fileManager: new FileManager(),
26
- } as unknown as PluginDriver
27
- }
28
-
29
- /**
30
- * Creates a minimal `Adapter` mock for unit tests.
31
- * `parse` returns an empty `InputNode` by default; override via `options.parse`.
32
- * `getImports` returns `[]` by default.
33
- */
34
- export function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(
35
- options: {
36
- name?: TOptions['name']
37
- resolvedOptions?: TOptions['resolvedOptions']
38
- inputNode?: Adapter<TOptions>['inputNode']
39
- parse?: Adapter<TOptions>['parse']
40
- getImports?: Adapter<TOptions>['getImports']
41
- } = {},
42
- ): Adapter<TOptions> {
43
- const inputNode = options.inputNode ?? null
44
- return {
45
- name: (options.name ?? 'oas') as TOptions['name'],
46
- options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],
47
- inputNode,
48
- parse: options.parse ?? (async () => ({ kind: 'Input' as const, schemas: [], operations: [] })),
49
- getImports: options.getImports ?? ((_node: SchemaNode, _resolve: (schemaName: string) => { name: string; path: string }) => []),
50
- } as Adapter<TOptions>
51
- }
52
-
53
- /**
54
- * Creates a minimal plugin mock for unit tests.
55
- *
56
- * @example
57
- * `const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })`
58
- */
59
- export function createMockedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(params: {
60
- name: TOptions['name']
61
- options: TOptions['resolvedOptions']
62
- resolver?: TOptions['resolver']
63
- transformer?: Visitor
64
- dependencies?: Array<string>
65
- }): NormalizedPlugin<TOptions> {
66
- return {
67
- name: params.name,
68
- options: params.options,
69
- resolver: params.resolver,
70
- transformer: params.transformer,
71
- dependencies: params.dependencies,
72
- hooks: {},
73
- } as unknown as NormalizedPlugin<TOptions>
74
- }
75
-
76
- type RenderGeneratorOptions<TOptions extends PluginFactoryOptions> = {
77
- config: Config
78
- adapter: Adapter
79
- driver: PluginDriver
80
- plugin: NormalizedPlugin<TOptions>
81
- options: TOptions['resolvedOptions']
82
- resolver: TOptions['resolver']
83
- }
84
-
85
- function createMockedPluginContext<TOptions extends PluginFactoryOptions>(opts: RenderGeneratorOptions<TOptions>): Omit<GeneratorContext<TOptions>, 'options'> {
86
- const root = resolve(opts.config.root, opts.config.output.path)
87
-
88
- return {
89
- config: opts.config,
90
- root,
91
- getMode: (output: { path: string }) => PluginDriver.getMode(resolve(root, output.path)),
92
- adapter: opts.adapter,
93
- resolver: opts.resolver,
94
- plugin: opts.plugin,
95
- driver: opts.driver,
96
- getResolver: (name: string) => opts.driver.getResolver(name),
97
- inputNode: { kind: 'Input', schemas: [], operations: [] },
98
- addFile: async (...files: Array<FileNode>) => opts.driver.fileManager.add(...files),
99
- upsertFile: async (...files: Array<FileNode>) => opts.driver.fileManager.upsert(...files),
100
- hooks: opts.driver.hooks ?? ({} as never),
101
- warn: (msg: string) => console.warn(msg),
102
- error: (msg: string) => console.error(msg),
103
- info: (msg: string) => console.info(msg),
104
- openInStudio: async () => {},
105
- } as unknown as Omit<GeneratorContext<TOptions>, 'options'>
106
- }
107
-
108
- /**
109
- * Renders a generator's `schema` method in a test context.
110
- *
111
- * @example
112
- * ```ts
113
- * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
114
- * await matchFiles(driver.fileManager.files)
115
- * ```
116
- */
117
- export async function renderGeneratorSchema<TOptions extends PluginFactoryOptions>(
118
- generator: Generator<TOptions>,
119
- node: SchemaNode,
120
- opts: RenderGeneratorOptions<TOptions>,
121
- ): Promise<void> {
122
- if (!generator.schema) return
123
- const context = createMockedPluginContext(opts)
124
- const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node
125
- const result = await generator.schema(transformedNode, {
126
- ...context,
127
- options: opts.options,
128
- })
129
- await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
130
- }
131
-
132
- /**
133
- * Renders a generator's `operation` method in a test context.
134
- *
135
- * @example
136
- * ```ts
137
- * await renderGeneratorOperation(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
138
- * await matchFiles(driver.fileManager.files)
139
- * ```
140
- */
141
- export async function renderGeneratorOperation<TOptions extends PluginFactoryOptions>(
142
- generator: Generator<TOptions>,
143
- node: OperationNode,
144
- opts: RenderGeneratorOptions<TOptions>,
145
- ): Promise<void> {
146
- if (!generator.operation) return
147
- const context = createMockedPluginContext(opts)
148
- const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node
149
- const result = await generator.operation(transformedNode, {
150
- ...context,
151
- options: opts.options,
152
- })
153
- await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
154
- }
155
-
156
- /**
157
- * Renders a generator's `operations` method in a test context.
158
- *
159
- * @example
160
- * ```ts
161
- * await renderGeneratorOperations(classClientGenerator, nodes, { config, adapter, driver, plugin, options, resolver })
162
- * await matchFiles(driver.fileManager.files)
163
- * ```
164
- */
165
- export async function renderGeneratorOperations<TOptions extends PluginFactoryOptions>(
166
- generator: Generator<TOptions>,
167
- nodes: Array<OperationNode>,
168
- opts: RenderGeneratorOptions<TOptions>,
169
- ): Promise<void> {
170
- if (!generator.operations) return
171
- const context = createMockedPluginContext(opts)
172
- const transformedNodes = opts.plugin.transformer ? nodes.map((n) => transform(n, opts.plugin.transformer!)) : nodes
173
- const result = await generator.operations(transformedNodes, {
174
- ...context,
175
- options: opts.options,
176
- })
177
- await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
178
- }
package/src/renderNode.ts DELETED
@@ -1,35 +0,0 @@
1
- import type { FileNode } from '@kubb/ast'
2
- import type { RendererFactory } from './createRenderer.ts'
3
- import type { PluginDriver } from './PluginDriver.ts'
4
-
5
- /**
6
- * Handles the return value of a plugin AST hook or generator method.
7
- *
8
- * - Renderer output → rendered via the provided `rendererFactory` (e.g. JSX), files stored in `driver.fileManager`
9
- * - `Array<FileNode>` → added directly into `driver.fileManager`
10
- * - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
11
- *
12
- * Pass a `rendererFactory` (e.g. `jsxRenderer` from `@kubb/renderer-jsx`) when the result
13
- * may be a renderer element. Generators that only return `Array<FileNode>` do not need one.
14
- */
15
- export async function applyHookResult<TElement = unknown>(
16
- result: TElement | Array<FileNode> | void,
17
- driver: PluginDriver,
18
- rendererFactory?: RendererFactory<TElement>,
19
- ): Promise<void> {
20
- if (!result) return
21
-
22
- if (Array.isArray(result)) {
23
- driver.fileManager.upsert(...(result as Array<FileNode>))
24
- return
25
- }
26
-
27
- if (!rendererFactory) {
28
- return
29
- }
30
-
31
- const renderer = rendererFactory()
32
- await renderer.render(result)
33
- driver.fileManager.upsert(...renderer.files)
34
- renderer.unmount()
35
- }
@@ -1,114 +0,0 @@
1
- import type { Dirent } from 'node:fs'
2
- import { access, readdir, readFile, rm } from 'node:fs/promises'
3
- import { join, resolve } from 'node:path'
4
- import { clean, write } from '@internals/utils'
5
- import { createStorage } from '../createStorage.ts'
6
-
7
- /**
8
- * Detects the filesystem error used to indicate that a path does not exist.
9
- */
10
- function isMissingPathError(error: unknown): error is NodeJS.ErrnoException {
11
- return typeof error === 'object' && error !== null && 'code' in error && (error as NodeJS.ErrnoException).code === 'ENOENT'
12
- }
13
-
14
- /**
15
- * Built-in filesystem storage driver.
16
- *
17
- * This is the default storage when no `storage` option is configured in the root config.
18
- * Keys are resolved against `process.cwd()`, so root-relative paths such as
19
- * `src/gen/api/getPets.ts` are written to the correct location without extra configuration.
20
- *
21
- * Internally uses the `write` utility from `@internals/utils`, which:
22
- * - trims leading/trailing whitespace before writing
23
- * - skips the write when file content is already identical (deduplication)
24
- * - creates missing parent directories automatically
25
- * - supports Bun's native file API when running under Bun
26
- *
27
- * @example
28
- * ```ts
29
- * import { fsStorage } from '@kubb/core'
30
- * import { defineConfig } from 'kubb'
31
- *
32
- * export default defineConfig({
33
- * input: { path: './petStore.yaml' },
34
- * output: { path: './src/gen' },
35
- * storage: fsStorage(),
36
- * })
37
- * ```
38
- */
39
- export const fsStorage = createStorage(() => ({
40
- name: 'fs',
41
- async hasItem(key: string) {
42
- try {
43
- await access(resolve(key))
44
- return true
45
- } catch (error) {
46
- if (isMissingPathError(error)) {
47
- return false
48
- }
49
-
50
- throw new Error(`Failed to access storage item "${key}"`, {
51
- cause: error as Error,
52
- })
53
- }
54
- },
55
- async getItem(key: string) {
56
- try {
57
- return await readFile(resolve(key), 'utf8')
58
- } catch (error) {
59
- if (isMissingPathError(error)) {
60
- return null
61
- }
62
-
63
- throw new Error(`Failed to read storage item "${key}"`, {
64
- cause: error as Error,
65
- })
66
- }
67
- },
68
- async setItem(key: string, value: string) {
69
- await write(resolve(key), value, { sanity: false })
70
- },
71
- async removeItem(key: string) {
72
- await rm(resolve(key), { force: true })
73
- },
74
- async getKeys(base?: string) {
75
- const keys: Array<string> = []
76
- const resolvedBase = resolve(base ?? process.cwd())
77
-
78
- async function walk(dir: string, prefix: string): Promise<void> {
79
- let entries: Array<Dirent>
80
- try {
81
- entries = (await readdir(dir, {
82
- withFileTypes: true,
83
- })) as Array<Dirent>
84
- } catch (error) {
85
- if (isMissingPathError(error)) {
86
- return
87
- }
88
-
89
- throw new Error(`Failed to list storage keys under "${resolvedBase}"`, {
90
- cause: error as Error,
91
- })
92
- }
93
- for (const entry of entries) {
94
- const rel = prefix ? `${prefix}/${entry.name}` : entry.name
95
- if (entry.isDirectory()) {
96
- await walk(join(dir, entry.name), rel)
97
- } else {
98
- keys.push(rel)
99
- }
100
- }
101
- }
102
-
103
- await walk(resolvedBase, '')
104
-
105
- return keys
106
- },
107
- async clear(base?: string) {
108
- if (!base) {
109
- return
110
- }
111
-
112
- await clean(resolve(base))
113
- },
114
- }))
@@ -1,55 +0,0 @@
1
- import { createStorage } from '../createStorage.ts'
2
-
3
- /**
4
- * In-memory storage driver. Useful for testing and dry-run scenarios where
5
- * generated output should be captured without touching the filesystem.
6
- *
7
- * All data lives in a `Map` scoped to the storage instance and is discarded
8
- * when the instance is garbage-collected.
9
- *
10
- * @example
11
- * ```ts
12
- * import { memoryStorage } from '@kubb/core'
13
- * import { defineConfig } from 'kubb'
14
- *
15
- * export default defineConfig({
16
- * input: { path: './petStore.yaml' },
17
- * output: { path: './src/gen' },
18
- * storage: memoryStorage(),
19
- * })
20
- * ```
21
- */
22
- export const memoryStorage = createStorage(() => {
23
- const store = new Map<string, string>()
24
-
25
- return {
26
- name: 'memory',
27
- async hasItem(key: string) {
28
- return store.has(key)
29
- },
30
- async getItem(key: string) {
31
- return store.get(key) ?? null
32
- },
33
- async setItem(key: string, value: string) {
34
- store.set(key, value)
35
- },
36
- async removeItem(key: string) {
37
- store.delete(key)
38
- },
39
- async getKeys(base?: string) {
40
- const keys = [...store.keys()]
41
- return base ? keys.filter((k) => k.startsWith(base)) : keys
42
- },
43
- async clear(base?: string) {
44
- if (!base) {
45
- store.clear()
46
- return
47
- }
48
- for (const key of store.keys()) {
49
- if (key.startsWith(base)) {
50
- store.delete(key)
51
- }
52
- }
53
- },
54
- }
55
- })