@vkontakte/api-schema-typescript-generator 0.13.0 → 0.14.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/src/helpers.ts CHANGED
@@ -4,6 +4,7 @@ import { capitalizeFirstLetter, trimArray } from './utils';
4
4
  import { newLineChar, primitiveTypes, spaceChar } from './constants';
5
5
  import { Dictionary } from './types';
6
6
  import { consoleLogErrorAndExit } from './log';
7
+ import prettier from 'prettier';
7
8
 
8
9
  export async function readJSONFile(path: string): Promise<any> {
9
10
  const content = await fsPromises.readFile(path, 'utf-8');
@@ -31,16 +32,29 @@ export function prepareBuildDirectory(directoryPath: string) {
31
32
 
32
33
  export function writeFile(filePath: string, code: string, insertAutoGeneratedNote = true) {
33
34
  if (insertAutoGeneratedNote) {
34
- code = [
35
- '/**',
36
- ' * This is auto-generated file, don\'t modify this file manually',
37
- ' */',
38
- // '/* eslint-disable max-len */',
39
- // '/* eslint-disable @typescript-eslint/no-empty-interface */',
40
- ].join(newLineChar) + newLineChar.repeat(2) + code.trim();
35
+ code =
36
+ [
37
+ '/**',
38
+ " * This is auto-generated file, don't modify this file manually",
39
+ ' */',
40
+ // '/* eslint-disable max-len */',
41
+ // '/* eslint-disable @typescript-eslint/no-empty-interface */',
42
+ ].join(newLineChar) +
43
+ newLineChar.repeat(2) +
44
+ code.trim();
41
45
  }
42
46
 
43
- fs.mkdirSync(filePath.replace(path.basename(filePath), ''), { recursive: true });
47
+ code = prettier.format(code, {
48
+ semi: true,
49
+ singleQuote: true,
50
+ trailingComma: 'all',
51
+ quoteProps: 'consistent',
52
+ parser: 'typescript',
53
+ });
54
+
55
+ fs.mkdirSync(filePath.replace(path.basename(filePath), ''), {
56
+ recursive: true,
57
+ });
44
58
  fs.writeFileSync(filePath, code.trim() + newLineChar);
45
59
  }
46
60
 
@@ -49,10 +63,13 @@ export function prepareMethodsPattern(methodsPattern: string): Dictionary<boolea
49
63
  consoleLogErrorAndExit('methodsPattern is empty. Pass "*" to generate all methods');
50
64
  }
51
65
 
52
- return methodsPattern.replace(/\s+/g, '').split(',').reduce<Dictionary<boolean>>((acc, pattern) => {
53
- acc[pattern] = true;
54
- return acc;
55
- }, {});
66
+ return methodsPattern
67
+ .replace(/\s+/g, '')
68
+ .split(',')
69
+ .reduce<Dictionary<boolean>>((acc, pattern) => {
70
+ acc[pattern] = true;
71
+ return acc;
72
+ }, {});
56
73
  }
57
74
 
58
75
  export function isMethodNeeded(methodsPattern: Dictionary<boolean>, method: string): boolean {
@@ -77,18 +94,17 @@ export function getMethodSection(methodName: string): string {
77
94
  }
78
95
 
79
96
  export function getInterfaceName(name: string): string {
80
- name = name.replace(/\.|(\s+)|_/g, ' ')
97
+ name = name
98
+ .replace(/\.|(\s+)|_/g, ' ')
81
99
  .split(' ')
82
- .map((v) => capitalizeFirstLetter(v)).join('');
100
+ .map((v) => capitalizeFirstLetter(v))
101
+ .join('');
83
102
 
84
103
  return capitalizeFirstLetter(name);
85
104
  }
86
105
 
87
106
  export function getEnumPropertyName(name: string): string {
88
- return name.toUpperCase()
89
- .replace(/\s+/g, '_')
90
- .replace(/-/g, '_')
91
- .replace(/\./g, '_');
107
+ return name.toUpperCase().replace(/\s+/g, '_').replace(/-/g, '_').replace(/\./g, '_');
92
108
  }
93
109
 
94
110
  export function getObjectNameByRef(ref: string): string {
@@ -111,9 +127,10 @@ export function areQuotesNeededForProperty(name: string | number): boolean {
111
127
  return false;
112
128
  }
113
129
 
114
- if (/[&]/.test(name)) {
130
+ if (/[&-]/.test(name)) {
115
131
  return true;
116
132
  }
133
+
117
134
  return !(/^[a-z_]([a-z0-9_])+$/i.test(name) || /^[a-z_]/i.test(name));
118
135
  }
119
136
 
@@ -135,10 +152,7 @@ export function joinCommentLines(indent = 2, ...description: Array<string | stri
135
152
  ...trimArray((entry || '').trim().split(newLineChar)),
136
153
  ];
137
154
  } else if (Array.isArray(entry)) {
138
- descriptionLines = [
139
- ...descriptionLines,
140
- ...entry,
141
- ];
155
+ descriptionLines = [...descriptionLines, ...entry];
142
156
  }
143
157
  });
144
158
 
@@ -170,7 +184,7 @@ export function joinOneOfValues(values: Array<string | number>, primitive?: bool
170
184
  }
171
185
 
172
186
  export function formatArrayDepth(value: string, depth: number) {
173
- if (value.endsWith('\'') || value.includes('|')) {
187
+ if (value.endsWith("'") || value.includes('|')) {
174
188
  return `Array<${value}>` + '[]'.repeat(depth - 1); // Need decrement depth value because of Array<T> has its own depth
175
189
  } else {
176
190
  return value + '[]'.repeat(depth);
package/src/index.ts CHANGED
@@ -11,16 +11,24 @@ const helpMessage = `
11
11
 
12
12
  ${chalk.greenBright('--help')} Shows this help.
13
13
 
14
- ${chalk.greenBright('--schemaDir')} The relative path to directory with ${chalk.bold('methods.json')}, ${chalk.bold('objects.json')} and ${chalk.bold('responses.json')} files.
14
+ ${chalk.greenBright('--schemaDir')} The relative path to directory with ${chalk.bold(
15
+ 'methods.json',
16
+ )}, ${chalk.bold('objects.json')} and ${chalk.bold('responses.json')} files.
15
17
 
16
18
  ${chalk.greenBright('--outDir')} The directory where the files will be generated.
17
19
  If you skip this param, script will work in linter mode without emitting files to file system.
18
- ${chalk.bold('Please note')} that this folder will be cleared after starting the generation.
20
+ ${chalk.bold(
21
+ 'Please note',
22
+ )} that this folder will be cleared after starting the generation.
19
23
 
20
- ${chalk.greenBright('--methods')} List of methods to generate responses and all needed objects.
24
+ ${chalk.greenBright(
25
+ '--methods',
26
+ )} List of methods to generate responses and all needed objects.
21
27
  Example:
22
- - ${chalk.bold('\'*\'')} - to generate all responses and objects.
23
- - ${chalk.bold('\'messages.*, users.get, groups.isMember\'')} - to generate all methods from messages section, users.get and groups.isMember.
28
+ - ${chalk.bold("'*'")} - to generate all responses and objects.
29
+ - ${chalk.bold(
30
+ "'messages.*, users.get, groups.isMember'",
31
+ )} - to generate all methods from messages section, users.get and groups.isMember.
24
32
  `;
25
33
 
26
34
  export async function main() {
@@ -61,6 +69,7 @@ export async function main() {
61
69
  methodsDefinitions,
62
70
  { definitions: responsesDefinitions },
63
71
  { definitions: objectsDefinitions },
72
+ { errors: errorsDefinitions },
64
73
  ] = await Promise.all([
65
74
  readJSONFile(path.resolve(schemaDir, 'methods.json')),
66
75
  readJSONFile(path.resolve(schemaDir, 'responses.json')),
@@ -91,6 +100,7 @@ export async function main() {
91
100
  methodsDefinitions,
92
101
  objects: objectsDefinitions,
93
102
  responses: responsesDefinitions,
103
+ errors: errorsDefinitions,
94
104
  methodsPattern: methods.join(','),
95
105
  });
96
106
 
@@ -150,3 +150,10 @@ export interface Response {
150
150
  };
151
151
  additionalProperties?: boolean;
152
152
  }
153
+
154
+ export interface ErrorInterface {
155
+ code: number;
156
+ description: string;
157
+ $comment?: string;
158
+ subcodes?: Array<{ $ref: string }>;
159
+ }