@kubb/plugin-ts 5.0.0-alpha.9 → 5.0.0-beta.3

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