@kubb/ast 5.0.0-alpha.2 → 5.0.0-alpha.20

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.
package/src/printer.ts DELETED
@@ -1,129 +0,0 @@
1
- import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'
2
-
3
- /**
4
- * Handler context for `definePrinter` — mirrors `PluginContext` from `@kubb/core`.
5
- * Available as `this` inside each node handler.
6
- */
7
- export type PrinterHandlerContext<TOutput, TOptions extends object> = {
8
- /**
9
- * Recursively print a nested `SchemaNode`.
10
- */
11
- print: (node: SchemaNode) => TOutput | null | undefined
12
- /**
13
- * Options for this printer instance.
14
- */
15
- options: TOptions
16
- }
17
-
18
- /**
19
- * Handler for a specific `SchemaNode` variant identified by `SchemaType` key `T`.
20
- * Use a regular function (not an arrow function) so that `this` is available.
21
- */
22
- export type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
23
- this: PrinterHandlerContext<TOutput, TOptions>,
24
- node: SchemaNodeByType[T],
25
- ) => TOutput | null | undefined
26
-
27
- /**
28
- * Shape of the type parameter passed to `definePrinter`.
29
- * Mirrors `AdapterFactoryOptions` / `PluginFactoryOptions` from `@kubb/core`.
30
- *
31
- * - `TName` — unique string identifier (e.g. `'zod'`, `'ts'`)
32
- * - `TOptions` — options passed to and stored on the printer
33
- * - `TOutput` — the type emitted by `print` (typically `string`)
34
- */
35
- export type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown> = {
36
- name: TName
37
- options: TOptions
38
- output: TOutput
39
- }
40
-
41
- /**
42
- * The object returned by calling a `definePrinter` instance.
43
- * Mirrors the shape of `Adapter` from `@kubb/core`.
44
- */
45
- export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
46
- /**
47
- * Unique identifier supplied at creation time.
48
- */
49
- name: T['name']
50
- /**
51
- * Options for this printer instance.
52
- */
53
- options: T['options']
54
- /**
55
- * Emits `TOutput` from a `SchemaNode`. Returns `null | undefined` when no handler matches.
56
- */
57
- print: (node: SchemaNode) => T['output'] | null | undefined
58
- /**
59
- * Maps `print` over an array of `SchemaNode`s.
60
- */
61
- for: (nodes: Array<SchemaNode>) => Array<T['output'] | null | undefined>
62
- }
63
-
64
- type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
65
- name: T['name']
66
- /**
67
- * Options to store on the printer.
68
- */
69
- options: T['options']
70
- nodes: Partial<{
71
- [K in SchemaType]: PrinterHandler<T['output'], T['options'], K>
72
- }>
73
- }
74
-
75
- /**
76
- * Creates a named printer factory. Mirrors the `definePlugin` / `defineAdapter` pattern
77
- * from `@kubb/core` — wraps a builder to make options optional and separates raw options
78
- * from resolved options.
79
- *
80
- * @example
81
- * ```ts
82
- * type ZodPrinter = PrinterFactoryOptions<'zod', { strict?: boolean }, { strict: boolean }, string>
83
- *
84
- * export const zodPrinter = definePrinter<ZodPrinter>((options) => {
85
- * const { strict = true } = options
86
- * return {
87
- * name: 'zod',
88
- * options: { strict },
89
- * nodes: {
90
- * string(node) {
91
- * return `z.string()`
92
- * },
93
- * object(node) {
94
- * const props = node.properties
95
- * ?.map(p => `${p.name}: ${this.print(p)}`)
96
- * .join(', ') ?? ''
97
- * return `z.object({ ${props} })`
98
- * },
99
- * },
100
- * }
101
- * })
102
- *
103
- * const printer = zodPrinter({ strict: false })
104
- * printer.name // 'zod'
105
- * printer.options // { strict: false }
106
- * printer.print(node) // 'z.string()'
107
- * ```
108
- */
109
- export function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {
110
- return (options) => {
111
- const { name, options: resolvedOptions, nodes } = build(options ?? ({} as T['options']))
112
-
113
- const context: PrinterHandlerContext<T['output'], T['options']> = {
114
- options: resolvedOptions,
115
- print: (node: SchemaNode) => {
116
- const type = node.type as SchemaType
117
- const handler = nodes[type]
118
- return handler ? (handler as PrinterHandler<T['output'], T['options']>).call(context, node as SchemaNodeByType[SchemaType]) : undefined
119
- },
120
- }
121
-
122
- return {
123
- name,
124
- options: resolvedOptions,
125
- print: context.print,
126
- for: (nodes) => nodes.map(context.print),
127
- }
128
- }
129
- }