@kubb/plugin-ts 5.0.0-beta.42 → 5.0.0-beta.64

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,315 +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
- * Where the generated `.ts` files are written and how they are exported.
183
- *
184
- * @default { path: 'types', barrel: { type: 'named' } }
185
- */
186
- output?: Output
187
- /**
188
- * Media type read from the OpenAPI spec when an operation defines several.
189
- * Defaults to the first JSON-compatible media type Kubb finds.
190
- */
191
- contentType?: 'application/json' | (string & {})
192
- /**
193
- * Split generated files into subfolders based on the operation's tag.
194
- */
195
- group?: Group
196
- /**
197
- * Skip operations matching at least one entry in the list.
198
- */
199
- exclude?: Array<Exclude>
200
- /**
201
- * Restrict generation to operations matching at least one entry in the list.
202
- */
203
- include?: Array<Include>
204
- /**
205
- * Apply a different options object to operations matching a pattern.
206
- */
207
- override?: Array<Override<ResolvedOptions>>
208
- /**
209
- * Whether object schemas are emitted as `type` aliases or `interface` declarations.
210
- * - `'type'` emits closed type aliases. Safer default for generated code.
211
- * - `'interface'` emits interfaces. Useful when consumers rely on declaration merging.
212
- *
213
- * @default 'type'
214
- * @see https://www.totaltypescript.com/type-vs-interface-which-should-you-use
215
- */
216
- syntaxType?: 'type' | 'interface'
217
- /**
218
- * How optional properties are written in generated types.
219
- * - `'questionToken'` — `type?: string`. The property may be missing.
220
- * - `'undefined'` — `type: string | undefined`. Required to exist, may be `undefined`.
221
- * - `'questionTokenAndUndefined'` — `type?: string | undefined`. Strictest.
222
- *
223
- * @default 'questionToken'
224
- * @note Pick `'questionTokenAndUndefined'` when `exactOptionalPropertyTypes` is on in `tsconfig.json`.
225
- */
226
- optionalType?: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
227
- /**
228
- * Syntax used for array types.
229
- * - `'array'` — `Type[]`. Shorter.
230
- * - `'generic'` — `Array<Type>`. More readable for complex element types.
231
- *
232
- * @default 'array'
233
- */
234
- arrayType?: 'generic' | 'array'
235
- /**
236
- * Rename properties inside `PathParams`, `QueryParams`, and `HeaderParams` types.
237
- * Response and request body types are not affected.
238
- *
239
- * @note Every plugin that touches operation parameters must use the same value.
240
- */
241
- paramsCasing?: 'camelcase'
242
- /**
243
- * Custom generators that run alongside the built-in TypeScript generators.
244
- */
245
- generators?: Array<Generator<PluginTs>>
246
- /**
247
- * Override how names and file paths are built for generated symbols.
248
- * Methods you omit fall back to the default `resolverTs`. `this` is bound to the
249
- * full resolver, so `this.default(name, 'function')` delegates to the built-in
250
- * implementation.
251
- */
252
- resolver?: Partial<ResolverTs> & ThisType<ResolverTs>
253
- /**
254
- * AST visitor applied to each schema or operation node before printing.
255
- * Methods you omit fall back to the preset transformer.
256
- *
257
- * @example Drop writeOnly properties from response types
258
- * ```ts
259
- * transformer: {
260
- * property(node) {
261
- * if (node.schema.writeOnly) return undefined
262
- * }
263
- * }
264
- * ```
265
- */
266
- transformer?: ast.Visitor
267
- /**
268
- * Replace the TypeScript handler for a specific schema type (`'integer'`, `'date'`, ...).
269
- * Each handler returns a TypeScript AST node for that schema type. Use `this.transform`
270
- * to recurse into nested schema nodes.
271
- *
272
- * @example Use the JavaScript `Date` object for date schemas
273
- * ```ts
274
- * import ts from 'typescript'
275
- * pluginTs({
276
- * printer: {
277
- * nodes: {
278
- * date() {
279
- * return ts.factory.createTypeReferenceNode('Date', [])
280
- * },
281
- * },
282
- * },
283
- * })
284
- * ```
285
- */
286
- printer?: {
287
- nodes?: PrinterTsNodes
288
- }
289
- } & EnumTypeOptions
290
-
291
- type ResolvedOptions = {
292
- output: Output
293
- exclude: Array<Exclude>
294
- include: Array<Include> | undefined
295
- override: Array<Override<ResolvedOptions>>
296
- group: Group | null
297
- enumType: NonNullable<Options['enumType']>
298
- enumTypeSuffix: NonNullable<Options['enumTypeSuffix']>
299
- enumKeyCasing: EnumKeyCasing
300
- optionalType: NonNullable<Options['optionalType']>
301
- arrayType: NonNullable<Options['arrayType']>
302
- syntaxType: NonNullable<Options['syntaxType']>
303
- paramsCasing: Options['paramsCasing']
304
- printer: Options['printer']
305
- }
306
-
307
- export type PluginTs = PluginFactoryOptions<'plugin-ts', Options, ResolvedOptions, ResolverTs>
308
-
309
- declare global {
310
- namespace Kubb {
311
- interface PluginRegistry {
312
- 'plugin-ts': PluginTs
313
- }
314
- }
315
- }
package/src/utils.ts DELETED
@@ -1,140 +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
- const hasDescription = meta && 'description' in meta && meta.description
19
-
20
- const formatComment =
21
- meta && 'format' in meta && meta.format
22
- ? hasDescription
23
- ? // Empty line between description and format
24
- [' ', `Format: \`${meta.format}\``]
25
- : ['@description', `Format: \`${meta.format}\``]
26
- : []
27
-
28
- return [
29
- hasDescription ? `@description ${jsStringEscape(meta.description)}` : null,
30
- ...formatComment,
31
- meta && 'deprecated' in meta && meta.deprecated ? '@deprecated' : null,
32
- // minItems/maxItems on arrays should not be emitted as @minLength/@maxLength
33
- !isArray && meta && 'min' in meta && meta.min !== undefined ? `@minLength ${meta.min}` : null,
34
- !isArray && meta && 'max' in meta && meta.max !== undefined ? `@maxLength ${meta.max}` : null,
35
- meta && 'pattern' in meta && meta.pattern ? `@pattern ${meta.pattern}` : null,
36
- meta && 'default' in meta && meta.default !== undefined
37
- ? `@default ${'primitive' in meta && meta.primitive === 'string' ? stringify(meta.default as string) : meta.default}`
38
- : null,
39
- meta && 'example' in meta && meta.example !== undefined ? `@example ${meta.example}` : null,
40
- meta && 'primitive' in meta && meta.primitive
41
- ? [`@type ${meta.primitive}`, 'optional' in schema && schema.optional ? ' | undefined' : null].filter(Boolean).join('')
42
- : null,
43
- ].filter(Boolean)
44
- }
45
-
46
- type BuildParamsSchemaOptions = {
47
- params: Array<ast.ParameterNode>
48
- resolver: ResolverTs
49
- }
50
-
51
- type BuildOperationSchemaOptions = {
52
- resolver: ResolverTs
53
- }
54
-
55
- export function buildParams(node: ast.OperationNode, { params, resolver }: BuildParamsSchemaOptions): ast.SchemaNode {
56
- return ast.createSchema({
57
- type: 'object',
58
- properties: params.map((param) =>
59
- ast.createProperty({
60
- name: param.name,
61
- required: param.required,
62
- schema: ast.createSchema({
63
- type: 'ref',
64
- name: resolver.resolveParamName(node, param),
65
- }),
66
- }),
67
- ),
68
- })
69
- }
70
-
71
- export function buildData(node: ast.OperationNode, { resolver }: BuildOperationSchemaOptions): ast.SchemaNode {
72
- const { path: pathParams, query: queryParams, header: headerParams } = getOperationParameters(node)
73
-
74
- return ast.createSchema({
75
- type: 'object',
76
- deprecated: node.deprecated,
77
- properties: [
78
- ast.createProperty({
79
- name: 'data',
80
- schema: node.requestBody?.content?.[0]?.schema
81
- ? ast.createSchema({ type: 'ref', name: resolver.resolveDataName(node), optional: true })
82
- : ast.createSchema({ type: 'never', primitive: undefined, optional: true }),
83
- }),
84
- ast.createProperty({
85
- name: 'pathParams',
86
- required: pathParams.length > 0,
87
- schema: pathParams.length > 0 ? buildParams(node, { params: pathParams, resolver }) : ast.createSchema({ type: 'never', primitive: undefined }),
88
- }),
89
- ast.createProperty({
90
- name: 'queryParams',
91
- schema:
92
- queryParams.length > 0
93
- ? ast.createSchema({ ...buildParams(node, { params: queryParams, resolver }), optional: true })
94
- : ast.createSchema({ type: 'never', primitive: undefined, optional: true }),
95
- }),
96
- ast.createProperty({
97
- name: 'headerParams',
98
- schema:
99
- headerParams.length > 0
100
- ? ast.createSchema({ ...buildParams(node, { params: headerParams, resolver }), optional: true })
101
- : ast.createSchema({ type: 'never', primitive: undefined, optional: true }),
102
- }),
103
- ast.createProperty({
104
- name: 'url',
105
- required: true,
106
- schema: ast.createSchema({ type: 'url', path: node.path }),
107
- }),
108
- ],
109
- })
110
- }
111
-
112
- export function buildResponses(node: ast.OperationNode, { resolver }: BuildOperationSchemaOptions): ast.SchemaNode | null {
113
- if (node.responses.length === 0) {
114
- return null
115
- }
116
-
117
- return ast.createSchema({
118
- type: 'object',
119
- properties: node.responses.map((res) =>
120
- ast.createProperty({
121
- name: String(res.statusCode),
122
- required: true,
123
- schema: ast.createSchema({ type: 'ref', name: resolver.resolveResponseStatusName(node, res.statusCode) }),
124
- }),
125
- ),
126
- })
127
- }
128
-
129
- export function buildResponseUnion(node: ast.OperationNode, { resolver }: BuildOperationSchemaOptions): ast.SchemaNode | null {
130
- const responsesWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema))
131
-
132
- if (responsesWithSchema.length === 0) {
133
- return null
134
- }
135
-
136
- return ast.createSchema({
137
- type: 'union',
138
- members: responsesWithSchema.map((res) => ast.createSchema({ type: 'ref', name: resolver.resolveResponseStatusName(node, res.statusCode) })),
139
- })
140
- }