@kubb/plugin-ts 5.0.0-alpha.2 → 5.0.0-alpha.21

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 (76) hide show
  1. package/dist/Type-B6fo0gSk.js +120 -0
  2. package/dist/Type-B6fo0gSk.js.map +1 -0
  3. package/dist/Type-oFwUfkZv.cjs +131 -0
  4. package/dist/Type-oFwUfkZv.cjs.map +1 -0
  5. package/dist/builderTs-Cd3juc2G.cjs +120 -0
  6. package/dist/builderTs-Cd3juc2G.cjs.map +1 -0
  7. package/dist/builderTs-DausqHpc.js +116 -0
  8. package/dist/builderTs-DausqHpc.js.map +1 -0
  9. package/dist/builders.cjs +3 -0
  10. package/dist/builders.d.ts +8 -0
  11. package/dist/builders.js +2 -0
  12. package/dist/casing-BJHFg-zZ.js +84 -0
  13. package/dist/casing-BJHFg-zZ.js.map +1 -0
  14. package/dist/casing-DHfdqpLi.cjs +107 -0
  15. package/dist/casing-DHfdqpLi.cjs.map +1 -0
  16. package/dist/chunk-ByKO4r7w.cjs +38 -0
  17. package/dist/components.cjs +3 -2
  18. package/dist/components.d.ts +40 -11
  19. package/dist/components.js +2 -2
  20. package/dist/generators-ByK18qUn.js +551 -0
  21. package/dist/generators-ByK18qUn.js.map +1 -0
  22. package/dist/generators-aSsiTfUO.cjs +563 -0
  23. package/dist/generators-aSsiTfUO.cjs.map +1 -0
  24. package/dist/generators.cjs +3 -2
  25. package/dist/generators.d.ts +7 -492
  26. package/dist/generators.js +2 -2
  27. package/dist/index.cjs +148 -3
  28. package/dist/index.cjs.map +1 -0
  29. package/dist/index.d.ts +1 -1
  30. package/dist/index.js +146 -1
  31. package/dist/index.js.map +1 -0
  32. package/dist/printerTs-BgZucv4T.js +559 -0
  33. package/dist/printerTs-BgZucv4T.js.map +1 -0
  34. package/dist/printerTs-CFXc_LpP.cjs +595 -0
  35. package/dist/printerTs-CFXc_LpP.cjs.map +1 -0
  36. package/dist/printers.cjs +3 -0
  37. package/dist/printers.d.ts +75 -0
  38. package/dist/printers.js +2 -0
  39. package/dist/resolverTsLegacy-DLl854-P.js +185 -0
  40. package/dist/resolverTsLegacy-DLl854-P.js.map +1 -0
  41. package/dist/resolverTsLegacy-sJ16Iqrl.cjs +196 -0
  42. package/dist/resolverTsLegacy-sJ16Iqrl.cjs.map +1 -0
  43. package/dist/resolvers.cjs +4 -0
  44. package/dist/resolvers.d.ts +52 -0
  45. package/dist/resolvers.js +2 -0
  46. package/dist/types-BcyuFDn9.d.ts +344 -0
  47. package/package.json +27 -8
  48. package/src/builders/builderTs.ts +92 -0
  49. package/src/builders/index.ts +1 -0
  50. package/src/components/Enum.tsx +83 -0
  51. package/src/components/Type.tsx +24 -145
  52. package/src/components/index.ts +1 -0
  53. package/src/constants.ts +29 -0
  54. package/src/factory.ts +14 -48
  55. package/src/generators/index.ts +1 -0
  56. package/src/generators/typeGenerator.tsx +119 -403
  57. package/src/generators/typeGeneratorLegacy.tsx +345 -0
  58. package/src/plugin.ts +80 -122
  59. package/src/presets.ts +26 -0
  60. package/src/printers/index.ts +1 -0
  61. package/src/printers/printerTs.ts +389 -0
  62. package/src/resolvers/index.ts +2 -0
  63. package/src/resolvers/resolverTs.ts +107 -0
  64. package/src/resolvers/resolverTsLegacy.ts +87 -0
  65. package/src/types.ts +261 -72
  66. package/dist/components-9wydyqUx.cjs +0 -848
  67. package/dist/components-9wydyqUx.cjs.map +0 -1
  68. package/dist/components-LmqJfxMv.js +0 -721
  69. package/dist/components-LmqJfxMv.js.map +0 -1
  70. package/dist/plugin-CNkzbtpl.cjs +0 -508
  71. package/dist/plugin-CNkzbtpl.cjs.map +0 -1
  72. package/dist/plugin-DoLrDl9P.js +0 -476
  73. package/dist/plugin-DoLrDl9P.js.map +0 -1
  74. package/dist/types-BpeKGgCn.d.ts +0 -170
  75. package/src/parser.ts +0 -396
  76. package/src/printer.ts +0 -221
@@ -1,170 +0,0 @@
1
- import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { Group, Output, PluginFactoryOptions, ResolveNameParams } from "@kubb/core";
3
- import { Exclude, Include, Override, ResolvePathOptions } from "@kubb/plugin-oas";
4
- import { Generator } from "@kubb/plugin-oas/generators";
5
- import ts from "typescript";
6
- import { Oas, contentType } from "@kubb/oas";
7
-
8
- //#region src/types.d.ts
9
- type Options = {
10
- /**
11
- * Specify the export location for the files and define the behavior of the output
12
- * @default { path: 'types', barrelType: 'named' }
13
- */
14
- output?: Output<Oas>;
15
- /**
16
- * Define which contentType should be used.
17
- * By default, uses the first valid JSON media type.
18
- */
19
- contentType?: contentType;
20
- /**
21
- * Group the clients based on the provided name.
22
- */
23
- group?: Group;
24
- /**
25
- * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
26
- */
27
- exclude?: Array<Exclude>;
28
- /**
29
- * Array containing include parameters to include tags/operations/methods/paths.
30
- */
31
- include?: Array<Include>;
32
- /**
33
- * Array containing override parameters to override `options` based on tags/operations/methods/paths.
34
- */
35
- override?: Array<Override<ResolvedOptions>>;
36
- /**
37
- * Choose to use enum, asConst, asPascalConst, constEnum, literal, or inlineLiteral for enums.
38
- * - 'enum' generates TypeScript enum declarations.
39
- * - 'asConst' generates const objects with camelCase names and as const assertion.
40
- * - 'asPascalConst' generates const objects with PascalCase names and as const assertion.
41
- * - 'constEnum' generates TypeScript const enum declarations.
42
- * - 'literal' generates literal union types.
43
- * - 'inlineLiteral' inline enum values directly into the type (default in v5).
44
- * @default 'asConst'
45
- * @note In Kubb v5, 'inlineLiteral' becomes the default.
46
- */
47
- enumType?: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal' | 'inlineLiteral';
48
- /**
49
- * Choose the casing for enum key names.
50
- * - 'screamingSnakeCase' generates keys in SCREAMING_SNAKE_CASE format.
51
- * - 'snakeCase' generates keys in snake_case format.
52
- * - 'pascalCase' generates keys in PascalCase format.
53
- * - 'camelCase' generates keys in camelCase format.
54
- * - 'none' uses the enum value as-is without transformation.
55
- * @default 'none'
56
- */
57
- enumKeyCasing?: 'screamingSnakeCase' | 'snakeCase' | 'pascalCase' | 'camelCase' | 'none';
58
- /**
59
- * Switch between type or interface for creating TypeScript types.
60
- * - 'type' generates type alias declarations.
61
- * - 'interface' generates interface declarations.
62
- * @default 'type'
63
- */
64
- syntaxType?: 'type' | 'interface';
65
- /**
66
- * Set a suffix for the generated enums.
67
- * @default 'enum'
68
- */
69
- enumSuffix?: string;
70
- /**
71
- * Choose to use date or datetime as JavaScript Date instead of string.
72
- * - 'string' represents dates as string values.
73
- * - 'date' represents dates as JavaScript Date objects.
74
- * @default 'string'
75
- */
76
- dateType?: 'string' | 'date';
77
- /**
78
- * Choose to use `number` or `bigint` for integer fields with `int64` format.
79
- * - 'number' uses the TypeScript `number` type (matches JSON.parse() runtime behavior).
80
- * - 'bigint' uses the TypeScript `bigint` type (accurate for values exceeding Number.MAX_SAFE_INTEGER).
81
- * @note in v5 of Kubb 'bigint' will become the default to better align with OpenAPI's int64 specification.
82
- * @default 'number'
83
- */
84
- integerType?: 'number' | 'bigint';
85
- /**
86
- * Which type to use when the Swagger/OpenAPI file is not providing more information.
87
- * - 'any' allows any value.
88
- * - 'unknown' requires type narrowing before use.
89
- * - 'void' represents no value.
90
- * @default 'any'
91
- */
92
- unknownType?: 'any' | 'unknown' | 'void';
93
- /**
94
- * Which type to use for empty schema values.
95
- * - 'any' allows any value.
96
- * - 'unknown' requires type narrowing before use.
97
- * - 'void' represents no value.
98
- * @default `unknownType`
99
- */
100
- emptySchemaType?: 'any' | 'unknown' | 'void';
101
- /**
102
- * Choose what to use as mode for an optional value.
103
- * - 'questionToken' marks the property as optional with ? (e.g., type?: string).
104
- * - 'undefined' adds undefined to the type union (e.g., type: string | undefined).
105
- * - 'questionTokenAndUndefined' combines both approaches (e.g., type?: string | undefined).
106
- * @default 'questionToken'
107
- */
108
- optionalType?: 'questionToken' | 'undefined' | 'questionTokenAndUndefined';
109
- /**
110
- * Choose between Array<string> or string[] for array types.
111
- * - 'generic' generates Array<Type> syntax.
112
- * - 'array' generates Type[] syntax.
113
- * @default 'array'
114
- */
115
- arrayType?: 'generic' | 'array';
116
- transformers?: {
117
- /**
118
- * Customize the names based on the type that is provided by the plugin.
119
- */
120
- name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
121
- };
122
- /**
123
- * @example
124
- * Use https://ts-ast-viewer.com to generate factory code(see createPropertySignature)
125
- * category: factory.createPropertySignature(
126
- * undefined,
127
- * factory.createIdentifier("category"),
128
- * factory.createToken(ts.SyntaxKind.QuestionToken),
129
- * factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
130
- * )
131
- */
132
- mapper?: Record<string, ts.PropertySignature>;
133
- /**
134
- * How to style your params, by default no casing is applied
135
- * - 'camelcase' uses camelCase for pathParams, queryParams and headerParams property names
136
- * @default undefined
137
- * @note response types (data/body) are NOT affected by this option
138
- */
139
- paramsCasing?: 'camelcase';
140
- /**
141
- * Define some generators next to the ts generators
142
- */
143
- generators?: Array<Generator<PluginTs>>;
144
- /**
145
- * Unstable naming for v5
146
- */
147
- UNSTABLE_NAMING?: true;
148
- };
149
- type ResolvedOptions = {
150
- output: Output<Oas>;
151
- group: Options['group'];
152
- override: NonNullable<Options['override']>;
153
- enumType: NonNullable<Options['enumType']>;
154
- enumKeyCasing: NonNullable<Options['enumKeyCasing']>;
155
- enumSuffix: NonNullable<Options['enumSuffix']>;
156
- dateType: NonNullable<Options['dateType']>;
157
- integerType: NonNullable<Options['integerType']>;
158
- unknownType: NonNullable<Options['unknownType']>;
159
- emptySchemaType: NonNullable<Options['emptySchemaType']>;
160
- optionalType: NonNullable<Options['optionalType']>;
161
- arrayType: NonNullable<Options['arrayType']>;
162
- transformers: NonNullable<Options['transformers']>;
163
- syntaxType: NonNullable<Options['syntaxType']>;
164
- mapper: Record<string, any>;
165
- paramsCasing: Options['paramsCasing'];
166
- };
167
- type PluginTs = PluginFactoryOptions<'plugin-ts', Options, ResolvedOptions, never, ResolvePathOptions>;
168
- //#endregion
169
- export { PluginTs as n, Options as t };
170
- //# sourceMappingURL=types-BpeKGgCn.d.ts.map
package/src/parser.ts DELETED
@@ -1,396 +0,0 @@
1
- import { jsStringEscape } from '@internals/utils'
2
- import type { SchemaKeywordMapper, SchemaMapper } from '@kubb/plugin-oas'
3
- import { createParser, isKeyword, schemaKeywords } from '@kubb/plugin-oas'
4
- import type ts from 'typescript'
5
- import * as factory from './factory.ts'
6
-
7
- export const typeKeywordMapper = {
8
- any: () => factory.keywordTypeNodes.any,
9
- unknown: () => factory.keywordTypeNodes.unknown,
10
- void: () => factory.keywordTypeNodes.void,
11
- number: () => factory.keywordTypeNodes.number,
12
- integer: () => factory.keywordTypeNodes.number,
13
- bigint: () => factory.keywordTypeNodes.bigint,
14
- object: (nodes?: ts.TypeElement[]) => {
15
- if (!nodes || !nodes.length) {
16
- return factory.keywordTypeNodes.object
17
- }
18
-
19
- return factory.createTypeLiteralNode(nodes)
20
- },
21
- string: () => factory.keywordTypeNodes.string,
22
- boolean: () => factory.keywordTypeNodes.boolean,
23
- undefined: () => factory.keywordTypeNodes.undefined,
24
- nullable: undefined,
25
- null: () => factory.keywordTypeNodes.null,
26
- nullish: undefined,
27
- array: (nodes?: ts.TypeNode[], arrayType?: 'array' | 'generic') => {
28
- if (!nodes) {
29
- return undefined
30
- }
31
-
32
- return factory.createArrayDeclaration({ nodes, arrayType })
33
- },
34
- tuple: (nodes?: ts.TypeNode[], rest?: ts.TypeNode, min?: number, max?: number) => {
35
- if (!nodes) {
36
- return undefined
37
- }
38
-
39
- if (max) {
40
- nodes = nodes.slice(0, max)
41
-
42
- if (nodes.length < max && rest) {
43
- nodes = [...nodes, ...Array(max - nodes.length).fill(rest)]
44
- }
45
- }
46
-
47
- if (min) {
48
- nodes = nodes.map((node, index) => (index >= min ? factory.createOptionalTypeNode(node) : node))
49
- }
50
-
51
- if (typeof max === 'undefined' && rest) {
52
- nodes.push(factory.createRestTypeNode(factory.createArrayTypeNode(rest)))
53
- }
54
-
55
- return factory.createTupleTypeNode(nodes)
56
- },
57
- enum: (name?: string) => {
58
- if (!name) {
59
- return undefined
60
- }
61
-
62
- return factory.createTypeReferenceNode(name, undefined)
63
- },
64
- union: (nodes?: ts.TypeNode[]) => {
65
- if (!nodes) {
66
- return undefined
67
- }
68
-
69
- return factory.createUnionDeclaration({
70
- withParentheses: true,
71
- nodes,
72
- })
73
- },
74
- const: (name?: string | number | boolean, format?: 'string' | 'number' | 'boolean') => {
75
- if (name === null || name === undefined || name === '') {
76
- return undefined
77
- }
78
-
79
- if (format === 'boolean') {
80
- if (name === true) {
81
- return factory.createLiteralTypeNode(factory.createTrue())
82
- }
83
-
84
- return factory.createLiteralTypeNode(factory.createFalse())
85
- }
86
-
87
- if (format === 'number' && typeof name === 'number') {
88
- return factory.createLiteralTypeNode(factory.createNumericLiteral(name))
89
- }
90
-
91
- return factory.createLiteralTypeNode(factory.createStringLiteral(name.toString()))
92
- },
93
- datetime: () => factory.keywordTypeNodes.string,
94
- date: (type: 'date' | 'string' = 'string') =>
95
- type === 'string' ? factory.keywordTypeNodes.string : factory.createTypeReferenceNode(factory.createIdentifier('Date')),
96
- time: (type: 'date' | 'string' = 'string') =>
97
- type === 'string' ? factory.keywordTypeNodes.string : factory.createTypeReferenceNode(factory.createIdentifier('Date')),
98
- uuid: () => factory.keywordTypeNodes.string,
99
- url: () => factory.keywordTypeNodes.string,
100
- default: undefined,
101
- and: (nodes?: ts.TypeNode[]) => {
102
- if (!nodes) {
103
- return undefined
104
- }
105
-
106
- return factory.createIntersectionDeclaration({
107
- withParentheses: true,
108
- nodes,
109
- })
110
- },
111
- describe: undefined,
112
- min: undefined,
113
- max: undefined,
114
- optional: undefined,
115
- matches: () => factory.keywordTypeNodes.string,
116
- email: () => factory.keywordTypeNodes.string,
117
- firstName: undefined,
118
- lastName: undefined,
119
- password: undefined,
120
- phone: undefined,
121
- readOnly: undefined,
122
- writeOnly: undefined,
123
- ref: (propertyName?: string) => {
124
- if (!propertyName) {
125
- return undefined
126
- }
127
-
128
- return factory.createTypeReferenceNode(propertyName, undefined)
129
- },
130
- blob: () => factory.createTypeReferenceNode('Blob', []),
131
- deprecated: undefined,
132
- example: undefined,
133
- schema: undefined,
134
- catchall: undefined,
135
- name: undefined,
136
- interface: undefined,
137
- exclusiveMaximum: undefined,
138
- exclusiveMinimum: undefined,
139
- } satisfies SchemaMapper<ts.TypeNode | null | undefined>
140
-
141
- type ParserOptions = {
142
- /**
143
- * @default `'questionToken'`
144
- */
145
- optionalType: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
146
- /**
147
- * @default `'array'`
148
- */
149
- arrayType: 'array' | 'generic'
150
- /**
151
- * Choose to use `enum`, `asConst`, `asPascalConst`, `constEnum`, `literal`, or `inlineLiteral` for enums.
152
- * - `enum`: TypeScript enum
153
- * - `asConst`: const with camelCase name (e.g., `petType`)
154
- * - `asPascalConst`: const with PascalCase name (e.g., `PetType`)
155
- * - `constEnum`: const enum
156
- * - `literal`: literal union type
157
- * - `inlineLiteral`: inline enum values directly into the type (default in v5)
158
- * @default `'asConst'`
159
- * @note In Kubb v5, `inlineLiteral` becomes the default.
160
- */
161
- enumType: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal' | 'inlineLiteral'
162
- mapper?: Record<string, ts.PropertySignature>
163
- }
164
-
165
- /**
166
- * Recursively parses a schema tree node into a corresponding TypeScript AST node.
167
- *
168
- * Maps OpenAPI schema keywords to TypeScript AST nodes using the `typeKeywordMapper`, handling complex types such as unions, intersections, arrays, tuples (with optional/rest elements and length constraints), enums, constants, references, and objects with property modifiers and documentation annotations.
169
- *
170
- * @param current - The schema node to parse.
171
- * @param siblings - Sibling schema nodes, used for context in certain mappings.
172
- * @param name - The name of the schema or property being parsed.
173
- * @param options - Parsing options controlling output style, property handling, and custom mappers.
174
- * @returns The generated TypeScript AST node, or `undefined` if the schema keyword is not mapped.
175
- */
176
- export const parse = createParser<ts.Node | null, ParserOptions>({
177
- mapper: typeKeywordMapper,
178
- handlers: {
179
- union(tree, options) {
180
- const { current, schema, name } = tree
181
-
182
- return typeKeywordMapper.union(
183
- current.args.map((it) => this.parse({ schema, parent: current, name, current: it, siblings: [] }, options)).filter(Boolean) as ts.TypeNode[],
184
- )
185
- },
186
- and(tree, options) {
187
- const { current, schema, name } = tree
188
-
189
- return typeKeywordMapper.and(
190
- current.args.map((it) => this.parse({ schema, parent: current, name, current: it, siblings: [] }, options)).filter(Boolean) as ts.TypeNode[],
191
- )
192
- },
193
- array(tree, options) {
194
- const { current, schema, name } = tree
195
-
196
- return typeKeywordMapper.array(
197
- current.args.items.map((it) => this.parse({ schema, parent: current, name, current: it, siblings: [] }, options)).filter(Boolean) as ts.TypeNode[],
198
- options.arrayType,
199
- )
200
- },
201
- enum(tree, options) {
202
- const { current } = tree
203
-
204
- // If enumType is 'inlineLiteral', generate the literal union inline instead of a type reference
205
- if (options.enumType === 'inlineLiteral') {
206
- const enumValues = current.args.items
207
- .map((item) => item.value)
208
- .filter((value): value is string | number | boolean => value !== undefined && value !== null)
209
- .map((value) => {
210
- const format = typeof value === 'number' ? 'number' : typeof value === 'boolean' ? 'boolean' : 'string'
211
- return typeKeywordMapper.const(value, format)
212
- })
213
- .filter(Boolean) as ts.TypeNode[]
214
-
215
- return typeKeywordMapper.union(enumValues)
216
- }
217
-
218
- // Adding suffix to enum (see https://github.com/kubb-labs/kubb/issues/1873)
219
- return typeKeywordMapper.enum(['asConst', 'asPascalConst'].includes(options.enumType) ? `${current.args.typeName}Key` : current.args.typeName)
220
- },
221
- ref(tree, _options) {
222
- const { current } = tree
223
-
224
- return typeKeywordMapper.ref(current.args.name)
225
- },
226
- blob() {
227
- return typeKeywordMapper.blob()
228
- },
229
- tuple(tree, options) {
230
- const { current, schema, name } = tree
231
-
232
- return typeKeywordMapper.tuple(
233
- current.args.items.map((it) => this.parse({ schema, parent: current, name, current: it, siblings: [] }, options)).filter(Boolean) as ts.TypeNode[],
234
- current.args.rest &&
235
- ((this.parse({ schema, parent: current, name, current: current.args.rest, siblings: [] }, options) ?? undefined) as ts.TypeNode | undefined),
236
- current.args.min,
237
- current.args.max,
238
- )
239
- },
240
- const(tree, _options) {
241
- const { current } = tree
242
-
243
- return typeKeywordMapper.const(current.args.name, current.args.format)
244
- },
245
- object(tree, options) {
246
- const { current, schema, name } = tree
247
-
248
- const properties = Object.entries(current.args?.properties || {})
249
- .filter((item) => {
250
- const schemas = item[1]
251
- return schemas && typeof schemas.map === 'function'
252
- })
253
- .map(([name, schemas]) => {
254
- const nameSchema = schemas.find((schema) => schema.keyword === schemaKeywords.name) as SchemaKeywordMapper['name']
255
- const mappedName = nameSchema?.args || name
256
-
257
- // custom mapper(pluginOptions)
258
- // Use Object.hasOwn to avoid matching inherited properties like 'toString', 'valueOf', etc.
259
- if (options.mapper && Object.hasOwn(options.mapper, mappedName)) {
260
- return options.mapper[mappedName]
261
- }
262
-
263
- const isNullish = schemas.some((schema) => schema.keyword === schemaKeywords.nullish)
264
- const isNullable = schemas.some((schema) => schema.keyword === schemaKeywords.nullable)
265
- const isOptional = schemas.some((schema) => schema.keyword === schemaKeywords.optional)
266
- const isReadonly = schemas.some((schema) => schema.keyword === schemaKeywords.readOnly)
267
- const describeSchema = schemas.find((schema) => schema.keyword === schemaKeywords.describe) as SchemaKeywordMapper['describe'] | undefined
268
- const deprecatedSchema = schemas.find((schema) => schema.keyword === schemaKeywords.deprecated) as SchemaKeywordMapper['deprecated'] | undefined
269
- const defaultSchema = schemas.find((schema) => schema.keyword === schemaKeywords.default) as SchemaKeywordMapper['default'] | undefined
270
- const exampleSchema = schemas.find((schema) => schema.keyword === schemaKeywords.example) as SchemaKeywordMapper['example'] | undefined
271
- const schemaSchema = schemas.find((schema) => schema.keyword === schemaKeywords.schema) as SchemaKeywordMapper['schema'] | undefined
272
- const minSchema = schemas.find((schema) => schema.keyword === schemaKeywords.min) as SchemaKeywordMapper['min'] | undefined
273
- const maxSchema = schemas.find((schema) => schema.keyword === schemaKeywords.max) as SchemaKeywordMapper['max'] | undefined
274
- const matchesSchema = schemas.find((schema) => schema.keyword === schemaKeywords.matches) as SchemaKeywordMapper['matches'] | undefined
275
-
276
- let type = schemas
277
- .map((it) =>
278
- this.parse(
279
- {
280
- schema,
281
- parent: current,
282
- name,
283
- current: it,
284
- siblings: schemas,
285
- },
286
- options,
287
- ),
288
- )
289
- .filter(Boolean)[0] as ts.TypeNode
290
-
291
- if (isNullable) {
292
- type = factory.createUnionDeclaration({
293
- nodes: [type, factory.keywordTypeNodes.null],
294
- }) as ts.TypeNode
295
- }
296
-
297
- if (isNullish && ['undefined', 'questionTokenAndUndefined'].includes(options.optionalType as string)) {
298
- type = factory.createUnionDeclaration({
299
- nodes: [type, factory.keywordTypeNodes.undefined],
300
- }) as ts.TypeNode
301
- }
302
-
303
- if (isOptional && ['undefined', 'questionTokenAndUndefined'].includes(options.optionalType as string)) {
304
- type = factory.createUnionDeclaration({
305
- nodes: [type, factory.keywordTypeNodes.undefined],
306
- }) as ts.TypeNode
307
- }
308
-
309
- const propertyNode = factory.createPropertySignature({
310
- questionToken: isOptional || isNullish ? ['questionToken', 'questionTokenAndUndefined'].includes(options.optionalType as string) : false,
311
- name: mappedName,
312
- type,
313
- readOnly: isReadonly,
314
- })
315
-
316
- return factory.appendJSDocToNode({
317
- node: propertyNode,
318
- comments: [
319
- describeSchema ? `@description ${jsStringEscape(describeSchema.args)}` : undefined,
320
- deprecatedSchema ? '@deprecated' : undefined,
321
- minSchema ? `@minLength ${minSchema.args}` : undefined,
322
- maxSchema ? `@maxLength ${maxSchema.args}` : undefined,
323
- matchesSchema ? `@pattern ${matchesSchema.args}` : undefined,
324
- defaultSchema ? `@default ${defaultSchema.args}` : undefined,
325
- exampleSchema ? `@example ${exampleSchema.args}` : undefined,
326
- schemaSchema?.args?.type || schemaSchema?.args?.format
327
- ? [`@type ${schemaSchema?.args?.type || 'unknown'}${!isOptional ? '' : ' | undefined'}`, schemaSchema?.args?.format].filter(Boolean).join(', ')
328
- : undefined,
329
- ].filter(Boolean),
330
- })
331
- })
332
-
333
- let additionalProperties: any
334
-
335
- if (current.args?.additionalProperties?.length) {
336
- let additionalPropertiesType = current.args.additionalProperties
337
- .map((it) => this.parse({ schema, parent: current, name, current: it, siblings: [] }, options))
338
- .filter(Boolean)
339
- .at(0) as ts.TypeNode
340
-
341
- const isNullable = current.args?.additionalProperties.some((schema) => isKeyword(schema, schemaKeywords.nullable))
342
- if (isNullable) {
343
- additionalPropertiesType = factory.createUnionDeclaration({
344
- nodes: [additionalPropertiesType, factory.keywordTypeNodes.null],
345
- }) as ts.TypeNode
346
- }
347
-
348
- // When there are typed properties alongside additionalProperties, use 'unknown' type
349
- // for the index signature to avoid TS2411 errors (index signature type conflicts with property types).
350
- // This occurs commonly in QueryParams where some params are typed (enums, objects) and
351
- // others are dynamic (additionalProperties with explode=true).
352
- const hasTypedProperties = properties.length > 0
353
- const indexSignatureType = hasTypedProperties ? factory.keywordTypeNodes.unknown : additionalPropertiesType
354
-
355
- additionalProperties = factory.createIndexSignature(indexSignatureType)
356
- }
357
-
358
- let patternProperties: ts.TypeNode | ts.IndexSignatureDeclaration | undefined
359
-
360
- if (current.args?.patternProperties) {
361
- const allPatternSchemas = Object.values(current.args.patternProperties).flat()
362
-
363
- if (allPatternSchemas.length > 0) {
364
- patternProperties = allPatternSchemas
365
- .map((it) => this.parse({ schema, parent: current, name, current: it, siblings: [] }, options))
366
- .filter(Boolean)
367
- .at(0) as ts.TypeNode
368
-
369
- const isNullable = allPatternSchemas.some((schema) => isKeyword(schema, schemaKeywords.nullable))
370
- if (isNullable) {
371
- patternProperties = factory.createUnionDeclaration({
372
- nodes: [patternProperties, factory.keywordTypeNodes.null],
373
- }) as ts.TypeNode
374
- }
375
-
376
- patternProperties = factory.createIndexSignature(patternProperties)
377
- }
378
- }
379
-
380
- return typeKeywordMapper.object([...properties, additionalProperties, patternProperties].filter(Boolean))
381
- },
382
- datetime() {
383
- return typeKeywordMapper.datetime()
384
- },
385
- date(tree) {
386
- const { current } = tree
387
-
388
- return typeKeywordMapper.date(current.args.type)
389
- },
390
- time(tree) {
391
- const { current } = tree
392
-
393
- return typeKeywordMapper.time(current.args.type)
394
- },
395
- },
396
- })