@kubb/plugin-ts 5.0.0-alpha.29 → 5.0.0-alpha.30
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 +72 -102
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +12 -2
- package/dist/index.js +74 -103
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/generators/typeGenerator.tsx +33 -46
- package/src/generators/typeGeneratorLegacy.tsx +16 -28
- package/src/plugin.ts +23 -49
- package/src/types.ts +12 -1
package/dist/index.d.ts
CHANGED
|
@@ -368,7 +368,7 @@ type Options = {
|
|
|
368
368
|
*/
|
|
369
369
|
transformer?: Visitor;
|
|
370
370
|
/**
|
|
371
|
-
* Override individual printer node handlers to
|
|
371
|
+
* Override individual printer node handlers to customize rendering of specific schema types.
|
|
372
372
|
*
|
|
373
373
|
* Each key is a `SchemaType` (e.g. `'date'`, `'string'`). The function replaces the
|
|
374
374
|
* built-in handler for that type. Use `this.transform` to recurse into nested schema nodes.
|
|
@@ -393,6 +393,9 @@ type Options = {
|
|
|
393
393
|
} & EnumTypeOptions;
|
|
394
394
|
type ResolvedOptions = {
|
|
395
395
|
output: Output;
|
|
396
|
+
exclude: Array<Exclude>;
|
|
397
|
+
include: Array<Include> | undefined;
|
|
398
|
+
override: Array<Override<ResolvedOptions>>;
|
|
396
399
|
group: Group | undefined;
|
|
397
400
|
enumType: NonNullable<Options['enumType']>;
|
|
398
401
|
enumTypeSuffix: NonNullable<Options['enumTypeSuffix']>;
|
|
@@ -404,6 +407,13 @@ type ResolvedOptions = {
|
|
|
404
407
|
printer: Options['printer'];
|
|
405
408
|
};
|
|
406
409
|
type PluginTs = PluginFactoryOptions<'plugin-ts', Options, ResolvedOptions, never, ResolvePathOptions, ResolverTs>;
|
|
410
|
+
declare global {
|
|
411
|
+
namespace Kubb {
|
|
412
|
+
interface PluginRegistry {
|
|
413
|
+
'plugin-ts': PluginTs;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
407
417
|
//#endregion
|
|
408
418
|
//#region src/components/Enum.d.ts
|
|
409
419
|
type Props$1 = {
|
|
@@ -464,7 +474,7 @@ declare function Type({
|
|
|
464
474
|
}: Props): FabricReactNode;
|
|
465
475
|
//#endregion
|
|
466
476
|
//#region src/generators/typeGenerator.d.ts
|
|
467
|
-
declare const typeGenerator: _$_kubb_core0.
|
|
477
|
+
declare const typeGenerator: _$_kubb_core0.Generator<PluginTs>;
|
|
468
478
|
//#endregion
|
|
469
479
|
//#region src/plugin.d.ts
|
|
470
480
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import "./chunk--u3MIqq1.js";
|
|
2
|
-
import path from "node:path";
|
|
3
2
|
import { safePrint } from "@kubb/fabric-core/parsers/typescript";
|
|
4
3
|
import { File } from "@kubb/react-fabric";
|
|
5
|
-
import { caseParams, collect, createPrinterFactory, createProperty, createSchema, extractRefName, isStringType, narrowSchema, schemaTypes, syncSchemaRef, transform
|
|
4
|
+
import { caseParams, collect, createPrinterFactory, createProperty, createSchema, extractRefName, isStringType, narrowSchema, schemaTypes, syncSchemaRef, transform } from "@kubb/ast";
|
|
6
5
|
import { isNumber } from "remeda";
|
|
7
6
|
import ts from "typescript";
|
|
8
7
|
import { Fragment, jsx, jsxs } from "@kubb/react-fabric/jsx-runtime";
|
|
9
|
-
import { createPlugin, defineGenerator, definePresets, definePrinter, defineResolver,
|
|
8
|
+
import { createPlugin, defineGenerator, definePresets, definePrinter, defineResolver, getPreset, mergeGenerators } from "@kubb/core";
|
|
10
9
|
//#region ../../internals/utils/src/casing.ts
|
|
11
10
|
/**
|
|
12
11
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -858,19 +857,17 @@ const printerTs = definePrinter((options) => {
|
|
|
858
857
|
//#region src/generators/typeGenerator.tsx
|
|
859
858
|
const typeGenerator = defineGenerator({
|
|
860
859
|
name: "typescript",
|
|
861
|
-
|
|
862
|
-
Schema({ node, adapter, options, config, resolver, plugin }) {
|
|
860
|
+
schema(node, options) {
|
|
863
861
|
const { enumType, enumTypeSuffix, enumKeyCasing, syntaxType, optionalType, arrayType, output, group, printer } = options;
|
|
864
|
-
const
|
|
865
|
-
if (!
|
|
866
|
-
const
|
|
867
|
-
const mode = getMode(path.resolve(root, output.path));
|
|
862
|
+
const { adapter, config, resolver, root } = this;
|
|
863
|
+
if (!node.name) return;
|
|
864
|
+
const mode = this.getMode(output);
|
|
868
865
|
const enumSchemaNames = new Set((adapter.rootNode?.schemas ?? []).filter((s) => narrowSchema(s, schemaTypes.enum) && s.name).map((s) => s.name));
|
|
869
866
|
function resolveImportName(schemaName) {
|
|
870
867
|
if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && enumTypeSuffix && enumSchemaNames.has(schemaName)) return resolver.resolveEnumKeyName({ name: schemaName }, enumTypeSuffix);
|
|
871
868
|
return resolver.resolveTypeName(schemaName);
|
|
872
869
|
}
|
|
873
|
-
const imports = adapter.getImports(
|
|
870
|
+
const imports = adapter.getImports(node, (schemaName) => ({
|
|
874
871
|
name: resolveImportName(schemaName),
|
|
875
872
|
path: resolver.resolveFile({
|
|
876
873
|
name: schemaName,
|
|
@@ -881,11 +878,11 @@ const typeGenerator = defineGenerator({
|
|
|
881
878
|
group
|
|
882
879
|
}).path
|
|
883
880
|
}));
|
|
884
|
-
const isEnumSchema = !!narrowSchema(
|
|
881
|
+
const isEnumSchema = !!narrowSchema(node, schemaTypes.enum);
|
|
885
882
|
const meta = {
|
|
886
|
-
name: ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && isEnumSchema ? resolver.resolveEnumKeyName(
|
|
883
|
+
name: ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && isEnumSchema ? resolver.resolveEnumKeyName(node, enumTypeSuffix) : resolver.resolveTypeName(node.name),
|
|
887
884
|
file: resolver.resolveFile({
|
|
888
|
-
name:
|
|
885
|
+
name: node.name,
|
|
889
886
|
extname: ".ts"
|
|
890
887
|
}, {
|
|
891
888
|
root,
|
|
@@ -900,7 +897,7 @@ const typeGenerator = defineGenerator({
|
|
|
900
897
|
enumTypeSuffix,
|
|
901
898
|
name: meta.name,
|
|
902
899
|
syntaxType,
|
|
903
|
-
description:
|
|
900
|
+
description: node.description,
|
|
904
901
|
resolver,
|
|
905
902
|
enumSchemaNames,
|
|
906
903
|
nodes: printer?.nodes
|
|
@@ -923,12 +920,12 @@ const typeGenerator = defineGenerator({
|
|
|
923
920
|
name: imp.name,
|
|
924
921
|
isTypeOnly: true
|
|
925
922
|
}, [
|
|
926
|
-
|
|
923
|
+
node.name,
|
|
927
924
|
imp.path,
|
|
928
925
|
imp.isTypeOnly
|
|
929
926
|
].join("-"))), /* @__PURE__ */ jsx(Type, {
|
|
930
927
|
name: meta.name,
|
|
931
|
-
node
|
|
928
|
+
node,
|
|
932
929
|
enumType,
|
|
933
930
|
enumTypeSuffix,
|
|
934
931
|
enumKeyCasing,
|
|
@@ -937,17 +934,16 @@ const typeGenerator = defineGenerator({
|
|
|
937
934
|
})]
|
|
938
935
|
});
|
|
939
936
|
},
|
|
940
|
-
|
|
937
|
+
operation(node, options) {
|
|
941
938
|
const { enumType, enumTypeSuffix, enumKeyCasing, optionalType, arrayType, syntaxType, paramsCasing, group, output, printer } = options;
|
|
942
|
-
const
|
|
943
|
-
const
|
|
944
|
-
const
|
|
945
|
-
const params = caseParams(transformedNode.parameters, paramsCasing);
|
|
939
|
+
const { adapter, config, resolver, root } = this;
|
|
940
|
+
const mode = this.getMode(output);
|
|
941
|
+
const params = caseParams(node.parameters, paramsCasing);
|
|
946
942
|
const meta = { file: resolver.resolveFile({
|
|
947
|
-
name:
|
|
943
|
+
name: node.operationId,
|
|
948
944
|
extname: ".ts",
|
|
949
|
-
tag:
|
|
950
|
-
path:
|
|
945
|
+
tag: node.tags[0] ?? "default",
|
|
946
|
+
path: node.path
|
|
951
947
|
}, {
|
|
952
948
|
root,
|
|
953
949
|
output,
|
|
@@ -1005,38 +1001,38 @@ const typeGenerator = defineGenerator({
|
|
|
1005
1001
|
}
|
|
1006
1002
|
const paramTypes = params.map((param) => renderSchemaType({
|
|
1007
1003
|
schema: param.schema,
|
|
1008
|
-
name: resolver.resolveParamName(
|
|
1004
|
+
name: resolver.resolveParamName(node, param)
|
|
1009
1005
|
}));
|
|
1010
|
-
const requestType =
|
|
1006
|
+
const requestType = node.requestBody?.schema ? renderSchemaType({
|
|
1011
1007
|
schema: {
|
|
1012
|
-
...
|
|
1013
|
-
description:
|
|
1008
|
+
...node.requestBody.schema,
|
|
1009
|
+
description: node.requestBody.description ?? node.requestBody.schema.description
|
|
1014
1010
|
},
|
|
1015
|
-
name: resolver.resolveDataName(
|
|
1016
|
-
keysToOmit:
|
|
1011
|
+
name: resolver.resolveDataName(node),
|
|
1012
|
+
keysToOmit: node.requestBody.keysToOmit
|
|
1017
1013
|
}) : null;
|
|
1018
|
-
const responseTypes =
|
|
1014
|
+
const responseTypes = node.responses.map((res) => renderSchemaType({
|
|
1019
1015
|
schema: res.schema,
|
|
1020
|
-
name: resolver.resolveResponseStatusName(
|
|
1016
|
+
name: resolver.resolveResponseStatusName(node, res.statusCode),
|
|
1021
1017
|
keysToOmit: res.keysToOmit
|
|
1022
1018
|
}));
|
|
1023
1019
|
const dataType = renderSchemaType({
|
|
1024
1020
|
schema: buildData({
|
|
1025
|
-
...
|
|
1021
|
+
...node,
|
|
1026
1022
|
parameters: params
|
|
1027
1023
|
}, { resolver }),
|
|
1028
|
-
name: resolver.resolveRequestConfigName(
|
|
1024
|
+
name: resolver.resolveRequestConfigName(node)
|
|
1029
1025
|
});
|
|
1030
1026
|
const responsesType = renderSchemaType({
|
|
1031
|
-
schema: buildResponses(
|
|
1032
|
-
name: resolver.resolveResponsesName(
|
|
1027
|
+
schema: buildResponses(node, { resolver }),
|
|
1028
|
+
name: resolver.resolveResponsesName(node)
|
|
1033
1029
|
});
|
|
1034
1030
|
const responseType = renderSchemaType({
|
|
1035
|
-
schema:
|
|
1036
|
-
...buildResponseUnion(
|
|
1031
|
+
schema: node.responses.some((res) => res.schema) ? {
|
|
1032
|
+
...buildResponseUnion(node, { resolver }),
|
|
1037
1033
|
description: "Union of all possible responses"
|
|
1038
1034
|
} : null,
|
|
1039
|
-
name: resolver.resolveResponseName(
|
|
1035
|
+
name: resolver.resolveResponseName(node)
|
|
1040
1036
|
});
|
|
1041
1037
|
return /* @__PURE__ */ jsxs(File, {
|
|
1042
1038
|
baseName: meta.file.baseName,
|
|
@@ -1062,6 +1058,9 @@ const typeGenerator = defineGenerator({
|
|
|
1062
1058
|
}
|
|
1063
1059
|
});
|
|
1064
1060
|
//#endregion
|
|
1061
|
+
//#region package.json
|
|
1062
|
+
var version = "5.0.0-alpha.30";
|
|
1063
|
+
//#endregion
|
|
1065
1064
|
//#region src/resolvers/resolverTs.ts
|
|
1066
1065
|
/**
|
|
1067
1066
|
* Resolver for `@kubb/plugin-ts` that provides the default naming and path-resolution
|
|
@@ -1335,14 +1334,12 @@ function nameUnnamedEnums(node, parentName) {
|
|
|
1335
1334
|
}
|
|
1336
1335
|
const typeGeneratorLegacy = defineGenerator({
|
|
1337
1336
|
name: "typescript-legacy",
|
|
1338
|
-
|
|
1339
|
-
Schema({ node, adapter, options, config, resolver, plugin }) {
|
|
1337
|
+
schema(node, options) {
|
|
1340
1338
|
const { enumType, enumTypeSuffix, enumKeyCasing, syntaxType, optionalType, arrayType, output, group } = options;
|
|
1341
|
-
const
|
|
1342
|
-
if (!
|
|
1343
|
-
const
|
|
1344
|
-
const
|
|
1345
|
-
const imports = adapter.getImports(transformedNode, (schemaName) => ({
|
|
1339
|
+
const { adapter, config, resolver, root } = this;
|
|
1340
|
+
if (!node.name) return;
|
|
1341
|
+
const mode = this.getMode(output);
|
|
1342
|
+
const imports = adapter.getImports(node, (schemaName) => ({
|
|
1346
1343
|
name: resolver.resolveTypeName(schemaName),
|
|
1347
1344
|
path: resolver.resolveFile({
|
|
1348
1345
|
name: schemaName,
|
|
@@ -1353,11 +1350,11 @@ const typeGeneratorLegacy = defineGenerator({
|
|
|
1353
1350
|
group
|
|
1354
1351
|
}).path
|
|
1355
1352
|
}));
|
|
1356
|
-
const isEnumSchema = !!narrowSchema(
|
|
1353
|
+
const isEnumSchema = !!narrowSchema(node, schemaTypes.enum);
|
|
1357
1354
|
const meta = {
|
|
1358
|
-
name: ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && isEnumSchema ? resolver.resolveEnumKeyName(
|
|
1355
|
+
name: ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && isEnumSchema ? resolver.resolveEnumKeyName(node, enumTypeSuffix) : resolver.resolveTypeName(node.name),
|
|
1359
1356
|
file: resolver.resolveFile({
|
|
1360
|
-
name:
|
|
1357
|
+
name: node.name,
|
|
1361
1358
|
extname: ".ts"
|
|
1362
1359
|
}, {
|
|
1363
1360
|
root,
|
|
@@ -1372,7 +1369,7 @@ const typeGeneratorLegacy = defineGenerator({
|
|
|
1372
1369
|
enumTypeSuffix,
|
|
1373
1370
|
name: meta.name,
|
|
1374
1371
|
syntaxType,
|
|
1375
|
-
description:
|
|
1372
|
+
description: node.description,
|
|
1376
1373
|
resolver
|
|
1377
1374
|
});
|
|
1378
1375
|
return /* @__PURE__ */ jsxs(File, {
|
|
@@ -1393,12 +1390,12 @@ const typeGeneratorLegacy = defineGenerator({
|
|
|
1393
1390
|
name: imp.name,
|
|
1394
1391
|
isTypeOnly: true
|
|
1395
1392
|
}, [
|
|
1396
|
-
|
|
1393
|
+
node.name,
|
|
1397
1394
|
imp.path,
|
|
1398
1395
|
imp.isTypeOnly
|
|
1399
1396
|
].join("-"))), /* @__PURE__ */ jsx(Type, {
|
|
1400
1397
|
name: meta.name,
|
|
1401
|
-
node
|
|
1398
|
+
node,
|
|
1402
1399
|
enumType,
|
|
1403
1400
|
enumTypeSuffix,
|
|
1404
1401
|
enumKeyCasing,
|
|
@@ -1407,17 +1404,16 @@ const typeGeneratorLegacy = defineGenerator({
|
|
|
1407
1404
|
})]
|
|
1408
1405
|
});
|
|
1409
1406
|
},
|
|
1410
|
-
|
|
1407
|
+
operation(node, options) {
|
|
1411
1408
|
const { enumType, enumTypeSuffix, enumKeyCasing, optionalType, arrayType, syntaxType, paramsCasing, group, output } = options;
|
|
1412
|
-
const
|
|
1413
|
-
const
|
|
1414
|
-
const mode = getMode(path.resolve(root, output.path));
|
|
1409
|
+
const { adapter, config, resolver, root } = this;
|
|
1410
|
+
const mode = this.getMode(output);
|
|
1415
1411
|
const params = caseParams(node.parameters, paramsCasing);
|
|
1416
1412
|
const meta = { file: resolver.resolveFile({
|
|
1417
|
-
name:
|
|
1413
|
+
name: node.operationId,
|
|
1418
1414
|
extname: ".ts",
|
|
1419
|
-
tag:
|
|
1420
|
-
path:
|
|
1415
|
+
tag: node.tags[0] ?? "default",
|
|
1416
|
+
path: node.path
|
|
1421
1417
|
}, {
|
|
1422
1418
|
root,
|
|
1423
1419
|
output,
|
|
@@ -1594,10 +1590,12 @@ const pluginTs = createPlugin((options) => {
|
|
|
1594
1590
|
transformer: userTransformer,
|
|
1595
1591
|
generators: userGenerators
|
|
1596
1592
|
});
|
|
1593
|
+
const mergedGenerator = mergeGenerators(preset.generators ?? []);
|
|
1597
1594
|
let resolveNameWarning = false;
|
|
1598
1595
|
let resolvePathWarning = false;
|
|
1599
1596
|
return {
|
|
1600
1597
|
name: pluginTsName,
|
|
1598
|
+
version,
|
|
1601
1599
|
get resolver() {
|
|
1602
1600
|
return preset.resolver;
|
|
1603
1601
|
},
|
|
@@ -1607,6 +1605,9 @@ const pluginTs = createPlugin((options) => {
|
|
|
1607
1605
|
get options() {
|
|
1608
1606
|
return {
|
|
1609
1607
|
output,
|
|
1608
|
+
exclude,
|
|
1609
|
+
include,
|
|
1610
|
+
override,
|
|
1610
1611
|
optionalType,
|
|
1611
1612
|
group: group ? {
|
|
1612
1613
|
...group,
|
|
@@ -1626,7 +1627,7 @@ const pluginTs = createPlugin((options) => {
|
|
|
1626
1627
|
},
|
|
1627
1628
|
resolvePath(baseName, pathMode, options) {
|
|
1628
1629
|
if (!resolvePathWarning) {
|
|
1629
|
-
this.
|
|
1630
|
+
this.warn("Do not use resolvePath for pluginTs, use resolverTs.resolvePath instead");
|
|
1630
1631
|
resolvePathWarning = true;
|
|
1631
1632
|
}
|
|
1632
1633
|
return this.plugin.resolver.resolvePath({
|
|
@@ -1635,59 +1636,29 @@ const pluginTs = createPlugin((options) => {
|
|
|
1635
1636
|
tag: options?.group?.tag,
|
|
1636
1637
|
path: options?.group?.path
|
|
1637
1638
|
}, {
|
|
1638
|
-
root:
|
|
1639
|
+
root: this.root,
|
|
1639
1640
|
output,
|
|
1640
1641
|
group: this.plugin.options.group
|
|
1641
1642
|
});
|
|
1642
1643
|
},
|
|
1643
1644
|
resolveName(name, type) {
|
|
1644
1645
|
if (!resolveNameWarning) {
|
|
1645
|
-
this.
|
|
1646
|
+
this.warn("Do not use resolveName for pluginTs, use resolverTs.default instead");
|
|
1646
1647
|
resolveNameWarning = true;
|
|
1647
1648
|
}
|
|
1648
1649
|
return this.plugin.resolver.default(name, type);
|
|
1649
1650
|
},
|
|
1650
|
-
async
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
include,
|
|
1662
|
-
override,
|
|
1663
|
-
fabric,
|
|
1664
|
-
adapter,
|
|
1665
|
-
config,
|
|
1666
|
-
driver
|
|
1667
|
-
};
|
|
1668
|
-
await walk(rootNode, {
|
|
1669
|
-
depth: "shallow",
|
|
1670
|
-
async schema(schemaNode) {
|
|
1671
|
-
await runGeneratorSchema(schemaNode, generatorContext);
|
|
1672
|
-
},
|
|
1673
|
-
async operation(operationNode) {
|
|
1674
|
-
if (resolver.resolveOptions(operationNode, {
|
|
1675
|
-
options: plugin.options,
|
|
1676
|
-
exclude,
|
|
1677
|
-
include,
|
|
1678
|
-
override
|
|
1679
|
-
}) !== null) collectedOperations.push(operationNode);
|
|
1680
|
-
await runGeneratorOperation(operationNode, generatorContext);
|
|
1681
|
-
}
|
|
1682
|
-
});
|
|
1683
|
-
await runGeneratorOperations(collectedOperations, generatorContext);
|
|
1684
|
-
const barrelFiles = await getBarrelFiles(this.fabric.files, {
|
|
1685
|
-
type: output.barrelType ?? "named",
|
|
1686
|
-
root,
|
|
1687
|
-
output,
|
|
1688
|
-
meta: { pluginName: this.plugin.name }
|
|
1689
|
-
});
|
|
1690
|
-
await this.upsertFile(...barrelFiles);
|
|
1651
|
+
async schema(node, options) {
|
|
1652
|
+
return mergedGenerator.schema?.call(this, node, options);
|
|
1653
|
+
},
|
|
1654
|
+
async operation(node, options) {
|
|
1655
|
+
return mergedGenerator.operation?.call(this, node, options);
|
|
1656
|
+
},
|
|
1657
|
+
async operations(nodes, options) {
|
|
1658
|
+
return mergedGenerator.operations?.call(this, nodes, options);
|
|
1659
|
+
},
|
|
1660
|
+
async buildStart() {
|
|
1661
|
+
await this.openInStudio({ ast: true });
|
|
1691
1662
|
}
|
|
1692
1663
|
};
|
|
1693
1664
|
});
|