@kubb/plugin-ts 5.0.0-beta.10 → 5.0.0-beta.103

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/src/types.ts DELETED
@@ -1,308 +0,0 @@
1
- import type { ast, Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, Resolver } from '@kubb/core'
2
- import type { PrinterTsNodes } from './printers/printerTs.ts'
3
- /**
4
- * The concrete resolver type for `@kubb/plugin-ts`.
5
- * Extends the base `Resolver` (which provides `default` and `resolveOptions`) with
6
- * plugin-specific naming helpers for operations, parameters, responses, and schemas.
7
- */
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
- }
179
-
180
- export type Options = {
181
- /**
182
- * Specify the export location for the files and define the behavior of the output
183
- * @default { path: 'types', barrelType: 'named' }
184
- */
185
- output?: Output
186
- /**
187
- * Define which contentType should be used.
188
- * By default, uses the first valid JSON media type.
189
- */
190
- contentType?: 'application/json' | (string & {})
191
- /**
192
- * Group the clients based on the provided name.
193
- */
194
- group?: Group
195
- /**
196
- * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
197
- */
198
- exclude?: Array<Exclude>
199
- /**
200
- * Array containing include parameters to include tags/operations/methods/paths.
201
- */
202
- include?: Array<Include>
203
- /**
204
- * Array containing override parameters to override `options` based on tags/operations/methods/paths.
205
- */
206
- override?: Array<Override<ResolvedOptions>>
207
- /**
208
- * Switch between type or interface for creating TypeScript types.
209
- * - 'type' generates type alias declarations.
210
- * - 'interface' generates interface declarations.
211
- * @default 'type'
212
- */
213
- syntaxType?: 'type' | 'interface'
214
- /**
215
- * Choose what to use as mode for an optional value.
216
- * - 'questionToken' marks the property as optional with ? (e.g., type?: string).
217
- * - 'undefined' adds undefined to the type union (e.g., type: string | undefined).
218
- * - 'questionTokenAndUndefined' combines both approaches (e.g., type?: string | undefined).
219
- * @default 'questionToken'
220
- */
221
- optionalType?: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
222
- /**
223
- * Choose between Array<string> or string[] for array types.
224
- * - 'generic' generates Array<Type> syntax.
225
- * - 'array' generates Type[] syntax.
226
- * @default 'array'
227
- */
228
- arrayType?: 'generic' | 'array'
229
- /**
230
- * How to style your params, by default no casing is applied
231
- * - 'camelcase' uses camelCase for pathParams, queryParams and headerParams property names
232
- * @default undefined
233
- * @note response types (data/body) are NOT affected by this option
234
- */
235
- paramsCasing?: 'camelcase'
236
- /**
237
- * Define some generators next to the ts generators
238
- */
239
- generators?: Array<Generator<PluginTs>>
240
- /**
241
- * Override naming conventions. When a method returns `null` or `undefined`, the preset
242
- * resolver (`resolverTs`) is used as fallback.
243
- */
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
283
-
284
- type ResolvedOptions = {
285
- output: Output
286
- exclude: Array<Exclude>
287
- include: Array<Include> | undefined
288
- override: Array<Override<ResolvedOptions>>
289
- group: Group | undefined
290
- enumType: NonNullable<Options['enumType']>
291
- enumTypeSuffix: NonNullable<Options['enumTypeSuffix']>
292
- enumKeyCasing: EnumKeyCasing
293
- optionalType: NonNullable<Options['optionalType']>
294
- arrayType: NonNullable<Options['arrayType']>
295
- syntaxType: NonNullable<Options['syntaxType']>
296
- paramsCasing: Options['paramsCasing']
297
- printer: Options['printer']
298
- }
299
-
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 DELETED
@@ -1,129 +0,0 @@
1
- import { jsStringEscape, stringify } from '@internals/utils'
2
- import { getOperationParameters } from '@internals/shared'
3
- import { ast } from '@kubb/core'
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: ast.SchemaNode): Array<string | undefined> {
14
- const meta = ast.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<ast.ParameterNode>
37
- resolver: ResolverTs
38
- }
39
-
40
- type BuildOperationSchemaOptions = {
41
- resolver: ResolverTs
42
- }
43
-
44
- export function buildParams(node: ast.OperationNode, { params, resolver }: BuildParamsSchemaOptions): ast.SchemaNode {
45
- return ast.createSchema({
46
- type: 'object',
47
- properties: params.map((param) =>
48
- ast.createProperty({
49
- name: param.name,
50
- required: param.required,
51
- schema: ast.createSchema({
52
- type: 'ref',
53
- name: resolver.resolveParamName(node, param),
54
- }),
55
- }),
56
- ),
57
- })
58
- }
59
-
60
- export function buildData(node: ast.OperationNode, { resolver }: BuildOperationSchemaOptions): ast.SchemaNode {
61
- const { path: pathParams, query: queryParams, header: headerParams } = getOperationParameters(node)
62
-
63
- return ast.createSchema({
64
- type: 'object',
65
- deprecated: node.deprecated,
66
- properties: [
67
- ast.createProperty({
68
- name: 'data',
69
- schema: node.requestBody?.content?.[0]?.schema
70
- ? ast.createSchema({ type: 'ref', name: resolver.resolveDataName(node), optional: true })
71
- : ast.createSchema({ type: 'never', primitive: undefined, optional: true }),
72
- }),
73
- ast.createProperty({
74
- name: 'pathParams',
75
- required: pathParams.length > 0,
76
- schema: pathParams.length > 0 ? buildParams(node, { params: pathParams, resolver }) : ast.createSchema({ type: 'never', primitive: undefined }),
77
- }),
78
- ast.createProperty({
79
- name: 'queryParams',
80
- schema:
81
- queryParams.length > 0
82
- ? ast.createSchema({ ...buildParams(node, { params: queryParams, resolver }), optional: true })
83
- : ast.createSchema({ type: 'never', primitive: undefined, optional: true }),
84
- }),
85
- ast.createProperty({
86
- name: 'headerParams',
87
- schema:
88
- headerParams.length > 0
89
- ? ast.createSchema({ ...buildParams(node, { params: headerParams, resolver }), optional: true })
90
- : ast.createSchema({ type: 'never', primitive: undefined, optional: true }),
91
- }),
92
- ast.createProperty({
93
- name: 'url',
94
- required: true,
95
- schema: ast.createSchema({ type: 'url', path: node.path }),
96
- }),
97
- ],
98
- })
99
- }
100
-
101
- export function buildResponses(node: ast.OperationNode, { resolver }: BuildOperationSchemaOptions): ast.SchemaNode | null {
102
- if (node.responses.length === 0) {
103
- return null
104
- }
105
-
106
- return ast.createSchema({
107
- type: 'object',
108
- properties: node.responses.map((res) =>
109
- ast.createProperty({
110
- name: String(res.statusCode),
111
- required: true,
112
- schema: ast.createSchema({ type: 'ref', name: resolver.resolveResponseStatusName(node, res.statusCode) }),
113
- }),
114
- ),
115
- })
116
- }
117
-
118
- export function buildResponseUnion(node: ast.OperationNode, { resolver }: BuildOperationSchemaOptions): ast.SchemaNode | null {
119
- const responsesWithSchema = node.responses.filter((res) => res.schema)
120
-
121
- if (responsesWithSchema.length === 0) {
122
- return null
123
- }
124
-
125
- return ast.createSchema({
126
- type: 'union',
127
- members: responsesWithSchema.map((res) => ast.createSchema({ type: 'ref', name: resolver.resolveResponseStatusName(node, res.statusCode) })),
128
- })
129
- }