@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.
- package/dist/index.cjs +1806 -3
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +590 -4
- package/dist/index.js +1776 -2
- package/dist/index.js.map +1 -0
- package/package.json +7 -27
- package/src/components/Enum.tsx +82 -0
- package/src/components/Type.tsx +29 -162
- package/src/constants.ts +39 -0
- package/src/factory.ts +134 -49
- package/src/generators/typeGenerator.tsx +165 -428
- package/src/generators/typeGeneratorLegacy.tsx +349 -0
- package/src/index.ts +9 -1
- package/src/plugin.ts +98 -176
- package/src/presets.ts +28 -0
- package/src/printers/functionPrinter.ts +196 -0
- package/src/printers/printerTs.ts +310 -0
- package/src/resolvers/resolverTs.ts +66 -0
- package/src/resolvers/resolverTsLegacy.ts +60 -0
- package/src/types.ts +258 -98
- package/src/utils.ts +131 -0
- package/dist/components-CRjwjdyE.js +0 -725
- package/dist/components-CRjwjdyE.js.map +0 -1
- package/dist/components-DI0aTIBg.cjs +0 -978
- package/dist/components-DI0aTIBg.cjs.map +0 -1
- package/dist/components.cjs +0 -3
- package/dist/components.d.ts +0 -38
- package/dist/components.js +0 -2
- package/dist/generators.cjs +0 -4
- package/dist/generators.d.ts +0 -503
- package/dist/generators.js +0 -2
- package/dist/plugin-D5rCK1zO.cjs +0 -992
- package/dist/plugin-D5rCK1zO.cjs.map +0 -1
- package/dist/plugin-DmwgRHK8.js +0 -944
- package/dist/plugin-DmwgRHK8.js.map +0 -1
- package/dist/types-BpeKGgCn.d.ts +0 -170
- package/src/components/index.ts +0 -1
- package/src/components/v2/Type.tsx +0 -165
- package/src/generators/index.ts +0 -2
- package/src/generators/v2/typeGenerator.tsx +0 -196
- package/src/parser.ts +0 -396
- package/src/printer.ts +0 -244
package/src/types.ts
CHANGED
|
@@ -1,24 +1,209 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { OperationParamsResolver } from '@kubb/ast'
|
|
2
|
+
import type { OperationNode, ParameterNode, StatusCode, Visitor } from '@kubb/ast/types'
|
|
3
|
+
import type {
|
|
4
|
+
CompatibilityPreset,
|
|
5
|
+
Exclude,
|
|
6
|
+
Generator,
|
|
7
|
+
Group,
|
|
8
|
+
Include,
|
|
9
|
+
Output,
|
|
10
|
+
Override,
|
|
11
|
+
PluginFactoryOptions,
|
|
12
|
+
ResolvePathOptions,
|
|
13
|
+
Resolver,
|
|
14
|
+
UserGroup,
|
|
15
|
+
} from '@kubb/core'
|
|
16
|
+
import type { PrinterTsNodes } from './printers/printerTs.ts'
|
|
17
|
+
/**
|
|
18
|
+
* The concrete resolver type for `@kubb/plugin-ts`.
|
|
19
|
+
* Extends the base `Resolver` (which provides `default` and `resolveOptions`) with
|
|
20
|
+
* plugin-specific naming helpers for operations, parameters, responses, and schemas.
|
|
21
|
+
*/
|
|
22
|
+
export type ResolverTs = Resolver &
|
|
23
|
+
OperationParamsResolver & {
|
|
24
|
+
/**
|
|
25
|
+
* Resolves the name for a given raw name (equivalent to `default(name, 'function')`).
|
|
26
|
+
* Since TypeScript only emits types, this is the canonical naming method.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* resolver.resolveName('list pets status 200') // → 'ListPetsStatus200'
|
|
30
|
+
*/
|
|
31
|
+
resolveTypeName(name: string): string
|
|
32
|
+
/**
|
|
33
|
+
* Resolves the file/path name for a given identifier using PascalCase.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* resolver.resolvePathName('list pets', 'file') // → 'ListPets'
|
|
37
|
+
*/
|
|
38
|
+
resolvePathName(name: string, type?: 'file' | 'function' | 'type' | 'const'): string
|
|
39
|
+
/**
|
|
40
|
+
* Resolves the request body type name for an operation (required on ResolverTs).
|
|
41
|
+
*/
|
|
42
|
+
resolveDataName(node: OperationNode): string
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Resolves the name for an operation response by status code.
|
|
46
|
+
* Encapsulates the `<operationId> Status <statusCode>` template with PascalCase applied to the result.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* resolver.resolveResponseStatusName(node, 200) // → 'ListPetsStatus200'
|
|
50
|
+
*/
|
|
51
|
+
resolveResponseStatusName(node: OperationNode, statusCode: StatusCode): string
|
|
52
|
+
/**
|
|
53
|
+
* Resolves the name for an operation's request config (`RequestConfig`).
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* resolver.resolveRequestConfigName(node) // → 'ListPetsRequestConfig'
|
|
57
|
+
*/
|
|
58
|
+
resolveRequestConfigName(node: OperationNode): string
|
|
59
|
+
/**
|
|
60
|
+
* Resolves the name for the collection of all operation responses (`Responses`).
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* resolver.resolveResponsesName(node) // → 'ListPetsResponses'
|
|
64
|
+
*/
|
|
65
|
+
resolveResponsesName(node: OperationNode): string
|
|
66
|
+
/**
|
|
67
|
+
* Resolves the name for the union of all operation responses (`Response`).
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* resolver.resolveResponseName(node) // → 'ListPetsResponse'
|
|
71
|
+
*/
|
|
72
|
+
resolveResponseName(node: OperationNode): string
|
|
73
|
+
/**
|
|
74
|
+
* Resolves the TypeScript type alias name for an enum schema's key variant.
|
|
75
|
+
* Appends `enumTypeSuffix` (default `'Key'`) after applying the default naming convention.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* resolver.resolveEnumKeyName(node, 'Key') // → 'PetStatusKey'
|
|
79
|
+
* resolver.resolveEnumKeyName(node, 'Value') // → 'PetStatusValue'
|
|
80
|
+
* resolver.resolveEnumKeyName(node, '') // → 'PetStatus'
|
|
81
|
+
*/
|
|
82
|
+
resolveEnumKeyName(node: { name?: string | null }, enumTypeSuffix: string): string
|
|
83
|
+
/**
|
|
84
|
+
* Resolves the name for an operation's grouped path parameters type.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* resolver.resolvePathParamsName(node, param) // → 'GetPetByIdPathParams'
|
|
88
|
+
*/
|
|
89
|
+
resolvePathParamsName(node: OperationNode, param: ParameterNode): string
|
|
90
|
+
/**
|
|
91
|
+
* Resolves the name for an operation's grouped query parameters type.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'
|
|
95
|
+
*/
|
|
96
|
+
resolveQueryParamsName(node: OperationNode, param: ParameterNode): string
|
|
97
|
+
/**
|
|
98
|
+
* Resolves the name for an operation's grouped header parameters type.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'
|
|
102
|
+
*/
|
|
103
|
+
resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
type EnumKeyCasing = 'screamingSnakeCase' | 'snakeCase' | 'pascalCase' | 'camelCase' | 'none'
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Discriminated union that ties `enumTypeSuffix` and `enumKeyCasing` to the enum types that actually use them.
|
|
110
|
+
*
|
|
111
|
+
* - `'asConst'` / `'asPascalConst'` — emit a `const` object; both `enumTypeSuffix` (type-alias suffix) and
|
|
112
|
+
* `enumKeyCasing` (key formatting) are meaningful.
|
|
113
|
+
* - `'enum'` / `'constEnum'` — emit a TypeScript enum; `enumKeyCasing` applies to member names,
|
|
114
|
+
* but there is no separate type alias so `enumTypeSuffix` is not used.
|
|
115
|
+
* - `'literal'` / `'inlineLiteral'` — emit only union literals; keys are discarded entirely,
|
|
116
|
+
* so neither `enumTypeSuffix` nor `enumKeyCasing` have any effect.
|
|
117
|
+
*/
|
|
118
|
+
type EnumTypeOptions =
|
|
119
|
+
| {
|
|
120
|
+
/**
|
|
121
|
+
* Choose to use enum, asConst, asPascalConst, constEnum, literal, or inlineLiteral for enums.
|
|
122
|
+
* - 'asConst' generates const objects with camelCase names and as const assertion.
|
|
123
|
+
* - 'asPascalConst' generates const objects with PascalCase names and as const assertion.
|
|
124
|
+
* @default 'asConst'
|
|
125
|
+
*/
|
|
126
|
+
enumType?: 'asConst' | 'asPascalConst'
|
|
127
|
+
/**
|
|
128
|
+
* Suffix appended to the generated type alias name.
|
|
129
|
+
*
|
|
130
|
+
* Only affects the type alias — the const object name is unchanged.
|
|
131
|
+
*
|
|
132
|
+
* @default 'Key'
|
|
133
|
+
* @example enumTypeSuffix: 'Value' → `export type PetStatusValue = …`
|
|
134
|
+
*/
|
|
135
|
+
enumTypeSuffix?: string
|
|
136
|
+
/**
|
|
137
|
+
* Choose the casing for enum key names.
|
|
138
|
+
* - 'screamingSnakeCase' generates keys in SCREAMING_SNAKE_CASE format.
|
|
139
|
+
* - 'snakeCase' generates keys in snake_case format.
|
|
140
|
+
* - 'pascalCase' generates keys in PascalCase format.
|
|
141
|
+
* - 'camelCase' generates keys in camelCase format.
|
|
142
|
+
* - 'none' uses the enum value as-is without transformation.
|
|
143
|
+
* @default 'none'
|
|
144
|
+
*/
|
|
145
|
+
enumKeyCasing?: EnumKeyCasing
|
|
146
|
+
}
|
|
147
|
+
| {
|
|
148
|
+
/**
|
|
149
|
+
* Choose to use enum, asConst, asPascalConst, constEnum, literal, or inlineLiteral for enums.
|
|
150
|
+
* - 'enum' generates TypeScript enum declarations.
|
|
151
|
+
* - 'constEnum' generates TypeScript const enum declarations.
|
|
152
|
+
* @default 'asConst'
|
|
153
|
+
*/
|
|
154
|
+
enumType?: 'enum' | 'constEnum'
|
|
155
|
+
/**
|
|
156
|
+
* `enumTypeSuffix` has no effect for this `enumType`.
|
|
157
|
+
* It is only used when `enumType` is `'asConst'` or `'asPascalConst'`.
|
|
158
|
+
*/
|
|
159
|
+
enumTypeSuffix?: never
|
|
160
|
+
/**
|
|
161
|
+
* Choose the casing for enum key names.
|
|
162
|
+
* - 'screamingSnakeCase' generates keys in SCREAMING_SNAKE_CASE format.
|
|
163
|
+
* - 'snakeCase' generates keys in snake_case format.
|
|
164
|
+
* - 'pascalCase' generates keys in PascalCase format.
|
|
165
|
+
* - 'camelCase' generates keys in camelCase format.
|
|
166
|
+
* - 'none' uses the enum value as-is without transformation.
|
|
167
|
+
* @default 'none'
|
|
168
|
+
*/
|
|
169
|
+
enumKeyCasing?: EnumKeyCasing
|
|
170
|
+
}
|
|
171
|
+
| {
|
|
172
|
+
/**
|
|
173
|
+
* Choose to use enum, asConst, asPascalConst, constEnum, literal, or inlineLiteral for enums.
|
|
174
|
+
* - 'literal' generates literal union types.
|
|
175
|
+
* - 'inlineLiteral' will inline enum values directly into the type (default in v5).
|
|
176
|
+
* @default 'asConst'
|
|
177
|
+
* @note In Kubb v5, 'inlineLiteral' becomes the default.
|
|
178
|
+
*/
|
|
179
|
+
enumType?: 'literal' | 'inlineLiteral'
|
|
180
|
+
/**
|
|
181
|
+
* `enumTypeSuffix` has no effect for this `enumType`.
|
|
182
|
+
* It is only used when `enumType` is `'asConst'` or `'asPascalConst'`.
|
|
183
|
+
*/
|
|
184
|
+
enumTypeSuffix?: never
|
|
185
|
+
/**
|
|
186
|
+
* `enumKeyCasing` has no effect for this `enumType`.
|
|
187
|
+
* Literal and inlineLiteral modes emit only values — keys are discarded entirely.
|
|
188
|
+
*/
|
|
189
|
+
enumKeyCasing?: never
|
|
190
|
+
}
|
|
6
191
|
|
|
7
192
|
export type Options = {
|
|
8
193
|
/**
|
|
9
194
|
* Specify the export location for the files and define the behavior of the output
|
|
10
195
|
* @default { path: 'types', barrelType: 'named' }
|
|
11
196
|
*/
|
|
12
|
-
output?: Output
|
|
197
|
+
output?: Output
|
|
13
198
|
/**
|
|
14
199
|
* Define which contentType should be used.
|
|
15
200
|
* By default, uses the first valid JSON media type.
|
|
16
201
|
*/
|
|
17
|
-
contentType?:
|
|
202
|
+
contentType?: 'application/json' | (string & {})
|
|
18
203
|
/**
|
|
19
204
|
* Group the clients based on the provided name.
|
|
20
205
|
*/
|
|
21
|
-
group?:
|
|
206
|
+
group?: UserGroup
|
|
22
207
|
/**
|
|
23
208
|
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
24
209
|
*/
|
|
@@ -31,28 +216,6 @@ export type Options = {
|
|
|
31
216
|
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
32
217
|
*/
|
|
33
218
|
override?: Array<Override<ResolvedOptions>>
|
|
34
|
-
/**
|
|
35
|
-
* Choose to use enum, asConst, asPascalConst, constEnum, literal, or inlineLiteral for enums.
|
|
36
|
-
* - 'enum' generates TypeScript enum declarations.
|
|
37
|
-
* - 'asConst' generates const objects with camelCase names and as const assertion.
|
|
38
|
-
* - 'asPascalConst' generates const objects with PascalCase names and as const assertion.
|
|
39
|
-
* - 'constEnum' generates TypeScript const enum declarations.
|
|
40
|
-
* - 'literal' generates literal union types.
|
|
41
|
-
* - 'inlineLiteral' inline enum values directly into the type (default in v5).
|
|
42
|
-
* @default 'asConst'
|
|
43
|
-
* @note In Kubb v5, 'inlineLiteral' becomes the default.
|
|
44
|
-
*/
|
|
45
|
-
enumType?: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal' | 'inlineLiteral'
|
|
46
|
-
/**
|
|
47
|
-
* Choose the casing for enum key names.
|
|
48
|
-
* - 'screamingSnakeCase' generates keys in SCREAMING_SNAKE_CASE format.
|
|
49
|
-
* - 'snakeCase' generates keys in snake_case format.
|
|
50
|
-
* - 'pascalCase' generates keys in PascalCase format.
|
|
51
|
-
* - 'camelCase' generates keys in camelCase format.
|
|
52
|
-
* - 'none' uses the enum value as-is without transformation.
|
|
53
|
-
* @default 'none'
|
|
54
|
-
*/
|
|
55
|
-
enumKeyCasing?: 'screamingSnakeCase' | 'snakeCase' | 'pascalCase' | 'camelCase' | 'none'
|
|
56
219
|
/**
|
|
57
220
|
* Switch between type or interface for creating TypeScript types.
|
|
58
221
|
* - 'type' generates type alias declarations.
|
|
@@ -60,42 +223,6 @@ export type Options = {
|
|
|
60
223
|
* @default 'type'
|
|
61
224
|
*/
|
|
62
225
|
syntaxType?: 'type' | 'interface'
|
|
63
|
-
/**
|
|
64
|
-
* Set a suffix for the generated enums.
|
|
65
|
-
* @default 'enum'
|
|
66
|
-
*/
|
|
67
|
-
enumSuffix?: string
|
|
68
|
-
/**
|
|
69
|
-
* Choose to use date or datetime as JavaScript Date instead of string.
|
|
70
|
-
* - 'string' represents dates as string values.
|
|
71
|
-
* - 'date' represents dates as JavaScript Date objects.
|
|
72
|
-
* @default 'string'
|
|
73
|
-
*/
|
|
74
|
-
dateType?: 'string' | 'date'
|
|
75
|
-
/**
|
|
76
|
-
* Choose to use `number` or `bigint` for integer fields with `int64` format.
|
|
77
|
-
* - 'number' uses the TypeScript `number` type (matches JSON.parse() runtime behavior).
|
|
78
|
-
* - 'bigint' uses the TypeScript `bigint` type (accurate for values exceeding Number.MAX_SAFE_INTEGER).
|
|
79
|
-
* @note in v5 of Kubb 'bigint' will become the default to better align with OpenAPI's int64 specification.
|
|
80
|
-
* @default 'number'
|
|
81
|
-
*/
|
|
82
|
-
integerType?: 'number' | 'bigint'
|
|
83
|
-
/**
|
|
84
|
-
* Which type to use when the Swagger/OpenAPI file is not providing more information.
|
|
85
|
-
* - 'any' allows any value.
|
|
86
|
-
* - 'unknown' requires type narrowing before use.
|
|
87
|
-
* - 'void' represents no value.
|
|
88
|
-
* @default 'any'
|
|
89
|
-
*/
|
|
90
|
-
unknownType?: 'any' | 'unknown' | 'void'
|
|
91
|
-
/**
|
|
92
|
-
* Which type to use for empty schema values.
|
|
93
|
-
* - 'any' allows any value.
|
|
94
|
-
* - 'unknown' requires type narrowing before use.
|
|
95
|
-
* - 'void' represents no value.
|
|
96
|
-
* @default `unknownType`
|
|
97
|
-
*/
|
|
98
|
-
emptySchemaType?: 'any' | 'unknown' | 'void'
|
|
99
226
|
/**
|
|
100
227
|
* Choose what to use as mode for an optional value.
|
|
101
228
|
* - 'questionToken' marks the property as optional with ? (e.g., type?: string).
|
|
@@ -111,23 +238,6 @@ export type Options = {
|
|
|
111
238
|
* @default 'array'
|
|
112
239
|
*/
|
|
113
240
|
arrayType?: 'generic' | 'array'
|
|
114
|
-
transformers?: {
|
|
115
|
-
/**
|
|
116
|
-
* Customize the names based on the type that is provided by the plugin.
|
|
117
|
-
*/
|
|
118
|
-
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* @example
|
|
122
|
-
* Use https://ts-ast-viewer.com to generate factory code(see createPropertySignature)
|
|
123
|
-
* category: factory.createPropertySignature(
|
|
124
|
-
* undefined,
|
|
125
|
-
* factory.createIdentifier("category"),
|
|
126
|
-
* factory.createToken(ts.SyntaxKind.QuestionToken),
|
|
127
|
-
* factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
|
|
128
|
-
* )
|
|
129
|
-
*/
|
|
130
|
-
mapper?: Record<string, ts.PropertySignature>
|
|
131
241
|
/**
|
|
132
242
|
* How to style your params, by default no casing is applied
|
|
133
243
|
* - 'camelcase' uses camelCase for pathParams, queryParams and headerParams property names
|
|
@@ -140,28 +250,78 @@ export type Options = {
|
|
|
140
250
|
*/
|
|
141
251
|
generators?: Array<Generator<PluginTs>>
|
|
142
252
|
/**
|
|
143
|
-
*
|
|
253
|
+
* Apply a compatibility naming preset.
|
|
254
|
+
* Use `kubbV4` for strict v4 type-generation compatibility.
|
|
255
|
+
* You can further customize naming with `resolvers`.
|
|
256
|
+
* @default 'default'
|
|
144
257
|
*/
|
|
145
|
-
|
|
146
|
-
|
|
258
|
+
compatibilityPreset?: CompatibilityPreset
|
|
259
|
+
/**
|
|
260
|
+
* Override naming conventions. When a method returns `null` or `undefined`, the preset
|
|
261
|
+
* resolver (`resolverTs` / `resolverTsLegacy`) is used as fallback.
|
|
262
|
+
*/
|
|
263
|
+
resolver?: Partial<ResolverTs> & ThisType<ResolverTs>
|
|
264
|
+
/**
|
|
265
|
+
* AST visitor applied to each schema/operation node before printing.
|
|
266
|
+
* Returning `null` or `undefined` from a visitor method falls back to the preset transformer.
|
|
267
|
+
*
|
|
268
|
+
* @example Remove writeOnly properties from response types
|
|
269
|
+
* ```ts
|
|
270
|
+
* transformer: {
|
|
271
|
+
* property(node) {
|
|
272
|
+
* if (node.schema.writeOnly) return undefined
|
|
273
|
+
* }
|
|
274
|
+
* }
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
transformer?: Visitor
|
|
278
|
+
/**
|
|
279
|
+
* Override individual printer node handlers to customize rendering of specific schema types.
|
|
280
|
+
*
|
|
281
|
+
* Each key is a `SchemaType` (e.g. `'date'`, `'string'`). The function replaces the
|
|
282
|
+
* built-in handler for that type. Use `this.transform` to recurse into nested schema nodes.
|
|
283
|
+
*
|
|
284
|
+
* @example Override the `date` node to use the `Date` object type
|
|
285
|
+
* ```ts
|
|
286
|
+
* import ts from 'typescript'
|
|
287
|
+
* pluginTs({
|
|
288
|
+
* printer: {
|
|
289
|
+
* nodes: {
|
|
290
|
+
* date(node) {
|
|
291
|
+
* return ts.factory.createTypeReferenceNode('Date', [])
|
|
292
|
+
* },
|
|
293
|
+
* },
|
|
294
|
+
* },
|
|
295
|
+
* })
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
298
|
+
printer?: {
|
|
299
|
+
nodes?: PrinterTsNodes
|
|
300
|
+
}
|
|
301
|
+
} & EnumTypeOptions
|
|
147
302
|
|
|
148
303
|
type ResolvedOptions = {
|
|
149
|
-
output: Output
|
|
150
|
-
|
|
151
|
-
|
|
304
|
+
output: Output
|
|
305
|
+
exclude: Array<Exclude>
|
|
306
|
+
include: Array<Include> | undefined
|
|
307
|
+
override: Array<Override<ResolvedOptions>>
|
|
308
|
+
group: Group | undefined
|
|
152
309
|
enumType: NonNullable<Options['enumType']>
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
dateType: NonNullable<Options['dateType']>
|
|
156
|
-
integerType: NonNullable<Options['integerType']>
|
|
157
|
-
unknownType: NonNullable<Options['unknownType']>
|
|
158
|
-
emptySchemaType: NonNullable<Options['emptySchemaType']>
|
|
310
|
+
enumTypeSuffix: NonNullable<Options['enumTypeSuffix']>
|
|
311
|
+
enumKeyCasing: EnumKeyCasing
|
|
159
312
|
optionalType: NonNullable<Options['optionalType']>
|
|
160
313
|
arrayType: NonNullable<Options['arrayType']>
|
|
161
|
-
transformers: NonNullable<Options['transformers']>
|
|
162
314
|
syntaxType: NonNullable<Options['syntaxType']>
|
|
163
|
-
mapper: Record<string, any>
|
|
164
315
|
paramsCasing: Options['paramsCasing']
|
|
316
|
+
printer: Options['printer']
|
|
165
317
|
}
|
|
166
318
|
|
|
167
|
-
export type PluginTs = PluginFactoryOptions<'plugin-ts', Options, ResolvedOptions, never, ResolvePathOptions>
|
|
319
|
+
export type PluginTs = PluginFactoryOptions<'plugin-ts', Options, ResolvedOptions, never, ResolvePathOptions, ResolverTs>
|
|
320
|
+
|
|
321
|
+
declare global {
|
|
322
|
+
namespace Kubb {
|
|
323
|
+
interface PluginRegistry {
|
|
324
|
+
'plugin-ts': PluginTs
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { jsStringEscape, stringify } from '@internals/utils'
|
|
2
|
+
import { createProperty, createSchema, syncSchemaRef } from '@kubb/ast'
|
|
3
|
+
import type { OperationNode, ParameterNode, SchemaNode } from '@kubb/ast/types'
|
|
4
|
+
import type { ResolverTs } from './types.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Collects JSDoc annotation strings for a schema node.
|
|
8
|
+
*
|
|
9
|
+
* Only uses official JSDoc tags from https://jsdoc.app/: `@description`, `@deprecated`, `@default`, `@example`, `@type`.
|
|
10
|
+
* Constraint metadata (min/max length, pattern, multipleOf, min/maxProperties) is emitted as plain-text lines.
|
|
11
|
+
|
|
12
|
+
*/
|
|
13
|
+
export function buildPropertyJSDocComments(schema: SchemaNode): Array<string | undefined> {
|
|
14
|
+
const meta = syncSchemaRef(schema)
|
|
15
|
+
|
|
16
|
+
const isArray = meta?.primitive === 'array'
|
|
17
|
+
|
|
18
|
+
return [
|
|
19
|
+
meta && 'description' in meta && meta.description ? `@description ${jsStringEscape(meta.description)}` : undefined,
|
|
20
|
+
meta && 'deprecated' in meta && meta.deprecated ? '@deprecated' : undefined,
|
|
21
|
+
// minItems/maxItems on arrays should not be emitted as @minLength/@maxLength
|
|
22
|
+
!isArray && meta && 'min' in meta && meta.min !== undefined ? `@minLength ${meta.min}` : undefined,
|
|
23
|
+
!isArray && meta && 'max' in meta && meta.max !== undefined ? `@maxLength ${meta.max}` : undefined,
|
|
24
|
+
meta && 'pattern' in meta && meta.pattern ? `@pattern ${meta.pattern}` : undefined,
|
|
25
|
+
meta && 'default' in meta && meta.default !== undefined
|
|
26
|
+
? `@default ${'primitive' in meta && meta.primitive === 'string' ? stringify(meta.default as string) : meta.default}`
|
|
27
|
+
: undefined,
|
|
28
|
+
meta && 'example' in meta && meta.example !== undefined ? `@example ${meta.example}` : undefined,
|
|
29
|
+
meta && 'primitive' in meta && meta.primitive
|
|
30
|
+
? [`@type ${meta.primitive}`, 'optional' in schema && schema.optional ? ' | undefined' : undefined].filter(Boolean).join('')
|
|
31
|
+
: undefined,
|
|
32
|
+
].filter(Boolean)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type BuildParamsSchemaOptions = {
|
|
36
|
+
params: Array<ParameterNode>
|
|
37
|
+
resolver: ResolverTs
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type BuildOperationSchemaOptions = {
|
|
41
|
+
resolver: ResolverTs
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function buildParams(node: OperationNode, { params, resolver }: BuildParamsSchemaOptions): SchemaNode {
|
|
45
|
+
return createSchema({
|
|
46
|
+
type: 'object',
|
|
47
|
+
properties: params.map((param) =>
|
|
48
|
+
createProperty({
|
|
49
|
+
name: param.name,
|
|
50
|
+
required: param.required,
|
|
51
|
+
schema: createSchema({
|
|
52
|
+
type: 'ref',
|
|
53
|
+
name: resolver.resolveParamName(node, param),
|
|
54
|
+
}),
|
|
55
|
+
}),
|
|
56
|
+
),
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function buildData(node: OperationNode, { resolver }: BuildOperationSchemaOptions): SchemaNode {
|
|
61
|
+
const pathParams = node.parameters.filter((p) => p.in === 'path')
|
|
62
|
+
const queryParams = node.parameters.filter((p) => p.in === 'query')
|
|
63
|
+
const headerParams = node.parameters.filter((p) => p.in === 'header')
|
|
64
|
+
|
|
65
|
+
return createSchema({
|
|
66
|
+
type: 'object',
|
|
67
|
+
deprecated: node.deprecated,
|
|
68
|
+
properties: [
|
|
69
|
+
createProperty({
|
|
70
|
+
name: 'data',
|
|
71
|
+
schema: node.requestBody?.schema
|
|
72
|
+
? createSchema({ type: 'ref', name: resolver.resolveDataName(node), optional: true })
|
|
73
|
+
: createSchema({ type: 'never', primitive: undefined, optional: true }),
|
|
74
|
+
}),
|
|
75
|
+
createProperty({
|
|
76
|
+
name: 'pathParams',
|
|
77
|
+
required: pathParams.length > 0,
|
|
78
|
+
schema: pathParams.length > 0 ? buildParams(node, { params: pathParams, resolver }) : createSchema({ type: 'never', primitive: undefined }),
|
|
79
|
+
}),
|
|
80
|
+
createProperty({
|
|
81
|
+
name: 'queryParams',
|
|
82
|
+
schema:
|
|
83
|
+
queryParams.length > 0
|
|
84
|
+
? createSchema({ ...buildParams(node, { params: queryParams, resolver }), optional: true })
|
|
85
|
+
: createSchema({ type: 'never', primitive: undefined, optional: true }),
|
|
86
|
+
}),
|
|
87
|
+
createProperty({
|
|
88
|
+
name: 'headerParams',
|
|
89
|
+
schema:
|
|
90
|
+
headerParams.length > 0
|
|
91
|
+
? createSchema({ ...buildParams(node, { params: headerParams, resolver }), optional: true })
|
|
92
|
+
: createSchema({ type: 'never', primitive: undefined, optional: true }),
|
|
93
|
+
}),
|
|
94
|
+
createProperty({
|
|
95
|
+
name: 'url',
|
|
96
|
+
required: true,
|
|
97
|
+
schema: createSchema({ type: 'url', path: node.path }),
|
|
98
|
+
}),
|
|
99
|
+
],
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function buildResponses(node: OperationNode, { resolver }: BuildOperationSchemaOptions): SchemaNode | null {
|
|
104
|
+
if (node.responses.length === 0) {
|
|
105
|
+
return null
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return createSchema({
|
|
109
|
+
type: 'object',
|
|
110
|
+
properties: node.responses.map((res) =>
|
|
111
|
+
createProperty({
|
|
112
|
+
name: String(res.statusCode),
|
|
113
|
+
required: true,
|
|
114
|
+
schema: createSchema({ type: 'ref', name: resolver.resolveResponseStatusName(node, res.statusCode) }),
|
|
115
|
+
}),
|
|
116
|
+
),
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function buildResponseUnion(node: OperationNode, { resolver }: BuildOperationSchemaOptions): SchemaNode | null {
|
|
121
|
+
const responsesWithSchema = node.responses.filter((res) => res.schema)
|
|
122
|
+
|
|
123
|
+
if (responsesWithSchema.length === 0) {
|
|
124
|
+
return null
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return createSchema({
|
|
128
|
+
type: 'union',
|
|
129
|
+
members: responsesWithSchema.map((res) => createSchema({ type: 'ref', name: resolver.resolveResponseStatusName(node, res.statusCode) })),
|
|
130
|
+
})
|
|
131
|
+
}
|