@kubb/core 5.0.0-beta.8 → 5.0.0-beta.80

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 (44) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +20 -123
  3. package/dist/diagnostics-CQYd4UQZ.d.ts +2888 -0
  4. package/dist/index.cjs +2350 -1102
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.d.ts +80 -138
  7. package/dist/index.js +2344 -1101
  8. package/dist/index.js.map +1 -1
  9. package/dist/memoryStorage-9cOBg7bL.cjs +1022 -0
  10. package/dist/memoryStorage-9cOBg7bL.cjs.map +1 -0
  11. package/dist/memoryStorage-C-3NpTDQ.js +884 -0
  12. package/dist/memoryStorage-C-3NpTDQ.js.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-Cu1Kj9S-.cjs +0 -1075
  20. package/dist/PluginDriver-Cu1Kj9S-.cjs.map +0 -1
  21. package/dist/PluginDriver-D8Z0Htid.js +0 -978
  22. package/dist/PluginDriver-D8Z0Htid.js.map +0 -1
  23. package/dist/createKubb-Cagd4PIe.d.ts +0 -2082
  24. package/src/FileManager.ts +0 -115
  25. package/src/FileProcessor.ts +0 -86
  26. package/src/PluginDriver.ts +0 -457
  27. package/src/constants.ts +0 -35
  28. package/src/createAdapter.ts +0 -108
  29. package/src/createKubb.ts +0 -1268
  30. package/src/createRenderer.ts +0 -57
  31. package/src/createStorage.ts +0 -70
  32. package/src/defineGenerator.ts +0 -175
  33. package/src/defineLogger.ts +0 -58
  34. package/src/defineMiddleware.ts +0 -62
  35. package/src/defineParser.ts +0 -44
  36. package/src/definePlugin.ts +0 -379
  37. package/src/defineResolver.ts +0 -654
  38. package/src/devtools.ts +0 -66
  39. package/src/index.ts +0 -20
  40. package/src/mocks.ts +0 -177
  41. package/src/storages/fsStorage.ts +0 -90
  42. package/src/storages/memoryStorage.ts +0 -55
  43. package/src/types.ts +0 -41
  44. /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/src/devtools.ts DELETED
@@ -1,66 +0,0 @@
1
- import type { InputNode } from '@kubb/ast'
2
- import { deflateSync, inflateSync } from 'fflate'
3
- import { x } from 'tinyexec'
4
-
5
- export type DevtoolsOptions = {
6
- /**
7
- * Open the AST inspector in Kubb Studio (`/ast`). Defaults to the main Studio page.
8
- * @default false
9
- */
10
- ast?: boolean
11
- }
12
-
13
- /**
14
- * Encodes an `InputNode` as a compressed, URL-safe string.
15
- *
16
- * The JSON representation is deflate-compressed with {@link deflateSync} before
17
- * base64url encoding, which typically reduces payload size by 70–80 % and
18
- * keeps URLs well within browser and server path-length limits.
19
- *
20
- * Use {@link decodeAst} to reverse.
21
- */
22
- export function encodeAst(input: InputNode): string {
23
- const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(input)))
24
- return Buffer.from(compressed).toString('base64url')
25
- }
26
-
27
- /**
28
- * Decodes an `InputNode` from a string produced by {@link encodeAst}.
29
- *
30
- * Works in both Node.js and the browser — no streaming APIs required.
31
- */
32
- export function decodeAst(encoded: string): InputNode {
33
- const bytes = Buffer.from(encoded, 'base64url')
34
- return JSON.parse(new TextDecoder().decode(inflateSync(bytes))) as InputNode
35
- }
36
-
37
- /**
38
- * Constructs the Kubb Studio URL for the given `InputNode`.
39
- * When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
40
- * The `input` is encoded and attached as the `?root=` query parameter so Studio
41
- * can decode and render it without a round-trip to any server.
42
- */
43
- export function getStudioUrl(input: InputNode, studioUrl: string, options: DevtoolsOptions = {}): string {
44
- const baseUrl = studioUrl.replace(/\/$/, '')
45
- const path = options.ast ? '/ast' : ''
46
-
47
- return `${baseUrl}${path}?root=${encodeAst(input)}`
48
- }
49
-
50
- /**
51
- * Opens the Kubb Studio URL for the given `InputNode` in the default browser —
52
- *
53
- * Falls back to printing the URL if the browser cannot be launched.
54
- */
55
- export async function openInStudio(input: InputNode, studioUrl: string, options: DevtoolsOptions = {}): Promise<void> {
56
- const url = getStudioUrl(input, studioUrl, options)
57
-
58
- const cmd = process.platform === 'win32' ? 'cmd' : process.platform === 'darwin' ? 'open' : 'xdg-open'
59
- const args = process.platform === 'win32' ? ['/c', 'start', '', url] : [url]
60
-
61
- try {
62
- await x(cmd, args)
63
- } catch {
64
- console.log(`\n ${url}\n`)
65
- }
66
- }
package/src/index.ts DELETED
@@ -1,20 +0,0 @@
1
- export { AsyncEventEmitter, URLPath } from '@internals/utils'
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'
8
- export { defineGenerator } from './defineGenerator.ts'
9
- export { defineLogger } from './defineLogger.ts'
10
- export { defineMiddleware } from './defineMiddleware.ts'
11
- export { defineParser } from './defineParser.ts'
12
- export { definePlugin } from './definePlugin.ts'
13
- export { defineResolver } from './defineResolver.ts'
14
- export { FileManager } from './FileManager.ts'
15
- export { FileProcessor } from './FileProcessor.ts'
16
- export { PluginDriver } from './PluginDriver.ts'
17
- export { fsStorage } from './storages/fsStorage.ts'
18
- export { memoryStorage } from './storages/memoryStorage.ts'
19
- export * from './types.ts'
20
- export { isInputPath } from './createKubb.ts'
package/src/mocks.ts DELETED
@@ -1,177 +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 { applyHookResult, PluginDriver } from './PluginDriver.ts'
6
- import type { Adapter, AdapterFactoryOptions, Config, Generator, GeneratorContext, NormalizedPlugin, PluginFactoryOptions } from './types.ts'
7
-
8
- /**
9
-
10
- * Creates a minimal `PluginDriver` mock for unit tests.
11
- */
12
- export function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): PluginDriver {
13
- return {
14
- config: options?.config ?? {
15
- root: '.',
16
- output: {
17
- path: './path',
18
- },
19
- },
20
- getPlugin(_pluginName: string): NormalizedPlugin | undefined {
21
- return options?.plugin
22
- },
23
- getResolver: (_pluginName: string) => options?.plugin?.resolver,
24
- fileManager: new FileManager(),
25
- } as unknown as PluginDriver
26
- }
27
-
28
- /**
29
- * Creates a minimal `Adapter` mock for unit tests.
30
- * `parse` returns an empty `InputNode` by default; override via `options.parse`.
31
- * `getImports` returns `[]` by default.
32
- */
33
- export function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(
34
- options: {
35
- name?: TOptions['name']
36
- resolvedOptions?: TOptions['resolvedOptions']
37
- inputNode?: Adapter<TOptions>['inputNode']
38
- parse?: Adapter<TOptions>['parse']
39
- getImports?: Adapter<TOptions>['getImports']
40
- } = {},
41
- ): Adapter<TOptions> {
42
- const inputNode = options.inputNode ?? null
43
- return {
44
- name: (options.name ?? 'oas') as TOptions['name'],
45
- options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],
46
- inputNode,
47
- parse: options.parse ?? (async () => ({ kind: 'Input' as const, schemas: [], operations: [] })),
48
- getImports: options.getImports ?? ((_node: SchemaNode, _resolve: (schemaName: string) => { name: string; path: string }) => []),
49
- } as Adapter<TOptions>
50
- }
51
-
52
- /**
53
- * Creates a minimal plugin mock for unit tests.
54
- *
55
- * @example
56
- * `const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })`
57
- */
58
- export function createMockedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(params: {
59
- name: TOptions['name']
60
- options: TOptions['resolvedOptions']
61
- resolver?: TOptions['resolver']
62
- transformer?: Visitor
63
- dependencies?: Array<string>
64
- }): NormalizedPlugin<TOptions> {
65
- return {
66
- name: params.name,
67
- options: params.options,
68
- resolver: params.resolver,
69
- transformer: params.transformer,
70
- dependencies: params.dependencies,
71
- hooks: {},
72
- } as unknown as NormalizedPlugin<TOptions>
73
- }
74
-
75
- type RenderGeneratorOptions<TOptions extends PluginFactoryOptions> = {
76
- config: Config
77
- adapter: Adapter
78
- driver: PluginDriver
79
- plugin: NormalizedPlugin<TOptions>
80
- options: TOptions['resolvedOptions']
81
- resolver: TOptions['resolver']
82
- }
83
-
84
- function createMockedPluginContext<TOptions extends PluginFactoryOptions>(opts: RenderGeneratorOptions<TOptions>): Omit<GeneratorContext<TOptions>, 'options'> {
85
- const root = resolve(opts.config.root, opts.config.output.path)
86
-
87
- return {
88
- config: opts.config,
89
- root,
90
- getMode: (output: { path: string }) => PluginDriver.getMode(resolve(root, output.path)),
91
- adapter: opts.adapter,
92
- resolver: opts.resolver,
93
- plugin: opts.plugin,
94
- driver: opts.driver,
95
- getResolver: (name: string) => opts.driver.getResolver(name),
96
- inputNode: { kind: 'Input', schemas: [], operations: [] },
97
- addFile: async (...files: Array<FileNode>) => opts.driver.fileManager.add(...files),
98
- upsertFile: async (...files: Array<FileNode>) => opts.driver.fileManager.upsert(...files),
99
- hooks: opts.driver.hooks ?? ({} as never),
100
- warn: (msg: string) => console.warn(msg),
101
- error: (msg: string) => console.error(msg),
102
- info: (msg: string) => console.info(msg),
103
- openInStudio: async () => {},
104
- } as unknown as Omit<GeneratorContext<TOptions>, 'options'>
105
- }
106
-
107
- /**
108
- * Renders a generator's `schema` method in a test context.
109
- *
110
- * @example
111
- * ```ts
112
- * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
113
- * await matchFiles(driver.fileManager.files)
114
- * ```
115
- */
116
- export async function renderGeneratorSchema<TOptions extends PluginFactoryOptions>(
117
- generator: Generator<TOptions>,
118
- node: SchemaNode,
119
- opts: RenderGeneratorOptions<TOptions>,
120
- ): Promise<void> {
121
- if (!generator.schema) return
122
- const context = createMockedPluginContext(opts)
123
- const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node
124
- const result = await generator.schema(transformedNode, {
125
- ...context,
126
- options: opts.options,
127
- })
128
- await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
129
- }
130
-
131
- /**
132
- * Renders a generator's `operation` method in a test context.
133
- *
134
- * @example
135
- * ```ts
136
- * await renderGeneratorOperation(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
137
- * await matchFiles(driver.fileManager.files)
138
- * ```
139
- */
140
- export async function renderGeneratorOperation<TOptions extends PluginFactoryOptions>(
141
- generator: Generator<TOptions>,
142
- node: OperationNode,
143
- opts: RenderGeneratorOptions<TOptions>,
144
- ): Promise<void> {
145
- if (!generator.operation) return
146
- const context = createMockedPluginContext(opts)
147
- const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node
148
- const result = await generator.operation(transformedNode, {
149
- ...context,
150
- options: opts.options,
151
- })
152
- await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
153
- }
154
-
155
- /**
156
- * Renders a generator's `operations` method in a test context.
157
- *
158
- * @example
159
- * ```ts
160
- * await renderGeneratorOperations(classClientGenerator, nodes, { config, adapter, driver, plugin, options, resolver })
161
- * await matchFiles(driver.fileManager.files)
162
- * ```
163
- */
164
- export async function renderGeneratorOperations<TOptions extends PluginFactoryOptions>(
165
- generator: Generator<TOptions>,
166
- nodes: Array<OperationNode>,
167
- opts: RenderGeneratorOptions<TOptions>,
168
- ): Promise<void> {
169
- if (!generator.operations) return
170
- const context = createMockedPluginContext(opts)
171
- const transformedNodes = opts.plugin.transformer ? nodes.map((n) => transform(n, opts.plugin.transformer!)) : nodes
172
- const result = await generator.operations(transformedNodes, {
173
- ...context,
174
- options: opts.options,
175
- })
176
- await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
177
- }
@@ -1,90 +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
- /**
9
- * Built-in filesystem storage driver.
10
- *
11
- * This is the default storage when no `storage` option is configured in the root config.
12
- * Keys are resolved against `process.cwd()`, so root-relative paths such as
13
- * `src/gen/api/getPets.ts` are written to the correct location without extra configuration.
14
- *
15
- * Internally uses the `write` utility from `@internals/utils`, which:
16
- * - trims leading/trailing whitespace before writing
17
- * - skips the write when file content is already identical (deduplication)
18
- * - creates missing parent directories automatically
19
- * - supports Bun's native file API when running under Bun
20
- *
21
- * @example
22
- * ```ts
23
- * import { fsStorage } from '@kubb/core'
24
- * import { defineConfig } from 'kubb'
25
- *
26
- * export default defineConfig({
27
- * input: { path: './petStore.yaml' },
28
- * output: { path: './src/gen' },
29
- * storage: fsStorage(),
30
- * })
31
- * ```
32
- */
33
- export const fsStorage = createStorage(() => ({
34
- name: 'fs',
35
- async hasItem(key: string) {
36
- try {
37
- await access(resolve(key))
38
- return true
39
- } catch (_error) {
40
- return false
41
- }
42
- },
43
- async getItem(key: string) {
44
- try {
45
- return await readFile(resolve(key), 'utf8')
46
- } catch (_error) {
47
- return null
48
- }
49
- },
50
- async setItem(key: string, value: string) {
51
- await write(resolve(key), value, { sanity: false })
52
- },
53
- async removeItem(key: string) {
54
- await rm(resolve(key), { force: true })
55
- },
56
- async getKeys(base?: string) {
57
- const keys: Array<string> = []
58
- const resolvedBase = resolve(base ?? process.cwd())
59
-
60
- async function walk(dir: string, prefix: string): Promise<void> {
61
- let entries: Array<Dirent>
62
- try {
63
- entries = (await readdir(dir, {
64
- withFileTypes: true,
65
- })) as Array<Dirent>
66
- } catch (_error) {
67
- return
68
- }
69
- for (const entry of entries) {
70
- const rel = prefix ? `${prefix}/${entry.name}` : entry.name
71
- if (entry.isDirectory()) {
72
- await walk(join(dir, entry.name), rel)
73
- } else {
74
- keys.push(rel)
75
- }
76
- }
77
- }
78
-
79
- await walk(resolvedBase, '')
80
-
81
- return keys
82
- },
83
- async clear(base?: string) {
84
- if (!base) {
85
- return
86
- }
87
-
88
- await clean(resolve(base))
89
- },
90
- }))
@@ -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
- })
package/src/types.ts DELETED
@@ -1,41 +0,0 @@
1
- export type { DevtoolsOptions } from './devtools.ts'
2
- export type { Adapter, AdapterFactoryOptions, AdapterSource } from './createAdapter.ts'
3
- export type {
4
- BuildOutput,
5
- CLIOptions,
6
- Config,
7
- InputData,
8
- InputPath,
9
- Kubb,
10
- KubbBuildEndContext,
11
- KubbBuildStartContext,
12
- KubbConfigEndContext,
13
- KubbDebugContext,
14
- KubbErrorContext,
15
- KubbFileProcessingUpdateContext,
16
- KubbFilesProcessingEndContext,
17
- KubbFilesProcessingStartContext,
18
- KubbGenerationEndContext,
19
- KubbGenerationStartContext,
20
- KubbGenerationSummaryContext,
21
- KubbHookEndContext,
22
- KubbHookStartContext,
23
- KubbHooks,
24
- KubbInfoContext,
25
- KubbLifecycleStartContext,
26
- KubbPluginsEndContext,
27
- KubbSuccessContext,
28
- KubbVersionNewContext,
29
- KubbWarnContext,
30
- PossibleConfig,
31
- UserConfig,
32
- } from './createKubb.ts'
33
- export type { Renderer, RendererFactory } from './createRenderer.ts'
34
- export type { Storage } from './createStorage.ts'
35
- export type { Generator, GeneratorContext } from './defineGenerator.ts'
36
- export type { Logger, LoggerContext, LoggerOptions, UserLogger } from './defineLogger.ts'
37
- export type { Middleware } from './defineMiddleware.ts'
38
- export type { Parser } from './defineParser.ts'
39
- export type { Exclude, Group, Include, Output, Override } from './definePlugin.ts'
40
- export type { KubbPluginEndContext, KubbPluginSetupContext, KubbPluginStartContext, NormalizedPlugin, Plugin, PluginFactoryOptions } from './definePlugin.ts'
41
- export type { ResolveBannerContext, ResolveOptionsContext, Resolver, ResolverContext, ResolverFileParams, ResolverPathParams } from './defineResolver.ts'