@kubb/plugin-ts 5.0.0-alpha.10 → 5.0.0-alpha.12
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/{components-CRu8IKY3.js → Type-CX1HRooG.js} +377 -365
- package/dist/Type-CX1HRooG.js.map +1 -0
- package/dist/Type-Cat0_htq.cjs +808 -0
- package/dist/Type-Cat0_htq.cjs.map +1 -0
- package/dist/components.cjs +3 -2
- package/dist/components.d.ts +40 -9
- package/dist/components.js +2 -2
- package/dist/generators-CLuCmfUz.js +532 -0
- package/dist/generators-CLuCmfUz.js.map +1 -0
- package/dist/generators-DWBU-MuW.cjs +536 -0
- package/dist/generators-DWBU-MuW.cjs.map +1 -0
- package/dist/generators.cjs +2 -3
- package/dist/generators.d.ts +3 -503
- package/dist/generators.js +2 -2
- package/dist/index.cjs +308 -4
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +26 -21
- package/dist/index.js +305 -2
- package/dist/index.js.map +1 -0
- package/dist/{types-mSXmB8WU.d.ts → types-BA1ZCQ5p.d.ts} +73 -57
- package/package.json +5 -5
- package/src/components/{v2/Enum.tsx → Enum.tsx} +27 -11
- package/src/components/Type.tsx +23 -141
- package/src/components/index.ts +1 -0
- package/src/generators/index.ts +0 -1
- package/src/generators/typeGenerator.tsx +189 -413
- package/src/generators/utils.ts +298 -0
- package/src/index.ts +1 -1
- package/src/plugin.ts +81 -129
- package/src/printer.ts +15 -4
- package/src/resolverTs.ts +109 -1
- package/src/types.ts +68 -52
- package/dist/components-CRu8IKY3.js.map +0 -1
- package/dist/components-DeNDKlzf.cjs +0 -982
- package/dist/components-DeNDKlzf.cjs.map +0 -1
- package/dist/plugin-BZkBwnEA.js +0 -1269
- package/dist/plugin-BZkBwnEA.js.map +0 -1
- package/dist/plugin-Bunz1oGa.cjs +0 -1322
- package/dist/plugin-Bunz1oGa.cjs.map +0 -1
- package/src/components/v2/Type.tsx +0 -59
- package/src/generators/v2/typeGenerator.tsx +0 -167
- package/src/generators/v2/utils.ts +0 -140
- package/src/parser.ts +0 -389
|
@@ -0,0 +1,808 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __name = (target, value) => __defProp(target, "name", {
|
|
5
|
+
value,
|
|
6
|
+
configurable: true
|
|
7
|
+
});
|
|
8
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
10
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
14
|
+
key = keys[i];
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
16
|
+
get: ((k) => from[k]).bind(null, key),
|
|
17
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
//#endregion
|
|
27
|
+
let _kubb_ast = require("@kubb/ast");
|
|
28
|
+
let _kubb_core = require("@kubb/core");
|
|
29
|
+
let _kubb_react_fabric = require("@kubb/react-fabric");
|
|
30
|
+
let _kubb_fabric_core_parsers_typescript = require("@kubb/fabric-core/parsers/typescript");
|
|
31
|
+
let remeda = require("remeda");
|
|
32
|
+
let typescript = require("typescript");
|
|
33
|
+
typescript = __toESM(typescript);
|
|
34
|
+
let _kubb_react_fabric_jsx_runtime = require("@kubb/react-fabric/jsx-runtime");
|
|
35
|
+
//#region ../../internals/utils/src/casing.ts
|
|
36
|
+
/**
|
|
37
|
+
* Shared implementation for camelCase and PascalCase conversion.
|
|
38
|
+
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
|
|
39
|
+
* and capitalizes each word according to `pascal`.
|
|
40
|
+
*
|
|
41
|
+
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
|
|
42
|
+
*/
|
|
43
|
+
function toCamelOrPascal(text, pascal) {
|
|
44
|
+
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
|
|
45
|
+
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
46
|
+
if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
|
|
47
|
+
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
48
|
+
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Splits `text` on `.` and applies `transformPart` to each segment.
|
|
52
|
+
* The last segment receives `isLast = true`, all earlier segments receive `false`.
|
|
53
|
+
* Segments are joined with `/` to form a file path.
|
|
54
|
+
*
|
|
55
|
+
* Only splits on dots followed by a letter so that version numbers
|
|
56
|
+
* embedded in operationIds (e.g. `v2025.0`) are kept intact.
|
|
57
|
+
*/
|
|
58
|
+
function applyToFileParts(text, transformPart) {
|
|
59
|
+
const parts = text.split(/\.(?=[a-zA-Z])/);
|
|
60
|
+
return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Converts `text` to camelCase.
|
|
64
|
+
* When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* camelCase('hello-world') // 'helloWorld'
|
|
68
|
+
* camelCase('pet.petId', { isFile: true }) // 'pet/petId'
|
|
69
|
+
*/
|
|
70
|
+
function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
71
|
+
if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
|
|
72
|
+
prefix,
|
|
73
|
+
suffix
|
|
74
|
+
} : {}));
|
|
75
|
+
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Converts `text` to PascalCase.
|
|
79
|
+
* When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* pascalCase('hello-world') // 'HelloWorld'
|
|
83
|
+
* pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
|
|
84
|
+
*/
|
|
85
|
+
function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
86
|
+
if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
|
|
87
|
+
prefix,
|
|
88
|
+
suffix
|
|
89
|
+
}) : camelCase(part));
|
|
90
|
+
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Converts `text` to snake_case.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* snakeCase('helloWorld') // 'hello_world'
|
|
97
|
+
* snakeCase('Hello-World') // 'hello_world'
|
|
98
|
+
*/
|
|
99
|
+
function snakeCase(text, { prefix = "", suffix = "" } = {}) {
|
|
100
|
+
return `${prefix} ${text} ${suffix}`.trim().replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[\s\-.]+/g, "_").replace(/[^a-zA-Z0-9_]/g, "").toLowerCase().split("_").filter(Boolean).join("_");
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Converts `text` to SCREAMING_SNAKE_CASE.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* screamingSnakeCase('helloWorld') // 'HELLO_WORLD'
|
|
107
|
+
*/
|
|
108
|
+
function screamingSnakeCase(text, { prefix = "", suffix = "" } = {}) {
|
|
109
|
+
return snakeCase(text, {
|
|
110
|
+
prefix,
|
|
111
|
+
suffix
|
|
112
|
+
}).toUpperCase();
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region ../../internals/utils/src/string.ts
|
|
116
|
+
/**
|
|
117
|
+
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
|
|
118
|
+
* Returns the string unchanged when no balanced quote pair is found.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* trimQuotes('"hello"') // 'hello'
|
|
122
|
+
* trimQuotes('hello') // 'hello'
|
|
123
|
+
*/
|
|
124
|
+
function trimQuotes(text) {
|
|
125
|
+
if (text.length >= 2) {
|
|
126
|
+
const first = text[0];
|
|
127
|
+
const last = text[text.length - 1];
|
|
128
|
+
if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1);
|
|
129
|
+
}
|
|
130
|
+
return text;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Escapes characters that are not allowed inside JS string literals.
|
|
134
|
+
* Handles quotes, backslashes, and Unicode line terminators (U+2028 / U+2029).
|
|
135
|
+
*
|
|
136
|
+
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
|
|
137
|
+
*/
|
|
138
|
+
function jsStringEscape(input) {
|
|
139
|
+
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
|
|
140
|
+
switch (character) {
|
|
141
|
+
case "\"":
|
|
142
|
+
case "'":
|
|
143
|
+
case "\\": return `\\${character}`;
|
|
144
|
+
case "\n": return "\\n";
|
|
145
|
+
case "\r": return "\\r";
|
|
146
|
+
case "\u2028": return "\\u2028";
|
|
147
|
+
case "\u2029": return "\\u2029";
|
|
148
|
+
default: return "";
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region ../../internals/utils/src/object.ts
|
|
154
|
+
/**
|
|
155
|
+
* Serializes a primitive value to a JSON string literal, stripping any surrounding quote characters first.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* stringify('hello') // '"hello"'
|
|
159
|
+
* stringify('"hello"') // '"hello"'
|
|
160
|
+
*/
|
|
161
|
+
function stringify(value) {
|
|
162
|
+
if (value === void 0 || value === null) return "\"\"";
|
|
163
|
+
return JSON.stringify(trimQuotes(value.toString()));
|
|
164
|
+
}
|
|
165
|
+
//#endregion
|
|
166
|
+
//#region src/constants.ts
|
|
167
|
+
/**
|
|
168
|
+
* `optionalType` values that cause a property's type to include `| undefined`.
|
|
169
|
+
*/
|
|
170
|
+
const OPTIONAL_ADDS_UNDEFINED = new Set(["undefined", "questionTokenAndUndefined"]);
|
|
171
|
+
/**
|
|
172
|
+
* `optionalType` values that render the property key with a `?` token.
|
|
173
|
+
*/
|
|
174
|
+
const OPTIONAL_ADDS_QUESTION_TOKEN = new Set(["questionToken", "questionTokenAndUndefined"]);
|
|
175
|
+
/**
|
|
176
|
+
* `enumType` values that append a `Key` suffix to the generated enum type alias.
|
|
177
|
+
*/
|
|
178
|
+
const ENUM_TYPES_WITH_KEY_SUFFIX = new Set(["asConst", "asPascalConst"]);
|
|
179
|
+
/**
|
|
180
|
+
* `enumType` values that require a runtime value declaration (object, enum, or literal).
|
|
181
|
+
*/
|
|
182
|
+
const ENUM_TYPES_WITH_RUNTIME_VALUE = new Set([
|
|
183
|
+
"enum",
|
|
184
|
+
"asConst",
|
|
185
|
+
"asPascalConst",
|
|
186
|
+
"constEnum",
|
|
187
|
+
"literal",
|
|
188
|
+
void 0
|
|
189
|
+
]);
|
|
190
|
+
/**
|
|
191
|
+
* `enumType` values whose type declaration is type-only (no runtime value emitted for the type alias).
|
|
192
|
+
*/
|
|
193
|
+
const ENUM_TYPES_WITH_TYPE_ONLY = new Set([
|
|
194
|
+
"asConst",
|
|
195
|
+
"asPascalConst",
|
|
196
|
+
"literal",
|
|
197
|
+
void 0
|
|
198
|
+
]);
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/factory.ts
|
|
201
|
+
const { SyntaxKind, factory } = typescript.default;
|
|
202
|
+
const modifiers = {
|
|
203
|
+
async: factory.createModifier(typescript.default.SyntaxKind.AsyncKeyword),
|
|
204
|
+
export: factory.createModifier(typescript.default.SyntaxKind.ExportKeyword),
|
|
205
|
+
const: factory.createModifier(typescript.default.SyntaxKind.ConstKeyword),
|
|
206
|
+
static: factory.createModifier(typescript.default.SyntaxKind.StaticKeyword)
|
|
207
|
+
};
|
|
208
|
+
const syntaxKind = {
|
|
209
|
+
union: SyntaxKind.UnionType,
|
|
210
|
+
literalType: SyntaxKind.LiteralType,
|
|
211
|
+
stringLiteral: SyntaxKind.StringLiteral
|
|
212
|
+
};
|
|
213
|
+
function isValidIdentifier(str) {
|
|
214
|
+
if (!str.length || str.trim() !== str) return false;
|
|
215
|
+
const node = typescript.default.parseIsolatedEntityName(str, typescript.default.ScriptTarget.Latest);
|
|
216
|
+
return !!node && node.kind === typescript.default.SyntaxKind.Identifier && typescript.default.identifierToKeywordKind(node.kind) === void 0;
|
|
217
|
+
}
|
|
218
|
+
function propertyName(name) {
|
|
219
|
+
if (typeof name === "string") return isValidIdentifier(name) ? factory.createIdentifier(name) : factory.createStringLiteral(name);
|
|
220
|
+
return name;
|
|
221
|
+
}
|
|
222
|
+
const questionToken = factory.createToken(typescript.default.SyntaxKind.QuestionToken);
|
|
223
|
+
function createQuestionToken(token) {
|
|
224
|
+
if (!token) return;
|
|
225
|
+
if (token === true) return questionToken;
|
|
226
|
+
return token;
|
|
227
|
+
}
|
|
228
|
+
function createIntersectionDeclaration({ nodes, withParentheses }) {
|
|
229
|
+
if (!nodes.length) return null;
|
|
230
|
+
if (nodes.length === 1) return nodes[0] || null;
|
|
231
|
+
const node = factory.createIntersectionTypeNode(nodes);
|
|
232
|
+
if (withParentheses) return factory.createParenthesizedType(node);
|
|
233
|
+
return node;
|
|
234
|
+
}
|
|
235
|
+
function createArrayDeclaration({ nodes, arrayType = "array" }) {
|
|
236
|
+
if (!nodes.length) return factory.createTupleTypeNode([]);
|
|
237
|
+
if (nodes.length === 1) {
|
|
238
|
+
const node = nodes[0];
|
|
239
|
+
if (!node) return null;
|
|
240
|
+
if (arrayType === "generic") return factory.createTypeReferenceNode(factory.createIdentifier("Array"), [node]);
|
|
241
|
+
return factory.createArrayTypeNode(node);
|
|
242
|
+
}
|
|
243
|
+
const unionType = factory.createUnionTypeNode(nodes);
|
|
244
|
+
if (arrayType === "generic") return factory.createTypeReferenceNode(factory.createIdentifier("Array"), [unionType]);
|
|
245
|
+
return factory.createArrayTypeNode(factory.createParenthesizedType(unionType));
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Minimum nodes length of 2
|
|
249
|
+
* @example `string | number`
|
|
250
|
+
*/
|
|
251
|
+
function createUnionDeclaration({ nodes, withParentheses }) {
|
|
252
|
+
if (!nodes.length) return keywordTypeNodes.any;
|
|
253
|
+
if (nodes.length === 1) return nodes[0];
|
|
254
|
+
const node = factory.createUnionTypeNode(nodes);
|
|
255
|
+
if (withParentheses) return factory.createParenthesizedType(node);
|
|
256
|
+
return node;
|
|
257
|
+
}
|
|
258
|
+
function createPropertySignature({ readOnly, modifiers = [], name, questionToken, type }) {
|
|
259
|
+
return factory.createPropertySignature([...modifiers, readOnly ? factory.createToken(typescript.default.SyntaxKind.ReadonlyKeyword) : void 0].filter(Boolean), propertyName(name), createQuestionToken(questionToken), type);
|
|
260
|
+
}
|
|
261
|
+
function createParameterSignature(name, { modifiers, dotDotDotToken, questionToken, type, initializer }) {
|
|
262
|
+
return factory.createParameterDeclaration(modifiers, dotDotDotToken, name, createQuestionToken(questionToken), type, initializer);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* @link https://github.com/microsoft/TypeScript/issues/44151
|
|
266
|
+
*/
|
|
267
|
+
function appendJSDocToNode({ node, comments }) {
|
|
268
|
+
const filteredComments = comments.filter(Boolean);
|
|
269
|
+
if (!filteredComments.length) return node;
|
|
270
|
+
const text = filteredComments.reduce((acc = "", comment = "") => {
|
|
271
|
+
return `${acc}\n * ${comment.replaceAll("*/", "*\\/")}`;
|
|
272
|
+
}, "*");
|
|
273
|
+
return typescript.default.addSyntheticLeadingComment(node, typescript.default.SyntaxKind.MultiLineCommentTrivia, `${text || "*"}\n`, true);
|
|
274
|
+
}
|
|
275
|
+
function createIndexSignature(type, { modifiers, indexName = "key", indexType = factory.createKeywordTypeNode(typescript.default.SyntaxKind.StringKeyword) } = {}) {
|
|
276
|
+
return factory.createIndexSignature(modifiers, [createParameterSignature(indexName, { type: indexType })], type);
|
|
277
|
+
}
|
|
278
|
+
function createTypeAliasDeclaration({ modifiers, name, typeParameters, type }) {
|
|
279
|
+
return factory.createTypeAliasDeclaration(modifiers, name, typeParameters, type);
|
|
280
|
+
}
|
|
281
|
+
function createInterfaceDeclaration({ modifiers, name, typeParameters, members }) {
|
|
282
|
+
return factory.createInterfaceDeclaration(modifiers, name, typeParameters, void 0, members);
|
|
283
|
+
}
|
|
284
|
+
function createTypeDeclaration({ syntax, isExportable, comments, name, type }) {
|
|
285
|
+
if (syntax === "interface" && "members" in type) return appendJSDocToNode({
|
|
286
|
+
node: createInterfaceDeclaration({
|
|
287
|
+
members: type.members,
|
|
288
|
+
modifiers: isExportable ? [modifiers.export] : [],
|
|
289
|
+
name,
|
|
290
|
+
typeParameters: void 0
|
|
291
|
+
}),
|
|
292
|
+
comments
|
|
293
|
+
});
|
|
294
|
+
return appendJSDocToNode({
|
|
295
|
+
node: createTypeAliasDeclaration({
|
|
296
|
+
type,
|
|
297
|
+
modifiers: isExportable ? [modifiers.export] : [],
|
|
298
|
+
name,
|
|
299
|
+
typeParameters: void 0
|
|
300
|
+
}),
|
|
301
|
+
comments
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Apply casing transformation to enum keys
|
|
306
|
+
*/
|
|
307
|
+
function applyEnumKeyCasing(key, casing = "none") {
|
|
308
|
+
if (casing === "none") return key;
|
|
309
|
+
if (casing === "screamingSnakeCase") return screamingSnakeCase(key);
|
|
310
|
+
if (casing === "snakeCase") return snakeCase(key);
|
|
311
|
+
if (casing === "pascalCase") return pascalCase(key);
|
|
312
|
+
if (casing === "camelCase") return camelCase(key);
|
|
313
|
+
return key;
|
|
314
|
+
}
|
|
315
|
+
function createEnumDeclaration({ type = "enum", name, typeName, enums, enumKeyCasing = "none" }) {
|
|
316
|
+
if (type === "literal" || type === "inlineLiteral") return [void 0, factory.createTypeAliasDeclaration([factory.createToken(typescript.default.SyntaxKind.ExportKeyword)], factory.createIdentifier(typeName), void 0, factory.createUnionTypeNode(enums.map(([_key, value]) => {
|
|
317
|
+
if ((0, remeda.isNumber)(value)) {
|
|
318
|
+
if (value < 0) return factory.createLiteralTypeNode(factory.createPrefixUnaryExpression(typescript.default.SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value))));
|
|
319
|
+
return factory.createLiteralTypeNode(factory.createNumericLiteral(value?.toString()));
|
|
320
|
+
}
|
|
321
|
+
if (typeof value === "boolean") return factory.createLiteralTypeNode(value ? factory.createTrue() : factory.createFalse());
|
|
322
|
+
if (value) return factory.createLiteralTypeNode(factory.createStringLiteral(value.toString()));
|
|
323
|
+
}).filter(Boolean)))];
|
|
324
|
+
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(Boolean), factory.createIdentifier(typeName), enums.map(([key, value]) => {
|
|
325
|
+
let initializer = factory.createStringLiteral(value?.toString());
|
|
326
|
+
if (Number.parseInt(value.toString(), 10) === value && (0, remeda.isNumber)(Number.parseInt(value.toString(), 10))) if (value < 0) initializer = factory.createPrefixUnaryExpression(typescript.default.SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value)));
|
|
327
|
+
else initializer = factory.createNumericLiteral(value);
|
|
328
|
+
if (typeof value === "boolean") initializer = value ? factory.createTrue() : factory.createFalse();
|
|
329
|
+
if ((0, remeda.isNumber)(Number.parseInt(key.toString(), 10))) {
|
|
330
|
+
const casingKey = applyEnumKeyCasing(`${typeName}_${key}`, enumKeyCasing);
|
|
331
|
+
return factory.createEnumMember(propertyName(casingKey), initializer);
|
|
332
|
+
}
|
|
333
|
+
if (key) {
|
|
334
|
+
const casingKey = applyEnumKeyCasing(key.toString(), enumKeyCasing);
|
|
335
|
+
return factory.createEnumMember(propertyName(casingKey), initializer);
|
|
336
|
+
}
|
|
337
|
+
}).filter(Boolean))];
|
|
338
|
+
const identifierName = name;
|
|
339
|
+
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))];
|
|
340
|
+
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]) => {
|
|
341
|
+
let initializer = factory.createStringLiteral(value?.toString());
|
|
342
|
+
if ((0, remeda.isNumber)(value)) if (value < 0) initializer = factory.createPrefixUnaryExpression(typescript.default.SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value)));
|
|
343
|
+
else initializer = factory.createNumericLiteral(value);
|
|
344
|
+
if (typeof value === "boolean") initializer = value ? factory.createTrue() : factory.createFalse();
|
|
345
|
+
if (key) {
|
|
346
|
+
const casingKey = applyEnumKeyCasing(key.toString(), enumKeyCasing);
|
|
347
|
+
return factory.createPropertyAssignment(propertyName(casingKey), initializer);
|
|
348
|
+
}
|
|
349
|
+
}).filter(Boolean), 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))))];
|
|
350
|
+
}
|
|
351
|
+
function createOmitDeclaration({ keys, type, nonNullable }) {
|
|
352
|
+
const node = nonNullable ? factory.createTypeReferenceNode(factory.createIdentifier("NonNullable"), [type]) : type;
|
|
353
|
+
if (Array.isArray(keys)) return factory.createTypeReferenceNode(factory.createIdentifier("Omit"), [node, factory.createUnionTypeNode(keys.map((key) => {
|
|
354
|
+
return factory.createLiteralTypeNode(factory.createStringLiteral(key));
|
|
355
|
+
}))]);
|
|
356
|
+
return factory.createTypeReferenceNode(factory.createIdentifier("Omit"), [node, factory.createLiteralTypeNode(factory.createStringLiteral(keys))]);
|
|
357
|
+
}
|
|
358
|
+
const keywordTypeNodes = {
|
|
359
|
+
any: factory.createKeywordTypeNode(typescript.default.SyntaxKind.AnyKeyword),
|
|
360
|
+
unknown: factory.createKeywordTypeNode(typescript.default.SyntaxKind.UnknownKeyword),
|
|
361
|
+
void: factory.createKeywordTypeNode(typescript.default.SyntaxKind.VoidKeyword),
|
|
362
|
+
number: factory.createKeywordTypeNode(typescript.default.SyntaxKind.NumberKeyword),
|
|
363
|
+
integer: factory.createKeywordTypeNode(typescript.default.SyntaxKind.NumberKeyword),
|
|
364
|
+
bigint: factory.createKeywordTypeNode(typescript.default.SyntaxKind.BigIntKeyword),
|
|
365
|
+
object: factory.createKeywordTypeNode(typescript.default.SyntaxKind.ObjectKeyword),
|
|
366
|
+
string: factory.createKeywordTypeNode(typescript.default.SyntaxKind.StringKeyword),
|
|
367
|
+
boolean: factory.createKeywordTypeNode(typescript.default.SyntaxKind.BooleanKeyword),
|
|
368
|
+
undefined: factory.createKeywordTypeNode(typescript.default.SyntaxKind.UndefinedKeyword),
|
|
369
|
+
null: factory.createLiteralTypeNode(factory.createToken(typescript.default.SyntaxKind.NullKeyword)),
|
|
370
|
+
never: factory.createKeywordTypeNode(typescript.default.SyntaxKind.NeverKeyword)
|
|
371
|
+
};
|
|
372
|
+
/**
|
|
373
|
+
* Converts a path like '/pet/{petId}/uploadImage' to a template literal type
|
|
374
|
+
* like `/pet/${string}/uploadImage`
|
|
375
|
+
*/
|
|
376
|
+
/**
|
|
377
|
+
* Converts an OAS-style path (e.g. `/pets/{petId}`) or an Express-style path
|
|
378
|
+
* (e.g. `/pets/:petId`) to a TypeScript template literal type
|
|
379
|
+
* like `` `/pets/${string}` ``.
|
|
380
|
+
*/
|
|
381
|
+
function createUrlTemplateType(path) {
|
|
382
|
+
const normalized = path.replace(/:([^/]+)/g, "{$1}");
|
|
383
|
+
if (!normalized.includes("{")) return factory.createLiteralTypeNode(factory.createStringLiteral(normalized));
|
|
384
|
+
const segments = normalized.split(/(\{[^}]+\})/);
|
|
385
|
+
const parts = [];
|
|
386
|
+
const parameterIndices = [];
|
|
387
|
+
segments.forEach((segment) => {
|
|
388
|
+
if (segment.startsWith("{") && segment.endsWith("}")) {
|
|
389
|
+
parameterIndices.push(parts.length);
|
|
390
|
+
parts.push(segment);
|
|
391
|
+
} else if (segment) parts.push(segment);
|
|
392
|
+
});
|
|
393
|
+
const head = typescript.default.factory.createTemplateHead(parts[0] || "");
|
|
394
|
+
const templateSpans = [];
|
|
395
|
+
parameterIndices.forEach((paramIndex, i) => {
|
|
396
|
+
const isLast = i === parameterIndices.length - 1;
|
|
397
|
+
const nextPart = parts[paramIndex + 1] || "";
|
|
398
|
+
const literal = isLast ? typescript.default.factory.createTemplateTail(nextPart) : typescript.default.factory.createTemplateMiddle(nextPart);
|
|
399
|
+
templateSpans.push(typescript.default.factory.createTemplateLiteralTypeSpan(keywordTypeNodes.string, literal));
|
|
400
|
+
});
|
|
401
|
+
return typescript.default.factory.createTemplateLiteralType(head, templateSpans);
|
|
402
|
+
}
|
|
403
|
+
const createTypeLiteralNode = factory.createTypeLiteralNode;
|
|
404
|
+
const createTypeReferenceNode = factory.createTypeReferenceNode;
|
|
405
|
+
const createNumericLiteral = factory.createNumericLiteral;
|
|
406
|
+
const createStringLiteral = factory.createStringLiteral;
|
|
407
|
+
const createArrayTypeNode = factory.createArrayTypeNode;
|
|
408
|
+
factory.createParenthesizedType;
|
|
409
|
+
const createLiteralTypeNode = factory.createLiteralTypeNode;
|
|
410
|
+
factory.createNull;
|
|
411
|
+
const createIdentifier = factory.createIdentifier;
|
|
412
|
+
const createOptionalTypeNode = factory.createOptionalTypeNode;
|
|
413
|
+
const createTupleTypeNode = factory.createTupleTypeNode;
|
|
414
|
+
const createRestTypeNode = factory.createRestTypeNode;
|
|
415
|
+
const createTrue = factory.createTrue;
|
|
416
|
+
const createFalse = factory.createFalse;
|
|
417
|
+
factory.createIndexedAccessTypeNode;
|
|
418
|
+
factory.createTypeOperatorNode;
|
|
419
|
+
const createPrefixUnaryExpression = factory.createPrefixUnaryExpression;
|
|
420
|
+
//#endregion
|
|
421
|
+
//#region src/printer.ts
|
|
422
|
+
/**
|
|
423
|
+
* Converts a primitive const value to a TypeScript literal type node.
|
|
424
|
+
* Handles negative numbers via a prefix unary expression.
|
|
425
|
+
*/
|
|
426
|
+
function constToTypeNode(value, format) {
|
|
427
|
+
if (format === "boolean") return createLiteralTypeNode(value === true ? createTrue() : createFalse());
|
|
428
|
+
if (format === "number" && typeof value === "number") {
|
|
429
|
+
if (value < 0) return createLiteralTypeNode(createPrefixUnaryExpression(SyntaxKind.MinusToken, createNumericLiteral(Math.abs(value))));
|
|
430
|
+
return createLiteralTypeNode(createNumericLiteral(value));
|
|
431
|
+
}
|
|
432
|
+
return createLiteralTypeNode(createStringLiteral(String(value)));
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Returns a `Date` reference type node when `representation` is `'date'`, otherwise falls back to `string`.
|
|
436
|
+
*/
|
|
437
|
+
function dateOrStringNode(node) {
|
|
438
|
+
return node.representation === "date" ? createTypeReferenceNode(createIdentifier("Date")) : keywordTypeNodes.string;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Maps an array of `SchemaNode`s through the printer, filtering out `null` and `undefined` results.
|
|
442
|
+
*/
|
|
443
|
+
function buildMemberNodes(members, print) {
|
|
444
|
+
return (members ?? []).map(print).filter(Boolean);
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Builds a TypeScript tuple type node from an array schema's `items`,
|
|
448
|
+
* applying min/max slice and optional/rest element rules.
|
|
449
|
+
*/
|
|
450
|
+
function buildTupleNode(node, print) {
|
|
451
|
+
let items = (node.items ?? []).map(print).filter(Boolean);
|
|
452
|
+
const restNode = node.rest ? print(node.rest) ?? void 0 : void 0;
|
|
453
|
+
const { min, max } = node;
|
|
454
|
+
if (max !== void 0) {
|
|
455
|
+
items = items.slice(0, max);
|
|
456
|
+
if (items.length < max && restNode) items = [...items, ...Array(max - items.length).fill(restNode)];
|
|
457
|
+
}
|
|
458
|
+
if (min !== void 0) items = items.map((item, i) => i >= min ? createOptionalTypeNode(item) : item);
|
|
459
|
+
if (max === void 0 && restNode) items.push(createRestTypeNode(createArrayTypeNode(restNode)));
|
|
460
|
+
return createTupleTypeNode(items);
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Applies `nullable` and optional/nullish `| undefined` union modifiers to a property's resolved base type.
|
|
464
|
+
*/
|
|
465
|
+
function buildPropertyType(schema, baseType, optionalType) {
|
|
466
|
+
const addsUndefined = OPTIONAL_ADDS_UNDEFINED.has(optionalType);
|
|
467
|
+
let type = baseType;
|
|
468
|
+
if (schema.nullable) type = createUnionDeclaration({ nodes: [type, keywordTypeNodes.null] });
|
|
469
|
+
if ((schema.nullish || schema.optional) && addsUndefined) type = createUnionDeclaration({ nodes: [type, keywordTypeNodes.undefined] });
|
|
470
|
+
return type;
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Collects JSDoc annotation strings (description, deprecated, min/max, pattern, default, example, type) for a schema node.
|
|
474
|
+
*/
|
|
475
|
+
function buildPropertyJSDocComments(schema) {
|
|
476
|
+
return [
|
|
477
|
+
"description" in schema && schema.description ? `@description ${jsStringEscape(schema.description)}` : void 0,
|
|
478
|
+
"deprecated" in schema && schema.deprecated ? "@deprecated" : void 0,
|
|
479
|
+
"min" in schema && schema.min !== void 0 ? `@minLength ${schema.min}` : void 0,
|
|
480
|
+
"max" in schema && schema.max !== void 0 ? `@maxLength ${schema.max}` : void 0,
|
|
481
|
+
"pattern" in schema && schema.pattern ? `@pattern ${schema.pattern}` : void 0,
|
|
482
|
+
"default" in schema && schema.default !== void 0 ? `@default ${"primitive" in schema && schema.primitive === "string" ? stringify(schema.default) : schema.default}` : void 0,
|
|
483
|
+
"example" in schema && schema.example !== void 0 ? `@example ${schema.example}` : void 0,
|
|
484
|
+
"primitive" in schema && schema.primitive ? [`@type ${schema.primitive || "unknown"}`, "optional" in schema && schema.optional ? " | undefined" : void 0].filter(Boolean).join("") : void 0
|
|
485
|
+
];
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Creates TypeScript index signatures for `additionalProperties` and `patternProperties` on an object schema node.
|
|
489
|
+
*/
|
|
490
|
+
function buildIndexSignatures(node, propertyCount, print) {
|
|
491
|
+
const elements = [];
|
|
492
|
+
if (node.additionalProperties && node.additionalProperties !== true) {
|
|
493
|
+
const additionalType = print(node.additionalProperties) ?? keywordTypeNodes.unknown;
|
|
494
|
+
elements.push(createIndexSignature(propertyCount > 0 ? keywordTypeNodes.unknown : additionalType));
|
|
495
|
+
} else if (node.additionalProperties === true) elements.push(createIndexSignature(keywordTypeNodes.unknown));
|
|
496
|
+
if (node.patternProperties) {
|
|
497
|
+
const first = Object.values(node.patternProperties)[0];
|
|
498
|
+
if (first) {
|
|
499
|
+
let patternType = print(first) ?? keywordTypeNodes.unknown;
|
|
500
|
+
if (first.nullable) patternType = createUnionDeclaration({ nodes: [patternType, keywordTypeNodes.null] });
|
|
501
|
+
elements.push(createIndexSignature(patternType));
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
return elements;
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* TypeScript type printer built with `definePrinter`.
|
|
508
|
+
*
|
|
509
|
+
* Converts a `SchemaNode` AST node into a TypeScript AST node:
|
|
510
|
+
* - **`printer.print(node)`** — when `options.typeName` is set, returns a full
|
|
511
|
+
* `type Name = …` or `interface Name { … }` declaration (`ts.Node`).
|
|
512
|
+
* Without `typeName`, returns the raw `ts.TypeNode` for the schema.
|
|
513
|
+
*
|
|
514
|
+
* Dispatches on `node.type` to the appropriate handler in `nodes`. Options are closed
|
|
515
|
+
* over per printer instance, so each call to `printerTs(options)` produces an independent printer.
|
|
516
|
+
*
|
|
517
|
+
* @example Raw type node (no `typeName`)
|
|
518
|
+
* ```ts
|
|
519
|
+
* const printer = printerTs({ optionalType: 'questionToken', arrayType: 'array', enumType: 'inlineLiteral' })
|
|
520
|
+
* const typeNode = printer.print(schemaNode) // ts.TypeNode
|
|
521
|
+
* ```
|
|
522
|
+
*
|
|
523
|
+
* @example Full declaration (with `typeName`)
|
|
524
|
+
* ```ts
|
|
525
|
+
* const printer = printerTs({ optionalType: 'questionToken', arrayType: 'array', enumType: 'inlineLiteral', typeName: 'MyType' })
|
|
526
|
+
* const declaration = printer.print(schemaNode) // ts.TypeAliasDeclaration | ts.InterfaceDeclaration
|
|
527
|
+
* ```
|
|
528
|
+
*/
|
|
529
|
+
const printerTs = (0, _kubb_core.definePrinter)((options) => {
|
|
530
|
+
const addsUndefined = OPTIONAL_ADDS_UNDEFINED.has(options.optionalType);
|
|
531
|
+
return {
|
|
532
|
+
name: "typescript",
|
|
533
|
+
options,
|
|
534
|
+
nodes: {
|
|
535
|
+
any: () => keywordTypeNodes.any,
|
|
536
|
+
unknown: () => keywordTypeNodes.unknown,
|
|
537
|
+
void: () => keywordTypeNodes.void,
|
|
538
|
+
never: () => keywordTypeNodes.never,
|
|
539
|
+
boolean: () => keywordTypeNodes.boolean,
|
|
540
|
+
null: () => keywordTypeNodes.null,
|
|
541
|
+
blob: () => createTypeReferenceNode("Blob", []),
|
|
542
|
+
string: () => keywordTypeNodes.string,
|
|
543
|
+
uuid: () => keywordTypeNodes.string,
|
|
544
|
+
email: () => keywordTypeNodes.string,
|
|
545
|
+
url: (node) => {
|
|
546
|
+
if (node.path) return createUrlTemplateType(node.path);
|
|
547
|
+
return keywordTypeNodes.string;
|
|
548
|
+
},
|
|
549
|
+
datetime: () => keywordTypeNodes.string,
|
|
550
|
+
number: () => keywordTypeNodes.number,
|
|
551
|
+
integer: () => keywordTypeNodes.number,
|
|
552
|
+
bigint: () => keywordTypeNodes.bigint,
|
|
553
|
+
date: dateOrStringNode,
|
|
554
|
+
time: dateOrStringNode,
|
|
555
|
+
ref(node) {
|
|
556
|
+
if (!node.name) return;
|
|
557
|
+
const refName = node.ref ? node.ref.split("/").at(-1) ?? node.name : node.name;
|
|
558
|
+
return createTypeReferenceNode(node.ref ? this.options.resolver.default(refName, "type") : refName, void 0);
|
|
559
|
+
},
|
|
560
|
+
enum(node) {
|
|
561
|
+
const values = node.namedEnumValues?.map((v) => v.value) ?? node.enumValues ?? [];
|
|
562
|
+
if (this.options.enumType === "inlineLiteral" || !node.name) return createUnionDeclaration({
|
|
563
|
+
withParentheses: true,
|
|
564
|
+
nodes: values.filter((v) => v !== null).map((value) => constToTypeNode(value, typeof value)).filter(Boolean)
|
|
565
|
+
}) ?? void 0;
|
|
566
|
+
const resolvedName = this.options.resolver.default(node.name, "type");
|
|
567
|
+
return createTypeReferenceNode(ENUM_TYPES_WITH_KEY_SUFFIX.has(this.options.enumType) ? `${resolvedName}Key` : resolvedName, void 0);
|
|
568
|
+
},
|
|
569
|
+
union(node) {
|
|
570
|
+
const members = node.members ?? [];
|
|
571
|
+
const hasStringLiteral = members.some((m) => m.type === "enum" && (m.enumType === "string" || m.primitive === "string"));
|
|
572
|
+
const hasPlainString = members.some((m) => (0, _kubb_ast.isPlainStringType)(m));
|
|
573
|
+
if (hasStringLiteral && hasPlainString) return createUnionDeclaration({
|
|
574
|
+
withParentheses: true,
|
|
575
|
+
nodes: members.map((m) => {
|
|
576
|
+
if ((0, _kubb_ast.isPlainStringType)(m)) return createIntersectionDeclaration({
|
|
577
|
+
nodes: [keywordTypeNodes.string, createTypeLiteralNode([])],
|
|
578
|
+
withParentheses: true
|
|
579
|
+
});
|
|
580
|
+
return this.print(m);
|
|
581
|
+
}).filter(Boolean)
|
|
582
|
+
}) ?? void 0;
|
|
583
|
+
return createUnionDeclaration({
|
|
584
|
+
withParentheses: true,
|
|
585
|
+
nodes: buildMemberNodes(members, this.print)
|
|
586
|
+
}) ?? void 0;
|
|
587
|
+
},
|
|
588
|
+
intersection(node) {
|
|
589
|
+
return createIntersectionDeclaration({
|
|
590
|
+
withParentheses: true,
|
|
591
|
+
nodes: buildMemberNodes(node.members, this.print)
|
|
592
|
+
}) ?? void 0;
|
|
593
|
+
},
|
|
594
|
+
array(node) {
|
|
595
|
+
return createArrayDeclaration({
|
|
596
|
+
nodes: (node.items ?? []).map((item) => this.print(item)).filter(Boolean),
|
|
597
|
+
arrayType: this.options.arrayType
|
|
598
|
+
}) ?? void 0;
|
|
599
|
+
},
|
|
600
|
+
tuple(node) {
|
|
601
|
+
return buildTupleNode(node, this.print);
|
|
602
|
+
},
|
|
603
|
+
object(node) {
|
|
604
|
+
const { print, options } = this;
|
|
605
|
+
const addsQuestionToken = OPTIONAL_ADDS_QUESTION_TOKEN.has(options.optionalType);
|
|
606
|
+
const propertyNodes = node.properties.map((prop) => {
|
|
607
|
+
const baseType = print(prop.schema) ?? keywordTypeNodes.unknown;
|
|
608
|
+
const type = buildPropertyType(prop.schema, baseType, options.optionalType);
|
|
609
|
+
return appendJSDocToNode({
|
|
610
|
+
node: createPropertySignature({
|
|
611
|
+
questionToken: prop.schema.optional || prop.schema.nullish ? addsQuestionToken : false,
|
|
612
|
+
name: prop.name,
|
|
613
|
+
type,
|
|
614
|
+
readOnly: prop.schema.readOnly
|
|
615
|
+
}),
|
|
616
|
+
comments: buildPropertyJSDocComments(prop.schema)
|
|
617
|
+
});
|
|
618
|
+
});
|
|
619
|
+
const allElements = [...propertyNodes, ...buildIndexSignatures(node, propertyNodes.length, print)];
|
|
620
|
+
if (!allElements.length) return keywordTypeNodes.object;
|
|
621
|
+
return createTypeLiteralNode(allElements);
|
|
622
|
+
}
|
|
623
|
+
},
|
|
624
|
+
print(node) {
|
|
625
|
+
let type = this.print(node);
|
|
626
|
+
if (!type) return;
|
|
627
|
+
if (node.nullable) type = createUnionDeclaration({ nodes: [type, keywordTypeNodes.null] });
|
|
628
|
+
if ((node.nullish || node.optional) && addsUndefined) type = createUnionDeclaration({ nodes: [type, keywordTypeNodes.undefined] });
|
|
629
|
+
const { typeName, syntaxType = "type", description, keysToOmit } = this.options;
|
|
630
|
+
if (!typeName) return type;
|
|
631
|
+
const useTypeGeneration = syntaxType === "type" || type.kind === syntaxKind.union || !!keysToOmit?.length;
|
|
632
|
+
return createTypeDeclaration({
|
|
633
|
+
name: typeName,
|
|
634
|
+
isExportable: true,
|
|
635
|
+
type: keysToOmit?.length ? createOmitDeclaration({
|
|
636
|
+
keys: keysToOmit,
|
|
637
|
+
type,
|
|
638
|
+
nonNullable: true
|
|
639
|
+
}) : type,
|
|
640
|
+
syntax: useTypeGeneration ? "type" : "interface",
|
|
641
|
+
comments: [
|
|
642
|
+
node?.title ? jsStringEscape(node.title) : void 0,
|
|
643
|
+
description ? `@description ${jsStringEscape(description)}` : void 0,
|
|
644
|
+
node?.deprecated ? "@deprecated" : void 0,
|
|
645
|
+
node && "min" in node && node.min !== void 0 ? `@minLength ${node.min}` : void 0,
|
|
646
|
+
node && "max" in node && node.max !== void 0 ? `@maxLength ${node.max}` : void 0,
|
|
647
|
+
node && "pattern" in node && node.pattern ? `@pattern ${node.pattern}` : void 0,
|
|
648
|
+
node?.default ? `@default ${node.default}` : void 0,
|
|
649
|
+
node?.example ? `@example ${node.example}` : void 0
|
|
650
|
+
]
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
});
|
|
655
|
+
//#endregion
|
|
656
|
+
//#region src/components/Enum.tsx
|
|
657
|
+
/**
|
|
658
|
+
* Resolves the runtime identifier name and the TypeScript type name for an enum schema node.
|
|
659
|
+
*
|
|
660
|
+
* The raw `node.name` may be a YAML key such as `"enumNames.Type"` which is not a
|
|
661
|
+
* valid TypeScript identifier. The resolver normalizes it; for inline enum
|
|
662
|
+
* properties the adapter already emits a PascalCase+suffix name so resolution is typically a no-op.
|
|
663
|
+
*/
|
|
664
|
+
function getEnumNames({ node, enumType, resolver }) {
|
|
665
|
+
const resolved = resolver.default(node.name, "type");
|
|
666
|
+
return {
|
|
667
|
+
enumName: enumType === "asPascalConst" ? resolved : camelCase(node.name),
|
|
668
|
+
typeName: ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) ? `${resolved}Key` : resolved,
|
|
669
|
+
refName: resolved
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Renders the enum declaration(s) for a single named `EnumSchemaNode`.
|
|
674
|
+
*
|
|
675
|
+
* Depending on `enumType` this may emit:
|
|
676
|
+
* - A runtime object (`asConst` / `asPascalConst`) plus a `typeof` type alias
|
|
677
|
+
* - A `const enum` or plain `enum` declaration (`constEnum` / `enum`)
|
|
678
|
+
* - A union literal type alias (`literal`)
|
|
679
|
+
*
|
|
680
|
+
* The emitted `File.Source` nodes carry the resolved names so that the barrel
|
|
681
|
+
* index picks up the correct export identifiers.
|
|
682
|
+
*/
|
|
683
|
+
function Enum({ node, enumType, enumKeyCasing, resolver }) {
|
|
684
|
+
const { enumName, typeName, refName } = getEnumNames({
|
|
685
|
+
node,
|
|
686
|
+
enumType,
|
|
687
|
+
resolver
|
|
688
|
+
});
|
|
689
|
+
const [nameNode, typeNode] = createEnumDeclaration({
|
|
690
|
+
name: enumName,
|
|
691
|
+
typeName,
|
|
692
|
+
enums: node.namedEnumValues?.map((v) => [trimQuotes(v.name.toString()), v.value]) ?? node.enumValues?.filter((v) => v !== null && v !== void 0).map((v) => [trimQuotes(v.toString()), v]) ?? [],
|
|
693
|
+
type: enumType,
|
|
694
|
+
enumKeyCasing
|
|
695
|
+
});
|
|
696
|
+
const needsRefAlias = ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && refName !== typeName;
|
|
697
|
+
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [
|
|
698
|
+
nameNode && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
|
|
699
|
+
name: enumName,
|
|
700
|
+
isExportable: true,
|
|
701
|
+
isIndexable: true,
|
|
702
|
+
isTypeOnly: false,
|
|
703
|
+
children: (0, _kubb_fabric_core_parsers_typescript.safePrint)(nameNode)
|
|
704
|
+
}),
|
|
705
|
+
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
|
|
706
|
+
name: typeName,
|
|
707
|
+
isIndexable: true,
|
|
708
|
+
isExportable: ENUM_TYPES_WITH_RUNTIME_VALUE.has(enumType),
|
|
709
|
+
isTypeOnly: ENUM_TYPES_WITH_TYPE_ONLY.has(enumType),
|
|
710
|
+
children: (0, _kubb_fabric_core_parsers_typescript.safePrint)(typeNode)
|
|
711
|
+
}),
|
|
712
|
+
needsRefAlias && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
|
|
713
|
+
name: refName,
|
|
714
|
+
isExportable: true,
|
|
715
|
+
isIndexable: true,
|
|
716
|
+
isTypeOnly: true,
|
|
717
|
+
children: `export type ${refName} = ${typeName}`
|
|
718
|
+
})
|
|
719
|
+
] });
|
|
720
|
+
}
|
|
721
|
+
//#endregion
|
|
722
|
+
//#region src/components/Type.tsx
|
|
723
|
+
function Type({ name, typedName, node, keysToOmit, optionalType, arrayType, syntaxType, enumType, enumKeyCasing, description, resolver }) {
|
|
724
|
+
const resolvedDescription = description || node?.description;
|
|
725
|
+
const enumSchemaNodes = (0, _kubb_ast.collect)(node, { schema(n) {
|
|
726
|
+
if (n.type === "enum" && n.name) return n;
|
|
727
|
+
} });
|
|
728
|
+
const typeNode = printerTs({
|
|
729
|
+
optionalType,
|
|
730
|
+
arrayType,
|
|
731
|
+
enumType,
|
|
732
|
+
typeName: name,
|
|
733
|
+
syntaxType,
|
|
734
|
+
description: resolvedDescription,
|
|
735
|
+
keysToOmit,
|
|
736
|
+
resolver
|
|
737
|
+
}).print(node);
|
|
738
|
+
if (!typeNode) return;
|
|
739
|
+
const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
|
|
740
|
+
return {
|
|
741
|
+
node,
|
|
742
|
+
...getEnumNames({
|
|
743
|
+
node,
|
|
744
|
+
enumType,
|
|
745
|
+
resolver
|
|
746
|
+
})
|
|
747
|
+
};
|
|
748
|
+
});
|
|
749
|
+
const shouldExportEnums = enumType !== "inlineLiteral";
|
|
750
|
+
const shouldExportType = enumType === "inlineLiteral" || enums.every((item) => item.typeName !== name);
|
|
751
|
+
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [shouldExportEnums && enums.map(({ node }) => /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(Enum, {
|
|
752
|
+
node,
|
|
753
|
+
enumType,
|
|
754
|
+
enumKeyCasing,
|
|
755
|
+
resolver
|
|
756
|
+
})), shouldExportType && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
|
|
757
|
+
name: typedName,
|
|
758
|
+
isTypeOnly: true,
|
|
759
|
+
isExportable: true,
|
|
760
|
+
isIndexable: true,
|
|
761
|
+
children: (0, _kubb_fabric_core_parsers_typescript.safePrint)(typeNode)
|
|
762
|
+
})] });
|
|
763
|
+
}
|
|
764
|
+
//#endregion
|
|
765
|
+
Object.defineProperty(exports, "ENUM_TYPES_WITH_KEY_SUFFIX", {
|
|
766
|
+
enumerable: true,
|
|
767
|
+
get: function() {
|
|
768
|
+
return ENUM_TYPES_WITH_KEY_SUFFIX;
|
|
769
|
+
}
|
|
770
|
+
});
|
|
771
|
+
Object.defineProperty(exports, "Enum", {
|
|
772
|
+
enumerable: true,
|
|
773
|
+
get: function() {
|
|
774
|
+
return Enum;
|
|
775
|
+
}
|
|
776
|
+
});
|
|
777
|
+
Object.defineProperty(exports, "Type", {
|
|
778
|
+
enumerable: true,
|
|
779
|
+
get: function() {
|
|
780
|
+
return Type;
|
|
781
|
+
}
|
|
782
|
+
});
|
|
783
|
+
Object.defineProperty(exports, "__name", {
|
|
784
|
+
enumerable: true,
|
|
785
|
+
get: function() {
|
|
786
|
+
return __name;
|
|
787
|
+
}
|
|
788
|
+
});
|
|
789
|
+
Object.defineProperty(exports, "__toESM", {
|
|
790
|
+
enumerable: true,
|
|
791
|
+
get: function() {
|
|
792
|
+
return __toESM;
|
|
793
|
+
}
|
|
794
|
+
});
|
|
795
|
+
Object.defineProperty(exports, "camelCase", {
|
|
796
|
+
enumerable: true,
|
|
797
|
+
get: function() {
|
|
798
|
+
return camelCase;
|
|
799
|
+
}
|
|
800
|
+
});
|
|
801
|
+
Object.defineProperty(exports, "pascalCase", {
|
|
802
|
+
enumerable: true,
|
|
803
|
+
get: function() {
|
|
804
|
+
return pascalCase;
|
|
805
|
+
}
|
|
806
|
+
});
|
|
807
|
+
|
|
808
|
+
//# sourceMappingURL=Type-Cat0_htq.cjs.map
|