@kubb/plugin-ts 5.0.0-beta.76 → 5.0.0-beta.77
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 +68 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +68 -42
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -589,7 +589,7 @@ function applyEnumKeyCasing(key, casing = "none") {
|
|
|
589
589
|
* type: 'enum',
|
|
590
590
|
* name: 'petType',
|
|
591
591
|
* typeName: 'PetType',
|
|
592
|
-
* enums: [['cat', 'cat'], ['dog', 'dog']],
|
|
592
|
+
* enums: [['cat', 'cat', 'A cat'], ['dog', 'dog']],
|
|
593
593
|
* })
|
|
594
594
|
* ```
|
|
595
595
|
*/
|
|
@@ -602,30 +602,39 @@ function createEnumDeclaration({ type = "enum", name, typeName, enums, enumKeyCa
|
|
|
602
602
|
if (typeof value === "boolean") return factory.createLiteralTypeNode(value ? factory.createTrue() : factory.createFalse());
|
|
603
603
|
if (value !== null && value !== void 0) return factory.createLiteralTypeNode(factory.createStringLiteral(value.toString()));
|
|
604
604
|
}).filter((node) => node !== void 0)))];
|
|
605
|
-
if (type === "enum" || type === "constEnum") return [void 0, factory.createEnumDeclaration([factory.createToken(typescript.default.SyntaxKind.ExportKeyword), type === "constEnum" ? factory.createToken(typescript.default.SyntaxKind.ConstKeyword) : void 0].filter((modifier) => modifier !== void 0), factory.createIdentifier(typeName), enums.map(([key, value]) => {
|
|
605
|
+
if (type === "enum" || type === "constEnum") return [void 0, factory.createEnumDeclaration([factory.createToken(typescript.default.SyntaxKind.ExportKeyword), type === "constEnum" ? factory.createToken(typescript.default.SyntaxKind.ConstKeyword) : void 0].filter((modifier) => modifier !== void 0), factory.createIdentifier(typeName), enums.map(([key, value, description]) => {
|
|
606
606
|
let initializer = factory.createStringLiteral(value?.toString());
|
|
607
607
|
if (Number.parseInt(value.toString(), 10) === value && isNumber(Number.parseInt(value.toString(), 10))) if (value < 0) initializer = factory.createPrefixUnaryExpression(typescript.default.SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value)));
|
|
608
608
|
else initializer = factory.createNumericLiteral(value);
|
|
609
609
|
if (typeof value === "boolean") initializer = value ? factory.createTrue() : factory.createFalse();
|
|
610
610
|
if (isNumber(Number.parseInt(key.toString(), 10))) {
|
|
611
611
|
const casingKey = applyEnumKeyCasing(`${typeName}_${key}`, enumKeyCasing);
|
|
612
|
-
return
|
|
612
|
+
return appendJSDocToNode({
|
|
613
|
+
node: factory.createEnumMember(propertyName(casingKey), initializer),
|
|
614
|
+
comments: [description]
|
|
615
|
+
});
|
|
613
616
|
}
|
|
614
617
|
if (key !== null && key !== void 0) {
|
|
615
618
|
const casingKey = applyEnumKeyCasing(key.toString(), enumKeyCasing);
|
|
616
|
-
return
|
|
619
|
+
return appendJSDocToNode({
|
|
620
|
+
node: factory.createEnumMember(propertyName(casingKey), initializer),
|
|
621
|
+
comments: [description]
|
|
622
|
+
});
|
|
617
623
|
}
|
|
618
624
|
}).filter((member) => member !== void 0))];
|
|
619
625
|
const identifierName = name;
|
|
620
626
|
if (enums.length === 0) return [void 0, factory.createTypeAliasDeclaration([factory.createToken(typescript.default.SyntaxKind.ExportKeyword)], factory.createIdentifier(typeName), void 0, factory.createKeywordTypeNode(typescript.default.SyntaxKind.NeverKeyword))];
|
|
621
|
-
return [factory.createVariableStatement([factory.createToken(typescript.default.SyntaxKind.ExportKeyword)], factory.createVariableDeclarationList([factory.createVariableDeclaration(factory.createIdentifier(identifierName), void 0, void 0, factory.createAsExpression(factory.createObjectLiteralExpression(enums.map(([key, value]) => {
|
|
627
|
+
return [factory.createVariableStatement([factory.createToken(typescript.default.SyntaxKind.ExportKeyword)], factory.createVariableDeclarationList([factory.createVariableDeclaration(factory.createIdentifier(identifierName), void 0, void 0, factory.createAsExpression(factory.createObjectLiteralExpression(enums.map(([key, value, description]) => {
|
|
622
628
|
let initializer = factory.createStringLiteral(value?.toString());
|
|
623
629
|
if (isNumber(value)) if (value < 0) initializer = factory.createPrefixUnaryExpression(typescript.default.SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value)));
|
|
624
630
|
else initializer = factory.createNumericLiteral(value);
|
|
625
631
|
if (typeof value === "boolean") initializer = value ? factory.createTrue() : factory.createFalse();
|
|
626
632
|
if (key !== null && key !== void 0) {
|
|
627
633
|
const casingKey = applyEnumKeyCasing(key.toString(), enumKeyCasing);
|
|
628
|
-
return
|
|
634
|
+
return appendJSDocToNode({
|
|
635
|
+
node: factory.createPropertyAssignment(propertyName(casingKey), initializer),
|
|
636
|
+
comments: [description]
|
|
637
|
+
});
|
|
629
638
|
}
|
|
630
639
|
}).filter((property) => property !== void 0), true), factory.createTypeReferenceNode(factory.createIdentifier("const"), void 0)))], typescript.default.NodeFlags.Const)), factory.createTypeAliasDeclaration([factory.createToken(typescript.default.SyntaxKind.ExportKeyword)], factory.createIdentifier(typeName), void 0, factory.createIndexedAccessTypeNode(factory.createParenthesizedType(factory.createTypeQueryNode(factory.createIdentifier(identifierName), void 0)), factory.createTypeOperatorNode(typescript.default.SyntaxKind.KeyOfKeyword, factory.createTypeQueryNode(factory.createIdentifier(identifierName), void 0))))];
|
|
631
640
|
}
|
|
@@ -856,7 +865,11 @@ function Enum({ node, enum: enumOptions, resolver }) {
|
|
|
856
865
|
const [nameNode, typeNode] = createEnumDeclaration({
|
|
857
866
|
name: enumName,
|
|
858
867
|
typeName,
|
|
859
|
-
enums: node.namedEnumValues?.map((v) => [
|
|
868
|
+
enums: node.namedEnumValues?.map((v) => [
|
|
869
|
+
(0, _kubb_ast_utils.trimQuotes)(v.name.toString()),
|
|
870
|
+
v.value,
|
|
871
|
+
v.description
|
|
872
|
+
]) ?? node.enumValues?.filter((v) => v !== null && v !== void 0).map((v) => [(0, _kubb_ast_utils.trimQuotes)(v.toString()), v]) ?? [],
|
|
860
873
|
type: enumOptions.type,
|
|
861
874
|
enumKeyCasing: enumOptions.keyCasing
|
|
862
875
|
});
|
|
@@ -876,39 +889,6 @@ function Enum({ node, enum: enumOptions, resolver }) {
|
|
|
876
889
|
})] });
|
|
877
890
|
}
|
|
878
891
|
//#endregion
|
|
879
|
-
//#region src/components/Type.tsx
|
|
880
|
-
function Type({ name, node, printer, enum: enumOptions, resolver }) {
|
|
881
|
-
const enumSchemaNodes = _kubb_core.ast.collect(node, { schema(n) {
|
|
882
|
-
const enumNode = _kubb_core.ast.narrowSchema(n, _kubb_core.ast.schemaTypes.enum);
|
|
883
|
-
if (enumNode?.name) return enumNode;
|
|
884
|
-
} });
|
|
885
|
-
const output = printer.print(node);
|
|
886
|
-
if (!output) return;
|
|
887
|
-
const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
|
|
888
|
-
return {
|
|
889
|
-
node,
|
|
890
|
-
...getEnumNames({
|
|
891
|
-
node,
|
|
892
|
-
enum: enumOptions,
|
|
893
|
-
resolver
|
|
894
|
-
})
|
|
895
|
-
};
|
|
896
|
-
});
|
|
897
|
-
const shouldExportEnums = enumOptions.type !== "inlineLiteral";
|
|
898
|
-
const shouldExportType = enumOptions.type === "inlineLiteral" || enums.every((item) => item.typeName !== name);
|
|
899
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [shouldExportEnums && enums.map(({ node }) => /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(Enum, {
|
|
900
|
-
node,
|
|
901
|
-
enum: enumOptions,
|
|
902
|
-
resolver
|
|
903
|
-
}, node.name)), shouldExportType && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
904
|
-
name,
|
|
905
|
-
isTypeOnly: true,
|
|
906
|
-
isExportable: true,
|
|
907
|
-
isIndexable: true,
|
|
908
|
-
children: output
|
|
909
|
-
})] });
|
|
910
|
-
}
|
|
911
|
-
//#endregion
|
|
912
892
|
//#region ../../internals/shared/src/params.ts
|
|
913
893
|
const caseParamsCache = /* @__PURE__ */ new WeakMap();
|
|
914
894
|
/**
|
|
@@ -1022,6 +1002,18 @@ function createGroupConfig(group) {
|
|
|
1022
1002
|
//#endregion
|
|
1023
1003
|
//#region src/utils.ts
|
|
1024
1004
|
/**
|
|
1005
|
+
* Tells whether a `const` (single-value enum) should render as a bare literal type (`'active'`)
|
|
1006
|
+
* rather than a named enum reference or a runtime enum declaration.
|
|
1007
|
+
*
|
|
1008
|
+
* The parser folds `const` into a single-value enum node. The adapter decides which schemas are
|
|
1009
|
+
* named enums and lists them in `enumSchemaNames`, and references to those names get suffixed (for
|
|
1010
|
+
* example `StatusKey`). A const renders as a literal only when the adapter has not registered it as
|
|
1011
|
+
* a named enum, which keeps the declaration and its references in sync across adapter versions.
|
|
1012
|
+
*/
|
|
1013
|
+
function isInlineConstEnum(node, enumSchemaNames) {
|
|
1014
|
+
return (node.namedEnumValues ?? node.enumValues ?? []).length === 1 && !(node.name && enumSchemaNames?.has(node.name));
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1025
1017
|
* Collects JSDoc annotation strings for a schema node.
|
|
1026
1018
|
*
|
|
1027
1019
|
* Only uses official JSDoc tags from https://jsdoc.app/: `@description`, `@deprecated`, `@default`, `@example`, `@type`.
|
|
@@ -1159,6 +1151,39 @@ function buildResponseUnion(node, { resolver }) {
|
|
|
1159
1151
|
});
|
|
1160
1152
|
}
|
|
1161
1153
|
//#endregion
|
|
1154
|
+
//#region src/components/Type.tsx
|
|
1155
|
+
function Type({ name, node, printer, enum: enumOptions, resolver }) {
|
|
1156
|
+
const enumSchemaNodes = _kubb_core.ast.collect(node, { schema(n) {
|
|
1157
|
+
const enumNode = _kubb_core.ast.narrowSchema(n, _kubb_core.ast.schemaTypes.enum);
|
|
1158
|
+
if (enumNode?.name && !isInlineConstEnum(enumNode, printer.options.enumSchemaNames)) return enumNode;
|
|
1159
|
+
} });
|
|
1160
|
+
const output = printer.print(node);
|
|
1161
|
+
if (!output) return;
|
|
1162
|
+
const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
|
|
1163
|
+
return {
|
|
1164
|
+
node,
|
|
1165
|
+
...getEnumNames({
|
|
1166
|
+
node,
|
|
1167
|
+
enum: enumOptions,
|
|
1168
|
+
resolver
|
|
1169
|
+
})
|
|
1170
|
+
};
|
|
1171
|
+
});
|
|
1172
|
+
const shouldExportEnums = enumOptions.type !== "inlineLiteral";
|
|
1173
|
+
const shouldExportType = enumOptions.type === "inlineLiteral" || enums.every((item) => item.typeName !== name);
|
|
1174
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [shouldExportEnums && enums.map(({ node }) => /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(Enum, {
|
|
1175
|
+
node,
|
|
1176
|
+
enum: enumOptions,
|
|
1177
|
+
resolver
|
|
1178
|
+
}, node.name)), shouldExportType && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1179
|
+
name,
|
|
1180
|
+
isTypeOnly: true,
|
|
1181
|
+
isExportable: true,
|
|
1182
|
+
isIndexable: true,
|
|
1183
|
+
children: output
|
|
1184
|
+
})] });
|
|
1185
|
+
}
|
|
1186
|
+
//#endregion
|
|
1162
1187
|
//#region src/printers/printerTs.ts
|
|
1163
1188
|
const { isNonNullable } = factory_exports;
|
|
1164
1189
|
/**
|
|
@@ -1219,7 +1244,7 @@ const printerTs = _kubb_core.ast.createPrinter((options) => {
|
|
|
1219
1244
|
},
|
|
1220
1245
|
enum(node) {
|
|
1221
1246
|
const values = node.namedEnumValues?.map((v) => v.value) ?? node.enumValues ?? [];
|
|
1222
|
-
if (this.options.enum.type === "inlineLiteral" || !node.name) return createUnionDeclaration({
|
|
1247
|
+
if (this.options.enum.type === "inlineLiteral" || !node.name || isInlineConstEnum(node, this.options.enumSchemaNames)) return createUnionDeclaration({
|
|
1223
1248
|
withParentheses: true,
|
|
1224
1249
|
nodes: values.filter((v) => v !== null && v !== void 0).map((value) => constToTypeNode(value, typeof value)).filter(isNonNullable)
|
|
1225
1250
|
}) ?? void 0;
|
|
@@ -1349,7 +1374,8 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1349
1374
|
group: group ?? void 0
|
|
1350
1375
|
}).path
|
|
1351
1376
|
}));
|
|
1352
|
-
const
|
|
1377
|
+
const enumNode = _kubb_core.ast.narrowSchema(node, _kubb_core.ast.schemaTypes.enum);
|
|
1378
|
+
const isEnumSchema = !!enumNode && !isInlineConstEnum(enumNode, enumSchemaNames);
|
|
1353
1379
|
const meta = {
|
|
1354
1380
|
name: ENUM_TYPES_WITH_KEY_SUFFIX.has(enumOptions.type) && isEnumSchema ? resolver.resolveEnumKeyName(node, enumOptions.typeSuffix) : resolver.resolveTypeName(node.name),
|
|
1355
1381
|
file: resolver.resolveFile({
|