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

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 CHANGED
@@ -1,4 +1,4 @@
1
- import type { ast, Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, Resolver } from '@kubb/core'
1
+ import type { ast, Exclude, Generator, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver } from '@kubb/core'
2
2
  import type { PrinterTsNodes } from './printers/printerTs.ts'
3
3
  /**
4
4
  * The concrete resolver type for `@kubb/plugin-ts`.
@@ -93,34 +93,49 @@ export type ResolverTs = Resolver &
93
93
 
94
94
  type EnumKeyCasing = 'screamingSnakeCase' | 'snakeCase' | 'pascalCase' | 'camelCase' | 'none'
95
95
 
96
+ type EnumConstCasing = 'camelCase' | 'pascalCase'
97
+
96
98
  /**
97
- * Discriminated union that ties `enumTypeSuffix` and `enumKeyCasing` to the enum types that actually use them.
99
+ * Grouped enum settings. Each `type` uses only some of the other fields.
100
+ *
101
+ * - `'asConst'` emits a `const` object plus a `typeof` type alias, so `constCasing`, `typeSuffix`, and `keyCasing` all apply.
102
+ * - `'enum'` and `'constEnum'` emit a TypeScript enum, so only `keyCasing` (the member names) applies.
103
+ * - `'literal'` and `'inlineLiteral'` emit union literals and drop the keys, so none of the other fields apply.
98
104
  *
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
+ * @example Share one name between the const and the type
106
+ * ```ts
107
+ * enum: { type: 'asConst', constCasing: 'pascalCase', typeSuffix: '' }
108
+ * // export const VehicleType = { } as const
109
+ * // export type VehicleType = (typeof VehicleType)[keyof typeof VehicleType]
110
+ * ```
105
111
  */
106
- type EnumTypeOptions =
112
+ type EnumOptions =
107
113
  | {
108
114
  /**
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.
115
+ * Emit a `const` object asserted with `as const`, paired with a `typeof` type alias.
116
+ * This is tree-shakeable and adds no enum runtime.
117
+ *
112
118
  * @default 'asConst'
113
119
  */
114
- enumType?: 'asConst' | 'asPascalConst'
120
+ type?: 'asConst'
115
121
  /**
116
- * Suffix appended to the generated type alias name.
122
+ * Casing of the generated const variable.
123
+ * - 'camelCase' names the const `vehicleType`.
124
+ * - 'pascalCase' names the const `VehicleType`, matching the schema name.
117
125
  *
118
- * Only affects the type alias; the const object name is unchanged.
126
+ * @default 'camelCase'
127
+ */
128
+ constCasing?: EnumConstCasing
129
+ /**
130
+ * Suffix appended to the generated type alias name. Only the type alias is renamed. The const
131
+ * object name stays the same. Set it to `''` together with `constCasing: 'pascalCase'` to merge
132
+ * the const and type under the schema's exact name.
119
133
  *
120
134
  * @default 'Key'
121
- * @example enumTypeSuffix: 'Value' → `export type PetStatusValue = …`
135
+ * @example A custom suffix
136
+ * `typeSuffix: 'Value'` renames the alias to `PetStatusValue`
122
137
  */
123
- enumTypeSuffix?: string
138
+ typeSuffix?: string
124
139
  /**
125
140
  * Choose the casing for enum key names.
126
141
  * - 'screamingSnakeCase' generates keys in SCREAMING_SNAKE_CASE format.
@@ -130,23 +145,25 @@ type EnumTypeOptions =
130
145
  * - 'none' uses the enum value as-is without transformation.
131
146
  * @default 'none'
132
147
  */
133
- enumKeyCasing?: EnumKeyCasing
148
+ keyCasing?: EnumKeyCasing
134
149
  }
135
150
  | {
136
151
  /**
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.
152
+ * Emit a TypeScript `enum` (`'enum'`) or `const enum` (`'constEnum'`) declaration.
153
+ *
140
154
  * @default 'asConst'
141
155
  */
142
- enumType?: 'enum' | 'constEnum'
156
+ type?: 'enum' | 'constEnum'
143
157
  /**
144
- * `enumTypeSuffix` has no effect for this `enumType`.
145
- * It is only used when `enumType` is `'asConst'` or `'asPascalConst'`.
158
+ * `constCasing` has no effect for this `type`. Only `'asConst'` emits a const object.
146
159
  */
147
- enumTypeSuffix?: never
160
+ constCasing?: never
148
161
  /**
149
- * Choose the casing for enum key names.
162
+ * `typeSuffix` has no effect for this `type`. Only `'asConst'` emits a separate type alias.
163
+ */
164
+ typeSuffix?: never
165
+ /**
166
+ * Choose the casing for enum member names.
150
167
  * - 'screamingSnakeCase' generates keys in SCREAMING_SNAKE_CASE format.
151
168
  * - 'snakeCase' generates keys in snake_case format.
152
169
  * - 'pascalCase' generates keys in PascalCase format.
@@ -154,45 +171,39 @@ type EnumTypeOptions =
154
171
  * - 'none' uses the enum value as-is without transformation.
155
172
  * @default 'none'
156
173
  */
157
- enumKeyCasing?: EnumKeyCasing
174
+ keyCasing?: EnumKeyCasing
158
175
  }
159
176
  | {
160
177
  /**
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).
178
+ * Emit a union of literals as a named alias (`'literal'`) or inline the union at every usage
179
+ * site (`'inlineLiteral'`).
180
+ *
164
181
  * @default 'asConst'
165
182
  * @note In Kubb v5, 'inlineLiteral' becomes the default.
166
183
  */
167
- enumType?: 'literal' | 'inlineLiteral'
184
+ type?: 'literal' | 'inlineLiteral'
168
185
  /**
169
- * `enumTypeSuffix` has no effect for this `enumType`.
170
- * It is only used when `enumType` is `'asConst'` or `'asPascalConst'`.
186
+ * `constCasing` has no effect for this `type`; literal modes emit no const object.
171
187
  */
172
- enumTypeSuffix?: never
188
+ constCasing?: never
173
189
  /**
174
- * `enumKeyCasing` has no effect for this `enumType`.
175
- * Literal and inlineLiteral modes emit only values; keys are discarded entirely.
190
+ * `typeSuffix` has no effect for this `type`; literal modes emit no separate type alias.
176
191
  */
177
- enumKeyCasing?: never
192
+ typeSuffix?: never
193
+ /**
194
+ * `keyCasing` has no effect for this `type`. Literal and inlineLiteral modes emit only values,
195
+ * so the keys are discarded.
196
+ */
197
+ keyCasing?: never
178
198
  }
179
199
 
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
200
+ /**
201
+ * Where the generated `.ts` files are written and how they are exported, plus the optional
202
+ * `group` strategy. The `group` option organizes `output.mode: 'directory'` output into per-tag or per-path subdirectories.
203
+ *
204
+ * @default { path: 'types', barrel: { type: 'named' } }
205
+ */
206
+ export type Options = OutputOptions & {
196
207
  /**
197
208
  * Skip operations matching at least one entry in the list.
198
209
  */
@@ -286,7 +297,18 @@ export type Options = {
286
297
  printer?: {
287
298
  nodes?: PrinterTsNodes
288
299
  }
289
- } & EnumTypeOptions
300
+ /**
301
+ * How OpenAPI enums are represented in the generated TypeScript, and how their names are cased.
302
+ */
303
+ enum?: EnumOptions
304
+ }
305
+
306
+ type ResolvedEnumOptions = {
307
+ type: NonNullable<EnumOptions['type']>
308
+ constCasing: EnumConstCasing
309
+ typeSuffix: string
310
+ keyCasing: EnumKeyCasing
311
+ }
290
312
 
291
313
  type ResolvedOptions = {
292
314
  output: Output
@@ -294,9 +316,7 @@ type ResolvedOptions = {
294
316
  include: Array<Include> | undefined
295
317
  override: Array<Override<ResolvedOptions>>
296
318
  group: Group | null
297
- enumType: NonNullable<Options['enumType']>
298
- enumTypeSuffix: NonNullable<Options['enumTypeSuffix']>
299
- enumKeyCasing: EnumKeyCasing
319
+ enum: ResolvedEnumOptions
300
320
  optionalType: NonNullable<Options['optionalType']>
301
321
  arrayType: NonNullable<Options['arrayType']>
302
322
  syntaxType: NonNullable<Options['syntaxType']>
package/src/utils.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { jsStringEscape, stringify } from '@internals/utils'
1
+ import { jsStringEscape, stringify } from '@kubb/ast/utils'
2
2
  import { getOperationParameters } from '@internals/shared'
3
3
  import { ast } from '@kubb/core'
4
4
  import type { ResolverTs } from './types.ts'