@kubb/plugin-ts 5.0.0-beta.3 → 5.0.0-beta.31
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/README.md +26 -5
- package/dist/index.cjs +416 -117
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +81 -58
- package/dist/index.js +415 -120
- package/dist/index.js.map +1 -1
- package/extension.yaml +1078 -0
- package/package.json +11 -13
- package/src/components/Enum.tsx +3 -3
- package/src/factory.ts +23 -20
- package/src/generators/typeGenerator.tsx +100 -39
- package/src/plugin.ts +20 -21
- package/src/printers/printerTs.ts +31 -30
- package/src/resolvers/resolverTs.ts +28 -25
- package/src/types.ts +44 -37
- package/src/utils.ts +23 -13
package/dist/index.d.ts
CHANGED
|
@@ -82,7 +82,7 @@ type PrinterTsOptions = {
|
|
|
82
82
|
* Properties to exclude using `Omit<Type, Keys>`.
|
|
83
83
|
* Forces type alias syntax regardless of `syntaxType` setting.
|
|
84
84
|
*/
|
|
85
|
-
keysToOmit?: Array<string
|
|
85
|
+
keysToOmit?: Array<string> | null;
|
|
86
86
|
/**
|
|
87
87
|
* Transforms raw schema names into valid TypeScript identifiers.
|
|
88
88
|
*/
|
|
@@ -238,7 +238,7 @@ type EnumTypeOptions = {
|
|
|
238
238
|
/**
|
|
239
239
|
* Suffix appended to the generated type alias name.
|
|
240
240
|
*
|
|
241
|
-
* Only affects the type alias
|
|
241
|
+
* Only affects the type alias; the const object name is unchanged.
|
|
242
242
|
*
|
|
243
243
|
* @default 'Key'
|
|
244
244
|
* @example enumTypeSuffix: 'Value' → `export type PetStatusValue = …`
|
|
@@ -293,80 +293,88 @@ type EnumTypeOptions = {
|
|
|
293
293
|
enumTypeSuffix?: never;
|
|
294
294
|
/**
|
|
295
295
|
* `enumKeyCasing` has no effect for this `enumType`.
|
|
296
|
-
* Literal and inlineLiteral modes emit only values
|
|
296
|
+
* Literal and inlineLiteral modes emit only values; keys are discarded entirely.
|
|
297
297
|
*/
|
|
298
298
|
enumKeyCasing?: never;
|
|
299
299
|
};
|
|
300
300
|
type Options = {
|
|
301
301
|
/**
|
|
302
|
-
*
|
|
303
|
-
*
|
|
302
|
+
* Where the generated `.ts` files are written and how they are exported.
|
|
303
|
+
*
|
|
304
|
+
* @default { path: 'types', barrel: { type: 'named' } }
|
|
304
305
|
*/
|
|
305
306
|
output?: Output;
|
|
306
307
|
/**
|
|
307
|
-
*
|
|
308
|
-
*
|
|
308
|
+
* Media type read from the OpenAPI spec when an operation defines several.
|
|
309
|
+
* Defaults to the first JSON-compatible media type Kubb finds.
|
|
309
310
|
*/
|
|
310
311
|
contentType?: 'application/json' | (string & {});
|
|
311
312
|
/**
|
|
312
|
-
*
|
|
313
|
+
* Split generated files into subfolders based on the operation's tag.
|
|
313
314
|
*/
|
|
314
315
|
group?: Group;
|
|
315
316
|
/**
|
|
316
|
-
*
|
|
317
|
+
* Skip operations matching at least one entry in the list.
|
|
317
318
|
*/
|
|
318
319
|
exclude?: Array<Exclude>;
|
|
319
320
|
/**
|
|
320
|
-
*
|
|
321
|
+
* Restrict generation to operations matching at least one entry in the list.
|
|
321
322
|
*/
|
|
322
323
|
include?: Array<Include>;
|
|
323
324
|
/**
|
|
324
|
-
*
|
|
325
|
+
* Apply a different options object to operations matching a pattern.
|
|
325
326
|
*/
|
|
326
327
|
override?: Array<Override<ResolvedOptions>>;
|
|
327
328
|
/**
|
|
328
|
-
*
|
|
329
|
-
* - 'type'
|
|
330
|
-
* - 'interface'
|
|
329
|
+
* Whether object schemas are emitted as `type` aliases or `interface` declarations.
|
|
330
|
+
* - `'type'` emits closed type aliases. Safer default for generated code.
|
|
331
|
+
* - `'interface'` emits interfaces. Useful when consumers rely on declaration merging.
|
|
332
|
+
*
|
|
331
333
|
* @default 'type'
|
|
334
|
+
* @see https://www.totaltypescript.com/type-vs-interface-which-should-you-use
|
|
332
335
|
*/
|
|
333
336
|
syntaxType?: 'type' | 'interface';
|
|
334
337
|
/**
|
|
335
|
-
*
|
|
336
|
-
* - 'questionToken'
|
|
337
|
-
* - 'undefined'
|
|
338
|
-
* - 'questionTokenAndUndefined'
|
|
338
|
+
* How optional properties are written in generated types.
|
|
339
|
+
* - `'questionToken'` — `type?: string`. The property may be missing.
|
|
340
|
+
* - `'undefined'` — `type: string | undefined`. Required to exist, may be `undefined`.
|
|
341
|
+
* - `'questionTokenAndUndefined'` — `type?: string | undefined`. Strictest.
|
|
342
|
+
*
|
|
339
343
|
* @default 'questionToken'
|
|
344
|
+
* @note Pick `'questionTokenAndUndefined'` when `exactOptionalPropertyTypes` is on in `tsconfig.json`.
|
|
340
345
|
*/
|
|
341
346
|
optionalType?: 'questionToken' | 'undefined' | 'questionTokenAndUndefined';
|
|
342
347
|
/**
|
|
343
|
-
*
|
|
344
|
-
* - '
|
|
345
|
-
* - '
|
|
348
|
+
* Syntax used for array types.
|
|
349
|
+
* - `'array'` — `Type[]`. Shorter.
|
|
350
|
+
* - `'generic'` — `Array<Type>`. More readable for complex element types.
|
|
351
|
+
*
|
|
346
352
|
* @default 'array'
|
|
347
353
|
*/
|
|
348
354
|
arrayType?: 'generic' | 'array';
|
|
349
355
|
/**
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
* @note
|
|
356
|
+
* Rename properties inside `PathParams`, `QueryParams`, and `HeaderParams` types.
|
|
357
|
+
* Response and request body types are not affected.
|
|
358
|
+
*
|
|
359
|
+
* @note Every plugin that touches operation parameters must use the same value.
|
|
354
360
|
*/
|
|
355
361
|
paramsCasing?: 'camelcase';
|
|
356
362
|
/**
|
|
357
|
-
*
|
|
363
|
+
* Custom generators that run alongside the built-in TypeScript generators.
|
|
358
364
|
*/
|
|
359
365
|
generators?: Array<Generator<PluginTs>>;
|
|
360
366
|
/**
|
|
361
|
-
* Override
|
|
362
|
-
*
|
|
367
|
+
* Override how names and file paths are built for generated symbols.
|
|
368
|
+
* Methods you omit fall back to the default `resolverTs`. `this` is bound to the
|
|
369
|
+
* full resolver, so `this.default(name, 'function')` delegates to the built-in
|
|
370
|
+
* implementation.
|
|
363
371
|
*/
|
|
364
372
|
resolver?: Partial<ResolverTs> & ThisType<ResolverTs>;
|
|
365
373
|
/**
|
|
366
|
-
* AST visitor applied to each schema
|
|
367
|
-
*
|
|
374
|
+
* AST visitor applied to each schema or operation node before printing.
|
|
375
|
+
* Methods you omit fall back to the preset transformer.
|
|
368
376
|
*
|
|
369
|
-
* @example
|
|
377
|
+
* @example Drop writeOnly properties from response types
|
|
370
378
|
* ```ts
|
|
371
379
|
* transformer: {
|
|
372
380
|
* property(node) {
|
|
@@ -377,18 +385,17 @@ type Options = {
|
|
|
377
385
|
*/
|
|
378
386
|
transformer?: ast.Visitor;
|
|
379
387
|
/**
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
* built-in handler for that type. Use `this.transform` to recurse into nested schema nodes.
|
|
388
|
+
* Replace the TypeScript handler for a specific schema type (`'integer'`, `'date'`, ...).
|
|
389
|
+
* Each handler returns a TypeScript AST node for that schema type. Use `this.transform`
|
|
390
|
+
* to recurse into nested schema nodes.
|
|
384
391
|
*
|
|
385
|
-
* @example
|
|
392
|
+
* @example Use the JavaScript `Date` object for date schemas
|
|
386
393
|
* ```ts
|
|
387
394
|
* import ts from 'typescript'
|
|
388
395
|
* pluginTs({
|
|
389
396
|
* printer: {
|
|
390
397
|
* nodes: {
|
|
391
|
-
* date(
|
|
398
|
+
* date() {
|
|
392
399
|
* return ts.factory.createTypeReferenceNode('Date', [])
|
|
393
400
|
* },
|
|
394
401
|
* },
|
|
@@ -405,7 +412,7 @@ type ResolvedOptions = {
|
|
|
405
412
|
exclude: Array<Exclude>;
|
|
406
413
|
include: Array<Include> | undefined;
|
|
407
414
|
override: Array<Override<ResolvedOptions>>;
|
|
408
|
-
group: Group |
|
|
415
|
+
group: Group | null;
|
|
409
416
|
enumType: NonNullable<Options['enumType']>;
|
|
410
417
|
enumTypeSuffix: NonNullable<Options['enumTypeSuffix']>;
|
|
411
418
|
enumKeyCasing: EnumKeyCasing;
|
|
@@ -484,26 +491,41 @@ declare function Type({
|
|
|
484
491
|
}: Props): KubbReactNode;
|
|
485
492
|
//#endregion
|
|
486
493
|
//#region src/generators/typeGenerator.d.ts
|
|
494
|
+
/**
|
|
495
|
+
* Built-in generator for `@kubb/plugin-ts`. Emits one TypeScript file per
|
|
496
|
+
* schema in the spec plus per-operation request, response, and parameter
|
|
497
|
+
* types. Drop-replace with a custom `Generator<PluginTs>` to change how
|
|
498
|
+
* TypeScript output is produced.
|
|
499
|
+
*/
|
|
487
500
|
declare const typeGenerator: _$_kubb_core0.Generator<PluginTs, unknown>;
|
|
488
501
|
//#endregion
|
|
489
502
|
//#region src/plugin.d.ts
|
|
490
503
|
/**
|
|
491
|
-
* Canonical plugin name for `@kubb/plugin-ts
|
|
504
|
+
* Canonical plugin name for `@kubb/plugin-ts`. Used for driver lookups and
|
|
505
|
+
* cross-plugin dependency references.
|
|
492
506
|
*/
|
|
493
507
|
declare const pluginTsName = "plugin-ts";
|
|
494
508
|
/**
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
* and writes barrel files based on `output.barrelType`.
|
|
509
|
+
* Generates TypeScript `type` aliases and `interface` declarations from an
|
|
510
|
+
* OpenAPI spec. The foundation that every other Kubb plugin builds on:
|
|
511
|
+
* clients, query hooks, mocks, and validators all reference the names this
|
|
512
|
+
* plugin produces.
|
|
500
513
|
*
|
|
501
514
|
* @example
|
|
502
515
|
* ```ts
|
|
503
|
-
* import
|
|
516
|
+
* import { defineConfig } from 'kubb'
|
|
517
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
504
518
|
*
|
|
505
519
|
* export default defineConfig({
|
|
506
|
-
*
|
|
520
|
+
* input: { path: './petStore.yaml' },
|
|
521
|
+
* output: { path: './src/gen' },
|
|
522
|
+
* plugins: [
|
|
523
|
+
* pluginTs({
|
|
524
|
+
* output: { path: './types' },
|
|
525
|
+
* enumType: 'asConst',
|
|
526
|
+
* optionalType: 'questionTokenAndUndefined',
|
|
527
|
+
* }),
|
|
528
|
+
* ],
|
|
507
529
|
* })
|
|
508
530
|
* ```
|
|
509
531
|
*/
|
|
@@ -552,26 +574,27 @@ type FunctionPrinterOptions = {
|
|
|
552
574
|
declare const functionPrinter: (options?: FunctionPrinterOptions | undefined) => {
|
|
553
575
|
name: "functionParameters";
|
|
554
576
|
options: FunctionPrinterOptions;
|
|
555
|
-
transform: (node: ast.FunctionParamNode) => string | null
|
|
556
|
-
print: (node: ast.FunctionParamNode) => string | null
|
|
577
|
+
transform: (node: ast.FunctionParamNode) => string | null;
|
|
578
|
+
print: (node: ast.FunctionParamNode) => string | null;
|
|
557
579
|
};
|
|
558
580
|
//#endregion
|
|
559
581
|
//#region src/resolvers/resolverTs.d.ts
|
|
560
582
|
/**
|
|
561
|
-
*
|
|
562
|
-
*
|
|
563
|
-
*
|
|
583
|
+
* Default resolver used by `@kubb/plugin-ts`. Decides the names and file paths
|
|
584
|
+
* for every generated TypeScript type. Import this in other plugins that need
|
|
585
|
+
* to reference the exact names `plugin-ts` produces without duplicating the
|
|
586
|
+
* casing/file-layout rules.
|
|
564
587
|
*
|
|
565
|
-
* The `default` method is
|
|
566
|
-
*
|
|
588
|
+
* The `default` method is supplied by `defineResolver`. It uses PascalCase for
|
|
589
|
+
* type names and PascalCase-with-isFile for files.
|
|
567
590
|
*
|
|
568
|
-
* @example
|
|
591
|
+
* @example Resolve a type and file name
|
|
569
592
|
* ```ts
|
|
570
|
-
* import {
|
|
593
|
+
* import { resolverTs } from '@kubb/plugin-ts'
|
|
571
594
|
*
|
|
572
|
-
*
|
|
573
|
-
*
|
|
574
|
-
*
|
|
595
|
+
* resolverTs.default('list pets', 'type') // 'ListPets'
|
|
596
|
+
* resolverTs.resolvePathName('list pets', 'file') // 'ListPets'
|
|
597
|
+
* resolverTs.resolveResponseStatusName(node, 200) // 'ListPetsStatus200'
|
|
575
598
|
* ```
|
|
576
599
|
*/
|
|
577
600
|
declare const resolverTs: ResolverTs;
|