@kubb/plugin-oas 4.10.0 → 4.11.0

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 (47) hide show
  1. package/dist/{SchemaGenerator-C2Rqz6y8.cjs → SchemaGenerator-BEyE5yFD.cjs} +6 -6
  2. package/dist/{SchemaGenerator-C2Rqz6y8.cjs.map → SchemaGenerator-BEyE5yFD.cjs.map} +1 -1
  3. package/dist/{SchemaGenerator-g4VcV8ci.js → SchemaGenerator-Bzj3I5Yv.js} +6 -6
  4. package/dist/{SchemaGenerator-g4VcV8ci.js.map → SchemaGenerator-Bzj3I5Yv.js.map} +1 -1
  5. package/dist/{SchemaMapper-BUV8vhg0.cjs → SchemaMapper-BN2pgCUs.cjs} +1 -1
  6. package/dist/{SchemaMapper-BUV8vhg0.cjs.map → SchemaMapper-BN2pgCUs.cjs.map} +1 -1
  7. package/dist/{SchemaMapper-GAIl5QsJ.js → SchemaMapper-eCHsqfmg.js} +1 -1
  8. package/dist/{SchemaMapper-GAIl5QsJ.js.map → SchemaMapper-eCHsqfmg.js.map} +1 -1
  9. package/dist/{createGenerator-DTwdwprw.d.ts → createGenerator-CD_IVg6x.d.ts} +5 -5
  10. package/dist/{createGenerator-BK8WKARk.d.cts → createGenerator-ChN9hQ6p.d.cts} +5 -5
  11. package/dist/{generators--9_lTBjl.cjs → generators--YV0NNUJ.cjs} +2 -2
  12. package/dist/{generators--9_lTBjl.cjs.map → generators--YV0NNUJ.cjs.map} +1 -1
  13. package/dist/{generators-BoBhVZzp.js → generators-Cw71XBIe.js} +2 -2
  14. package/dist/{generators-BoBhVZzp.js.map → generators-Cw71XBIe.js.map} +1 -1
  15. package/dist/generators.cjs +1 -2
  16. package/dist/generators.d.cts +1 -1
  17. package/dist/generators.d.ts +1 -1
  18. package/dist/generators.js +1 -2
  19. package/dist/{getFooter-fhkUhUdk.js → getFooter-C2vyr3vj.js} +2 -2
  20. package/dist/{getFooter-fhkUhUdk.js.map → getFooter-C2vyr3vj.js.map} +1 -1
  21. package/dist/{getFooter-BWck5e2D.cjs → getFooter-D-b_lr3d.cjs} +2 -2
  22. package/dist/{getFooter-BWck5e2D.cjs.map → getFooter-D-b_lr3d.cjs.map} +1 -1
  23. package/dist/{getSchemas-BTCpbjet.cjs → getSchemas-DdrlFGPi.cjs} +1 -1
  24. package/dist/{getSchemas-BTCpbjet.cjs.map → getSchemas-DdrlFGPi.cjs.map} +1 -1
  25. package/dist/{getSchemas-CyMcV4aw.js → getSchemas-XWPSn2uf.js} +1 -1
  26. package/dist/{getSchemas-CyMcV4aw.js.map → getSchemas-XWPSn2uf.js.map} +1 -1
  27. package/dist/hooks.cjs +2 -3
  28. package/dist/hooks.cjs.map +1 -1
  29. package/dist/hooks.d.cts +1 -1
  30. package/dist/hooks.d.ts +1 -1
  31. package/dist/hooks.js +2 -3
  32. package/dist/hooks.js.map +1 -1
  33. package/dist/index.cjs +74 -6
  34. package/dist/index.cjs.map +1 -1
  35. package/dist/index.d.cts +100 -2
  36. package/dist/index.d.ts +100 -2
  37. package/dist/index.js +73 -7
  38. package/dist/index.js.map +1 -1
  39. package/dist/mocks.cjs +1 -1
  40. package/dist/mocks.js +1 -1
  41. package/dist/utils.cjs +3 -3
  42. package/dist/utils.d.cts +1 -1
  43. package/dist/utils.d.ts +1 -1
  44. package/dist/utils.js +3 -3
  45. package/package.json +13 -13
  46. package/src/createParser.ts +141 -0
  47. package/src/index.ts +2 -0
@@ -0,0 +1,141 @@
1
+ import { SchemaGenerator } from './SchemaGenerator.ts'
2
+ import type { Schema, SchemaKeywordMapper, SchemaMapper, SchemaTree } from './SchemaMapper.ts'
3
+ import { schemaKeywords } from './SchemaMapper.ts'
4
+
5
+ /**
6
+ * Handler context with parse method available via `this`
7
+ */
8
+ export type HandlerContext<TOutput, TOptions> = {
9
+ parse: (tree: SchemaTree, options: TOptions) => TOutput | null | undefined
10
+ }
11
+
12
+ /**
13
+ * Handler function type for custom keyword processing
14
+ * Handlers can access the parse function via `this.parse`
15
+ */
16
+ export type KeywordHandler<TOutput, TOptions> = (this: HandlerContext<TOutput, TOptions>, tree: SchemaTree, options: TOptions) => TOutput | null | undefined
17
+
18
+ /**
19
+ * Configuration for createParser
20
+ */
21
+ export type CreateParserConfig<TOutput, TOptions> = {
22
+ /**
23
+ * The keyword mapper that maps schema keywords to output generators
24
+ */
25
+ mapper: SchemaMapper<TOutput>
26
+
27
+ /**
28
+ * Custom handlers for specific schema keywords
29
+ * These provide the implementation for complex types that need special processing
30
+ *
31
+ * Use function syntax (not arrow functions) to enable use of `this` keyword:
32
+ * ```typescript
33
+ * handlers: {
34
+ * enum(tree, options, parse) {
35
+ * // Implementation
36
+ * }
37
+ * }
38
+ * ```
39
+ *
40
+ * Common keywords that typically need handlers:
41
+ * - union: Combine multiple schemas into a union
42
+ * - and: Combine multiple schemas into an intersection
43
+ * - array: Handle array types with items
44
+ * - object: Handle object types with properties
45
+ * - enum: Handle enum types
46
+ * - tuple: Handle tuple types
47
+ * - const: Handle literal/const types
48
+ * - ref: Handle references to other schemas
49
+ * - string/number/integer: Handle primitives with constraints (min/max)
50
+ * - matches: Handle regex patterns
51
+ * - default/describe/optional/nullable: Handle modifiers
52
+ */
53
+ handlers: Partial<{
54
+ [K in keyof SchemaKeywordMapper]: KeywordHandler<TOutput, TOptions>
55
+ }>
56
+ }
57
+
58
+ /**
59
+ * Creates a parser function that converts schema trees to output using the provided mapper and handlers
60
+ *
61
+ * This function provides a framework for building parsers by:
62
+ * 1. Checking for custom handlers for each keyword
63
+ * 2. Falling back to the mapper for simple keywords
64
+ * 3. Providing utilities for common operations (finding siblings, etc.)
65
+ *
66
+ * The generated parser is recursive and can handle nested schemas.
67
+ *
68
+ * @template TOutput - The output type (e.g., string for Zod/Faker, ts.TypeNode for TypeScript)
69
+ * @template TOptions - The parser options type
70
+ * @param config - Configuration object containing mapper and handlers
71
+ * @returns A parse function that converts SchemaTree to TOutput
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * // Create a simple string-based parser
76
+ * const parse = createParser({
77
+ * mapper: zodKeywordMapper,
78
+ * handlers: {
79
+ * union(tree, options) {
80
+ * const items = tree.current.args
81
+ * .map(it => this.parse({ ...tree, current: it }, options))
82
+ * .filter(Boolean)
83
+ * return `z.union([${items.join(', ')}])`
84
+ * },
85
+ * string(tree, options) {
86
+ * const minSchema = findSchemaKeyword(tree.siblings, 'min')
87
+ * const maxSchema = findSchemaKeyword(tree.siblings, 'max')
88
+ * return zodKeywordMapper.string(false, minSchema?.args, maxSchema?.args)
89
+ * }
90
+ * }
91
+ * })
92
+ * ```
93
+ */
94
+ export function createParser<TOutput, TOptions extends Record<string, any>>(
95
+ config: CreateParserConfig<TOutput, TOptions>,
96
+ ): (tree: SchemaTree, options: TOptions) => TOutput | null | undefined {
97
+ const { mapper, handlers } = config
98
+
99
+ function parse(tree: SchemaTree, options: TOptions): TOutput | null | undefined {
100
+ const { current } = tree
101
+
102
+ // Check if there's a custom handler for this keyword
103
+ const handler = handlers[current.keyword as keyof SchemaKeywordMapper]
104
+ if (handler) {
105
+ // Create context object with parse method accessible via `this`
106
+ const context: HandlerContext<TOutput, TOptions> = { parse }
107
+ return handler.call(context, tree, options)
108
+ }
109
+
110
+ // Fall back to simple mapper lookup
111
+ const value = mapper[current.keyword as keyof typeof mapper]
112
+
113
+ if (!value) {
114
+ return undefined
115
+ }
116
+
117
+ // For simple keywords without args, call the mapper directly
118
+ if (current.keyword in mapper) {
119
+ return value()
120
+ }
121
+
122
+ return undefined
123
+ }
124
+
125
+ return parse
126
+ }
127
+
128
+ /**
129
+ * Helper to find a schema keyword in siblings
130
+ * Useful in handlers when you need to find related schemas (e.g., min/max for string)
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * const minSchema = findSchemaKeyword(tree.siblings, 'min')
135
+ * const maxSchema = findSchemaKeyword(tree.siblings, 'max')
136
+ * return zodKeywordMapper.string(false, minSchema?.args, maxSchema?.args)
137
+ * ```
138
+ */
139
+ export function findSchemaKeyword<K extends keyof SchemaKeywordMapper>(siblings: Schema[], keyword: K): SchemaKeywordMapper[K] | undefined {
140
+ return SchemaGenerator.find(siblings, schemaKeywords[keyword]) as SchemaKeywordMapper[K] | undefined
141
+ }
package/src/index.ts CHANGED
@@ -3,6 +3,8 @@ import { createGenerator as _createGenerator } from './generators/createGenerato
3
3
  import { createReactGenerator as _createReactGenerator } from './generators/createReactGenerator.ts'
4
4
  import type { Generator as _Generator } from './generators/types.ts'
5
5
 
6
+ export type { CreateParserConfig, KeywordHandler } from './createParser.ts'
7
+ export { createParser, findSchemaKeyword } from './createParser.ts'
6
8
  export type { OperationMethodResult } from './OperationGenerator.ts'
7
9
  export { OperationGenerator } from './OperationGenerator.ts'
8
10
  export { pluginOas, pluginOasName } from './plugin.ts'