@kubb/core 5.0.0-beta.3 → 5.0.0-beta.31

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 (46) hide show
  1. package/README.md +8 -38
  2. package/dist/KubbDriver-CFx2DdhF.js +2131 -0
  3. package/dist/KubbDriver-CFx2DdhF.js.map +1 -0
  4. package/dist/KubbDriver-vyD7F0Ip.cjs +2252 -0
  5. package/dist/KubbDriver-vyD7F0Ip.cjs.map +1 -0
  6. package/dist/{types-CC09VtBt.d.ts → createKubb-6zii1jo-.d.ts} +1610 -1257
  7. package/dist/index.cjs +351 -1125
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +3 -186
  10. package/dist/index.js +341 -1119
  11. package/dist/index.js.map +1 -1
  12. package/dist/mocks.cjs +30 -21
  13. package/dist/mocks.cjs.map +1 -1
  14. package/dist/mocks.d.ts +5 -5
  15. package/dist/mocks.js +29 -20
  16. package/dist/mocks.js.map +1 -1
  17. package/package.json +6 -18
  18. package/src/FileManager.ts +78 -61
  19. package/src/FileProcessor.ts +48 -38
  20. package/src/KubbDriver.ts +930 -0
  21. package/src/constants.ts +11 -6
  22. package/src/createAdapter.ts +113 -17
  23. package/src/createKubb.ts +1039 -478
  24. package/src/createRenderer.ts +58 -27
  25. package/src/createStorage.ts +36 -23
  26. package/src/defineGenerator.ts +127 -15
  27. package/src/defineLogger.ts +66 -7
  28. package/src/defineMiddleware.ts +19 -17
  29. package/src/defineParser.ts +30 -13
  30. package/src/definePlugin.ts +329 -14
  31. package/src/defineResolver.ts +365 -167
  32. package/src/devtools.ts +8 -1
  33. package/src/index.ts +2 -2
  34. package/src/mocks.ts +11 -14
  35. package/src/storages/fsStorage.ts +13 -37
  36. package/src/types.ts +48 -1292
  37. package/dist/PluginDriver-BXibeQk-.cjs +0 -1036
  38. package/dist/PluginDriver-BXibeQk-.cjs.map +0 -1
  39. package/dist/PluginDriver-DV3p2Hky.js +0 -945
  40. package/dist/PluginDriver-DV3p2Hky.js.map +0 -1
  41. package/src/Kubb.ts +0 -300
  42. package/src/PluginDriver.ts +0 -424
  43. package/src/renderNode.ts +0 -35
  44. package/src/utils/diagnostics.ts +0 -18
  45. package/src/utils/isInputPath.ts +0 -10
  46. package/src/utils/packageJSON.ts +0 -99
package/src/constants.ts CHANGED
@@ -3,12 +3,7 @@ import type { FileNode } from '@kubb/ast'
3
3
  /**
4
4
  * Base URL for the Kubb Studio web app.
5
5
  */
6
- export const DEFAULT_STUDIO_URL = 'https://studio.kubb.dev' as const
7
-
8
- /**
9
- * Maximum number of files processed in parallel by FileProcessor.
10
- */
11
- export const PARALLEL_CONCURRENCY_LIMIT = 100
6
+ export const DEFAULT_STUDIO_URL = 'https://kubb.studio' as const
12
7
 
13
8
  /**
14
9
  * Default banner style written at the top of every generated file.
@@ -20,6 +15,16 @@ export const DEFAULT_BANNER = 'simple' as const
20
15
  */
21
16
  export const DEFAULT_EXTENSION: Record<FileNode['extname'], FileNode['extname'] | ''> = { '.ts': '.ts' }
22
17
 
18
+ /**
19
+ * Number of file writes to batch in parallel during `flushPendingFiles`.
20
+ */
21
+ export const STREAM_FLUSH_EVERY = 50
22
+
23
+ /**
24
+ * Number of schema/operation nodes to dispatch concurrently during generation.
25
+ */
26
+ export const SCHEMA_PARALLEL = 8
27
+
23
28
  /**
24
29
  * Numeric log-level thresholds used internally to compare verbosity.
25
30
  *
@@ -1,30 +1,126 @@
1
- import type { Adapter, AdapterFactoryOptions } from './types.ts'
1
+ import type { PossiblePromise } from '@internals/utils'
2
+ import type { ImportNode, InputNode, InputStreamNode, SchemaNode } from '@kubb/ast'
2
3
 
3
- type AdapterBuilder<T extends AdapterFactoryOptions> = (options: T['options']) => Adapter<T>
4
+ /**
5
+ * Source data handed to an adapter's `parse` function. Mirrors the config
6
+ * input shape with paths resolved to absolute.
7
+ *
8
+ * - `{ type: 'path' }`: single file on disk.
9
+ * - `{ type: 'paths' }`: multiple files (e.g. split spec).
10
+ * - `{ type: 'data' }`: raw string or parsed object provided inline.
11
+ */
12
+ export type AdapterSource = { type: 'path'; path: string } | { type: 'data'; data: string | unknown } | { type: 'paths'; paths: Array<string> }
4
13
 
5
14
  /**
6
- * Factory for implementing custom adapters that translate non-OpenAPI specs into Kubb's AST.
15
+ * Generic parameters used by `createAdapter` and the resulting `Adapter` type.
7
16
  *
8
- * Use this to support GraphQL schemas, gRPC definitions, AsyncAPI, or custom domain-specific languages.
9
- * Built-in adapters include `@kubb/adapter-oas` for OpenAPI and Swagger documents.
17
+ * - `TName`: unique adapter identifier (`'oas'`, `'asyncapi'`, ...).
18
+ * - `TOptions`: user-facing options accepted by the adapter factory.
19
+ * - `TResolvedOptions`: options after defaults are applied.
20
+ * - `TDocument`: type of the parsed source document.
21
+ */
22
+ export type AdapterFactoryOptions<
23
+ TName extends string = string,
24
+ TOptions extends object = object,
25
+ TResolvedOptions extends object = TOptions,
26
+ TDocument = unknown,
27
+ > = {
28
+ name: TName
29
+ options: TOptions
30
+ resolvedOptions: TResolvedOptions
31
+ document: TDocument
32
+ }
33
+
34
+ /**
35
+ * Converts input files or inline data into Kubb's universal AST `InputNode`.
10
36
  *
11
- * @note Adapters must parse their input format to Kubb's `InputNode` structure.
37
+ * Adapters live between the spec format and the plugins. The built-in
38
+ * `@kubb/adapter-oas` handles OpenAPI 2.0, 3.0, and 3.1; custom adapters can
39
+ * support GraphQL, gRPC, AsyncAPI, or any domain-specific schema language.
12
40
  *
13
41
  * @example
14
42
  * ```ts
15
- * export const myAdapter = createAdapter<MyAdapter>((options) => {
16
- * return {
17
- * name: 'my-adapter',
18
- * options,
19
- * async parse(source) {
20
- * // Transform source format to InputNode
21
- * return { ... }
22
- * },
23
- * }
43
+ * import { defineConfig } from 'kubb'
44
+ * import { adapterOas } from '@kubb/adapter-oas'
45
+ * import { pluginTs } from '@kubb/plugin-ts'
46
+ *
47
+ * export default defineConfig({
48
+ * input: { path: './petStore.yaml' },
49
+ * output: { path: './src/gen' },
50
+ * adapter: adapterOas(),
51
+ * plugins: [pluginTs()],
24
52
  * })
53
+ * ```
54
+ */
55
+ export type Adapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions> = {
56
+ /**
57
+ * Human-readable adapter identifier (e.g. `'oas'`, `'asyncapi'`).
58
+ */
59
+ name: TOptions['name']
60
+ /**
61
+ * Resolved adapter options after defaults have been applied.
62
+ */
63
+ options: TOptions['resolvedOptions']
64
+ /**
65
+ * Parsed source document after the first `parse()` call. `null` before parsing.
66
+ */
67
+ document: TOptions['document'] | null
68
+ /**
69
+ * Parse the source into a universal `InputNode`.
70
+ */
71
+ parse: (source: AdapterSource) => PossiblePromise<InputNode>
72
+ /**
73
+ * Extract `ImportNode` entries for a schema tree.
74
+ * Returns an empty array before the first `parse()` call.
75
+ *
76
+ * The `resolve` callback receives the collision-corrected schema name and must
77
+ * return `{ name, path }` for the import, or `undefined` to skip it.
78
+ */
79
+ getImports: (node: SchemaNode, resolve: (schemaName: string) => { name: string; path: string }) => Array<ImportNode>
80
+ /**
81
+ * Validate the document at the given path or URL.
82
+ */
83
+ validate: (input: string, options?: { throwOnError?: boolean }) => Promise<void>
84
+ /**
85
+ * Memory-efficient streaming variant of `parse()`.
86
+ *
87
+ * Returns an `InputStreamNode` whose `schemas` and `operations` are `AsyncIterable`.
88
+ * Each `for await` loop creates a fresh parse pass over the cached in-memory document.
89
+ * No pre-built arrays are held in memory.
90
+ */
91
+ stream?: (source: AdapterSource) => Promise<InputStreamNode>
92
+ }
93
+
94
+ type AdapterBuilder<T extends AdapterFactoryOptions> = (options: T['options']) => Adapter<T>
95
+
96
+ /**
97
+ * Defines a custom adapter that translates a spec format into Kubb's universal
98
+ * AST. Use this when you need to consume GraphQL, gRPC, AsyncAPI, or another
99
+ * domain-specific schema. Built-in adapters: `@kubb/adapter-oas` for
100
+ * OpenAPI/Swagger documents.
101
+ *
102
+ * Adapters must return an `InputNode` from `parse`. That node is what every
103
+ * plugin in the build consumes.
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * import { createAdapter, ast, type AdapterFactoryOptions } from '@kubb/core'
108
+ *
109
+ * type MyAdapter = AdapterFactoryOptions<'my-adapter', { validate?: boolean }>
25
110
  *
26
- * // Instantiate:
27
- * const adapter = myAdapter({ validate: true })
111
+ * export const myAdapter = createAdapter<MyAdapter>((options) => ({
112
+ * name: 'my-adapter',
113
+ * options,
114
+ * document: null,
115
+ * async parse(_source) {
116
+ * // Convert `source` (path or inline data) into an InputNode.
117
+ * return ast.createInput()
118
+ * },
119
+ * getImports: () => [],
120
+ * async validate() {
121
+ * // Throw or call ctx.error here when the spec is invalid.
122
+ * },
123
+ * }))
28
124
  * ```
29
125
  */
30
126
  export function createAdapter<T extends AdapterFactoryOptions = AdapterFactoryOptions>(build: AdapterBuilder<T>): (options?: T['options']) => Adapter<T> {