@vkontakte/api-schema-typescript-generator 0.15.0 → 0.16.0

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/cli.js CHANGED
@@ -7,7 +7,7 @@ exports.parseArguments = void 0;
7
7
  const arg_1 = __importDefault(require("arg"));
8
8
  const utils_1 = require("./utils");
9
9
  function parseArguments() {
10
- const args = arg_1.default({
10
+ const args = (0, arg_1.default)({
11
11
  '--help': Boolean,
12
12
  '--schemaDir': String,
13
13
  '--outDir': String,
@@ -21,9 +21,9 @@ function parseArguments() {
21
21
  const outDir = args['--outDir'];
22
22
  return {
23
23
  help: args['--help'] || false,
24
- schemaDir: utils_1.isString(schemaDir) ? schemaDir.trim() : null,
25
- outDir: utils_1.isString(outDir) ? outDir.trim() : null,
26
- methods: utils_1.trimArray(args['--methods'] || []),
24
+ schemaDir: (0, utils_1.isString)(schemaDir) ? schemaDir.trim() : null,
25
+ outDir: (0, utils_1.isString)(outDir) ? outDir.trim() : null,
26
+ methods: (0, utils_1.trimArray)(args['--methods'] || []),
27
27
  };
28
28
  }
29
29
  exports.parseArguments = parseArguments;
package/dist/constants.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.baseAPIParamsInterfaceName = exports.basePropertyExistsRef = exports.baseOkResponseRef = exports.baseBoolIntRef = exports.newLineChar = exports.tabChar = exports.spaceChar = exports.primitiveTypes = exports.scalarTypes = exports.PropertyType = exports.DEFAULT_API_VERSION = void 0;
4
4
  const os_1 = require("os");
5
- exports.DEFAULT_API_VERSION = '5.130';
5
+ exports.DEFAULT_API_VERSION = '5.131';
6
6
  exports.PropertyType = {
7
7
  INTEGER: 'integer',
8
8
  BOOLEAN: 'boolean',
package/dist/generator.js CHANGED
@@ -9,11 +9,11 @@ function generateImportsBlock(refs, section, type) {
9
9
  let importRefs = Object.entries(refs)
10
10
  .filter(([, type]) => type === types_1.RefsDictionaryType.GenerateAndImport)
11
11
  .map(([key]) => key);
12
- importRefs = utils_1.uniqueArray(importRefs);
12
+ importRefs = (0, utils_1.uniqueArray)(importRefs);
13
13
  const paths = {};
14
14
  importRefs.forEach((objectName) => {
15
- const importSection = helpers_1.getSectionFromObjectName(objectName);
16
- const interfaceName = helpers_1.getInterfaceName(objectName);
15
+ const importSection = (0, helpers_1.getSectionFromObjectName)(objectName);
16
+ const interfaceName = (0, helpers_1.getInterfaceName)(objectName);
17
17
  let path;
18
18
  if (type === types_1.ObjectType.Object) {
19
19
  if (section === importSection) {
@@ -32,8 +32,8 @@ function generateImportsBlock(refs, section, type) {
32
32
  paths[path].push(interfaceName);
33
33
  });
34
34
  const importLines = [];
35
- utils_1.sortArrayAlphabetically(Object.keys(paths)).forEach((path) => {
36
- const interfaces = utils_1.sortArrayAlphabetically(paths[path]).join(', ');
35
+ (0, utils_1.sortArrayAlphabetically)(Object.keys(paths)).forEach((path) => {
36
+ const interfaces = (0, utils_1.sortArrayAlphabetically)(paths[path]).join(', ');
37
37
  importLines.push(`import { ${interfaces} } from '${path}';`);
38
38
  });
39
39
  return importLines.join(constants_1.newLineChar);
@@ -17,11 +17,12 @@ const CommentCodeBlock_1 = require("./CommentCodeBlock");
17
17
  const log_1 = require("../log");
18
18
  const generator_1 = require("../generator");
19
19
  const typeString_1 = require("./typeString");
20
+ const mergeImports_1 = require("./utils/mergeImports");
20
21
  class APITypingsGenerator {
21
22
  constructor(options) {
22
23
  this.needEmit = options.needEmit;
23
24
  this.outDirPath = options.outDirPath;
24
- this.methodsPattern = helpers_1.prepareMethodsPattern(options.methodsPattern);
25
+ this.methodsPattern = (0, helpers_1.prepareMethodsPattern)(options.methodsPattern);
25
26
  this.methodsDefinitions = options.methodsDefinitions;
26
27
  this.methodsList = options.methodsDefinitions.methods || [];
27
28
  this.objects = this.convertJSONSchemaDictionary(options.objects);
@@ -78,10 +79,7 @@ class APITypingsGenerator {
78
79
  codeBlocks: [],
79
80
  };
80
81
  this.methodFilesMap[section] = {
81
- imports: {
82
- ...methodFile.imports,
83
- ...imports,
84
- },
82
+ imports: (0, mergeImports_1.mergeImports)(methodFile.imports, imports),
85
83
  codeBlocks: [...methodFile.codeBlocks, ...codeBlocks],
86
84
  };
87
85
  }
@@ -102,7 +100,7 @@ class APITypingsGenerator {
102
100
  allOfItem = tempAllOfItem;
103
101
  }
104
102
  else {
105
- log_1.consoleLogErrorAndExit(`${refName} ref not found`);
103
+ (0, log_1.consoleLogErrorAndExit)(`${refName} ref not found`);
106
104
  }
107
105
  }
108
106
  if (allOfItem.allOf) {
@@ -118,7 +116,7 @@ class APITypingsGenerator {
118
116
  let properties = object.properties || [];
119
117
  if (object.allOf) {
120
118
  this.collectAllOf(object).forEach((allOfItem) => {
121
- object.required = utils_1.uniqueArray([...object.required, ...allOfItem.required]);
119
+ object.required = (0, utils_1.uniqueArray)([...object.required, ...allOfItem.required]);
122
120
  let additionalProperties = [];
123
121
  if (allOfItem.properties) {
124
122
  additionalProperties = allOfItem.properties;
@@ -126,7 +124,7 @@ class APITypingsGenerator {
126
124
  else if (allOfItem.ref) {
127
125
  const refObject = this.getObjectByRef(allOfItem.ref);
128
126
  if (!refObject) {
129
- log_1.consoleLogErrorAndExit(`${object.name} ref object in allOf is not found`);
127
+ (0, log_1.consoleLogErrorAndExit)(`${object.name} ref object in allOf is not found`);
130
128
  return;
131
129
  }
132
130
  additionalProperties = this.getObjectProperties(refObject, deep + 1);
@@ -166,7 +164,7 @@ class APITypingsGenerator {
166
164
  const codeBlock = new TypeCodeBlock_1.TypeCodeBlock({
167
165
  type: TypeCodeBlock_1.TypeScriptCodeTypes.Interface,
168
166
  refName: object.name,
169
- interfaceName: helpers_1.getInterfaceName(object.name),
167
+ interfaceName: (0, helpers_1.getInterfaceName)(object.name),
170
168
  needExport: true,
171
169
  description: object.oneOf ? 'Object has oneOf' : '',
172
170
  properties: [],
@@ -175,16 +173,16 @@ class APITypingsGenerator {
175
173
  return this.getPrimitiveInterfaceCode(object);
176
174
  }
177
175
  properties.forEach((property) => {
178
- const { imports: newImports, value, codeBlocks: newCodeBlocks, description, } = typeString_1.generateTypeString(property, this.objects, {
176
+ const { imports: newImports, value, codeBlocks: newCodeBlocks, description, } = (0, typeString_1.generateTypeString)(property, this.objects, {
179
177
  objectParentName: object.name,
180
178
  });
181
- imports = { ...imports, ...newImports };
179
+ imports = (0, mergeImports_1.mergeImports)(imports, newImports);
182
180
  codeBlocks = [...codeBlocks, ...newCodeBlocks];
183
181
  codeBlock.addProperty({
184
182
  name: property.name,
185
183
  description: [property.description, description].join(constants_1.newLineChar),
186
184
  value,
187
- isRequired: helpers_1.isPatternProperty(property.name) || property.isRequired,
185
+ isRequired: (0, helpers_1.isPatternProperty)(property.name) || property.isRequired,
188
186
  });
189
187
  });
190
188
  return {
@@ -198,7 +196,7 @@ class APITypingsGenerator {
198
196
  return this.getObjectCodeBlockAsType(object);
199
197
  }
200
198
  if (object.enum) {
201
- const { codeBlocks } = enums_1.generateEnumAsUnionType(object);
199
+ const { codeBlocks } = (0, enums_1.generateEnumAsUnionType)(object);
202
200
  return {
203
201
  codeBlocks: codeBlocks,
204
202
  imports: {},
@@ -215,7 +213,7 @@ class APITypingsGenerator {
215
213
  }
216
214
  this.generatedObjects[object.name] = true;
217
215
  let result = false;
218
- if (object.ref) {
216
+ if (object.ref && object.type === 'object') {
219
217
  result = this.getPrimitiveInterfaceCode(object);
220
218
  }
221
219
  else {
@@ -232,39 +230,39 @@ class APITypingsGenerator {
232
230
  break;
233
231
  default:
234
232
  if (!result) {
235
- log_1.consoleLogErrorAndExit(helpers_1.getInterfaceName(object.name), 'Unknown type of object', object);
233
+ (0, log_1.consoleLogErrorAndExit)((0, helpers_1.getInterfaceName)(object.name), 'Unknown type of object', object);
236
234
  }
237
235
  }
238
236
  }
239
237
  if (!result) {
240
- log_1.consoleLogErrorAndExit('empty object result', object);
238
+ (0, log_1.consoleLogErrorAndExit)('empty object result', object);
241
239
  return;
242
240
  }
243
241
  const { codeBlocks, imports } = result;
244
242
  const stringCodeBlocks = codeBlocks.map((codeBlock) => codeBlock.toString());
245
- const section = helpers_1.getSectionFromObjectName(object.name);
243
+ const section = (0, helpers_1.getSectionFromObjectName)(object.name);
246
244
  delete imports[object.name];
247
- stringCodeBlocks.unshift(generator_1.generateImportsBlock(imports, section, types_1.ObjectType.Object));
245
+ stringCodeBlocks.unshift((0, generator_1.generateImportsBlock)(imports, section, types_1.ObjectType.Object));
248
246
  if (stringCodeBlocks.length > 0) {
249
247
  const code = stringCodeBlocks.join(constants_1.newLineChar.repeat(2));
250
- this.registerResultFile(path_1.default.join('objects', section, `${helpers_1.getInterfaceName(object.name)}.ts`), code);
248
+ this.registerResultFile(path_1.default.join('objects', section, `${(0, helpers_1.getInterfaceName)(object.name)}.ts`), code);
251
249
  }
252
250
  codeBlocks.forEach((codeBlock) => {
253
251
  if (codeBlock instanceof TypeCodeBlock_1.TypeCodeBlock && codeBlock.needExport && codeBlock.interfaceName) {
254
- this.registerExport(`./objects/${section}/${helpers_1.getInterfaceName(object.name)}.ts`, codeBlock.interfaceName);
252
+ this.registerExport(`./objects/${section}/${(0, helpers_1.getInterfaceName)(object.name)}.ts`, codeBlock.interfaceName);
255
253
  }
256
254
  });
257
255
  this.generateObjectsFromImports(imports);
258
256
  }
259
257
  getObjectByRef(ref) {
260
- const refName = helpers_1.getObjectNameByRef(ref);
258
+ const refName = (0, helpers_1.getObjectNameByRef)(ref);
261
259
  return this.objects[refName];
262
260
  }
263
261
  generateObjectsFromRefs(refs) {
264
262
  Object.keys(refs).forEach((ref) => {
265
263
  const refObject = this.getObjectByRef(ref);
266
264
  if (!refObject) {
267
- log_1.consoleLogInfo(`"${ref}" ref is not found`);
265
+ (0, log_1.consoleLogInfo)(`"${ref}" ref is not found`);
268
266
  return;
269
267
  }
270
268
  this.generateObject(refObject);
@@ -274,29 +272,29 @@ class APITypingsGenerator {
274
272
  Object.keys(imports).forEach((ref) => {
275
273
  const refObject = this.getObjectByRef(ref);
276
274
  if (!refObject) {
277
- log_1.consoleLogInfo(`"${ref}" ref is not found`);
275
+ (0, log_1.consoleLogInfo)(`"${ref}" ref is not found`);
278
276
  return;
279
277
  }
280
278
  this.generateObject(refObject);
281
279
  });
282
280
  }
283
281
  generateMethodParams(methodInfo) {
284
- const section = helpers_1.getMethodSection(methodInfo.name);
282
+ const section = (0, helpers_1.getMethodSection)(methodInfo.name);
285
283
  const interfaceName = `${methodInfo.name} params`;
286
284
  let imports = {};
287
285
  let codeBlocks = [];
288
286
  const codeBlock = new TypeCodeBlock_1.TypeCodeBlock({
289
287
  type: TypeCodeBlock_1.TypeScriptCodeTypes.Interface,
290
- interfaceName: helpers_1.getInterfaceName(interfaceName),
288
+ interfaceName: (0, helpers_1.getInterfaceName)(interfaceName),
291
289
  needExport: true,
292
290
  allowEmptyInterface: true,
293
291
  properties: [],
294
292
  });
295
293
  methodInfo.parameters.forEach((property) => {
296
- const { imports: newImports, value, codeBlocks: newCodeBlocks, } = typeString_1.generateTypeString(property, this.objects, {
294
+ const { imports: newImports, value, codeBlocks: newCodeBlocks, } = (0, typeString_1.generateTypeString)(property, this.objects, {
297
295
  needEnumNamesConstant: false,
298
296
  });
299
- imports = { ...imports, ...newImports };
297
+ imports = (0, mergeImports_1.mergeImports)(imports, newImports);
300
298
  codeBlocks = [...codeBlocks, ...newCodeBlocks];
301
299
  codeBlock.addProperty({
302
300
  name: property.name,
@@ -309,7 +307,7 @@ class APITypingsGenerator {
309
307
  this.generateObjectsFromImports(imports);
310
308
  }
311
309
  getResponseObjectRef(ref) {
312
- const objectName = helpers_1.getObjectNameByRef(ref);
310
+ const objectName = (0, helpers_1.getObjectNameByRef)(ref);
313
311
  if (this.responses[objectName]) {
314
312
  return this.responses[objectName];
315
313
  }
@@ -319,15 +317,15 @@ class APITypingsGenerator {
319
317
  let codeBlocks = [];
320
318
  let imports = {};
321
319
  if (object.enum) {
322
- const { codeBlocks: newCodeBlocks } = enums_1.generateEnumAsUnionType(object);
320
+ const { codeBlocks: newCodeBlocks } = (0, enums_1.generateEnumAsUnionType)(object);
323
321
  codeBlocks = [...newCodeBlocks];
324
322
  }
325
323
  else {
326
- const { imports: newImports, value, codeBlocks: newCodeBlocks, } = typeString_1.generateTypeString(object, this.objects);
324
+ const { imports: newImports, value, codeBlocks: newCodeBlocks, } = (0, typeString_1.generateTypeString)(object, this.objects);
327
325
  const codeBlock = new TypeCodeBlock_1.TypeCodeBlock({
328
326
  type: TypeCodeBlock_1.TypeScriptCodeTypes.Type,
329
327
  refName: object.name,
330
- interfaceName: helpers_1.getInterfaceName(object.name),
328
+ interfaceName: (0, helpers_1.getInterfaceName)(object.name),
331
329
  description: object.description,
332
330
  needExport: true,
333
331
  properties: [],
@@ -343,13 +341,13 @@ class APITypingsGenerator {
343
341
  };
344
342
  }
345
343
  getResponseCodeBlockAsType(object, response) {
346
- const { imports, value, codeBlocks, description } = typeString_1.generateTypeString(response, this.objects, {
344
+ const { imports, value, codeBlocks, description } = (0, typeString_1.generateTypeString)(response, this.objects, {
347
345
  objectParentName: ' ', // TODO: Refactor
348
346
  });
349
347
  const codeBlock = new TypeCodeBlock_1.TypeCodeBlock({
350
348
  type: TypeCodeBlock_1.TypeScriptCodeTypes.Type,
351
349
  refName: object.name,
352
- interfaceName: helpers_1.getInterfaceName(object.name),
350
+ interfaceName: (0, helpers_1.getInterfaceName)(object.name),
353
351
  description: [object.description, description || ''].join(constants_1.newLineChar),
354
352
  needExport: true,
355
353
  properties: [],
@@ -364,7 +362,7 @@ class APITypingsGenerator {
364
362
  }
365
363
  getResponseCodeBlock(object) {
366
364
  if (!object.ref) {
367
- log_1.consoleLogError(`response schema object "${object.name}" has no ref`, object);
365
+ (0, log_1.consoleLogError)(`response schema object "${object.name}" has no ref`, object);
368
366
  return false;
369
367
  }
370
368
  const nonBuildableRefs = {
@@ -372,13 +370,13 @@ class APITypingsGenerator {
372
370
  [constants_1.baseOkResponseRef]: true,
373
371
  [constants_1.basePropertyExistsRef]: true,
374
372
  };
375
- const objectName = helpers_1.getObjectNameByRef(object.ref);
373
+ const objectName = (0, helpers_1.getObjectNameByRef)(object.ref);
376
374
  if (nonBuildableRefs[objectName]) {
377
375
  return this.getObjectCodeBlockAsType(object);
378
376
  }
379
377
  let response = this.getResponseObjectRef(object.ref);
380
378
  if (!response) {
381
- log_1.consoleLogError(`response schema object "${object.name}" has no response`, object);
379
+ (0, log_1.consoleLogError)(`response schema object "${object.name}" has no response`, object);
382
380
  return false;
383
381
  }
384
382
  // VK API JSON Schema specific heuristic
@@ -399,7 +397,7 @@ class APITypingsGenerator {
399
397
  case 'array':
400
398
  return this.getResponseCodeBlockAsType(object, response);
401
399
  default:
402
- log_1.consoleLogErrorAndExit(response.name, 'unknown type', response.type);
400
+ (0, log_1.consoleLogErrorAndExit)(response.name, 'unknown type', response.type);
403
401
  return false;
404
402
  }
405
403
  }
@@ -414,13 +412,13 @@ class APITypingsGenerator {
414
412
  }
415
413
  generateMethodParamsAndResponses(method) {
416
414
  const { name: methodName } = method;
417
- const section = helpers_1.getMethodSection(methodName);
418
- if (!utils_1.isObject(method.responses)) {
419
- log_1.consoleLogErrorAndExit(`"${methodName}" "responses" field is not an object.`);
415
+ const section = (0, helpers_1.getMethodSection)(methodName);
416
+ if (!(0, utils_1.isObject)(method.responses)) {
417
+ (0, log_1.consoleLogErrorAndExit)(`"${methodName}" "responses" field is not an object.`);
420
418
  return;
421
419
  }
422
420
  if (Object.keys(method.responses).length === 0) {
423
- log_1.consoleLogErrorAndExit(`"${methodName}" "responses" field is empty.`);
421
+ (0, log_1.consoleLogErrorAndExit)(`"${methodName}" "responses" field is empty.`);
424
422
  return;
425
423
  }
426
424
  // Comment with method name for visual sections in file
@@ -429,7 +427,7 @@ class APITypingsGenerator {
429
427
  methodNameComment.appendLines(['', method.description]);
430
428
  }
431
429
  this.appendToFileMap(section, {}, [methodNameComment]);
432
- const { method: normalizedMethod, parameterRefs } = methods_1.normalizeMethodInfo(method);
430
+ const { method: normalizedMethod, parameterRefs } = (0, methods_1.normalizeMethodInfo)(method);
433
431
  method = normalizedMethod;
434
432
  this.generateObjectsFromRefs(parameterRefs);
435
433
  this.generateMethodParams(new SchemaObject_1.SchemaObject(method.name, method));
@@ -442,9 +440,9 @@ class APITypingsGenerator {
442
440
  });
443
441
  }
444
442
  generateMethods() {
445
- log_1.consoleLogInfo('creating method params and responses...');
443
+ (0, log_1.consoleLogInfo)('creating method params and responses...');
446
444
  this.methodsList.forEach((methodInfo) => {
447
- if (helpers_1.isMethodNeeded(this.methodsPattern, methodInfo.name)) {
445
+ if ((0, helpers_1.isMethodNeeded)(this.methodsPattern, methodInfo.name)) {
448
446
  this.generateMethodParamsAndResponses(methodInfo);
449
447
  }
450
448
  });
@@ -455,12 +453,12 @@ class APITypingsGenerator {
455
453
  this.registerExport(`./methods/${section}`, codeBlock.interfaceName);
456
454
  }
457
455
  });
458
- const code = [generator_1.generateImportsBlock(imports, null), ...codeBlocks];
456
+ const code = [(0, generator_1.generateImportsBlock)(imports, null), ...codeBlocks];
459
457
  this.registerResultFile(path_1.default.join('methods', `${section}.ts`), code.join(constants_1.newLineChar.repeat(2)));
460
458
  });
461
459
  }
462
460
  generateErrors() {
463
- log_1.consoleLogInfo('creating errors...');
461
+ (0, log_1.consoleLogInfo)('creating errors...');
464
462
  const code = [];
465
463
  Object.entries(this.errors)
466
464
  .reduce((acc, [name, error]) => {
@@ -485,14 +483,14 @@ class APITypingsGenerator {
485
483
  this.registerResultFile(path_1.default.join('common', 'errors.ts'), code.join(constants_1.newLineChar.repeat(2)));
486
484
  }
487
485
  createCommonTypes() {
488
- log_1.consoleLogInfo('creating common types...');
486
+ (0, log_1.consoleLogInfo)('creating common types...');
489
487
  const code = [];
490
488
  const apiVersion = this.methodsDefinitions.version || constants_1.DEFAULT_API_VERSION;
491
489
  code.push(`export const API_VERSION = '${apiVersion}'`);
492
490
  code.push('export type ValueOf<T> = T[keyof T];');
493
491
  code.push(new TypeCodeBlock_1.TypeCodeBlock({
494
492
  type: TypeCodeBlock_1.TypeScriptCodeTypes.Interface,
495
- interfaceName: helpers_1.getInterfaceName(constants_1.baseAPIParamsInterfaceName),
493
+ interfaceName: (0, helpers_1.getInterfaceName)(constants_1.baseAPIParamsInterfaceName),
496
494
  needExport: true,
497
495
  properties: [
498
496
  {
@@ -516,24 +514,24 @@ class APITypingsGenerator {
516
514
  ],
517
515
  }).toString());
518
516
  this.registerExport('./common/common', 'API_VERSION');
519
- this.registerExport('./common/common', helpers_1.getInterfaceName(constants_1.baseAPIParamsInterfaceName));
517
+ this.registerExport('./common/common', (0, helpers_1.getInterfaceName)(constants_1.baseAPIParamsInterfaceName));
520
518
  this.registerResultFile(path_1.default.join('common', 'common.ts'), code.join(constants_1.newLineChar.repeat(2)));
521
519
  }
522
520
  /**
523
521
  * This method creates index.ts file with exports of all generated params, responses and objects
524
522
  */
525
523
  createIndexExports() {
526
- log_1.consoleLogInfo('creating index.ts exports...');
524
+ (0, log_1.consoleLogInfo)('creating index.ts exports...');
527
525
  const blocks = [];
528
526
  let exportedObjects = {};
529
- utils_1.sortArrayAlphabetically(Object.keys(this.exports)).forEach((path) => {
527
+ (0, utils_1.sortArrayAlphabetically)(Object.keys(this.exports)).forEach((path) => {
530
528
  const objects = Object.keys(this.exports[path]);
531
529
  if (!objects.length) {
532
530
  return;
533
531
  }
534
532
  const blockLines = [];
535
533
  blockLines.push('export {');
536
- utils_1.sortArrayAlphabetically(objects).forEach((object) => {
534
+ (0, utils_1.sortArrayAlphabetically)(objects).forEach((object) => {
537
535
  if (exportedObjects[object]) {
538
536
  return;
539
537
  }
@@ -544,21 +542,21 @@ class APITypingsGenerator {
544
542
  blocks.push(blockLines.join(constants_1.newLineChar));
545
543
  });
546
544
  this.registerResultFile('index.ts', blocks.join(constants_1.newLineChar.repeat(2)));
547
- log_1.consoleLogInfo(`${Object.keys(exportedObjects).length} objects successfully generated`);
545
+ (0, log_1.consoleLogInfo)(`${Object.keys(exportedObjects).length} objects successfully generated`);
548
546
  }
549
547
  generate() {
550
- log_1.consoleLogInfo('generate');
548
+ (0, log_1.consoleLogInfo)('generate');
551
549
  this.generateMethods();
552
550
  this.generateErrors();
553
551
  if (this.needEmit) {
554
552
  this.createCommonTypes();
555
553
  this.createIndexExports();
556
- log_1.consoleLogInfo('prepare out directory');
557
- helpers_1.prepareBuildDirectory(this.outDirPath);
558
- log_1.consoleLogInfo('write files');
554
+ (0, log_1.consoleLogInfo)('prepare out directory');
555
+ (0, helpers_1.prepareBuildDirectory)(this.outDirPath);
556
+ (0, log_1.consoleLogInfo)('write files');
559
557
  Object.keys(this.resultFiles).forEach((filePath) => {
560
558
  const fileContent = this.resultFiles[filePath];
561
- helpers_1.writeFile(path_1.default.join(this.outDirPath, filePath), fileContent);
559
+ (0, helpers_1.writeFile)(path_1.default.join(this.outDirPath, filePath), fileContent);
562
560
  });
563
561
  }
564
562
  }
@@ -6,8 +6,8 @@ const helpers_1 = require("../helpers");
6
6
  const log_1 = require("../log");
7
7
  class SchemaObject {
8
8
  constructor(name, object, parentName) {
9
- if (!utils_1.isObject(object)) {
10
- log_1.consoleLogErrorAndExit(`[SchemaObject] "${name}" is not an object.`, {
9
+ if (!(0, utils_1.isObject)(object)) {
10
+ (0, log_1.consoleLogErrorAndExit)(`[SchemaObject] "${name}" is not an object.`, {
11
11
  name,
12
12
  object,
13
13
  parentName,
@@ -18,16 +18,16 @@ class SchemaObject {
18
18
  if (parentName) {
19
19
  this.parentObjectName = parentName;
20
20
  }
21
- if (utils_1.isString(object.type)) {
21
+ if ((0, utils_1.isString)(object.type)) {
22
22
  this.type = object.type;
23
23
  }
24
24
  else if (Array.isArray(object.type)) {
25
25
  this.type = object.type;
26
26
  }
27
- if (utils_1.isString(object.description)) {
27
+ if ((0, utils_1.isString)(object.description)) {
28
28
  this.description = object.description;
29
29
  }
30
- if (utils_1.isString(object.$ref)) {
30
+ if ((0, utils_1.isString)(object.$ref)) {
31
31
  this.ref = object.$ref;
32
32
  }
33
33
  if (Array.isArray(object.enum)) {
@@ -59,10 +59,10 @@ class SchemaObject {
59
59
  }
60
60
  if (object.patternProperties) {
61
61
  Object.entries(object.patternProperties).forEach(([propertyName, property]) => {
62
- this.properties.push(new SchemaObject(helpers_1.transformPatternPropertyName(propertyName), property, name));
62
+ this.properties.push(new SchemaObject((0, helpers_1.transformPatternPropertyName)(propertyName), property, name));
63
63
  });
64
64
  }
65
- if (utils_1.isObject(object.items)) {
65
+ if ((0, utils_1.isObject)(object.items)) {
66
66
  this.items = new SchemaObject(name + '_items', object.items, this.name);
67
67
  }
68
68
  if (Array.isArray(object.oneOf) && object.oneOf.length > 0) {
@@ -13,7 +13,7 @@ var TypeScriptCodeTypes;
13
13
  TypeScriptCodeTypes["ConstantObject"] = "constant_object";
14
14
  TypeScriptCodeTypes["Type"] = "type";
15
15
  TypeScriptCodeTypes["Const"] = "const";
16
- })(TypeScriptCodeTypes = exports.TypeScriptCodeTypes || (exports.TypeScriptCodeTypes = {}));
16
+ })(TypeScriptCodeTypes || (exports.TypeScriptCodeTypes = TypeScriptCodeTypes = {}));
17
17
  class TypeCodeBlock extends BaseCodeBlock_1.BaseCodeBlock {
18
18
  constructor(options) {
19
19
  super();
@@ -40,7 +40,7 @@ class TypeCodeBlock extends BaseCodeBlock_1.BaseCodeBlock {
40
40
  this.properties.push(property);
41
41
  }
42
42
  getPropertiesCode() {
43
- const quoteChar = this.properties.some((property) => helpers_1.areQuotesNeededForProperty(property.name))
43
+ const quoteChar = this.properties.some((property) => (0, helpers_1.areQuotesNeededForProperty)(property.name))
44
44
  ? "'"
45
45
  : '';
46
46
  return this.properties
@@ -61,12 +61,12 @@ class TypeCodeBlock extends BaseCodeBlock_1.BaseCodeBlock {
61
61
  lineEnd = ',';
62
62
  break;
63
63
  }
64
- let value = property.wrapValue ? utils_1.quoteJavaScriptValue(property.value) : property.value;
64
+ let value = property.wrapValue ? (0, utils_1.quoteJavaScriptValue)(property.value) : property.value;
65
65
  let propertyCode = [
66
66
  ` ${quoteChar}${property.name}${quoteChar}${divider} ${value}${lineEnd}`,
67
67
  ];
68
68
  if (property.description) {
69
- const commentLines = helpers_1.joinCommentLines(2, property.description);
69
+ const commentLines = (0, helpers_1.joinCommentLines)(2, property.description);
70
70
  if (commentLines.length) {
71
71
  propertyCode.unshift(commentLines.join(constants_1.newLineChar));
72
72
  }
@@ -85,7 +85,7 @@ class TypeCodeBlock extends BaseCodeBlock_1.BaseCodeBlock {
85
85
  before.push(`// ${this.refName}`);
86
86
  }
87
87
  if (this.description) {
88
- before = [...before, ...helpers_1.joinCommentLines(0, this.description)];
88
+ before = [...before, ...(0, helpers_1.joinCommentLines)(0, this.description)];
89
89
  }
90
90
  switch (this.type) {
91
91
  case TypeScriptCodeTypes.Interface: {
@@ -101,7 +101,7 @@ class TypeCodeBlock extends BaseCodeBlock_1.BaseCodeBlock {
101
101
  ? this.extendsInterfaces.join(', ')
102
102
  : '';
103
103
  code = [
104
- utils_1.trimStringDoubleSpaces(`${exportKeyword} interface ${this.interfaceName} ${extendsInterfaces} {`),
104
+ (0, utils_1.trimStringDoubleSpaces)(`${exportKeyword} interface ${this.interfaceName} ${extendsInterfaces} {`),
105
105
  propertiesCode,
106
106
  '}',
107
107
  ].join(propertiesCode.length ? constants_1.newLineChar : '');
@@ -109,32 +109,32 @@ class TypeCodeBlock extends BaseCodeBlock_1.BaseCodeBlock {
109
109
  }
110
110
  case TypeScriptCodeTypes.Enum:
111
111
  code = [
112
- utils_1.trimStringDoubleSpaces(`${exportKeyword} enum ${this.interfaceName} {`),
112
+ (0, utils_1.trimStringDoubleSpaces)(`${exportKeyword} enum ${this.interfaceName} {`),
113
113
  propertiesCode,
114
114
  '}',
115
115
  ].join(constants_1.newLineChar);
116
116
  break;
117
117
  case TypeScriptCodeTypes.ConstantObject:
118
118
  code = [
119
- utils_1.trimStringDoubleSpaces(`${exportKeyword} const ${this.interfaceName} = {`),
119
+ (0, utils_1.trimStringDoubleSpaces)(`${exportKeyword} const ${this.interfaceName} = {`),
120
120
  propertiesCode,
121
121
  '} as const;',
122
122
  ].join(constants_1.newLineChar);
123
123
  break;
124
124
  case TypeScriptCodeTypes.Type:
125
125
  if (!this.value) {
126
- log_1.consoleLogErrorAndExit(`"${this.interfaceName}" type has empty value`);
126
+ (0, log_1.consoleLogErrorAndExit)(`"${this.interfaceName}" type has empty value`);
127
127
  }
128
128
  code = [
129
- utils_1.trimStringDoubleSpaces(`${exportKeyword} type ${this.interfaceName} = ${this.value};`),
129
+ (0, utils_1.trimStringDoubleSpaces)(`${exportKeyword} type ${this.interfaceName} = ${this.value};`),
130
130
  ].join(constants_1.newLineChar);
131
131
  break;
132
132
  case TypeScriptCodeTypes.Const:
133
133
  if (!this.value) {
134
- log_1.consoleLogErrorAndExit(`"${this.interfaceName}" type has empty value`);
134
+ (0, log_1.consoleLogErrorAndExit)(`"${this.interfaceName}" type has empty value`);
135
135
  }
136
136
  code = [
137
- utils_1.trimStringDoubleSpaces(`${exportKeyword} const ${this.interfaceName} = ${this.value};`),
137
+ (0, utils_1.trimStringDoubleSpaces)(`${exportKeyword} const ${this.interfaceName} = ${this.value};`),
138
138
  ].join(constants_1.newLineChar);
139
139
  break;
140
140
  }
@@ -17,7 +17,7 @@ function getEnumNamesIdentifier(name) {
17
17
  }
18
18
  exports.getEnumNamesIdentifier = getEnumNamesIdentifier;
19
19
  function generateEnumConstantObject(object, objectName, enumNames) {
20
- const enumInterfaceName = helpers_1.getInterfaceName(objectName);
20
+ const enumInterfaceName = (0, helpers_1.getInterfaceName)(objectName);
21
21
  const codeBlock = new TypeCodeBlock_1.TypeCodeBlock({
22
22
  type: TypeCodeBlock_1.TypeScriptCodeTypes.ConstantObject,
23
23
  refName: objectName,
@@ -27,7 +27,7 @@ function generateEnumConstantObject(object, objectName, enumNames) {
27
27
  });
28
28
  enumNames.forEach((name, index) => {
29
29
  codeBlock.addProperty({
30
- name: helpers_1.getEnumPropertyName(name.toString()),
30
+ name: (0, helpers_1.getEnumPropertyName)(name.toString()),
31
31
  value: object.enum[index],
32
32
  wrapValue: true,
33
33
  });
@@ -45,7 +45,7 @@ function generateEnumAsUnionType(object) {
45
45
  const unionType = new TypeCodeBlock_1.TypeCodeBlock({
46
46
  type: TypeCodeBlock_1.TypeScriptCodeTypes.Type,
47
47
  refName: object.name,
48
- interfaceName: helpers_1.getInterfaceName(object.name),
48
+ interfaceName: (0, helpers_1.getInterfaceName)(object.name),
49
49
  description: [object.description, description].join(constants_1.newLineChar),
50
50
  needExport: true,
51
51
  properties: [],
@@ -88,7 +88,7 @@ function generateInlineEnum(object, options = {}) {
88
88
  if (isNumericEnum && options.refName) {
89
89
  descriptionLines.push('');
90
90
  descriptionLines.push('@note This enum have auto-generated constant with keys and values');
91
- descriptionLines.push(`@see ${helpers_1.getInterfaceName(options.refName)}`);
91
+ descriptionLines.push(`@see ${(0, helpers_1.getInterfaceName)(options.refName)}`);
92
92
  }
93
93
  descriptionLines.push('');
94
94
  enumNames.forEach((name, index) => {
@@ -104,11 +104,11 @@ function generateInlineEnum(object, options = {}) {
104
104
  codeBlocks.push(codeBlock);
105
105
  }
106
106
  }
107
- const values = object.enum.map((value) => utils_1.quoteJavaScriptValue(value));
107
+ const values = object.enum.map((value) => (0, utils_1.quoteJavaScriptValue)(value));
108
108
  return {
109
109
  codeBlocks,
110
110
  imports: {},
111
- value: helpers_1.joinOneOfValues(values, true),
111
+ value: (0, helpers_1.joinOneOfValues)(values, true),
112
112
  description: descriptionLines.join(constants_1.newLineChar),
113
113
  };
114
114
  }
@@ -30,7 +30,7 @@ function normalizeMethodInfo(method) {
30
30
  parameterRefs[ref] = types_1.RefsDictionaryType.Generate;
31
31
  parameter.description +=
32
32
  constants_1.newLineChar.repeat(2) +
33
- [`@see ${helpers_1.getInterfaceName(helpers_1.getObjectNameByRef(ref))} (${ref})`].join(constants_1.newLineChar);
33
+ [`@see ${(0, helpers_1.getInterfaceName)((0, helpers_1.getObjectNameByRef)(ref))} (${ref})`].join(constants_1.newLineChar);
34
34
  }
35
35
  });
36
36
  return {
@@ -7,13 +7,14 @@ const helpers_1 = require("../helpers");
7
7
  const log_1 = require("../log");
8
8
  const types_1 = require("../types");
9
9
  const utils_1 = require("../utils");
10
+ const mergeImports_1 = require("./utils/mergeImports");
10
11
  function generateBaseType(object, options) {
11
12
  let codeBlocks = [];
12
13
  let typeString = 'any /* default type */';
13
14
  let imports = {};
14
15
  let description = '';
15
16
  if (object.enum) {
16
- const { value, codeBlocks: newCodeBlocks, description: newDescription, } = enums_1.generateInlineEnum(object, {
17
+ const { value, codeBlocks: newCodeBlocks, description: newDescription, } = (0, enums_1.generateInlineEnum)(object, {
17
18
  // TODO: Refactor
18
19
  // section_object_name -> property_name -> items => section_object_name_property_name_items enumNames
19
20
  objectParentName: options.objectParentName || object.parentObjectName,
@@ -23,15 +24,15 @@ function generateBaseType(object, options) {
23
24
  codeBlocks = newCodeBlocks;
24
25
  description = newDescription;
25
26
  }
26
- else if (utils_1.isString(object.type)) {
27
+ else if ((0, utils_1.isString)(object.type)) {
27
28
  const primitive = constants_1.primitiveTypes[object.type];
28
29
  if (!primitive) {
29
- log_1.consoleLogErrorAndExit(object.name, `Error, type "${object.type}" is not declared type`);
30
+ (0, log_1.consoleLogErrorAndExit)(object.name, `Error, type "${object.type}" is not declared type`);
30
31
  }
31
32
  typeString = primitive;
32
33
  }
33
34
  else if (Array.isArray(object.type)) {
34
- const primitivesTypesArray = helpers_1.resolvePrimitiveTypesArray(object.type);
35
+ const primitivesTypesArray = (0, helpers_1.resolvePrimitiveTypesArray)(object.type);
35
36
  if (primitivesTypesArray !== null) {
36
37
  typeString = primitivesTypesArray;
37
38
  }
@@ -55,10 +56,10 @@ function generateTypeString(object, objects, options = {}) {
55
56
  if (object.oneOf) {
56
57
  const values = object.oneOf.map((oneOfObject) => {
57
58
  const { value, imports: newImports } = generateTypeString(oneOfObject, objects);
58
- imports = { ...imports, ...newImports };
59
+ imports = (0, mergeImports_1.mergeImports)(imports, newImports);
59
60
  return value;
60
61
  });
61
- typeString = helpers_1.joinOneOfValues(values);
62
+ typeString = (0, helpers_1.joinOneOfValues)(values);
62
63
  }
63
64
  else if (object.type === constants_1.PropertyType.ARRAY && object.items) {
64
65
  let depth = 1;
@@ -74,13 +75,13 @@ function generateTypeString(object, objects, options = {}) {
74
75
  }
75
76
  }
76
77
  if (items.ref) {
77
- const refName = helpers_1.getObjectNameByRef(items.ref);
78
+ const refName = (0, helpers_1.getObjectNameByRef)(items.ref);
78
79
  const refObject = objects[refName];
79
80
  if (!refObject) {
80
- log_1.consoleLogErrorAndExit(`Error, object for "${refName}" ref is not found.`);
81
+ (0, log_1.consoleLogErrorAndExit)(`Error, object for "${refName}" ref is not found.`);
81
82
  }
82
83
  imports[refName] = types_1.RefsDictionaryType.GenerateAndImport;
83
- typeString = helpers_1.formatArrayDepth(helpers_1.getInterfaceName(refName), depth);
84
+ typeString = (0, helpers_1.formatArrayDepth)((0, helpers_1.getInterfaceName)(refName), depth);
84
85
  }
85
86
  else {
86
87
  const { value, description: newDescription, imports: newImports, codeBlocks: newCodeBlocks, } = generateBaseType(items, {
@@ -88,14 +89,14 @@ function generateTypeString(object, objects, options = {}) {
88
89
  // TODO: Refactor
89
90
  objectParentName: object.parentObjectName,
90
91
  });
91
- typeString = helpers_1.formatArrayDepth(value, depth);
92
+ typeString = (0, helpers_1.formatArrayDepth)(value, depth);
92
93
  description = newDescription;
93
- imports = { ...imports, ...newImports };
94
+ imports = (0, mergeImports_1.mergeImports)(imports, newImports);
94
95
  codeBlocks = [...codeBlocks, ...newCodeBlocks];
95
96
  }
96
97
  }
97
98
  else if (object.ref) {
98
- const refName = helpers_1.getObjectNameByRef(object.ref);
99
+ const refName = (0, helpers_1.getObjectNameByRef)(object.ref);
99
100
  switch (refName) {
100
101
  case constants_1.baseOkResponseRef:
101
102
  case constants_1.basePropertyExistsRef:
@@ -107,26 +108,30 @@ function generateTypeString(object, objects, options = {}) {
107
108
  default: {
108
109
  const refObject = objects[refName];
109
110
  if (!refObject) {
110
- log_1.consoleLogErrorAndExit(`Error, object for "${refName}" ref is not found.`);
111
+ (0, log_1.consoleLogErrorAndExit)(`Error, object for "${refName}" ref is not found.`);
111
112
  }
112
113
  if (refObject.enum) {
113
114
  imports[refName] = types_1.RefsDictionaryType.GenerateAndImport;
114
- typeString = helpers_1.getInterfaceName(refName);
115
+ typeString = (0, helpers_1.getInterfaceName)(refName);
115
116
  }
116
117
  else if (refObject.oneOf) {
117
118
  const values = refObject.oneOf.map((oneOfObject) => {
118
119
  const { value, imports: newImports } = generateTypeString(oneOfObject, objects);
119
- imports = { ...imports, ...newImports };
120
+ imports = (0, mergeImports_1.mergeImports)(imports, newImports);
120
121
  return value;
121
122
  });
122
- typeString = helpers_1.joinOneOfValues(values);
123
+ typeString = (0, helpers_1.joinOneOfValues)(values);
123
124
  }
124
- else if (utils_1.isString(refObject.type) && constants_1.scalarTypes[refObject.type] && !refObject.ref) {
125
+ else if ((0, utils_1.isString)(refObject.type) && constants_1.scalarTypes[refObject.type] && !refObject.ref) {
125
126
  typeString = constants_1.scalarTypes[refObject.type];
126
127
  }
128
+ else if (object.type === constants_1.PropertyType.STRING) {
129
+ imports[refName] = types_1.RefsDictionaryType.Generate;
130
+ typeString = constants_1.scalarTypes.string;
131
+ }
127
132
  else {
128
133
  imports[refName] = types_1.RefsDictionaryType.GenerateAndImport;
129
- typeString = helpers_1.getInterfaceName(refName);
134
+ typeString = (0, helpers_1.getInterfaceName)(refName);
130
135
  }
131
136
  }
132
137
  }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mergeImports = void 0;
4
+ const types_1 = require("../../types");
5
+ function mergeImports(oldImports, newImports) {
6
+ const result = { ...oldImports };
7
+ Object.entries(newImports).forEach(([name, newImportValue]) => {
8
+ const oldImportValue = oldImports[name];
9
+ if (oldImportValue === types_1.RefsDictionaryType.GenerateAndImport) {
10
+ return;
11
+ }
12
+ result[name] = newImportValue;
13
+ });
14
+ return result;
15
+ }
16
+ exports.mergeImports = mergeImports;
package/dist/helpers.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -81,7 +85,7 @@ function writeFile(filePath, code, insertAutoGeneratedNote = true) {
81
85
  exports.writeFile = writeFile;
82
86
  function prepareMethodsPattern(methodsPattern) {
83
87
  if (!methodsPattern) {
84
- log_1.consoleLogErrorAndExit('methodsPattern is empty. Pass "*" to generate all methods');
88
+ (0, log_1.consoleLogErrorAndExit)('methodsPattern is empty. Pass "*" to generate all methods');
85
89
  }
86
90
  return methodsPattern
87
91
  .replace(/\s+/g, '')
@@ -114,9 +118,9 @@ function getInterfaceName(name) {
114
118
  name = name
115
119
  .replace(/\.|(\s+)|_/g, ' ')
116
120
  .split(' ')
117
- .map((v) => utils_1.capitalizeFirstLetter(v))
121
+ .map((v) => (0, utils_1.capitalizeFirstLetter)(v))
118
122
  .join('');
119
- return utils_1.capitalizeFirstLetter(name);
123
+ return (0, utils_1.capitalizeFirstLetter)(name);
120
124
  }
121
125
  exports.getInterfaceName = getInterfaceName;
122
126
  function getEnumPropertyName(name) {
@@ -160,14 +164,14 @@ function joinCommentLines(indent = 2, ...description) {
160
164
  if (typeof entry === 'string') {
161
165
  descriptionLines = [
162
166
  ...descriptionLines,
163
- ...utils_1.trimArray((entry || '').trim().split(constants_1.newLineChar)),
167
+ ...(0, utils_1.trimArray)((entry || '').trim().split(constants_1.newLineChar)),
164
168
  ];
165
169
  }
166
170
  else if (Array.isArray(entry)) {
167
171
  descriptionLines = [...descriptionLines, ...entry];
168
172
  }
169
173
  });
170
- descriptionLines = utils_1.trimArray(descriptionLines);
174
+ descriptionLines = (0, utils_1.trimArray)(descriptionLines);
171
175
  if (!descriptionLines.length) {
172
176
  return [];
173
177
  }
@@ -2,11 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const helpers_1 = require("./helpers");
4
4
  test('areQuotesNeededForProperty', () => {
5
- expect(helpers_1.areQuotesNeededForProperty('user_id')).toBe(false);
6
- expect(helpers_1.areQuotesNeededForProperty('uuid4')).toBe(false);
7
- expect(helpers_1.areQuotesNeededForProperty('_foo')).toBe(false);
8
- expect(helpers_1.areQuotesNeededForProperty('4uuid')).toBe(true);
9
- expect(helpers_1.areQuotesNeededForProperty('user-id')).toBe(true);
10
- expect(helpers_1.areQuotesNeededForProperty('user&id')).toBe(true);
11
- expect(helpers_1.areQuotesNeededForProperty('идентификатор')).toBe(true);
5
+ expect((0, helpers_1.areQuotesNeededForProperty)('user_id')).toBe(false);
6
+ expect((0, helpers_1.areQuotesNeededForProperty)('uuid4')).toBe(false);
7
+ expect((0, helpers_1.areQuotesNeededForProperty)('_foo')).toBe(false);
8
+ expect((0, helpers_1.areQuotesNeededForProperty)('4uuid')).toBe(true);
9
+ expect((0, helpers_1.areQuotesNeededForProperty)('user-id')).toBe(true);
10
+ expect((0, helpers_1.areQuotesNeededForProperty)('user&id')).toBe(true);
11
+ expect((0, helpers_1.areQuotesNeededForProperty)('идентификатор')).toBe(true);
12
12
  });
package/dist/index.js CHANGED
@@ -30,7 +30,7 @@ const helpMessage = `
30
30
  async function main() {
31
31
  console.log(chalk_1.default.bold('VK API Schema TypeScript generator'));
32
32
  const startTime = perf_hooks_1.performance.now();
33
- const args = cli_1.parseArguments();
33
+ const args = (0, cli_1.parseArguments)();
34
34
  let { help, schemaDir, outDir, methods } = args;
35
35
  if (help) {
36
36
  console.log(helpMessage);
@@ -38,36 +38,36 @@ async function main() {
38
38
  }
39
39
  const helpHint = `Use ${chalk_1.default.greenBright('--help')} to see all options.`;
40
40
  if (!schemaDir) {
41
- log_1.consoleLogErrorAndExit(`You should specify ${chalk_1.default.greenBright('schemaDir')}. ${helpHint}`);
41
+ (0, log_1.consoleLogErrorAndExit)(`You should specify ${chalk_1.default.greenBright('schemaDir')}. ${helpHint}`);
42
42
  return;
43
43
  }
44
44
  if (!outDir) {
45
- log_1.consoleLogInfo(`${chalk_1.default.greenBright('outDir')} option is empty. ${helpHint}`);
46
- log_1.consoleLogInfo('Script will work in linter mode without emitting files to file system.');
45
+ (0, log_1.consoleLogInfo)(`${chalk_1.default.greenBright('outDir')} option is empty. ${helpHint}`);
46
+ (0, log_1.consoleLogInfo)('Script will work in linter mode without emitting files to file system.');
47
47
  }
48
48
  if (!Array.isArray(methods) || !methods.length) {
49
- log_1.consoleLogErrorAndExit(`You should specify ${chalk_1.default.greenBright('methods')}. ${helpHint}`);
49
+ (0, log_1.consoleLogErrorAndExit)(`You should specify ${chalk_1.default.greenBright('methods')}. ${helpHint}`);
50
50
  return;
51
51
  }
52
52
  schemaDir = path_1.default.resolve(schemaDir);
53
53
  outDir = outDir ? path_1.default.resolve(outDir) : '';
54
54
  // Read and check required schema files
55
55
  const [methodsDefinitions, { definitions: responsesDefinitions }, { definitions: objectsDefinitions }, { errors: errorsDefinitions },] = await Promise.all([
56
- helpers_1.readJSONFile(path_1.default.resolve(schemaDir, 'methods.json')),
57
- helpers_1.readJSONFile(path_1.default.resolve(schemaDir, 'responses.json')),
58
- helpers_1.readJSONFile(path_1.default.resolve(schemaDir, 'objects.json')),
59
- helpers_1.readJSONFile(path_1.default.resolve(schemaDir, 'errors.json')),
56
+ (0, helpers_1.readJSONFile)(path_1.default.resolve(schemaDir, 'methods.json')),
57
+ (0, helpers_1.readJSONFile)(path_1.default.resolve(schemaDir, 'responses.json')),
58
+ (0, helpers_1.readJSONFile)(path_1.default.resolve(schemaDir, 'objects.json')),
59
+ (0, helpers_1.readJSONFile)(path_1.default.resolve(schemaDir, 'errors.json')),
60
60
  ]);
61
61
  if (!Object.keys(methodsDefinitions).length) {
62
- log_1.consoleLogErrorAndExit(`${chalk_1.default.greenBright('responses.json')} file is invalid.`);
62
+ (0, log_1.consoleLogErrorAndExit)(`${chalk_1.default.greenBright('responses.json')} file is invalid.`);
63
63
  return;
64
64
  }
65
65
  if (!Object.keys(responsesDefinitions).length) {
66
- log_1.consoleLogErrorAndExit(`${chalk_1.default.greenBright('responses.json')} file is invalid.`);
66
+ (0, log_1.consoleLogErrorAndExit)(`${chalk_1.default.greenBright('responses.json')} file is invalid.`);
67
67
  return;
68
68
  }
69
69
  if (!Object.keys(objectsDefinitions).length) {
70
- log_1.consoleLogErrorAndExit(`${chalk_1.default.greenBright('objects.json')} file is invalid.`);
70
+ (0, log_1.consoleLogErrorAndExit)(`${chalk_1.default.greenBright('objects.json')} file is invalid.`);
71
71
  return;
72
72
  }
73
73
  const needEmit = !!outDir;
@@ -82,6 +82,6 @@ async function main() {
82
82
  });
83
83
  generator.generate();
84
84
  const endTime = perf_hooks_1.performance.now();
85
- log_1.consoleLog(`✨ Done in ${((endTime - startTime) / 1000).toFixed(2)}s.`);
85
+ (0, log_1.consoleLog)(`✨ Done in ${((endTime - startTime) / 1000).toFixed(2)}s.`);
86
86
  }
87
87
  exports.main = main;
package/dist/log.js CHANGED
@@ -9,7 +9,7 @@ const util_1 = require("util");
9
9
  function getInspectArgs(args) {
10
10
  return args.map((arg) => {
11
11
  if (typeof arg === 'object') {
12
- return util_1.inspect(arg, {
12
+ return (0, util_1.inspect)(arg, {
13
13
  showHidden: false,
14
14
  depth: 10,
15
15
  colors: true,
package/dist/types.js CHANGED
@@ -5,10 +5,10 @@ var RefsDictionaryType;
5
5
  (function (RefsDictionaryType) {
6
6
  RefsDictionaryType[RefsDictionaryType["GenerateAndImport"] = 0] = "GenerateAndImport";
7
7
  RefsDictionaryType[RefsDictionaryType["Generate"] = 1] = "Generate";
8
- })(RefsDictionaryType = exports.RefsDictionaryType || (exports.RefsDictionaryType = {}));
8
+ })(RefsDictionaryType || (exports.RefsDictionaryType = RefsDictionaryType = {}));
9
9
  var ObjectType;
10
10
  (function (ObjectType) {
11
11
  ObjectType["Object"] = "object";
12
12
  ObjectType["Response"] = "response";
13
13
  ObjectType["Params"] = "params";
14
- })(ObjectType = exports.ObjectType || (exports.ObjectType = {}));
14
+ })(ObjectType || (exports.ObjectType = ObjectType = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vkontakte/api-schema-typescript-generator",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "license": "MIT",
5
5
  "description": "VK API TypeScript generator",
6
6
  "author": {
@@ -46,17 +46,17 @@
46
46
  },
47
47
  "devDependencies": {
48
48
  "@types/jest": "^28.1.5",
49
- "@types/node": "^14.0.13",
49
+ "@types/node": "^18.17.3",
50
50
  "@typescript-eslint/eslint-plugin": "5.30.6",
51
51
  "@typescript-eslint/parser": "5.62.0",
52
52
  "@vkontakte/eslint-config": "3.1.0",
53
53
  "eslint": "8.19.0",
54
- "eslint-plugin-react": "7.30.1",
54
+ "eslint-plugin-react": "7.33.1",
55
55
  "eslint-plugin-react-hooks": "4.6.0",
56
56
  "jest": "28.1.3",
57
57
  "pre-commit": "1.2.2",
58
58
  "rimraf": "^3.0.2",
59
59
  "ts-jest": "^28.0.6",
60
- "typescript": "4.3.5"
60
+ "typescript": "^5.1.6"
61
61
  }
62
62
  }
package/src/constants.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { EOL } from 'os';
2
2
  import { Dictionary } from './types';
3
3
 
4
- export const DEFAULT_API_VERSION = '5.130';
4
+ export const DEFAULT_API_VERSION = '5.131';
5
5
 
6
6
  export const PropertyType = {
7
7
  INTEGER: 'integer',
@@ -31,6 +31,7 @@ import { consoleLogError, consoleLogErrorAndExit, consoleLogInfo } from '../log'
31
31
  import { generateImportsBlock } from '../generator';
32
32
  import { generateTypeString } from './typeString';
33
33
  import { ErrorInterface } from '../types/schema';
34
+ import { mergeImports } from './utils/mergeImports';
34
35
 
35
36
  interface APITypingsGeneratorOptions {
36
37
  needEmit: boolean;
@@ -131,10 +132,7 @@ export class APITypingsGenerator {
131
132
  };
132
133
 
133
134
  this.methodFilesMap[section] = {
134
- imports: {
135
- ...methodFile.imports,
136
- ...imports,
137
- },
135
+ imports: mergeImports(methodFile.imports, imports),
138
136
  codeBlocks: [...methodFile.codeBlocks, ...codeBlocks],
139
137
  };
140
138
  }
@@ -253,7 +251,7 @@ export class APITypingsGenerator {
253
251
  objectParentName: object.name,
254
252
  });
255
253
 
256
- imports = { ...imports, ...newImports };
254
+ imports = mergeImports(imports, newImports);
257
255
  codeBlocks = [...codeBlocks, ...newCodeBlocks];
258
256
 
259
257
  codeBlock.addProperty({
@@ -297,7 +295,7 @@ export class APITypingsGenerator {
297
295
 
298
296
  let result: GeneratorResultInterface | false = false;
299
297
 
300
- if (object.ref) {
298
+ if (object.ref && object.type === 'object') {
301
299
  result = this.getPrimitiveInterfaceCode(object);
302
300
  } else {
303
301
  switch (object.type) {
@@ -406,7 +404,7 @@ export class APITypingsGenerator {
406
404
  needEnumNamesConstant: false,
407
405
  });
408
406
 
409
- imports = { ...imports, ...newImports };
407
+ imports = mergeImports(imports, newImports);
410
408
  codeBlocks = [...codeBlocks, ...newCodeBlocks];
411
409
 
412
410
  codeBlock.addProperty({
@@ -19,6 +19,7 @@ import { Dictionary, RefsDictionary, RefsDictionaryType } from '../types';
19
19
  import { isString } from '../utils';
20
20
  import { CodeBlocksArray, GeneratorResultInterface } from './BaseCodeBlock';
21
21
  import { SchemaObject } from './SchemaObject';
22
+ import { mergeImports } from './utils/mergeImports';
22
23
 
23
24
  interface GenerateTypeStringOptions {
24
25
  objectParentName?: string;
@@ -92,7 +93,7 @@ export function generateTypeString(
92
93
  if (object.oneOf) {
93
94
  const values = object.oneOf.map((oneOfObject) => {
94
95
  const { value, imports: newImports } = generateTypeString(oneOfObject, objects);
95
- imports = { ...imports, ...newImports };
96
+ imports = mergeImports(imports, newImports);
96
97
  return value;
97
98
  });
98
99
 
@@ -134,7 +135,7 @@ export function generateTypeString(
134
135
 
135
136
  typeString = formatArrayDepth(value, depth);
136
137
  description = newDescription;
137
- imports = { ...imports, ...newImports };
138
+ imports = mergeImports(imports, newImports);
138
139
  codeBlocks = [...codeBlocks, ...newCodeBlocks];
139
140
  }
140
141
  } else if (object.ref) {
@@ -162,13 +163,16 @@ export function generateTypeString(
162
163
  } else if (refObject.oneOf) {
163
164
  const values = refObject.oneOf.map((oneOfObject) => {
164
165
  const { value, imports: newImports } = generateTypeString(oneOfObject, objects);
165
- imports = { ...imports, ...newImports };
166
+ imports = mergeImports(imports, newImports);
166
167
  return value;
167
168
  });
168
169
 
169
170
  typeString = joinOneOfValues(values);
170
171
  } else if (isString(refObject.type) && scalarTypes[refObject.type] && !refObject.ref) {
171
172
  typeString = scalarTypes[refObject.type];
173
+ } else if (object.type === PropertyType.STRING) {
174
+ imports[refName] = RefsDictionaryType.Generate;
175
+ typeString = scalarTypes.string;
172
176
  } else {
173
177
  imports[refName] = RefsDictionaryType.GenerateAndImport;
174
178
  typeString = getInterfaceName(refName);
@@ -0,0 +1,17 @@
1
+ import { RefsDictionary, RefsDictionaryType } from '../../types';
2
+
3
+ export function mergeImports(oldImports: RefsDictionary, newImports: RefsDictionary) {
4
+ const result = { ...oldImports };
5
+
6
+ Object.entries(newImports).forEach(([name, newImportValue]) => {
7
+ const oldImportValue = oldImports[name];
8
+
9
+ if (oldImportValue === RefsDictionaryType.GenerateAndImport) {
10
+ return;
11
+ }
12
+
13
+ result[name] = newImportValue;
14
+ });
15
+
16
+ return result;
17
+ }