@kubb/core 5.0.0-alpha.6 → 5.0.0-alpha.60

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 (72) hide show
  1. package/README.md +3 -2
  2. package/dist/PluginDriver-Bc0HQM8V.js +948 -0
  3. package/dist/PluginDriver-Bc0HQM8V.js.map +1 -0
  4. package/dist/PluginDriver-Dyl2fwfQ.cjs +1039 -0
  5. package/dist/PluginDriver-Dyl2fwfQ.cjs.map +1 -0
  6. package/dist/index.cjs +691 -1798
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +279 -265
  9. package/dist/index.js +678 -1765
  10. package/dist/index.js.map +1 -1
  11. package/dist/mocks.cjs +138 -0
  12. package/dist/mocks.cjs.map +1 -0
  13. package/dist/mocks.d.ts +74 -0
  14. package/dist/mocks.js +133 -0
  15. package/dist/mocks.js.map +1 -0
  16. package/dist/types-i0b4_23K.d.ts +1903 -0
  17. package/package.json +51 -57
  18. package/src/FileManager.ts +110 -0
  19. package/src/FileProcessor.ts +86 -0
  20. package/src/Kubb.ts +205 -130
  21. package/src/PluginDriver.ts +424 -0
  22. package/src/constants.ts +20 -47
  23. package/src/createAdapter.ts +25 -0
  24. package/src/createKubb.ts +527 -0
  25. package/src/createRenderer.ts +57 -0
  26. package/src/createStorage.ts +58 -0
  27. package/src/defineGenerator.ts +88 -100
  28. package/src/defineLogger.ts +13 -3
  29. package/src/defineMiddleware.ts +59 -0
  30. package/src/defineParser.ts +45 -0
  31. package/src/definePlugin.ts +78 -7
  32. package/src/defineResolver.ts +521 -0
  33. package/src/devtools.ts +14 -14
  34. package/src/index.ts +13 -17
  35. package/src/mocks.ts +171 -0
  36. package/src/renderNode.ts +35 -0
  37. package/src/storages/fsStorage.ts +40 -11
  38. package/src/storages/memoryStorage.ts +4 -3
  39. package/src/types.ts +738 -218
  40. package/src/utils/diagnostics.ts +4 -1
  41. package/src/utils/isInputPath.ts +10 -0
  42. package/src/utils/packageJSON.ts +99 -0
  43. package/dist/PluginManager-vZodFEMe.d.ts +0 -1056
  44. package/dist/chunk-ByKO4r7w.cjs +0 -38
  45. package/dist/hooks.cjs +0 -60
  46. package/dist/hooks.cjs.map +0 -1
  47. package/dist/hooks.d.ts +0 -64
  48. package/dist/hooks.js +0 -56
  49. package/dist/hooks.js.map +0 -1
  50. package/src/BarrelManager.ts +0 -74
  51. package/src/PackageManager.ts +0 -180
  52. package/src/PluginManager.ts +0 -667
  53. package/src/PromiseManager.ts +0 -40
  54. package/src/build.ts +0 -419
  55. package/src/config.ts +0 -56
  56. package/src/defineAdapter.ts +0 -22
  57. package/src/defineStorage.ts +0 -56
  58. package/src/errors.ts +0 -1
  59. package/src/hooks/index.ts +0 -4
  60. package/src/hooks/useKubb.ts +0 -55
  61. package/src/hooks/useMode.ts +0 -11
  62. package/src/hooks/usePlugin.ts +0 -11
  63. package/src/hooks/usePluginManager.ts +0 -11
  64. package/src/utils/FunctionParams.ts +0 -155
  65. package/src/utils/TreeNode.ts +0 -215
  66. package/src/utils/executeStrategies.ts +0 -81
  67. package/src/utils/formatters.ts +0 -56
  68. package/src/utils/getBarrelFiles.ts +0 -79
  69. package/src/utils/getConfigs.ts +0 -30
  70. package/src/utils/getPlugins.ts +0 -23
  71. package/src/utils/linters.ts +0 -25
  72. package/src/utils/resolveOptions.ts +0 -93
@@ -0,0 +1,527 @@
1
+ import { resolve } from 'node:path'
2
+ import { AsyncEventEmitter, BuildError, exists, formatMs, getElapsedMs, URLPath } from '@internals/utils'
3
+ import type { FileNode, OperationNode } from '@kubb/ast'
4
+ import { transform, walk } from '@kubb/ast'
5
+ import { DEFAULT_BANNER, DEFAULT_EXTENSION, DEFAULT_STUDIO_URL } from './constants.ts'
6
+ import type { RendererFactory } from './createRenderer.ts'
7
+ import type { Generator } from './defineGenerator.ts'
8
+ import type { Parser } from './defineParser.ts'
9
+ import type { Plugin } from './definePlugin.ts'
10
+ import { FileProcessor } from './FileProcessor.ts'
11
+ import type { Kubb } from './Kubb.ts'
12
+ import { PluginDriver } from './PluginDriver.ts'
13
+ import { applyHookResult } from './renderNode.ts'
14
+ import { fsStorage } from './storages/fsStorage.ts'
15
+ import type { AdapterSource, Config, GeneratorContext, KubbHooks, NormalizedPlugin, Storage, UserConfig } from './types.ts'
16
+ import { getDiagnosticInfo } from './utils/diagnostics.ts'
17
+ import { isInputPath } from './utils/isInputPath.ts'
18
+
19
+ type SetupOptions = {
20
+ hooks?: AsyncEventEmitter<KubbHooks>
21
+ }
22
+
23
+ /**
24
+ * Full output produced by a successful or failed build.
25
+ */
26
+ export type BuildOutput = {
27
+ /**
28
+ * Plugins that threw during installation, paired with the caught error.
29
+ */
30
+ failedPlugins: Set<{ plugin: Plugin; error: Error }>
31
+ files: Array<FileNode>
32
+ driver: PluginDriver
33
+ /**
34
+ * Elapsed time in milliseconds for each plugin, keyed by plugin name.
35
+ */
36
+ pluginTimings: Map<string, number>
37
+ error?: Error
38
+ /**
39
+ * Raw generated source, keyed by absolute file path.
40
+ */
41
+ sources: Map<string, string>
42
+ }
43
+
44
+ type SetupResult = {
45
+ hooks: AsyncEventEmitter<KubbHooks>
46
+ driver: PluginDriver
47
+ sources: Map<string, string>
48
+ config: Config
49
+ storage: Storage | null
50
+ }
51
+
52
+ async function setup(userConfig: UserConfig, options: SetupOptions = {}): Promise<SetupResult> {
53
+ const hooks = options.hooks ?? new AsyncEventEmitter<KubbHooks>()
54
+
55
+ const sources: Map<string, string> = new Map<string, string>()
56
+ const diagnosticInfo = getDiagnosticInfo()
57
+
58
+ if (Array.isArray(userConfig.input)) {
59
+ await hooks.emit('kubb:warn', { message: 'This feature is still under development — use with caution' })
60
+ }
61
+
62
+ await hooks.emit('kubb:debug', {
63
+ date: new Date(),
64
+ logs: [
65
+ 'Configuration:',
66
+ ` • Name: ${userConfig.name || 'unnamed'}`,
67
+ ` • Root: ${userConfig.root || process.cwd()}`,
68
+ ` • Output: ${userConfig.output?.path || 'not specified'}`,
69
+ ` • Plugins: ${userConfig.plugins?.length || 0}`,
70
+ 'Output Settings:',
71
+ ` • Storage: ${userConfig.output?.storage ? `custom(${userConfig.output.storage.name})` : userConfig.output?.write === false ? 'disabled' : 'filesystem (default)'}`,
72
+ ` • Formatter: ${userConfig.output?.format || 'none'}`,
73
+ ` • Linter: ${userConfig.output?.lint || 'none'}`,
74
+ 'Environment:',
75
+ Object.entries(diagnosticInfo)
76
+ .map(([key, value]) => ` • ${key}: ${value}`)
77
+ .join('\n'),
78
+ ],
79
+ })
80
+
81
+ try {
82
+ if (isInputPath(userConfig) && !new URLPath(userConfig.input.path).isURL) {
83
+ await exists(userConfig.input.path)
84
+
85
+ await hooks.emit('kubb:debug', {
86
+ date: new Date(),
87
+ logs: [`✓ Input file validated: ${userConfig.input.path}`],
88
+ })
89
+ }
90
+ } catch (caughtError) {
91
+ if (isInputPath(userConfig)) {
92
+ const error = caughtError as Error
93
+
94
+ throw new Error(
95
+ `Cannot read file/URL defined in \`input.path\` or set with \`kubb generate PATH\` in the CLI of your Kubb config ${userConfig.input.path}`,
96
+ {
97
+ cause: error,
98
+ },
99
+ )
100
+ }
101
+ }
102
+
103
+ if (!userConfig.adapter) {
104
+ throw new Error('Adapter should be defined')
105
+ }
106
+
107
+ const config: Config = {
108
+ ...userConfig,
109
+ root: userConfig.root || process.cwd(),
110
+ parsers: userConfig.parsers ?? [],
111
+ adapter: userConfig.adapter,
112
+ output: {
113
+ write: true,
114
+ extension: DEFAULT_EXTENSION,
115
+ defaultBanner: DEFAULT_BANNER,
116
+ ...userConfig.output,
117
+ },
118
+ devtools: userConfig.devtools
119
+ ? {
120
+ studioUrl: DEFAULT_STUDIO_URL,
121
+ ...(typeof userConfig.devtools === 'boolean' ? {} : userConfig.devtools),
122
+ }
123
+ : undefined,
124
+ plugins: userConfig.plugins as unknown as Config['plugins'],
125
+ }
126
+
127
+ const storage: Storage | null = config.output.write === false ? null : (config.output.storage ?? fsStorage())
128
+
129
+ if (config.output.clean) {
130
+ await hooks.emit('kubb:debug', {
131
+ date: new Date(),
132
+ logs: ['Cleaning output directories', ` • Output: ${config.output.path}`],
133
+ })
134
+ await storage?.clear(resolve(config.root, config.output.path))
135
+ }
136
+
137
+ const driver = new PluginDriver(config, {
138
+ hooks,
139
+ })
140
+
141
+ // Install middleware listeners after all plugin hooks are registered.
142
+ // Because AsyncEventEmitter calls listeners in registration order,
143
+ // middleware hooks for any event fire after all plugin hooks for that event.
144
+ for (const middleware of config.middleware ?? []) {
145
+ middleware.install(hooks)
146
+ }
147
+
148
+ const adapter = config.adapter
149
+ if (!adapter) {
150
+ throw new Error('No adapter configured. Please provide an adapter in your kubb.config.ts.')
151
+ }
152
+ const source = inputToAdapterSource(config)
153
+
154
+ await hooks.emit('kubb:debug', {
155
+ date: new Date(),
156
+ logs: [`Running adapter: ${adapter.name}`],
157
+ })
158
+
159
+ driver.adapter = adapter
160
+ driver.inputNode = await adapter.parse(source)
161
+
162
+ await hooks.emit('kubb:debug', {
163
+ date: new Date(),
164
+ logs: [
165
+ `✓ Adapter '${adapter.name}' resolved InputNode`,
166
+ ` • Schemas: ${driver.inputNode.schemas.length}`,
167
+ ` • Operations: ${driver.inputNode.operations.length}`,
168
+ ],
169
+ })
170
+
171
+ return {
172
+ config,
173
+ hooks,
174
+ driver,
175
+ sources,
176
+ storage,
177
+ }
178
+ }
179
+
180
+ /**
181
+ * Walks the AST and dispatches nodes to a plugin's direct AST hooks
182
+ * (`schema`, `operation`, `operations`).
183
+ */
184
+ async function runPluginAstHooks(plugin: NormalizedPlugin, context: GeneratorContext): Promise<void> {
185
+ const { adapter, inputNode, resolver, driver } = context
186
+ const { exclude, include, override } = plugin.options
187
+
188
+ if (!adapter || !inputNode) {
189
+ throw new Error(`[${plugin.name}] No adapter found. Add an OAS adapter (e.g. pluginOas()) before this plugin in your Kubb config.`)
190
+ }
191
+
192
+ function resolveRenderer(gen: Generator): RendererFactory | undefined {
193
+ return gen.renderer === null ? undefined : (gen.renderer ?? plugin.renderer ?? context.config.renderer)
194
+ }
195
+
196
+ const generators = plugin.generators ?? []
197
+ const collectedOperations: Array<OperationNode> = []
198
+
199
+ const generatorContext = {
200
+ ...context,
201
+ resolver: driver.getResolver(plugin.name),
202
+ }
203
+
204
+ await walk(inputNode, {
205
+ depth: 'shallow',
206
+ async schema(node) {
207
+ const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node
208
+ const options = resolver.resolveOptions(transformedNode, {
209
+ options: plugin.options,
210
+ exclude,
211
+ include,
212
+ override,
213
+ })
214
+ if (options === null) return
215
+
216
+ const ctx = { ...generatorContext, options }
217
+
218
+ for (const gen of generators) {
219
+ if (!gen.schema) continue
220
+ const result = await gen.schema(transformedNode, ctx)
221
+ await applyHookResult(result, driver, resolveRenderer(gen))
222
+ }
223
+
224
+ await driver.hooks.emit('kubb:generate:schema', transformedNode, ctx)
225
+ },
226
+ async operation(node) {
227
+ const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node
228
+ const options = resolver.resolveOptions(transformedNode, {
229
+ options: plugin.options,
230
+ exclude,
231
+ include,
232
+ override,
233
+ })
234
+ if (options !== null) {
235
+ collectedOperations.push(transformedNode)
236
+
237
+ const ctx = { ...generatorContext, options }
238
+
239
+ for (const gen of generators) {
240
+ if (!gen.operation) continue
241
+ const result = await gen.operation(transformedNode, ctx)
242
+ await applyHookResult(result, driver, resolveRenderer(gen))
243
+ }
244
+
245
+ await driver.hooks.emit('kubb:generate:operation', transformedNode, ctx)
246
+ }
247
+ },
248
+ })
249
+
250
+ if (collectedOperations.length > 0) {
251
+ const ctx = { ...generatorContext, options: plugin.options }
252
+
253
+ for (const gen of generators) {
254
+ if (!gen.operations) continue
255
+ const result = await gen.operations(collectedOperations, ctx)
256
+ await applyHookResult(result, driver, resolveRenderer(gen))
257
+ }
258
+
259
+ await driver.hooks.emit('kubb:generate:operations', collectedOperations, ctx)
260
+ }
261
+ }
262
+
263
+ async function safeBuild(setupResult: SetupResult): Promise<BuildOutput> {
264
+ const { driver, hooks, sources, storage } = setupResult
265
+
266
+ const failedPlugins = new Set<{ plugin: Plugin; error: Error }>()
267
+ const pluginTimings = new Map<string, number>()
268
+ const config = driver.config
269
+
270
+ try {
271
+ await driver.emitSetupHooks()
272
+
273
+ if (driver.adapter && driver.inputNode) {
274
+ await hooks.emit('kubb:build:start', {
275
+ config,
276
+ adapter: driver.adapter,
277
+ inputNode: driver.inputNode,
278
+ getPlugin: driver.getPlugin.bind(driver),
279
+ get files() {
280
+ return driver.fileManager.files
281
+ },
282
+ upsertFile: (...files) => driver.fileManager.upsert(...files),
283
+ })
284
+ }
285
+
286
+ for (const plugin of driver.plugins.values()) {
287
+ const context = driver.getContext(plugin)
288
+ const hrStart = process.hrtime()
289
+
290
+ try {
291
+ const timestamp = new Date()
292
+
293
+ await hooks.emit('kubb:plugin:start', { plugin })
294
+
295
+ await hooks.emit('kubb:debug', {
296
+ date: timestamp,
297
+ logs: ['Starting plugin...', ` • Plugin Name: ${plugin.name}`],
298
+ })
299
+
300
+ if (plugin.generators?.length || driver.hasRegisteredGenerators(plugin.name)) {
301
+ await runPluginAstHooks(plugin, context)
302
+ }
303
+
304
+ const duration = getElapsedMs(hrStart)
305
+ pluginTimings.set(plugin.name, duration)
306
+
307
+ await hooks.emit('kubb:plugin:end', {
308
+ plugin,
309
+ duration,
310
+ success: true,
311
+ })
312
+
313
+ await hooks.emit('kubb:debug', {
314
+ date: new Date(),
315
+ logs: [`✓ Plugin started successfully (${formatMs(duration)})`],
316
+ })
317
+ } catch (caughtError) {
318
+ const error = caughtError as Error
319
+ const errorTimestamp = new Date()
320
+ const duration = getElapsedMs(hrStart)
321
+
322
+ await hooks.emit('kubb:plugin:end', {
323
+ plugin,
324
+ duration,
325
+ success: false,
326
+ error,
327
+ })
328
+
329
+ await hooks.emit('kubb:debug', {
330
+ date: errorTimestamp,
331
+ logs: [
332
+ '✗ Plugin start failed',
333
+ ` • Plugin Name: ${plugin.name}`,
334
+ ` • Error: ${error.constructor.name} - ${error.message}`,
335
+ ' • Stack Trace:',
336
+ error.stack || 'No stack trace available',
337
+ ],
338
+ })
339
+
340
+ failedPlugins.add({ plugin, error })
341
+ }
342
+ }
343
+
344
+ await hooks.emit('kubb:plugins:end', {
345
+ config,
346
+ get files() {
347
+ return driver.fileManager.files
348
+ },
349
+ upsertFile: (...files) => driver.fileManager.upsert(...files),
350
+ })
351
+
352
+ const files = driver.fileManager.files
353
+
354
+ const parsersMap = new Map<FileNode['extname'], Parser>()
355
+ for (const parser of config.parsers) {
356
+ if (parser.extNames) {
357
+ for (const extname of parser.extNames) {
358
+ parsersMap.set(extname, parser)
359
+ }
360
+ }
361
+ }
362
+
363
+ const fileProcessor = new FileProcessor()
364
+
365
+ await hooks.emit('kubb:debug', {
366
+ date: new Date(),
367
+ logs: [`Writing ${files.length} files...`],
368
+ })
369
+
370
+ await fileProcessor.run(files, {
371
+ parsers: parsersMap,
372
+ extension: config.output.extension,
373
+ onStart: async (processingFiles) => {
374
+ await hooks.emit('kubb:files:processing:start', { files: processingFiles })
375
+ },
376
+ onUpdate: async ({ file, source, processed, total, percentage }) => {
377
+ await hooks.emit('kubb:file:processing:update', {
378
+ file,
379
+ source,
380
+ processed,
381
+ total,
382
+ percentage,
383
+ config,
384
+ })
385
+ if (source) {
386
+ await storage?.setItem(file.path, source)
387
+ sources.set(file.path, source)
388
+ }
389
+ },
390
+ onEnd: async (processedFiles) => {
391
+ await hooks.emit('kubb:files:processing:end', { files: processedFiles })
392
+ await hooks.emit('kubb:debug', {
393
+ date: new Date(),
394
+ logs: [`✓ File write process completed for ${processedFiles.length} files`],
395
+ })
396
+ },
397
+ })
398
+
399
+ await hooks.emit('kubb:build:end', {
400
+ files,
401
+ config,
402
+ outputDir: resolve(config.root, config.output.path),
403
+ })
404
+
405
+ return {
406
+ failedPlugins,
407
+ files,
408
+ driver,
409
+ pluginTimings,
410
+ sources,
411
+ }
412
+ } catch (error) {
413
+ return {
414
+ failedPlugins,
415
+ files: [],
416
+ driver,
417
+ pluginTimings,
418
+ error: error as Error,
419
+ sources,
420
+ }
421
+ } finally {
422
+ driver.dispose()
423
+ }
424
+ }
425
+
426
+ async function build(setupResult: SetupResult): Promise<BuildOutput> {
427
+ const { files, driver, failedPlugins, pluginTimings, error, sources } = await safeBuild(setupResult)
428
+
429
+ if (error) {
430
+ throw error
431
+ }
432
+
433
+ if (failedPlugins.size > 0) {
434
+ const errors = [...failedPlugins].map(({ error }) => error)
435
+
436
+ throw new BuildError(`Build Error with ${failedPlugins.size} failed plugins`, { errors })
437
+ }
438
+
439
+ return {
440
+ failedPlugins,
441
+ files,
442
+ driver,
443
+ pluginTimings,
444
+ error: undefined,
445
+ sources,
446
+ }
447
+ }
448
+
449
+ function inputToAdapterSource(config: Config): AdapterSource {
450
+ if (Array.isArray(config.input)) {
451
+ return {
452
+ type: 'paths',
453
+ paths: config.input.map((i) => (new URLPath(i.path).isURL ? i.path : resolve(config.root, i.path))),
454
+ }
455
+ }
456
+
457
+ if ('data' in config.input) {
458
+ return { type: 'data', data: config.input.data }
459
+ }
460
+
461
+ if (new URLPath(config.input.path).isURL) {
462
+ return { type: 'path', path: config.input.path }
463
+ }
464
+
465
+ const resolved = resolve(config.root, config.input.path)
466
+ return { type: 'path', path: resolved }
467
+ }
468
+
469
+ type CreateKubbOptions = {
470
+ hooks?: AsyncEventEmitter<KubbHooks>
471
+ }
472
+
473
+ /**
474
+ * Creates a Kubb instance bound to a single config entry.
475
+ *
476
+ * Accepts a user-facing config shape and resolves it to a full {@link Config} during
477
+ * `setup()`. The instance then holds shared state (`hooks`, `sources`, `driver`, `config`)
478
+ * across the `setup → build` lifecycle. Attach event listeners to `kubb.hooks` before
479
+ * calling `setup()` or `build()`.
480
+ *
481
+ * @example
482
+ * ```ts
483
+ * const kubb = createKubb(userConfig)
484
+ *
485
+ * kubb.hooks.on('kubb:plugin:end', ({ plugin, duration }) => {
486
+ * console.log(`${plugin.name} completed in ${duration}ms`)
487
+ * })
488
+ *
489
+ * const { files, failedPlugins } = await kubb.safeBuild()
490
+ * ```
491
+ */
492
+ export function createKubb(userConfig: UserConfig, options: CreateKubbOptions = {}): Kubb {
493
+ const hooks = options.hooks ?? new AsyncEventEmitter<KubbHooks>()
494
+ let setupResult: SetupResult | undefined
495
+
496
+ const instance: Kubb = {
497
+ get hooks() {
498
+ return hooks
499
+ },
500
+ get sources() {
501
+ return setupResult?.sources ?? new Map()
502
+ },
503
+ get driver() {
504
+ return setupResult?.driver
505
+ },
506
+ get config() {
507
+ return setupResult?.config
508
+ },
509
+ async setup() {
510
+ setupResult = await setup(userConfig, { hooks })
511
+ },
512
+ async build() {
513
+ if (!setupResult) {
514
+ await instance.setup()
515
+ }
516
+ return build(setupResult!)
517
+ },
518
+ async safeBuild() {
519
+ if (!setupResult) {
520
+ await instance.setup()
521
+ }
522
+ return safeBuild(setupResult!)
523
+ },
524
+ }
525
+
526
+ return instance
527
+ }
@@ -0,0 +1,57 @@
1
+ import type { FileNode } from '@kubb/ast'
2
+
3
+ /**
4
+ * Minimal interface any Kubb renderer must satisfy.
5
+ *
6
+ * The generic `TElement` is the type of the element the renderer accepts —
7
+ * e.g. `KubbReactElement` for `@kubb/renderer-jsx`, or a custom type for
8
+ * your own renderer. Defaults to `unknown` so that generators which do not
9
+ * care about the element type continue to work without specifying it.
10
+ *
11
+ * This allows core to drive rendering without a hard dependency on
12
+ * `@kubb/renderer-jsx` or any specific renderer implementation.
13
+ */
14
+ export type Renderer<TElement = unknown> = {
15
+ render(element: TElement): Promise<void>
16
+ unmount(error?: Error | number | null): void
17
+ readonly files: Array<FileNode>
18
+ }
19
+
20
+ /**
21
+ * A factory function that produces a fresh {@link Renderer} per render.
22
+ *
23
+ * Generators use this to declare which renderer handles their output.
24
+ */
25
+ export type RendererFactory<TElement = unknown> = () => Renderer<TElement>
26
+
27
+ /**
28
+ * Creates a renderer factory for use in generator definitions.
29
+ *
30
+ * Wrap your renderer factory function with this helper to register it as the
31
+ * renderer for a generator. Core will call this factory once per render cycle
32
+ * to obtain a fresh renderer instance.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * // packages/renderer-jsx/src/index.ts
37
+ * export const jsxRenderer = createRenderer(() => {
38
+ * const runtime = new Runtime()
39
+ * return {
40
+ * async render(element) { await runtime.render(element) },
41
+ * get files() { return runtime.nodes },
42
+ * unmount(error) { runtime.unmount(error) },
43
+ * }
44
+ * })
45
+ *
46
+ * // packages/plugin-zod/src/generators/zodGenerator.tsx
47
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
48
+ * export const zodGenerator = defineGenerator<PluginZod>({
49
+ * name: 'zod',
50
+ * renderer: jsxRenderer,
51
+ * schema(node, options) { return <File ...>...</File> },
52
+ * })
53
+ * ```
54
+ */
55
+ export function createRenderer<TElement = unknown>(factory: RendererFactory<TElement>): RendererFactory<TElement> {
56
+ return factory
57
+ }
@@ -0,0 +1,58 @@
1
+ export type Storage = {
2
+ /**
3
+ * Identifier used for logging and debugging (e.g. `'fs'`, `'s3'`).
4
+ */
5
+ readonly name: string
6
+ /**
7
+ * Returns `true` when an entry for `key` exists in storage.
8
+ */
9
+ hasItem(key: string): Promise<boolean>
10
+ /**
11
+ * Returns the stored string value, or `null` when `key` does not exist.
12
+ */
13
+ getItem(key: string): Promise<string | null>
14
+ /**
15
+ * Persists `value` under `key`, creating any required structure.
16
+ */
17
+ setItem(key: string, value: string): Promise<void>
18
+ /**
19
+ * Removes the entry for `key`. No-ops when the key does not exist.
20
+ */
21
+ removeItem(key: string): Promise<void>
22
+ /**
23
+ * Returns all keys, optionally filtered to those starting with `base`.
24
+ */
25
+ getKeys(base?: string): Promise<Array<string>>
26
+ /**
27
+ * Removes all entries, optionally scoped to those starting with `base`.
28
+ */
29
+ clear(base?: string): Promise<void>
30
+ /**
31
+ * Optional teardown hook called after the build completes.
32
+ */
33
+ dispose?(): Promise<void>
34
+ }
35
+
36
+ /**
37
+ * Creates a storage factory. Call the returned function with optional options to get the storage instance.
38
+ *
39
+ * @example
40
+ * export const memoryStorage = createStorage(() => {
41
+ * const store = new Map<string, string>()
42
+ * return {
43
+ * name: 'memory',
44
+ * async hasItem(key) { return store.has(key) },
45
+ * async getItem(key) { return store.get(key) ?? null },
46
+ * async setItem(key, value) { store.set(key, value) },
47
+ * async removeItem(key) { store.delete(key) },
48
+ * async getKeys(base) {
49
+ * const keys = [...store.keys()]
50
+ * return base ? keys.filter((k) => k.startsWith(base)) : keys
51
+ * },
52
+ * async clear(base) { if (!base) store.clear() },
53
+ * }
54
+ * })
55
+ */
56
+ export function createStorage<TOptions = Record<string, never>>(build: (options: TOptions) => Storage): (options?: TOptions) => Storage {
57
+ return (options) => build(options ?? ({} as TOptions))
58
+ }