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

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/devtools.ts CHANGED
@@ -1,7 +1,14 @@
1
1
  import type { InputNode } from '@kubb/ast'
2
2
  import { deflateSync, inflateSync } from 'fflate'
3
3
  import { x } from 'tinyexec'
4
- import type { DevtoolsOptions } from './types.ts'
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
+ }
5
12
 
6
13
  /**
7
14
  * Encodes an `InputNode` as a compressed, URL-safe string.
package/src/index.ts CHANGED
@@ -13,8 +13,8 @@ export { definePlugin } from './definePlugin.ts'
13
13
  export { defineResolver } from './defineResolver.ts'
14
14
  export { FileManager } from './FileManager.ts'
15
15
  export { FileProcessor } from './FileProcessor.ts'
16
- export { PluginDriver } from './PluginDriver.ts'
16
+ export { KubbDriver } from './KubbDriver.ts'
17
17
  export { fsStorage } from './storages/fsStorage.ts'
18
18
  export { memoryStorage } from './storages/memoryStorage.ts'
19
19
  export * from './types.ts'
20
- export { isInputPath } from './utils/isInputPath.ts'
20
+ export { isInputPath } from './createKubb.ts'
package/src/mocks.ts CHANGED
@@ -1,16 +1,15 @@
1
1
  import { resolve } from 'node:path'
2
- import type { FileNode, OperationNode, SchemaNode, Visitor } from '@kubb/ast'
2
+ import type { FileNode, InputMeta, OperationNode, SchemaNode, Visitor } from '@kubb/ast'
3
3
  import { transform } from '@kubb/ast'
4
4
  import { FileManager } from './FileManager.ts'
5
- import { PluginDriver } from './PluginDriver.ts'
6
- import { applyHookResult } from './renderNode.ts'
5
+ import { applyHookResult, KubbDriver } from './KubbDriver.ts'
7
6
  import type { Adapter, AdapterFactoryOptions, Config, Generator, GeneratorContext, NormalizedPlugin, PluginFactoryOptions } from './types.ts'
8
7
 
9
8
  /**
10
9
 
11
10
  * Creates a minimal `PluginDriver` mock for unit tests.
12
11
  */
13
- export function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): PluginDriver {
12
+ export function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): KubbDriver {
14
13
  return {
15
14
  config: options?.config ?? {
16
15
  root: '.',
@@ -23,7 +22,7 @@ export function createMockedPluginDriver(options: { name?: string; plugin?: Norm
23
22
  },
24
23
  getResolver: (_pluginName: string) => options?.plugin?.resolver,
25
24
  fileManager: new FileManager(),
26
- } as unknown as PluginDriver
25
+ } as unknown as KubbDriver
27
26
  }
28
27
 
29
28
  /**
@@ -35,16 +34,13 @@ export function createMockedAdapter<TOptions extends AdapterFactoryOptions = Ada
35
34
  options: {
36
35
  name?: TOptions['name']
37
36
  resolvedOptions?: TOptions['resolvedOptions']
38
- inputNode?: Adapter<TOptions>['inputNode']
39
37
  parse?: Adapter<TOptions>['parse']
40
38
  getImports?: Adapter<TOptions>['getImports']
41
39
  } = {},
42
40
  ): Adapter<TOptions> {
43
- const inputNode = options.inputNode ?? null
44
41
  return {
45
42
  name: (options.name ?? 'oas') as TOptions['name'],
46
43
  options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],
47
- inputNode,
48
44
  parse: options.parse ?? (async () => ({ kind: 'Input' as const, schemas: [], operations: [] })),
49
45
  getImports: options.getImports ?? ((_node: SchemaNode, _resolve: (schemaName: string) => { name: string; path: string }) => []),
50
46
  } as Adapter<TOptions>
@@ -76,7 +72,8 @@ export function createMockedPlugin<TOptions extends PluginFactoryOptions = Plugi
76
72
  type RenderGeneratorOptions<TOptions extends PluginFactoryOptions> = {
77
73
  config: Config
78
74
  adapter: Adapter
79
- driver: PluginDriver
75
+ meta?: InputMeta
76
+ driver: KubbDriver
80
77
  plugin: NormalizedPlugin<TOptions>
81
78
  options: TOptions['resolvedOptions']
82
79
  resolver: TOptions['resolver']
@@ -88,13 +85,13 @@ function createMockedPluginContext<TOptions extends PluginFactoryOptions>(opts:
88
85
  return {
89
86
  config: opts.config,
90
87
  root,
91
- getMode: (output: { path: string }) => PluginDriver.getMode(resolve(root, output.path)),
88
+ getMode: (output: { path: string }) => KubbDriver.getMode(resolve(root, output.path)),
92
89
  adapter: opts.adapter,
93
90
  resolver: opts.resolver,
94
91
  plugin: opts.plugin,
95
92
  driver: opts.driver,
96
93
  getResolver: (name: string) => opts.driver.getResolver(name),
97
- inputNode: { kind: 'Input', schemas: [], operations: [] },
94
+ meta: opts.meta ?? { circularNames: [], enumNames: [] },
98
95
  addFile: async (...files: Array<FileNode>) => opts.driver.fileManager.add(...files),
99
96
  upsertFile: async (...files: Array<FileNode>) => opts.driver.fileManager.upsert(...files),
100
97
  hooks: opts.driver.hooks ?? ({} as never),
@@ -126,7 +123,7 @@ export async function renderGeneratorSchema<TOptions extends PluginFactoryOption
126
123
  ...context,
127
124
  options: opts.options,
128
125
  })
129
- await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
126
+ await applyHookResult({ result, driver: opts.driver, rendererFactory: generator.renderer })
130
127
  }
131
128
 
132
129
  /**
@@ -150,7 +147,7 @@ export async function renderGeneratorOperation<TOptions extends PluginFactoryOpt
150
147
  ...context,
151
148
  options: opts.options,
152
149
  })
153
- await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
150
+ await applyHookResult({ result, driver: opts.driver, rendererFactory: generator.renderer })
154
151
  }
155
152
 
156
153
  /**
@@ -174,5 +171,5 @@ export async function renderGeneratorOperations<TOptions extends PluginFactoryOp
174
171
  ...context,
175
172
  options: opts.options,
176
173
  })
177
- await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
174
+ await applyHookResult({ result, driver: opts.driver, rendererFactory: generator.renderer })
178
175
  }
@@ -4,13 +4,6 @@ import { join, resolve } from 'node:path'
4
4
  import { clean, write } from '@internals/utils'
5
5
  import { createStorage } from '../createStorage.ts'
6
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
7
  /**
15
8
  * Built-in filesystem storage driver.
16
9
  *
@@ -42,27 +35,15 @@ export const fsStorage = createStorage(() => ({
42
35
  try {
43
36
  await access(resolve(key))
44
37
  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
- })
38
+ } catch (_error) {
39
+ return false
53
40
  }
54
41
  },
55
42
  async getItem(key: string) {
56
43
  try {
57
44
  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
- })
45
+ } catch (_error) {
46
+ return null
66
47
  }
67
48
  },
68
49
  async setItem(key: string, value: string) {
@@ -72,36 +53,31 @@ export const fsStorage = createStorage(() => ({
72
53
  await rm(resolve(key), { force: true })
73
54
  },
74
55
  async getKeys(base?: string) {
75
- const keys: Array<string> = []
76
56
  const resolvedBase = resolve(base ?? process.cwd())
77
57
 
78
- async function walk(dir: string, prefix: string): Promise<void> {
58
+ async function* walk(dir: string, prefix: string): AsyncGenerator<string, void, undefined> {
79
59
  let entries: Array<Dirent>
80
60
  try {
81
61
  entries = (await readdir(dir, {
82
62
  withFileTypes: true,
83
63
  })) 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
- })
64
+ } catch (_error) {
65
+ return
92
66
  }
93
67
  for (const entry of entries) {
94
68
  const rel = prefix ? `${prefix}/${entry.name}` : entry.name
95
69
  if (entry.isDirectory()) {
96
- await walk(join(dir, entry.name), rel)
70
+ yield* walk(join(dir, entry.name), rel)
97
71
  } else {
98
- keys.push(rel)
72
+ yield rel
99
73
  }
100
74
  }
101
75
  }
102
76
 
103
- await walk(resolvedBase, '')
104
-
77
+ const keys: Array<string> = []
78
+ for await (const key of walk(resolvedBase, '')) {
79
+ keys.push(key)
80
+ }
105
81
  return keys
106
82
  },
107
83
  async clear(base?: string) {