vovk-cli 0.0.1-draft.245 → 0.0.1-draft.247

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.
@@ -4,7 +4,7 @@ export type Controllers = {
4
4
  <% Object.values(t.schema.segments.extensions.controllers).forEach((controllerSchema) => { %>
5
5
  <%= controllerSchema.rpcModuleName %>: {
6
6
  <% Object.entries(controllerSchema.handlers).forEach(([handlerName, handler]) => { %>
7
- <%= handlerName %>: (req: VovkRequest<<%- t.convertJSONSchemaToTypeScriptDef(handler.validation?.body) ?? 'null' %>, <%- t.convertJSONSchemaToTypeScriptDef(handler.validation?.query) ?? 'undefined' %>, <%- t.convertJSONSchemaToTypeScriptDef(handler.validation?.params ?? 'undefined') %>>) => <%- handler.validation?.output ? `Promise<${t.convertJSONSchemaToTypeScriptDef(handler.validation?.body)}>` : handler.validation?.iteration ? `Promise<VovkStreamAsyncIterable<${t.convertJSONSchemaToTypeScriptDef(handler.validation?.iteration)}>>` : 'Promise<KnownAny>' %>,
7
+ <%= handlerName %>: (req: VovkRequest<<%- t.convertJSONSchemaToTypeScriptDef(handler.validation?.body) ?? 'null' %>, <%- t.convertJSONSchemaToTypeScriptDef(handler.validation?.query) ?? 'undefined' %>, <%- t.convertJSONSchemaToTypeScriptDef(handler.validation?.params ?? 'undefined') %>>) => <%- handler.validation?.output ? `Promise<${t.convertJSONSchemaToTypeScriptDef(handler.validation?.output)}>` : handler.validation?.iteration ? `Promise<VovkStreamAsyncIterable<${t.convertJSONSchemaToTypeScriptDef(handler.validation?.iteration)}>>` : 'Promise<KnownAny>' %>,
8
8
  <% }) %>
9
9
  };
10
10
  <% }) %>
@@ -9,14 +9,74 @@ export function convertJSONSchemaToTypeScriptDef(schema) {
9
9
  const escapeJSDocComment = (str) => {
10
10
  return str.replace(/\*\//g, '*\\/');
11
11
  };
12
- // Pre-process the schema to handle definitions
13
- const processedSchema = schema;
12
+ // Helper function to check if a property name is a valid JavaScript identifier
13
+ const isValidIdentifier = (name) => {
14
+ // Check if it matches valid JavaScript identifier pattern and is not a reserved word
15
+ return (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name) &&
16
+ ![
17
+ 'break',
18
+ 'case',
19
+ 'catch',
20
+ 'class',
21
+ 'const',
22
+ 'continue',
23
+ 'debugger',
24
+ 'default',
25
+ 'delete',
26
+ 'do',
27
+ 'else',
28
+ 'export',
29
+ 'extends',
30
+ 'false',
31
+ 'finally',
32
+ 'for',
33
+ 'function',
34
+ 'if',
35
+ 'import',
36
+ 'in',
37
+ 'instanceof',
38
+ 'new',
39
+ 'null',
40
+ 'return',
41
+ 'super',
42
+ 'switch',
43
+ 'this',
44
+ 'throw',
45
+ 'true',
46
+ 'try',
47
+ 'typeof',
48
+ 'var',
49
+ 'void',
50
+ 'while',
51
+ 'with',
52
+ 'let',
53
+ 'static',
54
+ 'yield',
55
+ 'enum',
56
+ 'await',
57
+ 'implements',
58
+ 'interface',
59
+ 'package',
60
+ 'private',
61
+ 'protected',
62
+ 'public',
63
+ ].includes(name));
64
+ };
65
+ // Helper function to format property name (with quotes if needed)
66
+ const formatPropertyName = (name) => {
67
+ if (isValidIdentifier(name)) {
68
+ return name;
69
+ }
70
+ else {
71
+ return `'${escapeStringLiteral(name)}'`;
72
+ }
73
+ };
14
74
  // Helper function to resolve $ref references
15
75
  const resolveRef = (ref) => {
16
76
  if (!ref.startsWith('#/'))
17
77
  return null;
18
78
  const path = ref.substring(2).split('/');
19
- let currentSchema = processedSchema; // Use processedSchema instead of schema
79
+ let currentSchema = schema;
20
80
  for (const segment of path) {
21
81
  if (!currentSchema || typeof currentSchema !== 'object') {
22
82
  return null;
@@ -114,13 +174,16 @@ export function convertJSONSchemaToTypeScriptDef(schema) {
114
174
  const type = propSchema ? 'KnownAny' : 'never';
115
175
  const isOptional = !required.includes(propName);
116
176
  const jsDoc = getJSDoc(propSchema, indentation);
117
- return `${jsDoc}\n${indentation}${propName}${isOptional ? '?' : ''}: ${type};`;
177
+ return `${jsDoc}\n${indentation}${formatPropertyName(propName)}${isOptional ? '?' : ''}: ${type};`;
118
178
  }
119
179
  const isOptional = !required.includes(propName);
120
180
  const defaultValue = propSchema.default !== undefined ? ` // default: ${JSON.stringify(propSchema.default)}` : '';
121
181
  const jsDoc = getJSDoc(propSchema, indentation);
122
182
  const propType = schemaToType(propSchema, indentation + ' ');
123
- return [`${jsDoc}`, `${indentation}${propName}${isOptional ? '?' : ''}: ${propType};${defaultValue}`]
183
+ return [
184
+ `${jsDoc}`,
185
+ `${indentation}${formatPropertyName(propName)}${isOptional ? '?' : ''}: ${propType};${defaultValue}`,
186
+ ]
124
187
  .filter(Boolean)
125
188
  .join('\n');
126
189
  })
@@ -187,27 +250,8 @@ export function convertJSONSchemaToTypeScriptDef(schema) {
187
250
  return 'undefined';
188
251
  }
189
252
  };
190
- // Preprocess schema to extract $defs into top level for easier reference resolution
191
- function extractDefinitions(inputSchema) {
192
- // Create a shallow copy to avoid modifying the original
193
- const resultSchema = { ...inputSchema };
194
- // Move $defs to definitions for compatibility
195
- if (resultSchema.$defs && !resultSchema.definitions) {
196
- resultSchema.definitions = resultSchema.$defs;
197
- }
198
- return resultSchema;
199
- }
200
- const schemaWithExtractedDefs = extractDefinitions(schema);
201
253
  // Generate the interface
202
- const jsDoc = getJSDoc(schemaWithExtractedDefs);
203
- let interfaceBody;
204
- try {
205
- interfaceBody = schemaToType(schemaWithExtractedDefs);
206
- }
207
- catch (error) {
208
- // eslint-disable-next-line no-console
209
- console.error('Error generating TypeScript definition:', error);
210
- return null;
211
- }
254
+ const jsDoc = getJSDoc(schema);
255
+ const interfaceBody = schemaToType(schema);
212
256
  return `${jsDoc}\n${interfaceBody}`;
213
257
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk-cli",
3
- "version": "0.0.1-draft.245",
3
+ "version": "0.0.1-draft.247",
4
4
  "bin": {
5
5
  "vovk": "./dist/index.mjs"
6
6
  },