vovk-cli 0.0.1-draft.275 → 0.0.1-draft.276
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/utils/compileTs.mjs +17 -12
- package/package.json +1 -1
package/dist/utils/compileTs.mjs
CHANGED
|
@@ -8,7 +8,9 @@ export function compileTs(options) {
|
|
|
8
8
|
};
|
|
9
9
|
// Collect all definitions from the schema
|
|
10
10
|
collectDefinitions(options.schema, context.refs);
|
|
11
|
-
|
|
11
|
+
// Ensure the main type name is valid
|
|
12
|
+
const mainTypeName = sanitizeTypeName(options.name);
|
|
13
|
+
const mainType = compileSchema(options.schema, mainTypeName, context);
|
|
12
14
|
// Compile all referenced types, unless dontCreateRefTypes is set
|
|
13
15
|
const compiledRefs = options.dontCreateRefTypes
|
|
14
16
|
? ''
|
|
@@ -16,8 +18,8 @@ export function compileTs(options) {
|
|
|
16
18
|
.map(([, typeDecl]) => typeDecl)
|
|
17
19
|
.join('\n\n');
|
|
18
20
|
return compiledRefs
|
|
19
|
-
? `${compiledRefs}\n\n${options.schema.description ? `/** ${options.schema.description} */\n` : ''}export type ${
|
|
20
|
-
: `${options.schema.description ? `/** ${options.schema.description} */\n` : ''}export type ${
|
|
21
|
+
? `${compiledRefs}\n\n${options.schema.description ? `/** ${options.schema.description} */\n` : ''}export type ${mainTypeName} = ${mainType};`
|
|
22
|
+
: `${options.schema.description ? `/** ${options.schema.description} */\n` : ''}export type ${mainTypeName} = ${mainType};`;
|
|
21
23
|
}
|
|
22
24
|
function collectDefinitions(schema, refs) {
|
|
23
25
|
// Collect from $defs
|
|
@@ -158,7 +160,7 @@ function handleRef(ref, context) {
|
|
|
158
160
|
return typeName;
|
|
159
161
|
}
|
|
160
162
|
function handleAllOf(schemas, name, context) {
|
|
161
|
-
const types = schemas.map((s, i) => compileSchema(s, `${name}
|
|
163
|
+
const types = schemas.map((s, i) => compileSchema(s, sanitizeTypeName(`${name}-all-of-${i}`), context));
|
|
162
164
|
// For allOf, we need to intersect types
|
|
163
165
|
// If they're all objects, we can merge them properly
|
|
164
166
|
const objectTypes = types.filter((t) => t.startsWith('{') && t.endsWith('}'));
|
|
@@ -170,12 +172,12 @@ function handleAllOf(schemas, name, context) {
|
|
|
170
172
|
return types.join(' & ');
|
|
171
173
|
}
|
|
172
174
|
function handleAnyOf(schemas, name, context) {
|
|
173
|
-
const types = schemas.map((s, i) => compileSchema(s, `${name}
|
|
175
|
+
const types = schemas.map((s, i) => compileSchema(s, sanitizeTypeName(`${name}-any-of-${i}`), context));
|
|
174
176
|
return types.join(' | ');
|
|
175
177
|
}
|
|
176
178
|
function handleOneOf(schemas, name, context) {
|
|
177
179
|
// For TypeScript, oneOf behaves like anyOf
|
|
178
|
-
const types = schemas.map((s, i) => compileSchema(s, `${name}
|
|
180
|
+
const types = schemas.map((s, i) => compileSchema(s, sanitizeTypeName(`${name}-one-of-${i}`), context));
|
|
179
181
|
return types.join(' | ');
|
|
180
182
|
}
|
|
181
183
|
function handleEnum(enumValues) {
|
|
@@ -205,7 +207,9 @@ function handleObject(schema, name, context) {
|
|
|
205
207
|
if (!isSchema(propSchema))
|
|
206
208
|
continue;
|
|
207
209
|
const isRequired = required.has(propName);
|
|
208
|
-
|
|
210
|
+
// Ensure the generated type name for nested properties is valid
|
|
211
|
+
const nestedTypeName = sanitizeTypeName(`${name}-${propName}`);
|
|
212
|
+
const propType = compileSchema(propSchema, nestedTypeName, context);
|
|
209
213
|
const safePropName = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(propName) ? propName : `"${propName}"`;
|
|
210
214
|
// Add JSDoc comment if description is present
|
|
211
215
|
const comment = propSchema.description ? `\n/** ${propSchema.description} */\n` : '';
|
|
@@ -217,7 +221,8 @@ function handleObject(schema, name, context) {
|
|
|
217
221
|
props.push('[key: string]: any');
|
|
218
222
|
}
|
|
219
223
|
else if (schema.additionalProperties && isSchema(schema.additionalProperties)) {
|
|
220
|
-
const
|
|
224
|
+
const additionalTypeName = sanitizeTypeName(`${name}-additional`);
|
|
225
|
+
const additionalType = compileSchema(schema.additionalProperties, additionalTypeName, context);
|
|
221
226
|
props.push(`[key: string]: ${additionalType}`);
|
|
222
227
|
}
|
|
223
228
|
// Handle pattern properties
|
|
@@ -225,7 +230,7 @@ function handleObject(schema, name, context) {
|
|
|
225
230
|
// For simplicity, treat pattern properties as string index signature
|
|
226
231
|
const patternTypes = Object.values(schema.patternProperties)
|
|
227
232
|
.filter(isSchema)
|
|
228
|
-
.map((s, i) => compileSchema(s, `${name}
|
|
233
|
+
.map((s, i) => compileSchema(s, sanitizeTypeName(`${name}-pattern-${i}`), context));
|
|
229
234
|
if (patternTypes.length > 0) {
|
|
230
235
|
props.push(`[key: string]: ${patternTypes.join(' | ')}`);
|
|
231
236
|
}
|
|
@@ -239,10 +244,10 @@ function refToTypeName(ref) {
|
|
|
239
244
|
// Convert kebab-case to PascalCase
|
|
240
245
|
return upperFirst(camelCase(name));
|
|
241
246
|
}
|
|
242
|
-
function capitalize(str) {
|
|
243
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
244
|
-
}
|
|
245
247
|
function wrapUnionType(type) {
|
|
246
248
|
// Wrap union types in parentheses for array types
|
|
247
249
|
return type.includes('|') ? `(${type})` : type;
|
|
248
250
|
}
|
|
251
|
+
function sanitizeTypeName(name) {
|
|
252
|
+
return upperFirst(camelCase(name));
|
|
253
|
+
}
|