@kubb/plugin-ts 5.0.0-alpha.26 → 5.0.0-alpha.27

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,11 +1,116 @@
1
1
  import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { OperationParamsResolver } from "@kubb/ast";
2
+ import * as _$_kubb_ast0 from "@kubb/ast";
3
+ import { OperationParamsResolver, Printer } from "@kubb/ast";
3
4
  import ts from "typescript";
4
5
  import * as _$_kubb_core0 from "@kubb/core";
5
- import { CompatibilityPreset, Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, PrinterFactoryOptions, ResolvePathOptions, Resolver, UserGroup } from "@kubb/core";
6
+ import { CompatibilityPreset, Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, PrinterFactoryOptions, PrinterPartial, ResolvePathOptions, Resolver, UserGroup } from "@kubb/core";
6
7
  import { EnumSchemaNode, FunctionNode, OperationNode, ParameterNode, SchemaNode, StatusCode, Visitor } from "@kubb/ast/types";
7
8
  import { FabricReactNode } from "@kubb/react-fabric/types";
8
9
 
10
+ //#region src/printers/printerTs.d.ts
11
+ /**
12
+ * Partial map of node-type overrides for the TypeScript printer.
13
+ *
14
+ * Each key is a `SchemaType` string (e.g. `'date'`, `'string'`). The function
15
+ * replaces the built-in handler for that node type. Use `this.transform` to
16
+ * recurse into nested schema nodes, and `this.options` to read printer options.
17
+ *
18
+ * @example Override the `date` handler
19
+ * ```ts
20
+ * pluginTs({
21
+ * printer: {
22
+ * nodes: {
23
+ * date(node) {
24
+ * return ts.factory.createTypeReferenceNode('Date', [])
25
+ * },
26
+ * },
27
+ * },
28
+ * })
29
+ * ```
30
+ */
31
+ type PrinterTsNodes = PrinterPartial<ts.TypeNode, PrinterTsOptions>;
32
+ type PrinterTsOptions = {
33
+ /**
34
+ * @default `'questionToken'`
35
+ */
36
+ optionalType: PluginTs['resolvedOptions']['optionalType'];
37
+ /**
38
+ * @default `'array'`
39
+ */
40
+ arrayType: PluginTs['resolvedOptions']['arrayType'];
41
+ /**
42
+ * @default `'inlineLiteral'`
43
+ */
44
+ enumType: PluginTs['resolvedOptions']['enumType'];
45
+ /**
46
+ * Suffix appended to the generated type alias name when `enumType` is `asConst` or `asPascalConst`.
47
+ *
48
+ * @default `'Key'`
49
+ */
50
+ enumTypeSuffix?: PluginTs['resolvedOptions']['enumTypeSuffix'];
51
+ /**
52
+ * Controls whether a `type` alias or `interface` declaration is emitted.
53
+ * @default `'type'`
54
+ */
55
+ syntaxType?: PluginTs['resolvedOptions']['syntaxType'];
56
+ /**
57
+ * When set, `printer.print(node)` produces a full `type Name = …` declaration.
58
+ * When omitted, `printer.print(node)` returns the raw type node.
59
+ */
60
+ name?: string;
61
+ /**
62
+ * JSDoc `@description` comment added to the generated type or interface declaration.
63
+ */
64
+ description?: string;
65
+ /**
66
+ * Property keys to exclude from the generated type via `Omit<Type, Keys>`.
67
+ * Forces type-alias syntax even when `syntaxType` is `'interface'`.
68
+ */
69
+ keysToOmit?: Array<string>;
70
+ /**
71
+ * Resolver used to transform raw schema names into valid TypeScript identifiers.
72
+ */
73
+ resolver: ResolverTs;
74
+ /**
75
+ * Names of top-level schemas that are enums.
76
+ * When set, the `ref` handler uses the suffixed type name (e.g. `StatusKey`) for enum refs
77
+ * instead of the plain PascalCase name, so imports align with what the enum file actually exports.
78
+ */
79
+ enumSchemaNames?: Set<string>;
80
+ /**
81
+ * Partial map of node-type overrides. Each entry replaces the built-in handler for that node type.
82
+ */
83
+ nodes?: PrinterTsNodes;
84
+ };
85
+ /**
86
+ * TypeScript printer factory options: maps `SchemaNode` → `ts.TypeNode` (raw) or `ts.Node` (full declaration).
87
+ */
88
+ type PrinterTsFactory = PrinterFactoryOptions<'typescript', PrinterTsOptions, ts.TypeNode, string>;
89
+ /**
90
+ * TypeScript type printer built with `definePrinter`.
91
+ *
92
+ * Converts a `SchemaNode` AST node into a TypeScript AST node:
93
+ * - **`printer.print(node)`** — when `options.typeName` is set, returns a full
94
+ * `type Name = …` or `interface Name { … }` declaration (`ts.Node`).
95
+ * Without `typeName`, returns the raw `ts.TypeNode` for the schema.
96
+ *
97
+ * Dispatches on `node.type` to the appropriate handler in `nodes`. Options are closed
98
+ * over per printer instance, so each call to `printerTs(options)` produces an independent printer.
99
+ *
100
+ * @example Raw type node (no `typeName`)
101
+ * ```ts
102
+ * const printer = printerTs({ optionalType: 'questionToken', arrayType: 'array', enumType: 'inlineLiteral' })
103
+ * const typeNode = printer.print(schemaNode) // ts.TypeNode
104
+ * ```
105
+ *
106
+ * @example Full declaration (with `typeName`)
107
+ * ```ts
108
+ * const printer = printerTs({ optionalType: 'questionToken', arrayType: 'array', enumType: 'inlineLiteral', typeName: 'MyType' })
109
+ * const declaration = printer.print(schemaNode) // ts.TypeAliasDeclaration | ts.InterfaceDeclaration
110
+ * ```
111
+ */
112
+ declare const printerTs: (options?: PrinterTsOptions | undefined) => _$_kubb_ast0.Printer<PrinterTsFactory>;
113
+ //#endregion
9
114
  //#region src/types.d.ts
10
115
  /**
11
116
  * The concrete resolver type for `@kubb/plugin-ts`.
@@ -20,7 +125,7 @@ type ResolverTs = Resolver & OperationParamsResolver & {
20
125
  * @example
21
126
  * resolver.resolveName('list pets status 200') // → 'ListPetsStatus200'
22
127
  */
23
- resolveName(name: string): string;
128
+ resolveTypeName(name: string): string;
24
129
  /**
25
130
  * Resolves the file/path name for a given identifier using PascalCase.
26
131
  *
@@ -244,35 +349,47 @@ type Options = {
244
349
  */
245
350
  compatibilityPreset?: CompatibilityPreset;
246
351
  /**
247
- * Array of named resolvers that control naming conventions.
248
- * Later entries override earlier ones (last wins).
249
- * Built-in: `resolverTs` (default), `resolverTsLegacy`.
250
- * @default [resolverTs]
352
+ * Override naming conventions. When a method returns `null` or `undefined`, the preset
353
+ * resolver (`resolverTs` / `resolverTsLegacy`) is used as fallback.
251
354
  */
252
- resolvers?: Array<ResolverTs>;
355
+ resolver?: Partial<ResolverTs> & ThisType<ResolverTs>;
253
356
  /**
254
- * Array of AST visitors applied to each SchemaNode/OperationNode before printing.
255
- * Uses `transform()` from `@kubb/ast` visitors can modify, replace, or annotate nodes.
357
+ * AST visitor applied to each schema/operation node before printing.
358
+ * Returning `null` or `undefined` from a visitor method falls back to the preset transformer.
256
359
  *
257
360
  * @example Remove writeOnly properties from response types
258
361
  * ```ts
259
- * transformers: [{
362
+ * transformer: {
260
363
  * property(node) {
261
364
  * if (node.schema.writeOnly) return undefined
262
365
  * }
263
- * }]
366
+ * }
264
367
  * ```
368
+ */
369
+ transformer?: Visitor;
370
+ /**
371
+ * Override individual printer node handlers to customise rendering of specific schema types.
372
+ *
373
+ * Each key is a `SchemaType` (e.g. `'date'`, `'string'`). The function replaces the
374
+ * built-in handler for that type. Use `this.transform` to recurse into nested schema nodes.
265
375
  *
266
- * @example Force all dates to plain strings
376
+ * @example Override the `date` node to use the `Date` object type
267
377
  * ```ts
268
- * transformers: [{
269
- * schema(node) {
270
- * if (node.type === 'date') return { ...node, type: 'string' }
271
- * }
272
- * }]
378
+ * import ts from 'typescript'
379
+ * pluginTs({
380
+ * printer: {
381
+ * nodes: {
382
+ * date(node) {
383
+ * return ts.factory.createTypeReferenceNode('Date', [])
384
+ * },
385
+ * },
386
+ * },
387
+ * })
273
388
  * ```
274
389
  */
275
- transformers?: Array<Visitor>;
390
+ printer?: {
391
+ nodes?: PrinterTsNodes;
392
+ };
276
393
  } & EnumTypeOptions;
277
394
  type ResolvedOptions = {
278
395
  output: Output;
@@ -284,7 +401,7 @@ type ResolvedOptions = {
284
401
  arrayType: NonNullable<Options['arrayType']>;
285
402
  syntaxType: NonNullable<Options['syntaxType']>;
286
403
  paramsCasing: Options['paramsCasing'];
287
- transformers: Array<Visitor>;
404
+ printer: Options['printer'];
288
405
  };
289
406
  type PluginTs = PluginFactoryOptions<'plugin-ts', Options, ResolvedOptions, never, ResolvePathOptions, ResolverTs>;
290
407
  //#endregion
@@ -326,35 +443,24 @@ declare function Enum({
326
443
  type Props = {
327
444
  name: string;
328
445
  node: SchemaNode;
329
- optionalType: PluginTs['resolvedOptions']['optionalType'];
330
- arrayType: PluginTs['resolvedOptions']['arrayType'];
446
+ /**
447
+ * Pre-configured printer instance created by the generator.
448
+ * Created with `printerTs({ ..., nodes: options.printer?.nodes })`.
449
+ */
450
+ printer: Printer<PrinterTsFactory>;
331
451
  enumType: PluginTs['resolvedOptions']['enumType'];
332
452
  enumTypeSuffix: PluginTs['resolvedOptions']['enumTypeSuffix'];
333
453
  enumKeyCasing: PluginTs['resolvedOptions']['enumKeyCasing'];
334
- syntaxType: PluginTs['resolvedOptions']['syntaxType'];
335
454
  resolver: PluginTs['resolver'];
336
- description?: string;
337
- keysToOmit?: string[];
338
- /**
339
- * Names of top-level schemas that are enums.
340
- * Used so the printer's `ref` handler can use the suffixed type name (e.g. `StatusKey`)
341
- * instead of the plain PascalCase name (e.g. `Status`) when resolving `$ref` enum targets.
342
- */
343
- enumSchemaNames?: Set<string>;
344
455
  };
345
456
  declare function Type({
346
457
  name,
347
458
  node,
348
- keysToOmit,
349
- optionalType,
350
- arrayType,
351
- syntaxType,
459
+ printer,
352
460
  enumType,
353
461
  enumTypeSuffix,
354
462
  enumKeyCasing,
355
- description,
356
- resolver,
357
- enumSchemaNames
463
+ resolver
358
464
  }: Props): FabricReactNode;
359
465
  //#endregion
360
466
  //#region src/generators/typeGenerator.d.ts
@@ -430,85 +536,6 @@ declare const functionPrinter: (options?: FunctionPrinterOptions | undefined) =>
430
536
  print: (node: FunctionNode) => string | null | undefined;
431
537
  };
432
538
  //#endregion
433
- //#region src/printers/printerTs.d.ts
434
- type TsOptions = {
435
- /**
436
- * @default `'questionToken'`
437
- */
438
- optionalType: PluginTs['resolvedOptions']['optionalType'];
439
- /**
440
- * @default `'array'`
441
- */
442
- arrayType: PluginTs['resolvedOptions']['arrayType'];
443
- /**
444
- * @default `'inlineLiteral'`
445
- */
446
- enumType: PluginTs['resolvedOptions']['enumType'];
447
- /**
448
- * Suffix appended to the generated type alias name when `enumType` is `asConst` or `asPascalConst`.
449
- *
450
- * @default `'Key'`
451
- */
452
- enumTypeSuffix?: PluginTs['resolvedOptions']['enumTypeSuffix'];
453
- /**
454
- * Controls whether a `type` alias or `interface` declaration is emitted.
455
- * @default `'type'`
456
- */
457
- syntaxType?: PluginTs['resolvedOptions']['syntaxType'];
458
- /**
459
- * When set, `printer.print(node)` produces a full `type Name = …` declaration.
460
- * When omitted, `printer.print(node)` returns the raw type node.
461
- */
462
- name?: string;
463
- /**
464
- * JSDoc `@description` comment added to the generated type or interface declaration.
465
- */
466
- description?: string;
467
- /**
468
- * Property keys to exclude from the generated type via `Omit<Type, Keys>`.
469
- * Forces type-alias syntax even when `syntaxType` is `'interface'`.
470
- */
471
- keysToOmit?: Array<string>;
472
- /**
473
- * Resolver used to transform raw schema names into valid TypeScript identifiers.
474
- */
475
- resolver: ResolverTs;
476
- /**
477
- * Names of top-level schemas that are enums.
478
- * When set, the `ref` handler uses the suffixed type name (e.g. `StatusKey`) for enum refs
479
- * instead of the plain PascalCase name, so imports align with what the enum file actually exports.
480
- */
481
- enumSchemaNames?: Set<string>;
482
- };
483
- /**
484
- * TypeScript printer factory options: maps `SchemaNode` → `ts.TypeNode` (raw) or `ts.Node` (full declaration).
485
- */
486
- type TsPrinter = PrinterFactoryOptions<'typescript', TsOptions, ts.TypeNode, string>;
487
- /**
488
- * TypeScript type printer built with `definePrinter`.
489
- *
490
- * Converts a `SchemaNode` AST node into a TypeScript AST node:
491
- * - **`printer.print(node)`** — when `options.typeName` is set, returns a full
492
- * `type Name = …` or `interface Name { … }` declaration (`ts.Node`).
493
- * Without `typeName`, returns the raw `ts.TypeNode` for the schema.
494
- *
495
- * Dispatches on `node.type` to the appropriate handler in `nodes`. Options are closed
496
- * over per printer instance, so each call to `printerTs(options)` produces an independent printer.
497
- *
498
- * @example Raw type node (no `typeName`)
499
- * ```ts
500
- * const printer = printerTs({ optionalType: 'questionToken', arrayType: 'array', enumType: 'inlineLiteral' })
501
- * const typeNode = printer.print(schemaNode) // ts.TypeNode
502
- * ```
503
- *
504
- * @example Full declaration (with `typeName`)
505
- * ```ts
506
- * const printer = printerTs({ optionalType: 'questionToken', arrayType: 'array', enumType: 'inlineLiteral', typeName: 'MyType' })
507
- * const declaration = printer.print(schemaNode) // ts.TypeAliasDeclaration | ts.InterfaceDeclaration
508
- * ```
509
- */
510
- declare const printerTs: (options?: TsOptions | undefined) => _$_kubb_core0.Printer<TsPrinter>;
511
- //#endregion
512
539
  //#region src/resolvers/resolverTs.d.ts
513
540
  /**
514
541
  * Resolver for `@kubb/plugin-ts` that provides the default naming and path-resolution
@@ -555,5 +582,5 @@ declare const resolverTs: ResolverTs;
555
582
  */
556
583
  declare const resolverTsLegacy: ResolverTs;
557
584
  //#endregion
558
- export { Enum, type PluginTs, type ResolverTs, Type, functionPrinter, pluginTs, pluginTsName, printerTs, resolverTs, resolverTsLegacy, typeGenerator };
585
+ export { Enum, type PluginTs, type PrinterTsFactory, type PrinterTsNodes, type PrinterTsOptions, type ResolverTs, Type, functionPrinter, pluginTs, pluginTsName, printerTs, resolverTs, resolverTsLegacy, typeGenerator };
559
586
  //# sourceMappingURL=index.d.ts.map