@kubb/plugin-ts 5.0.0-beta.15 → 5.0.0-beta.25
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.cjs +79 -61
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +81 -58
- package/dist/index.js +80 -62
- package/dist/index.js.map +1 -1
- package/extension.yaml +690 -247
- package/package.json +5 -5
- package/src/components/Enum.tsx +3 -3
- package/src/factory.ts +8 -13
- package/src/generators/typeGenerator.tsx +22 -13
- package/src/plugin.ts +18 -9
- package/src/printers/printerTs.ts +24 -27
- package/src/resolvers/resolverTs.ts +11 -10
- package/src/types.ts +44 -37
- package/src/utils.ts +9 -9
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;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
-
import {
|
|
2
|
+
import { parserTs } from "@kubb/parser-ts";
|
|
3
3
|
import { File, jsxRendererSync } from "@kubb/renderer-jsx";
|
|
4
4
|
import { ast, defineGenerator, definePlugin, defineResolver } from "@kubb/core";
|
|
5
5
|
import { isNumber } from "remeda";
|
|
@@ -634,13 +634,13 @@ function Enum({ node, enumType, enumTypeSuffix, enumKeyCasing, resolver }) {
|
|
|
634
634
|
isExportable: true,
|
|
635
635
|
isIndexable: true,
|
|
636
636
|
isTypeOnly: false,
|
|
637
|
-
children:
|
|
637
|
+
children: parserTs.print(nameNode)
|
|
638
638
|
}), /* @__PURE__ */ jsx(File.Source, {
|
|
639
639
|
name: typeName,
|
|
640
640
|
isIndexable: true,
|
|
641
641
|
isExportable: ENUM_TYPES_WITH_RUNTIME_VALUE.has(enumType),
|
|
642
642
|
isTypeOnly: ENUM_TYPES_WITH_TYPE_ONLY.has(enumType),
|
|
643
|
-
children:
|
|
643
|
+
children: parserTs.print(typeNode)
|
|
644
644
|
})] });
|
|
645
645
|
}
|
|
646
646
|
//#endregion
|
|
@@ -703,14 +703,14 @@ function buildPropertyJSDocComments(schema) {
|
|
|
703
703
|
const meta = ast.syncSchemaRef(schema);
|
|
704
704
|
const isArray = meta?.primitive === "array";
|
|
705
705
|
return [
|
|
706
|
-
meta && "description" in meta && meta.description ? `@description ${jsStringEscape(meta.description)}` :
|
|
707
|
-
meta && "deprecated" in meta && meta.deprecated ? "@deprecated" :
|
|
708
|
-
!isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` :
|
|
709
|
-
!isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` :
|
|
710
|
-
meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` :
|
|
711
|
-
meta && "default" in meta && meta.default !== void 0 ? `@default ${"primitive" in meta && meta.primitive === "string" ? stringify(meta.default) : meta.default}` :
|
|
712
|
-
meta && "example" in meta && meta.example !== void 0 ? `@example ${meta.example}` :
|
|
713
|
-
meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, "optional" in schema && schema.optional ? " | undefined" :
|
|
706
|
+
meta && "description" in meta && meta.description ? `@description ${jsStringEscape(meta.description)}` : null,
|
|
707
|
+
meta && "deprecated" in meta && meta.deprecated ? "@deprecated" : null,
|
|
708
|
+
!isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` : null,
|
|
709
|
+
!isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` : null,
|
|
710
|
+
meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` : null,
|
|
711
|
+
meta && "default" in meta && meta.default !== void 0 ? `@default ${"primitive" in meta && meta.primitive === "string" ? stringify(meta.default) : meta.default}` : null,
|
|
712
|
+
meta && "example" in meta && meta.example !== void 0 ? `@example ${meta.example}` : null,
|
|
713
|
+
meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, "optional" in schema && schema.optional ? " | undefined" : null].filter(Boolean).join("") : null
|
|
714
714
|
].filter(Boolean);
|
|
715
715
|
}
|
|
716
716
|
function buildParams(node, { params, resolver }) {
|
|
@@ -876,7 +876,7 @@ const printerTs = ast.definePrinter((options) => {
|
|
|
876
876
|
date: dateOrStringNode,
|
|
877
877
|
time: dateOrStringNode,
|
|
878
878
|
ref(node) {
|
|
879
|
-
if (!node.name) return;
|
|
879
|
+
if (!node.name) return null;
|
|
880
880
|
const refName = node.ref ? ast.extractRefName(node.ref) ?? node.name : node.name;
|
|
881
881
|
return createTypeReferenceNode(node.ref && ENUM_TYPES_WITH_KEY_SUFFIX.has(this.options.enumType) && this.options.enumTypeSuffix && this.options.enumSchemaNames?.has(refName) ? this.options.resolver.resolveEnumKeyName({ name: refName }, this.options.enumTypeSuffix) : node.ref ? this.options.resolver.default(refName, "type") : refName, void 0);
|
|
882
882
|
},
|
|
@@ -913,16 +913,16 @@ const printerTs = ast.definePrinter((options) => {
|
|
|
913
913
|
return createIntersectionDeclaration({
|
|
914
914
|
withParentheses: true,
|
|
915
915
|
nodes: buildMemberNodes(node.members, this.transform)
|
|
916
|
-
}) ??
|
|
916
|
+
}) ?? null;
|
|
917
917
|
},
|
|
918
918
|
array(node) {
|
|
919
919
|
return createArrayDeclaration({
|
|
920
920
|
nodes: (node.items ?? []).map((item) => this.transform(item)).filter(isNonNullable),
|
|
921
921
|
arrayType: this.options.arrayType
|
|
922
|
-
}) ??
|
|
922
|
+
}) ?? null;
|
|
923
923
|
},
|
|
924
924
|
tuple(node) {
|
|
925
|
-
return buildTupleNode(node, this.transform);
|
|
925
|
+
return buildTupleNode(node, this.transform) ?? null;
|
|
926
926
|
},
|
|
927
927
|
object(node) {
|
|
928
928
|
const { transform, options } = this;
|
|
@@ -949,32 +949,34 @@ const printerTs = ast.definePrinter((options) => {
|
|
|
949
949
|
},
|
|
950
950
|
print(node) {
|
|
951
951
|
const { name, syntaxType = "type", description, keysToOmit } = this.options;
|
|
952
|
-
|
|
953
|
-
if (!
|
|
952
|
+
const transformed = this.transform(node);
|
|
953
|
+
if (!transformed) return null;
|
|
954
954
|
const meta = ast.syncSchemaRef(node);
|
|
955
955
|
if (!name) {
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
return
|
|
956
|
+
const withNullable = meta.nullable ? createUnionDeclaration({ nodes: [transformed, keywordTypeNodes.null] }) : transformed;
|
|
957
|
+
const result = (meta.nullish || meta.optional) && addsUndefined ? createUnionDeclaration({ nodes: [withNullable, keywordTypeNodes.undefined] }) : withNullable;
|
|
958
|
+
return parserTs.print(result);
|
|
959
959
|
}
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
960
|
+
const inner = (() => {
|
|
961
|
+
const omitted = keysToOmit?.length ? createOmitDeclaration({
|
|
962
|
+
keys: keysToOmit,
|
|
963
|
+
type: transformed,
|
|
964
|
+
nonNullable: true
|
|
965
|
+
}) : transformed;
|
|
966
|
+
const withNullable = meta.nullable ? createUnionDeclaration({ nodes: [omitted, keywordTypeNodes.null] }) : omitted;
|
|
967
|
+
return meta.nullish || meta.optional ? createUnionDeclaration({ nodes: [withNullable, keywordTypeNodes.undefined] }) : withNullable;
|
|
968
|
+
})();
|
|
969
|
+
const typeNode = createTypeDeclaration({
|
|
969
970
|
name,
|
|
970
971
|
isExportable: true,
|
|
971
972
|
type: inner,
|
|
972
|
-
syntax:
|
|
973
|
+
syntax: syntaxType === "type" || inner.kind === syntaxKind.union || !!keysToOmit?.length ? "type" : "interface",
|
|
973
974
|
comments: buildPropertyJSDocComments({
|
|
974
975
|
...meta,
|
|
975
976
|
description
|
|
976
977
|
})
|
|
977
|
-
})
|
|
978
|
+
});
|
|
979
|
+
return parserTs.print(typeNode);
|
|
978
980
|
}
|
|
979
981
|
};
|
|
980
982
|
});
|
|
@@ -993,15 +995,21 @@ function getPerContentTypeName(dataName, suffix) {
|
|
|
993
995
|
if (dataName.endsWith("Data")) return suffix.endsWith("Data") ? dataName.slice(0, -4) + suffix : `${dataName.slice(0, -4)}${suffix}Data`;
|
|
994
996
|
return dataName + suffix;
|
|
995
997
|
}
|
|
998
|
+
/**
|
|
999
|
+
* Built-in generator for `@kubb/plugin-ts`. Emits one TypeScript file per
|
|
1000
|
+
* schema in the spec plus per-operation request, response, and parameter
|
|
1001
|
+
* types. Drop-replace with a custom `Generator<PluginTs>` to change how
|
|
1002
|
+
* TypeScript output is produced.
|
|
1003
|
+
*/
|
|
996
1004
|
const typeGenerator = defineGenerator({
|
|
997
1005
|
name: "typescript",
|
|
998
1006
|
renderer: jsxRendererSync,
|
|
999
1007
|
schema(node, ctx) {
|
|
1000
1008
|
const { enumType, enumTypeSuffix, enumKeyCasing, syntaxType, optionalType, arrayType, output, group, printer } = ctx.options;
|
|
1001
|
-
const { adapter, config, resolver, root
|
|
1009
|
+
const { adapter, config, resolver, root } = ctx;
|
|
1002
1010
|
if (!node.name) return;
|
|
1003
1011
|
const mode = ctx.getMode(output);
|
|
1004
|
-
const enumSchemaNames = new Set(
|
|
1012
|
+
const enumSchemaNames = new Set(ctx.meta.enumNames);
|
|
1005
1013
|
function resolveImportName(schemaName) {
|
|
1006
1014
|
if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && enumTypeSuffix && enumSchemaNames.has(schemaName)) return resolver.resolveEnumKeyName({ name: schemaName }, enumTypeSuffix);
|
|
1007
1015
|
return resolver.resolveTypeName(schemaName);
|
|
@@ -1014,7 +1022,7 @@ const typeGenerator = defineGenerator({
|
|
|
1014
1022
|
}, {
|
|
1015
1023
|
root,
|
|
1016
1024
|
output,
|
|
1017
|
-
group
|
|
1025
|
+
group: group ?? void 0
|
|
1018
1026
|
}).path
|
|
1019
1027
|
}));
|
|
1020
1028
|
const isEnumSchema = !!ast.narrowSchema(node, ast.schemaTypes.enum);
|
|
@@ -1026,7 +1034,7 @@ const typeGenerator = defineGenerator({
|
|
|
1026
1034
|
}, {
|
|
1027
1035
|
root,
|
|
1028
1036
|
output,
|
|
1029
|
-
group
|
|
1037
|
+
group: group ?? void 0
|
|
1030
1038
|
})
|
|
1031
1039
|
};
|
|
1032
1040
|
const schemaPrinter = printerTs({
|
|
@@ -1045,11 +1053,11 @@ const typeGenerator = defineGenerator({
|
|
|
1045
1053
|
baseName: meta.file.baseName,
|
|
1046
1054
|
path: meta.file.path,
|
|
1047
1055
|
meta: meta.file.meta,
|
|
1048
|
-
banner: resolver.resolveBanner(
|
|
1056
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
1049
1057
|
output,
|
|
1050
1058
|
config
|
|
1051
1059
|
}),
|
|
1052
|
-
footer: resolver.resolveFooter(
|
|
1060
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
1053
1061
|
output,
|
|
1054
1062
|
config
|
|
1055
1063
|
}),
|
|
@@ -1075,7 +1083,7 @@ const typeGenerator = defineGenerator({
|
|
|
1075
1083
|
},
|
|
1076
1084
|
operation(node, ctx) {
|
|
1077
1085
|
const { enumType, enumTypeSuffix, enumKeyCasing, optionalType, arrayType, syntaxType, paramsCasing, group, output, printer } = ctx.options;
|
|
1078
|
-
const { adapter, config, resolver, root
|
|
1086
|
+
const { adapter, config, resolver, root } = ctx;
|
|
1079
1087
|
const mode = ctx.getMode(output);
|
|
1080
1088
|
const params = ast.caseParams(node.parameters, paramsCasing);
|
|
1081
1089
|
const meta = { file: resolver.resolveFile({
|
|
@@ -1086,9 +1094,9 @@ const typeGenerator = defineGenerator({
|
|
|
1086
1094
|
}, {
|
|
1087
1095
|
root,
|
|
1088
1096
|
output,
|
|
1089
|
-
group
|
|
1097
|
+
group: group ?? void 0
|
|
1090
1098
|
}) };
|
|
1091
|
-
const enumSchemaNames = new Set(
|
|
1099
|
+
const enumSchemaNames = new Set(ctx.meta.enumNames);
|
|
1092
1100
|
function resolveImportName(schemaName) {
|
|
1093
1101
|
if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && enumTypeSuffix && enumSchemaNames.has(schemaName)) return resolver.resolveEnumKeyName({ name: schemaName }, enumTypeSuffix);
|
|
1094
1102
|
return resolver.resolveTypeName(schemaName);
|
|
@@ -1103,7 +1111,7 @@ const typeGenerator = defineGenerator({
|
|
|
1103
1111
|
}, {
|
|
1104
1112
|
root,
|
|
1105
1113
|
output,
|
|
1106
|
-
group
|
|
1114
|
+
group: group ?? void 0
|
|
1107
1115
|
}).path
|
|
1108
1116
|
}));
|
|
1109
1117
|
const schemaPrinter = printerTs({
|
|
@@ -1227,11 +1235,11 @@ const typeGenerator = defineGenerator({
|
|
|
1227
1235
|
baseName: meta.file.baseName,
|
|
1228
1236
|
path: meta.file.path,
|
|
1229
1237
|
meta: meta.file.meta,
|
|
1230
|
-
banner: resolver.resolveBanner(
|
|
1238
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
1231
1239
|
output,
|
|
1232
1240
|
config
|
|
1233
1241
|
}),
|
|
1234
|
-
footer: resolver.resolveFooter(
|
|
1242
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
1235
1243
|
output,
|
|
1236
1244
|
config
|
|
1237
1245
|
}),
|
|
@@ -1249,20 +1257,21 @@ const typeGenerator = defineGenerator({
|
|
|
1249
1257
|
//#endregion
|
|
1250
1258
|
//#region src/resolvers/resolverTs.ts
|
|
1251
1259
|
/**
|
|
1252
|
-
*
|
|
1253
|
-
*
|
|
1254
|
-
*
|
|
1260
|
+
* Default resolver used by `@kubb/plugin-ts`. Decides the names and file paths
|
|
1261
|
+
* for every generated TypeScript type. Import this in other plugins that need
|
|
1262
|
+
* to reference the exact names `plugin-ts` produces without duplicating the
|
|
1263
|
+
* casing/file-layout rules.
|
|
1255
1264
|
*
|
|
1256
|
-
* The `default` method is
|
|
1257
|
-
*
|
|
1265
|
+
* The `default` method is supplied by `defineResolver`. It uses PascalCase for
|
|
1266
|
+
* type names and PascalCase-with-isFile for files.
|
|
1258
1267
|
*
|
|
1259
|
-
* @example
|
|
1268
|
+
* @example Resolve a type and file name
|
|
1260
1269
|
* ```ts
|
|
1261
|
-
* import {
|
|
1270
|
+
* import { resolverTs } from '@kubb/plugin-ts'
|
|
1262
1271
|
*
|
|
1263
|
-
*
|
|
1264
|
-
*
|
|
1265
|
-
*
|
|
1272
|
+
* resolverTs.default('list pets', 'type') // 'ListPets'
|
|
1273
|
+
* resolverTs.resolvePathName('list pets', 'file') // 'ListPets'
|
|
1274
|
+
* resolverTs.resolveResponseStatusName(node, 200) // 'ListPetsStatus200'
|
|
1266
1275
|
* ```
|
|
1267
1276
|
*/
|
|
1268
1277
|
const resolverTs = defineResolver(() => {
|
|
@@ -1313,22 +1322,31 @@ const resolverTs = defineResolver(() => {
|
|
|
1313
1322
|
//#endregion
|
|
1314
1323
|
//#region src/plugin.ts
|
|
1315
1324
|
/**
|
|
1316
|
-
* Canonical plugin name for `@kubb/plugin-ts
|
|
1325
|
+
* Canonical plugin name for `@kubb/plugin-ts`. Used for driver lookups and
|
|
1326
|
+
* cross-plugin dependency references.
|
|
1317
1327
|
*/
|
|
1318
1328
|
const pluginTsName = "plugin-ts";
|
|
1319
1329
|
/**
|
|
1320
|
-
*
|
|
1321
|
-
*
|
|
1322
|
-
*
|
|
1323
|
-
*
|
|
1324
|
-
* and writes barrel files based on `output.barrelType`.
|
|
1330
|
+
* Generates TypeScript `type` aliases and `interface` declarations from an
|
|
1331
|
+
* OpenAPI spec. The foundation that every other Kubb plugin builds on:
|
|
1332
|
+
* clients, query hooks, mocks, and validators all reference the names this
|
|
1333
|
+
* plugin produces.
|
|
1325
1334
|
*
|
|
1326
1335
|
* @example
|
|
1327
1336
|
* ```ts
|
|
1328
|
-
* import
|
|
1337
|
+
* import { defineConfig } from 'kubb'
|
|
1338
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
1329
1339
|
*
|
|
1330
1340
|
* export default defineConfig({
|
|
1331
|
-
*
|
|
1341
|
+
* input: { path: './petStore.yaml' },
|
|
1342
|
+
* output: { path: './src/gen' },
|
|
1343
|
+
* plugins: [
|
|
1344
|
+
* pluginTs({
|
|
1345
|
+
* output: { path: './types' },
|
|
1346
|
+
* enumType: 'asConst',
|
|
1347
|
+
* optionalType: 'questionTokenAndUndefined',
|
|
1348
|
+
* }),
|
|
1349
|
+
* ],
|
|
1332
1350
|
* })
|
|
1333
1351
|
* ```
|
|
1334
1352
|
*/
|
|
@@ -1343,7 +1361,7 @@ const pluginTs = definePlugin((options) => {
|
|
|
1343
1361
|
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
1344
1362
|
return `${camelCase(ctx.group)}Controller`;
|
|
1345
1363
|
}
|
|
1346
|
-
} :
|
|
1364
|
+
} : null;
|
|
1347
1365
|
return {
|
|
1348
1366
|
name: pluginTsName,
|
|
1349
1367
|
options,
|