@kubb/plugin-ts 5.0.0-alpha.3 → 5.0.0-alpha.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 (42) hide show
  1. package/dist/index.cjs +1806 -3
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.d.ts +590 -4
  4. package/dist/index.js +1776 -2
  5. package/dist/index.js.map +1 -0
  6. package/package.json +7 -27
  7. package/src/components/Enum.tsx +82 -0
  8. package/src/components/Type.tsx +29 -162
  9. package/src/constants.ts +39 -0
  10. package/src/factory.ts +134 -49
  11. package/src/generators/typeGenerator.tsx +165 -428
  12. package/src/generators/typeGeneratorLegacy.tsx +349 -0
  13. package/src/index.ts +9 -1
  14. package/src/plugin.ts +98 -176
  15. package/src/presets.ts +28 -0
  16. package/src/printers/functionPrinter.ts +196 -0
  17. package/src/printers/printerTs.ts +310 -0
  18. package/src/resolvers/resolverTs.ts +66 -0
  19. package/src/resolvers/resolverTsLegacy.ts +60 -0
  20. package/src/types.ts +258 -98
  21. package/src/utils.ts +131 -0
  22. package/dist/components-CRjwjdyE.js +0 -725
  23. package/dist/components-CRjwjdyE.js.map +0 -1
  24. package/dist/components-DI0aTIBg.cjs +0 -978
  25. package/dist/components-DI0aTIBg.cjs.map +0 -1
  26. package/dist/components.cjs +0 -3
  27. package/dist/components.d.ts +0 -38
  28. package/dist/components.js +0 -2
  29. package/dist/generators.cjs +0 -4
  30. package/dist/generators.d.ts +0 -503
  31. package/dist/generators.js +0 -2
  32. package/dist/plugin-D5rCK1zO.cjs +0 -992
  33. package/dist/plugin-D5rCK1zO.cjs.map +0 -1
  34. package/dist/plugin-DmwgRHK8.js +0 -944
  35. package/dist/plugin-DmwgRHK8.js.map +0 -1
  36. package/dist/types-BpeKGgCn.d.ts +0 -170
  37. package/src/components/index.ts +0 -1
  38. package/src/components/v2/Type.tsx +0 -165
  39. package/src/generators/index.ts +0 -2
  40. package/src/generators/v2/typeGenerator.tsx +0 -196
  41. package/src/parser.ts +0 -396
  42. package/src/printer.ts +0 -244
package/src/printer.ts DELETED
@@ -1,244 +0,0 @@
1
- import { jsStringEscape, stringify } from '@internals/utils'
2
- import { isPlainStringType } from '@kubb/ast'
3
- import type { ArraySchemaNode, SchemaNode } from '@kubb/ast/types'
4
- import type { PrinterFactoryOptions } from '@kubb/core'
5
- import { definePrinter } from '@kubb/core'
6
- import type ts from 'typescript'
7
- import * as factory from './factory.ts'
8
-
9
- type TsOptions = {
10
- /**
11
- * @default `'questionToken'`
12
- */
13
- optionalType: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
14
- /**
15
- * @default `'array'`
16
- */
17
- arrayType: 'array' | 'generic'
18
- /**
19
- * @default `'inlineLiteral'`
20
- */
21
- enumType: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal' | 'inlineLiteral'
22
- }
23
-
24
- type TsPrinter = PrinterFactoryOptions<'typescript', TsOptions, ts.TypeNode>
25
-
26
- function constToTypeNode(value: string | number | boolean, format: 'string' | 'number' | 'boolean'): ts.TypeNode | undefined {
27
- if (format === 'boolean') {
28
- return factory.createLiteralTypeNode(value === true ? factory.createTrue() : factory.createFalse())
29
- }
30
- if (format === 'number' && typeof value === 'number') {
31
- if (value < 0) {
32
- return factory.createLiteralTypeNode(factory.createPrefixUnaryExpression(factory.SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value))))
33
- }
34
- return factory.createLiteralTypeNode(factory.createNumericLiteral(value))
35
- }
36
- return factory.createLiteralTypeNode(factory.createStringLiteral(String(value)))
37
- }
38
-
39
- function dateOrStringNode(node: { representation?: string }): ts.TypeNode {
40
- return node.representation === 'date' ? factory.createTypeReferenceNode(factory.createIdentifier('Date')) : factory.keywordTypeNodes.string
41
- }
42
-
43
- function buildMemberNodes(members: Array<SchemaNode> | undefined, print: (node: SchemaNode) => ts.TypeNode | null | undefined): Array<ts.TypeNode> {
44
- return (members ?? []).map(print).filter(Boolean) as Array<ts.TypeNode>
45
- }
46
-
47
- function buildTupleNode(node: ArraySchemaNode, print: (node: SchemaNode) => ts.TypeNode | null | undefined): ts.TypeNode | undefined {
48
- let items = (node.items ?? []).map(print).filter(Boolean) as Array<ts.TypeNode>
49
-
50
- const restNode = node.rest ? (print(node.rest) ?? undefined) : undefined
51
- const { min, max } = node
52
-
53
- if (max !== undefined) {
54
- items = items.slice(0, max)
55
- if (items.length < max && restNode) {
56
- items = [...items, ...Array(max - items.length).fill(restNode)]
57
- }
58
- }
59
-
60
- if (min !== undefined) {
61
- items = items.map((item, i) => (i >= min ? factory.createOptionalTypeNode(item) : item))
62
- }
63
-
64
- if (max === undefined && restNode) {
65
- items.push(factory.createRestTypeNode(factory.createArrayTypeNode(restNode)))
66
- }
67
-
68
- return factory.createTupleTypeNode(items)
69
- }
70
-
71
- function buildPropertyType(schema: SchemaNode, baseType: ts.TypeNode, optionalType: TsOptions['optionalType']): ts.TypeNode {
72
- const addsUndefined = ['undefined', 'questionTokenAndUndefined'].includes(optionalType)
73
-
74
- let type = baseType
75
-
76
- if (schema.nullable) {
77
- type = factory.createUnionDeclaration({ nodes: [type, factory.keywordTypeNodes.null] }) as ts.TypeNode
78
- }
79
-
80
- if ((schema.nullish || schema.optional) && addsUndefined) {
81
- type = factory.createUnionDeclaration({ nodes: [type, factory.keywordTypeNodes.undefined] }) as ts.TypeNode
82
- }
83
-
84
- return type
85
- }
86
-
87
- function buildPropertyJSDocComments(schema: SchemaNode): Array<string | undefined> {
88
- return [
89
- 'description' in schema && schema.description ? `@description ${jsStringEscape(schema.description as string)}` : undefined,
90
- 'deprecated' in schema && schema.deprecated ? '@deprecated' : undefined,
91
- 'min' in schema && schema.min !== undefined ? `@minLength ${schema.min}` : undefined,
92
- 'max' in schema && schema.max !== undefined ? `@maxLength ${schema.max}` : undefined,
93
- 'pattern' in schema && schema.pattern ? `@pattern ${schema.pattern}` : undefined,
94
- 'default' in schema && schema.default !== undefined
95
- ? `@default ${'primitive' in schema && schema.primitive === 'string' ? stringify(schema.default as string) : schema.default}`
96
- : undefined,
97
- 'example' in schema && schema.example !== undefined ? `@example ${schema.example}` : undefined,
98
- 'primitive' in schema && schema.primitive
99
- ? [`@type ${schema.primitive || 'unknown'}`, 'optional' in schema && schema.optional ? ' | undefined' : undefined].filter(Boolean).join('')
100
- : undefined,
101
- ]
102
- }
103
-
104
- function buildIndexSignatures(
105
- node: { additionalProperties?: SchemaNode | boolean; patternProperties?: Record<string, SchemaNode> },
106
- propertyCount: number,
107
- print: (node: SchemaNode) => ts.TypeNode | null | undefined,
108
- ): Array<ts.TypeElement> {
109
- const elements: Array<ts.TypeElement> = []
110
-
111
- if (node.additionalProperties && node.additionalProperties !== true) {
112
- const additionalType = (print(node.additionalProperties as SchemaNode) ?? factory.keywordTypeNodes.unknown) as ts.TypeNode
113
- elements.push(factory.createIndexSignature(propertyCount > 0 ? factory.keywordTypeNodes.unknown : additionalType))
114
- } else if (node.additionalProperties === true) {
115
- elements.push(factory.createIndexSignature(factory.keywordTypeNodes.unknown))
116
- }
117
-
118
- if (node.patternProperties) {
119
- const first = Object.values(node.patternProperties)[0]
120
- if (first) {
121
- let patternType = (print(first) ?? factory.keywordTypeNodes.unknown) as ts.TypeNode
122
- if (first.nullable) {
123
- patternType = factory.createUnionDeclaration({ nodes: [patternType, factory.keywordTypeNodes.null] }) as ts.TypeNode
124
- }
125
- elements.push(factory.createIndexSignature(patternType))
126
- }
127
- }
128
-
129
- return elements
130
- }
131
-
132
- /**
133
- * Converts a `SchemaNode` AST node into a TypeScript `ts.TypeNode`.
134
- *
135
- * Built on `definePrinter` — dispatches on `node.type`, with options closed over
136
- * per printer instance. Produces the same `ts.TypeNode` output as the keyword-based
137
- * `parse` in `parser.ts`.
138
- */
139
- export const printerTs = definePrinter<TsPrinter>((options) => ({
140
- name: 'typescript',
141
- options,
142
- nodes: {
143
- any: () => factory.keywordTypeNodes.any,
144
- unknown: () => factory.keywordTypeNodes.unknown,
145
- void: () => factory.keywordTypeNodes.void,
146
- boolean: () => factory.keywordTypeNodes.boolean,
147
- null: () => factory.keywordTypeNodes.null,
148
- blob: () => factory.createTypeReferenceNode('Blob', []),
149
- string: () => factory.keywordTypeNodes.string,
150
- uuid: () => factory.keywordTypeNodes.string,
151
- email: () => factory.keywordTypeNodes.string,
152
- url: () => factory.keywordTypeNodes.string,
153
- datetime: () => factory.keywordTypeNodes.string,
154
- number: () => factory.keywordTypeNodes.number,
155
- integer: () => factory.keywordTypeNodes.number,
156
- bigint: () => factory.keywordTypeNodes.bigint,
157
- date: (node) => dateOrStringNode(node),
158
- time: (node) => dateOrStringNode(node),
159
- ref(node) {
160
- if (!node.name) {
161
- return undefined
162
- }
163
- return factory.createTypeReferenceNode(node.name, undefined)
164
- },
165
- enum(node) {
166
- const values = node.namedEnumValues?.map((v) => v.value) ?? node.enumValues ?? []
167
-
168
- if (this.options.enumType === 'inlineLiteral' || !node.name) {
169
- const literalNodes = values
170
- .filter((v): v is string | number | boolean => v !== null)
171
- .map((value) => constToTypeNode(value, typeof value === 'number' ? 'number' : typeof value === 'boolean' ? 'boolean' : 'string'))
172
- .filter(Boolean) as Array<ts.TypeNode>
173
-
174
- return factory.createUnionDeclaration({ withParentheses: true, nodes: literalNodes }) ?? undefined
175
- }
176
-
177
- const typeName = ['asConst', 'asPascalConst'].includes(this.options.enumType) ? `${node.name}Key` : node.name
178
-
179
- return factory.createTypeReferenceNode(typeName, undefined)
180
- },
181
- union(node) {
182
- const members = node.members ?? []
183
-
184
- const hasStringLiteral = members.some((m) => m.type === 'enum' && m.enumType === 'string')
185
- const hasPlainString = members.some((m) => isPlainStringType(m))
186
-
187
- if (hasStringLiteral && hasPlainString) {
188
- const nodes = members
189
- .map((m) => {
190
- if (isPlainStringType(m)) {
191
- return factory.createIntersectionDeclaration({
192
- nodes: [factory.keywordTypeNodes.string, factory.createTypeLiteralNode([])],
193
- withParentheses: true,
194
- }) as ts.TypeNode
195
- }
196
-
197
- return this.print(m)
198
- })
199
- .filter(Boolean) as Array<ts.TypeNode>
200
-
201
- return factory.createUnionDeclaration({ withParentheses: true, nodes }) ?? undefined
202
- }
203
-
204
- return factory.createUnionDeclaration({ withParentheses: true, nodes: buildMemberNodes(members, this.print) }) ?? undefined
205
- },
206
- intersection(node) {
207
- return factory.createIntersectionDeclaration({ withParentheses: true, nodes: buildMemberNodes(node.members, this.print) }) ?? undefined
208
- },
209
- array(node) {
210
- const itemNodes = (node.items ?? []).map((item) => this.print(item)).filter(Boolean) as Array<ts.TypeNode>
211
-
212
- return factory.createArrayDeclaration({ nodes: itemNodes, arrayType: this.options.arrayType }) ?? undefined
213
- },
214
- tuple(node) {
215
- return buildTupleNode(node, this.print)
216
- },
217
- object(node) {
218
- const addsQuestionToken = ['questionToken', 'questionTokenAndUndefined'].includes(this.options.optionalType)
219
- const { print } = this
220
-
221
- const propertyNodes: Array<ts.TypeElement> = node.properties.map((prop) => {
222
- const baseType = (print(prop.schema) ?? factory.keywordTypeNodes.unknown) as ts.TypeNode
223
- const type = buildPropertyType(prop.schema, baseType, this.options.optionalType)
224
-
225
- const propertyNode = factory.createPropertySignature({
226
- questionToken: prop.schema.optional || prop.schema.nullish ? addsQuestionToken : false,
227
- name: prop.name,
228
- type,
229
- readOnly: prop.schema.readOnly,
230
- })
231
-
232
- return factory.appendJSDocToNode({ node: propertyNode, comments: buildPropertyJSDocComments(prop.schema) })
233
- })
234
-
235
- const allElements = [...propertyNodes, ...buildIndexSignatures(node, propertyNodes.length, print)]
236
-
237
- if (!allElements.length) {
238
- return factory.keywordTypeNodes.object
239
- }
240
-
241
- return factory.createTypeLiteralNode(allElements)
242
- },
243
- },
244
- }))