@vkontakte/api-schema-typescript-generator 0.13.2 → 0.15.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/.eslintrc.json +3 -0
- package/.github/CODEOWNERS +1 -2
- package/.github/dependabot.yml +17 -0
- package/.github/workflows/publish.yml +5 -3
- package/.github/workflows/pull_request.yml +4 -3
- package/.prettierrc.js +10 -0
- package/dist/generators/APITypingsGenerator.js +62 -57
- package/dist/generators/CommentCodeBlock.js +2 -9
- package/dist/generators/SchemaObject.js +5 -1
- package/dist/generators/TypeCodeBlock.js +22 -18
- package/dist/generators/enums.js +2 -5
- package/dist/generators/methods.js +3 -3
- package/dist/helpers.js +32 -20
- package/dist/index.js +4 -3
- package/package.json +6 -4
- package/src/generator.ts +5 -1
- package/src/generators/APITypingsGenerator.ts +104 -81
- package/src/generators/CommentCodeBlock.ts +2 -9
- package/src/generators/SchemaObject.ts +17 -8
- package/src/generators/TypeCodeBlock.ts +56 -47
- package/src/generators/enums.ts +11 -11
- package/src/generators/methods.ts +3 -3
- package/src/generators/typeString.ts +4 -1
- package/src/helpers.ts +36 -23
- package/src/index.ts +15 -5
- package/src/types/schema.ts +7 -0
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
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
|
|
53
|
-
|
|
54
|
-
|
|
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
|
|
97
|
+
name = name
|
|
98
|
+
.replace(/\.|(\s+)|_/g, ' ')
|
|
81
99
|
.split(' ')
|
|
82
|
-
.map((v) => capitalizeFirstLetter(v))
|
|
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 {
|
|
@@ -136,10 +152,7 @@ export function joinCommentLines(indent = 2, ...description: Array<string | stri
|
|
|
136
152
|
...trimArray((entry || '').trim().split(newLineChar)),
|
|
137
153
|
];
|
|
138
154
|
} else if (Array.isArray(entry)) {
|
|
139
|
-
descriptionLines = [
|
|
140
|
-
...descriptionLines,
|
|
141
|
-
...entry,
|
|
142
|
-
];
|
|
155
|
+
descriptionLines = [...descriptionLines, ...entry];
|
|
143
156
|
}
|
|
144
157
|
});
|
|
145
158
|
|
|
@@ -171,7 +184,7 @@ export function joinOneOfValues(values: Array<string | number>, primitive?: bool
|
|
|
171
184
|
}
|
|
172
185
|
|
|
173
186
|
export function formatArrayDepth(value: string, depth: number) {
|
|
174
|
-
if (value.endsWith('
|
|
187
|
+
if (value.endsWith("'") || value.includes('|')) {
|
|
175
188
|
return `Array<${value}>` + '[]'.repeat(depth - 1); // Need decrement depth value because of Array<T> has its own depth
|
|
176
189
|
} else {
|
|
177
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(
|
|
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(
|
|
20
|
+
${chalk.bold(
|
|
21
|
+
'Please note',
|
|
22
|
+
)} that this folder will be cleared after starting the generation.
|
|
19
23
|
|
|
20
|
-
${chalk.greenBright(
|
|
24
|
+
${chalk.greenBright(
|
|
25
|
+
'--methods',
|
|
26
|
+
)} List of methods to generate responses and all needed objects.
|
|
21
27
|
Example:
|
|
22
|
-
- ${chalk.bold('
|
|
23
|
-
- ${chalk.bold(
|
|
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
|
|
package/src/types/schema.ts
CHANGED