@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/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { t as __name } from "./chunk-C0LytTxp.js";
2
- import { Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, Resolver, ast } from "@kubb/core";
3
2
  import ts from "typescript";
3
+ import { Exclude, Generator, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ast } from "@kubb/core";
4
4
  import { KubbReactNode } from "@kubb/renderer-jsx/types";
5
5
 
6
6
  //#region src/printers/printerTs.d.ts
@@ -43,23 +43,11 @@ type PrinterTsOptions = {
43
43
  */
44
44
  arrayType: PluginTs['resolvedOptions']['arrayType'];
45
45
  /**
46
- * Enum output format.
47
- * - `'inlineLiteral'` embeds literal unions inline
48
- * - `'asPascalConst'` generates named const unions
49
- * - `'asConst'` generates as const declarations
50
- *
51
- * @default `'inlineLiteral'`
52
- */
53
- enumType: PluginTs['resolvedOptions']['enumType'];
54
- /**
55
- * Suffix appended to enum key reference names.
56
- *
57
- * @example Enum key naming
58
- * `StatusKey` when `enumType` is `'asConst'`
59
- *
60
- * @default `'Key'`
46
+ * Grouped enum settings. The printer emits references to enums, not the enum declarations, so only
47
+ * `type` (the output format) and `typeSuffix` (the enum key reference suffix) matter here.
48
+ * `constCasing` and `keyCasing` are ignored.
61
49
  */
62
- enumTypeSuffix?: PluginTs['resolvedOptions']['enumTypeSuffix'];
50
+ enum: PluginTs['resolvedOptions']['enum'];
63
51
  /**
64
52
  * Syntax for generated declarations.
65
53
  * - `'type'` generates type aliases
@@ -112,13 +100,13 @@ type PrinterTsFactory = ast.PrinterFactoryOptions<'typescript', PrinterTsOptions
112
100
  *
113
101
  * @example Raw type node (no `typeName`)
114
102
  * ```ts
115
- * const printer = printerTs({ optionalType: 'questionToken', arrayType: 'array', enumType: 'inlineLiteral' })
103
+ * const printer = printerTs({ optionalType: 'questionToken', arrayType: 'array', enum: { type: 'inlineLiteral' } })
116
104
  * const typeNode = printer.print(schemaNode) // ts.TypeNode
117
105
  * ```
118
106
  *
119
107
  * @example Full declaration (with `typeName`)
120
108
  * ```ts
121
- * const printer = printerTs({ optionalType: 'questionToken', arrayType: 'array', enumType: 'inlineLiteral', typeName: 'MyType' })
109
+ * const printer = printerTs({ optionalType: 'questionToken', arrayType: 'array', enum: { type: 'inlineLiteral' }, typeName: 'MyType' })
122
110
  * const declaration = printer.print(schemaNode) // ts.TypeAliasDeclaration | ts.InterfaceDeclaration
123
111
  * ```
124
112
  */
@@ -216,33 +204,47 @@ type ResolverTs = Resolver & ast.OperationParamsResolver & {
216
204
  resolveHeaderParamsName(node: ast.OperationNode, param: ast.ParameterNode): string;
217
205
  };
218
206
  type EnumKeyCasing = 'screamingSnakeCase' | 'snakeCase' | 'pascalCase' | 'camelCase' | 'none';
207
+ type EnumConstCasing = 'camelCase' | 'pascalCase';
219
208
  /**
220
- * Discriminated union that ties `enumTypeSuffix` and `enumKeyCasing` to the enum types that actually use them.
209
+ * Grouped enum settings. Each `type` uses only some of the other fields.
221
210
  *
222
- * - `'asConst'` / `'asPascalConst'` — emit a `const` object; both `enumTypeSuffix` (type-alias suffix) and
223
- * `enumKeyCasing` (key formatting) are meaningful.
224
- * - `'enum'` / `'constEnum'` — emit a TypeScript enum; `enumKeyCasing` applies to member names,
225
- * but there is no separate type alias so `enumTypeSuffix` is not used.
226
- * - `'literal'` / `'inlineLiteral'` — emit only union literals; keys are discarded entirely,
227
- * so neither `enumTypeSuffix` nor `enumKeyCasing` have any effect.
211
+ * - `'asConst'` emits a `const` object plus a `typeof` type alias, so `constCasing`, `typeSuffix`, and `keyCasing` all apply.
212
+ * - `'enum'` and `'constEnum'` emit a TypeScript enum, so only `keyCasing` (the member names) applies.
213
+ * - `'literal'` and `'inlineLiteral'` emit union literals and drop the keys, so none of the other fields apply.
214
+ *
215
+ * @example Share one name between the const and the type
216
+ * ```ts
217
+ * enum: { type: 'asConst', constCasing: 'pascalCase', typeSuffix: '' }
218
+ * // export const VehicleType = { … } as const
219
+ * // export type VehicleType = (typeof VehicleType)[keyof typeof VehicleType]
220
+ * ```
228
221
  */
229
- type EnumTypeOptions = {
222
+ type EnumOptions$1 = {
230
223
  /**
231
- * Choose to use enum, asConst, asPascalConst, constEnum, literal, or inlineLiteral for enums.
232
- * - 'asConst' generates const objects with camelCase names and as const assertion.
233
- * - 'asPascalConst' generates const objects with PascalCase names and as const assertion.
224
+ * Emit a `const` object asserted with `as const`, paired with a `typeof` type alias.
225
+ * This is tree-shakeable and adds no enum runtime.
226
+ *
234
227
  * @default 'asConst'
235
228
  */
236
- enumType?: 'asConst' | 'asPascalConst';
229
+ type?: 'asConst';
237
230
  /**
238
- * Suffix appended to the generated type alias name.
231
+ * Casing of the generated const variable.
232
+ * - 'camelCase' names the const `vehicleType`.
233
+ * - 'pascalCase' names the const `VehicleType`, matching the schema name.
239
234
  *
240
- * Only affects the type alias; the const object name is unchanged.
235
+ * @default 'camelCase'
236
+ */
237
+ constCasing?: EnumConstCasing;
238
+ /**
239
+ * Suffix appended to the generated type alias name. Only the type alias is renamed. The const
240
+ * object name stays the same. Set it to `''` together with `constCasing: 'pascalCase'` to merge
241
+ * the const and type under the schema's exact name.
241
242
  *
242
243
  * @default 'Key'
243
- * @example enumTypeSuffix: 'Value' → `export type PetStatusValue = …`
244
+ * @example A custom suffix
245
+ * `typeSuffix: 'Value'` renames the alias to `PetStatusValue`
244
246
  */
245
- enumTypeSuffix?: string;
247
+ typeSuffix?: string;
246
248
  /**
247
249
  * Choose the casing for enum key names.
248
250
  * - 'screamingSnakeCase' generates keys in SCREAMING_SNAKE_CASE format.
@@ -252,22 +254,24 @@ type EnumTypeOptions = {
252
254
  * - 'none' uses the enum value as-is without transformation.
253
255
  * @default 'none'
254
256
  */
255
- enumKeyCasing?: EnumKeyCasing;
257
+ keyCasing?: EnumKeyCasing;
256
258
  } | {
257
259
  /**
258
- * Choose to use enum, asConst, asPascalConst, constEnum, literal, or inlineLiteral for enums.
259
- * - 'enum' generates TypeScript enum declarations.
260
- * - 'constEnum' generates TypeScript const enum declarations.
260
+ * Emit a TypeScript `enum` (`'enum'`) or `const enum` (`'constEnum'`) declaration.
261
+ *
261
262
  * @default 'asConst'
262
263
  */
263
- enumType?: 'enum' | 'constEnum';
264
+ type?: 'enum' | 'constEnum';
264
265
  /**
265
- * `enumTypeSuffix` has no effect for this `enumType`.
266
- * It is only used when `enumType` is `'asConst'` or `'asPascalConst'`.
266
+ * `constCasing` has no effect for this `type`. Only `'asConst'` emits a const object.
267
267
  */
268
- enumTypeSuffix?: never;
268
+ constCasing?: never;
269
269
  /**
270
- * Choose the casing for enum key names.
270
+ * `typeSuffix` has no effect for this `type`. Only `'asConst'` emits a separate type alias.
271
+ */
272
+ typeSuffix?: never;
273
+ /**
274
+ * Choose the casing for enum member names.
271
275
  * - 'screamingSnakeCase' generates keys in SCREAMING_SNAKE_CASE format.
272
276
  * - 'snakeCase' generates keys in snake_case format.
273
277
  * - 'pascalCase' generates keys in PascalCase format.
@@ -275,43 +279,37 @@ type EnumTypeOptions = {
275
279
  * - 'none' uses the enum value as-is without transformation.
276
280
  * @default 'none'
277
281
  */
278
- enumKeyCasing?: EnumKeyCasing;
282
+ keyCasing?: EnumKeyCasing;
279
283
  } | {
280
284
  /**
281
- * Choose to use enum, asConst, asPascalConst, constEnum, literal, or inlineLiteral for enums.
282
- * - 'literal' generates literal union types.
283
- * - 'inlineLiteral' will inline enum values directly into the type (default in v5).
285
+ * Emit a union of literals as a named alias (`'literal'`) or inline the union at every usage
286
+ * site (`'inlineLiteral'`).
287
+ *
284
288
  * @default 'asConst'
285
289
  * @note In Kubb v5, 'inlineLiteral' becomes the default.
286
290
  */
287
- enumType?: 'literal' | 'inlineLiteral';
291
+ type?: 'literal' | 'inlineLiteral';
288
292
  /**
289
- * `enumTypeSuffix` has no effect for this `enumType`.
290
- * It is only used when `enumType` is `'asConst'` or `'asPascalConst'`.
293
+ * `constCasing` has no effect for this `type`; literal modes emit no const object.
291
294
  */
292
- enumTypeSuffix?: never;
295
+ constCasing?: never;
293
296
  /**
294
- * `enumKeyCasing` has no effect for this `enumType`.
295
- * Literal and inlineLiteral modes emit only values; keys are discarded entirely.
297
+ * `typeSuffix` has no effect for this `type`; literal modes emit no separate type alias.
296
298
  */
297
- enumKeyCasing?: never;
298
- };
299
- type Options = {
299
+ typeSuffix?: never;
300
300
  /**
301
- * Where the generated `.ts` files are written and how they are exported.
302
- *
303
- * @default { path: 'types', barrel: { type: 'named' } }
301
+ * `keyCasing` has no effect for this `type`. Literal and inlineLiteral modes emit only values,
302
+ * so the keys are discarded.
304
303
  */
305
- output?: Output;
306
- /**
307
- * Media type read from the OpenAPI spec when an operation defines several.
308
- * Defaults to the first JSON-compatible media type Kubb finds.
309
- */
310
- contentType?: 'application/json' | (string & {});
311
- /**
312
- * Split generated files into subfolders based on the operation's tag.
313
- */
314
- group?: Group;
304
+ keyCasing?: never;
305
+ };
306
+ /**
307
+ * Where the generated `.ts` files are written and how they are exported, plus the optional
308
+ * `group` strategy. The `group` option organizes `output.mode: 'directory'` output into per-tag or per-path subdirectories.
309
+ *
310
+ * @default { path: 'types', barrel: { type: 'named' } }
311
+ */
312
+ type Options = OutputOptions & {
315
313
  /**
316
314
  * Skip operations matching at least one entry in the list.
317
315
  */
@@ -370,19 +368,22 @@ type Options = {
370
368
  */
371
369
  resolver?: Partial<ResolverTs> & ThisType<ResolverTs>;
372
370
  /**
373
- * AST visitor applied to each schema or operation node before printing.
374
- * Methods you omit fall back to the preset transformer.
371
+ * Macros applied to each schema or operation node before printing.
372
+ * Callbacks you omit fall back to the preset behavior.
375
373
  *
376
374
  * @example Drop writeOnly properties from response types
377
375
  * ```ts
378
- * transformer: {
379
- * property(node) {
380
- * if (node.schema.writeOnly) return undefined
381
- * }
382
- * }
376
+ * macros: [
377
+ * {
378
+ * name: 'drop-write-only',
379
+ * property(node) {
380
+ * if (node.schema.writeOnly) return undefined
381
+ * },
382
+ * },
383
+ * ]
383
384
  * ```
384
385
  */
385
- transformer?: ast.Visitor;
386
+ macros?: Array<ast.Macro>;
386
387
  /**
387
388
  * Replace the TypeScript handler for a specific schema type (`'integer'`, `'date'`, ...).
388
389
  * Each handler returns a TypeScript AST node for that schema type. Use `this.transform`
@@ -405,16 +406,24 @@ type Options = {
405
406
  printer?: {
406
407
  nodes?: PrinterTsNodes;
407
408
  };
408
- } & EnumTypeOptions;
409
+ /**
410
+ * How OpenAPI enums are represented in the generated TypeScript, and how their names are cased.
411
+ */
412
+ enum?: EnumOptions$1;
413
+ };
414
+ type ResolvedEnumOptions = {
415
+ type: NonNullable<EnumOptions$1['type']>;
416
+ constCasing: EnumConstCasing;
417
+ typeSuffix: string;
418
+ keyCasing: EnumKeyCasing;
419
+ };
409
420
  type ResolvedOptions = {
410
421
  output: Output;
411
422
  exclude: Array<Exclude>;
412
423
  include: Array<Include> | undefined;
413
424
  override: Array<Override<ResolvedOptions>>;
414
425
  group: Group | null;
415
- enumType: NonNullable<Options['enumType']>;
416
- enumTypeSuffix: NonNullable<Options['enumTypeSuffix']>;
417
- enumKeyCasing: EnumKeyCasing;
426
+ enum: ResolvedEnumOptions;
418
427
  optionalType: NonNullable<Options['optionalType']>;
419
428
  arrayType: NonNullable<Options['arrayType']>;
420
429
  syntaxType: NonNullable<Options['syntaxType']>;
@@ -431,11 +440,10 @@ declare global {
431
440
  }
432
441
  //#endregion
433
442
  //#region src/components/Enum.d.ts
443
+ type EnumOptions = PluginTs['resolvedOptions']['enum'];
434
444
  type Props$1 = {
435
445
  node: ast.EnumSchemaNode;
436
- enumType: PluginTs['resolvedOptions']['enumType'];
437
- enumTypeSuffix: PluginTs['resolvedOptions']['enumTypeSuffix'];
438
- enumKeyCasing: PluginTs['resolvedOptions']['enumKeyCasing'];
446
+ enum: EnumOptions;
439
447
  resolver: ResolverTs;
440
448
  key?: string | number | null;
441
449
  };
@@ -443,14 +451,17 @@ type Props$1 = {
443
451
  * Resolves the runtime identifier name and the TypeScript type name for an enum schema node.
444
452
  *
445
453
  * The raw `node.name` may be a YAML key such as `"enumNames.Type"` which is not a
446
- * valid TypeScript identifier. The resolver normalizes it; for inline enum
447
- * properties the adapter already emits a PascalCase+suffix name so resolution is typically a no-op.
454
+ * valid TypeScript identifier. The resolver normalizes it. For inline enum properties the adapter
455
+ * already emits a PascalCase+suffix name, so resolution is typically a no-op.
456
+ *
457
+ * When `constCasing` is `'pascalCase'` and `typeSuffix` is empty, the const and the type
458
+ * resolve to the same name, which TypeScript merges into a single value+type declaration.
448
459
  */
449
460
  /**
450
461
  * Renders the enum declaration(s) for a single named `EnumSchemaNode`.
451
462
  *
452
- * Depending on `enumType` this may emit:
453
- * - A runtime object (`asConst` / `asPascalConst`) plus a `typeof` type alias
463
+ * Depending on `enum.type` this may emit:
464
+ * - A runtime object (`asConst`) plus a `typeof` type alias
454
465
  * - A `const enum` or plain `enum` declaration (`constEnum` / `enum`)
455
466
  * - A union literal type alias (`literal`)
456
467
  *
@@ -459,9 +470,7 @@ type Props$1 = {
459
470
  */
460
471
  declare function Enum({
461
472
  node,
462
- enumType,
463
- enumTypeSuffix,
464
- enumKeyCasing,
473
+ enum: enumOptions,
465
474
  resolver
466
475
  }: Props$1): KubbReactNode;
467
476
  //#endregion
@@ -474,18 +483,14 @@ type Props = {
474
483
  * Created with `printerTs({ ..., nodes: options.printer?.nodes })`.
475
484
  */
476
485
  printer: ast.Printer<PrinterTsFactory>;
477
- enumType: PluginTs['resolvedOptions']['enumType'];
478
- enumTypeSuffix: PluginTs['resolvedOptions']['enumTypeSuffix'];
479
- enumKeyCasing: PluginTs['resolvedOptions']['enumKeyCasing'];
486
+ enum: PluginTs['resolvedOptions']['enum'];
480
487
  resolver: ResolverTs;
481
488
  };
482
489
  declare function Type({
483
490
  name,
484
491
  node,
485
492
  printer,
486
- enumType,
487
- enumTypeSuffix,
488
- enumKeyCasing,
493
+ enum: enumOptions,
489
494
  resolver
490
495
  }: Props): KubbReactNode;
491
496
  //#endregion
@@ -521,7 +526,7 @@ declare const pluginTsName = "plugin-ts";
521
526
  * plugins: [
522
527
  * pluginTs({
523
528
  * output: { path: './types' },
524
- * enumType: 'asConst',
529
+ * enum: { type: 'asConst' },
525
530
  * optionalType: 'questionTokenAndUndefined',
526
531
  * }),
527
532
  * ],
@@ -531,18 +536,27 @@ declare const pluginTsName = "plugin-ts";
531
536
  declare const pluginTs: (options?: Options | undefined) => import("@kubb/core").Plugin<PluginTs>;
532
537
  //#endregion
533
538
  //#region src/printers/functionPrinter.d.ts
539
+ /**
540
+ * Renders a {@link ast.TypeExpression} to its TypeScript source.
541
+ *
542
+ * - a `string` is a type reference, returned as-is
543
+ * - an `IndexedAccessType` becomes `objectType['indexType']`
544
+ * - a `TypeLiteral` becomes `{ key: Type; key?: Type }`
545
+ *
546
+ * `transformType` is applied once to the fully rendered type, matching how the
547
+ * printer wrapped reference types before the `ts.factory` model.
548
+ */
549
+ declare function renderType(type: ast.TypeExpression, transformType?: (type: string) => string): string;
534
550
  type FunctionPrinterOptions = {
535
551
  /**
536
552
  * Rendering modes supported by `functionPrinter`.
537
553
  *
538
- * | Mode | Output example | Use case |
539
- * |---------------|---------------------------------------------|---------------------------------|
540
- * | `declaration` | `id: string, config: Config = {}` | Function parameter declaration |
541
- * | `call` | `id, { method, url }` | Function call arguments |
542
- * | `keys` | `{ id, config }` | Key names only (destructuring) |
543
- * | `values` | `{ id: id, config: config }` | Key/value object entries |
554
+ * | Mode | Output example | Use case |
555
+ * |---------------|-----------------------------------|--------------------------------|
556
+ * | `declaration` | `id: string, config: Config = {}` | Function parameter declaration |
557
+ * | `call` | `id, { method, url }` | Function call arguments |
544
558
  */
545
- mode: 'declaration' | 'call' | 'keys' | 'values';
559
+ mode: 'declaration' | 'call';
546
560
  /**
547
561
  * Optional transformation applied to every parameter name before printing.
548
562
  */
@@ -553,8 +567,8 @@ type FunctionPrinterOptions = {
553
567
  transformType?: (type: string) => string;
554
568
  };
555
569
  /**
556
- * Default function-signature printer.
557
- * Covers the four standard output modes used across Kubb plugins.
570
+ * Default function-signature printer. Renders a parameter list in one of two modes:
571
+ * `declaration` for the function signature and `call` for the call arguments.
558
572
  *
559
573
  * @example
560
574
  * ```ts
@@ -585,7 +599,7 @@ declare const functionPrinter: (options?: FunctionPrinterOptions | undefined) =>
585
599
  * casing/file-layout rules.
586
600
  *
587
601
  * The `default` method is supplied by `defineResolver`. It uses PascalCase for
588
- * type names and PascalCase-with-isFile for files.
602
+ * type names and PascalCase file paths (dotted names become `/`-joined) for files.
589
603
  *
590
604
  * @example Resolve a type and file name
591
605
  * ```ts
@@ -598,5 +612,5 @@ declare const functionPrinter: (options?: FunctionPrinterOptions | undefined) =>
598
612
  */
599
613
  declare const resolverTs: ResolverTs;
600
614
  //#endregion
601
- export { Enum, type PluginTs, type PrinterTsFactory, type PrinterTsNodes, type PrinterTsOptions, type ResolverTs, Type, pluginTs as default, pluginTs, functionPrinter, pluginTsName, printerTs, resolverTs, typeGenerator };
615
+ export { Enum, type PluginTs, type PrinterTsFactory, type PrinterTsNodes, type PrinterTsOptions, type ResolverTs, Type, pluginTs as default, pluginTs, functionPrinter, pluginTsName, printerTs, renderType, resolverTs, typeGenerator };
602
616
  //# sourceMappingURL=index.d.ts.map