@vkontakte/api-schema-typescript-generator 0.14.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/.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/dist/cli.js +4 -4
- package/dist/constants.js +1 -1
- package/dist/generator.js +5 -5
- package/dist/generators/APITypingsGenerator.js +72 -72
- package/dist/generators/SchemaObject.js +7 -7
- package/dist/generators/TypeCodeBlock.js +12 -12
- package/dist/generators/enums.js +6 -6
- package/dist/generators/methods.js +1 -1
- package/dist/generators/typeString.js +23 -18
- package/dist/generators/utils/mergeImports.js +16 -0
- package/dist/helpers.js +10 -6
- package/dist/helpers.test.js +7 -7
- package/dist/index.js +13 -13
- package/dist/log.js +1 -1
- package/dist/types.js +2 -2
- package/package.json +5 -5
- package/src/constants.ts +1 -1
- package/src/generators/APITypingsGenerator.ts +24 -26
- package/src/generators/typeString.ts +7 -3
- package/src/generators/utils/mergeImports.ts +17 -0
package/.github/CODEOWNERS
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
.yarnrc @VKCOM/vk-sec
|
|
1
|
+
.github/dependabot.yml @VKCOM/vk-sec
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
- package-ecosystem: 'npm'
|
|
4
|
+
directory: '/'
|
|
5
|
+
schedule:
|
|
6
|
+
interval: 'daily'
|
|
7
|
+
allow:
|
|
8
|
+
- dependency-type: 'direct'
|
|
9
|
+
reviewers:
|
|
10
|
+
- 'VKCOM/vk-sec'
|
|
11
|
+
|
|
12
|
+
- package-ecosystem: 'github-actions'
|
|
13
|
+
directory: '/'
|
|
14
|
+
schedule:
|
|
15
|
+
interval: 'daily'
|
|
16
|
+
reviewers:
|
|
17
|
+
- 'VKCOM/vk-sec'
|
|
@@ -11,13 +11,15 @@ jobs:
|
|
|
11
11
|
publish:
|
|
12
12
|
runs-on: ubuntu-latest
|
|
13
13
|
steps:
|
|
14
|
-
- uses: actions/checkout@
|
|
14
|
+
- uses: actions/checkout@v3
|
|
15
15
|
with:
|
|
16
16
|
token: ${{ secrets.DEVTOOLS_GITHUB_TOKEN }}
|
|
17
17
|
|
|
18
|
-
- uses: actions/setup-node@
|
|
18
|
+
- uses: actions/setup-node@v3
|
|
19
19
|
with:
|
|
20
|
-
node-version:
|
|
20
|
+
node-version: 18
|
|
21
|
+
cache: 'yarn'
|
|
22
|
+
always-auth: true
|
|
21
23
|
registry-url: 'https://registry.npmjs.org'
|
|
22
24
|
|
|
23
25
|
- run: yarn install --frozen-lockfile
|
|
@@ -6,11 +6,12 @@ jobs:
|
|
|
6
6
|
test:
|
|
7
7
|
runs-on: ubuntu-latest
|
|
8
8
|
steps:
|
|
9
|
-
- uses: actions/checkout@
|
|
9
|
+
- uses: actions/checkout@v3
|
|
10
10
|
|
|
11
|
-
- uses: actions/setup-node@
|
|
11
|
+
- uses: actions/setup-node@v3
|
|
12
12
|
with:
|
|
13
|
-
node-version:
|
|
13
|
+
node-version: 18
|
|
14
|
+
cache: 'yarn'
|
|
14
15
|
|
|
15
16
|
- run: yarn install --frozen-lockfile
|
|
16
17
|
|
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.
|
|
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 =
|
|
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,54 +213,56 @@ class APITypingsGenerator {
|
|
|
215
213
|
}
|
|
216
214
|
this.generatedObjects[object.name] = true;
|
|
217
215
|
let result = false;
|
|
218
|
-
if (
|
|
216
|
+
if (object.ref && object.type === 'object') {
|
|
219
217
|
result = this.getPrimitiveInterfaceCode(object);
|
|
220
218
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
219
|
+
else {
|
|
220
|
+
switch (object.type) {
|
|
221
|
+
case 'object':
|
|
222
|
+
result = this.getObjectInterfaceCode(object);
|
|
223
|
+
break;
|
|
224
|
+
case 'string':
|
|
225
|
+
case 'number':
|
|
226
|
+
case 'integer':
|
|
227
|
+
case 'array':
|
|
228
|
+
case 'boolean':
|
|
229
|
+
result = this.getPrimitiveInterfaceCode(object);
|
|
230
|
+
break;
|
|
231
|
+
default:
|
|
232
|
+
if (!result) {
|
|
233
|
+
(0, log_1.consoleLogErrorAndExit)((0, helpers_1.getInterfaceName)(object.name), 'Unknown type of object', object);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
236
|
}
|
|
237
237
|
if (!result) {
|
|
238
|
-
log_1.consoleLogErrorAndExit('empty object result', object);
|
|
238
|
+
(0, log_1.consoleLogErrorAndExit)('empty object result', object);
|
|
239
239
|
return;
|
|
240
240
|
}
|
|
241
241
|
const { codeBlocks, imports } = result;
|
|
242
242
|
const stringCodeBlocks = codeBlocks.map((codeBlock) => codeBlock.toString());
|
|
243
|
-
const section = helpers_1.getSectionFromObjectName(object.name);
|
|
243
|
+
const section = (0, helpers_1.getSectionFromObjectName)(object.name);
|
|
244
244
|
delete imports[object.name];
|
|
245
|
-
stringCodeBlocks.unshift(generator_1.generateImportsBlock(imports, section, types_1.ObjectType.Object));
|
|
245
|
+
stringCodeBlocks.unshift((0, generator_1.generateImportsBlock)(imports, section, types_1.ObjectType.Object));
|
|
246
246
|
if (stringCodeBlocks.length > 0) {
|
|
247
247
|
const code = stringCodeBlocks.join(constants_1.newLineChar.repeat(2));
|
|
248
|
-
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);
|
|
249
249
|
}
|
|
250
250
|
codeBlocks.forEach((codeBlock) => {
|
|
251
251
|
if (codeBlock instanceof TypeCodeBlock_1.TypeCodeBlock && codeBlock.needExport && codeBlock.interfaceName) {
|
|
252
|
-
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);
|
|
253
253
|
}
|
|
254
254
|
});
|
|
255
255
|
this.generateObjectsFromImports(imports);
|
|
256
256
|
}
|
|
257
257
|
getObjectByRef(ref) {
|
|
258
|
-
const refName = helpers_1.getObjectNameByRef(ref);
|
|
258
|
+
const refName = (0, helpers_1.getObjectNameByRef)(ref);
|
|
259
259
|
return this.objects[refName];
|
|
260
260
|
}
|
|
261
261
|
generateObjectsFromRefs(refs) {
|
|
262
262
|
Object.keys(refs).forEach((ref) => {
|
|
263
263
|
const refObject = this.getObjectByRef(ref);
|
|
264
264
|
if (!refObject) {
|
|
265
|
-
log_1.consoleLogInfo(`"${ref}" ref is not found`);
|
|
265
|
+
(0, log_1.consoleLogInfo)(`"${ref}" ref is not found`);
|
|
266
266
|
return;
|
|
267
267
|
}
|
|
268
268
|
this.generateObject(refObject);
|
|
@@ -272,29 +272,29 @@ class APITypingsGenerator {
|
|
|
272
272
|
Object.keys(imports).forEach((ref) => {
|
|
273
273
|
const refObject = this.getObjectByRef(ref);
|
|
274
274
|
if (!refObject) {
|
|
275
|
-
log_1.consoleLogInfo(`"${ref}" ref is not found`);
|
|
275
|
+
(0, log_1.consoleLogInfo)(`"${ref}" ref is not found`);
|
|
276
276
|
return;
|
|
277
277
|
}
|
|
278
278
|
this.generateObject(refObject);
|
|
279
279
|
});
|
|
280
280
|
}
|
|
281
281
|
generateMethodParams(methodInfo) {
|
|
282
|
-
const section = helpers_1.getMethodSection(methodInfo.name);
|
|
282
|
+
const section = (0, helpers_1.getMethodSection)(methodInfo.name);
|
|
283
283
|
const interfaceName = `${methodInfo.name} params`;
|
|
284
284
|
let imports = {};
|
|
285
285
|
let codeBlocks = [];
|
|
286
286
|
const codeBlock = new TypeCodeBlock_1.TypeCodeBlock({
|
|
287
287
|
type: TypeCodeBlock_1.TypeScriptCodeTypes.Interface,
|
|
288
|
-
interfaceName: helpers_1.getInterfaceName(interfaceName),
|
|
288
|
+
interfaceName: (0, helpers_1.getInterfaceName)(interfaceName),
|
|
289
289
|
needExport: true,
|
|
290
290
|
allowEmptyInterface: true,
|
|
291
291
|
properties: [],
|
|
292
292
|
});
|
|
293
293
|
methodInfo.parameters.forEach((property) => {
|
|
294
|
-
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, {
|
|
295
295
|
needEnumNamesConstant: false,
|
|
296
296
|
});
|
|
297
|
-
imports =
|
|
297
|
+
imports = (0, mergeImports_1.mergeImports)(imports, newImports);
|
|
298
298
|
codeBlocks = [...codeBlocks, ...newCodeBlocks];
|
|
299
299
|
codeBlock.addProperty({
|
|
300
300
|
name: property.name,
|
|
@@ -307,7 +307,7 @@ class APITypingsGenerator {
|
|
|
307
307
|
this.generateObjectsFromImports(imports);
|
|
308
308
|
}
|
|
309
309
|
getResponseObjectRef(ref) {
|
|
310
|
-
const objectName = helpers_1.getObjectNameByRef(ref);
|
|
310
|
+
const objectName = (0, helpers_1.getObjectNameByRef)(ref);
|
|
311
311
|
if (this.responses[objectName]) {
|
|
312
312
|
return this.responses[objectName];
|
|
313
313
|
}
|
|
@@ -317,15 +317,15 @@ class APITypingsGenerator {
|
|
|
317
317
|
let codeBlocks = [];
|
|
318
318
|
let imports = {};
|
|
319
319
|
if (object.enum) {
|
|
320
|
-
const { codeBlocks: newCodeBlocks } = enums_1.generateEnumAsUnionType(object);
|
|
320
|
+
const { codeBlocks: newCodeBlocks } = (0, enums_1.generateEnumAsUnionType)(object);
|
|
321
321
|
codeBlocks = [...newCodeBlocks];
|
|
322
322
|
}
|
|
323
323
|
else {
|
|
324
|
-
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);
|
|
325
325
|
const codeBlock = new TypeCodeBlock_1.TypeCodeBlock({
|
|
326
326
|
type: TypeCodeBlock_1.TypeScriptCodeTypes.Type,
|
|
327
327
|
refName: object.name,
|
|
328
|
-
interfaceName: helpers_1.getInterfaceName(object.name),
|
|
328
|
+
interfaceName: (0, helpers_1.getInterfaceName)(object.name),
|
|
329
329
|
description: object.description,
|
|
330
330
|
needExport: true,
|
|
331
331
|
properties: [],
|
|
@@ -341,13 +341,13 @@ class APITypingsGenerator {
|
|
|
341
341
|
};
|
|
342
342
|
}
|
|
343
343
|
getResponseCodeBlockAsType(object, response) {
|
|
344
|
-
const { imports, value, codeBlocks, description } = typeString_1.generateTypeString(response, this.objects, {
|
|
344
|
+
const { imports, value, codeBlocks, description } = (0, typeString_1.generateTypeString)(response, this.objects, {
|
|
345
345
|
objectParentName: ' ', // TODO: Refactor
|
|
346
346
|
});
|
|
347
347
|
const codeBlock = new TypeCodeBlock_1.TypeCodeBlock({
|
|
348
348
|
type: TypeCodeBlock_1.TypeScriptCodeTypes.Type,
|
|
349
349
|
refName: object.name,
|
|
350
|
-
interfaceName: helpers_1.getInterfaceName(object.name),
|
|
350
|
+
interfaceName: (0, helpers_1.getInterfaceName)(object.name),
|
|
351
351
|
description: [object.description, description || ''].join(constants_1.newLineChar),
|
|
352
352
|
needExport: true,
|
|
353
353
|
properties: [],
|
|
@@ -362,7 +362,7 @@ class APITypingsGenerator {
|
|
|
362
362
|
}
|
|
363
363
|
getResponseCodeBlock(object) {
|
|
364
364
|
if (!object.ref) {
|
|
365
|
-
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);
|
|
366
366
|
return false;
|
|
367
367
|
}
|
|
368
368
|
const nonBuildableRefs = {
|
|
@@ -370,13 +370,13 @@ class APITypingsGenerator {
|
|
|
370
370
|
[constants_1.baseOkResponseRef]: true,
|
|
371
371
|
[constants_1.basePropertyExistsRef]: true,
|
|
372
372
|
};
|
|
373
|
-
const objectName = helpers_1.getObjectNameByRef(object.ref);
|
|
373
|
+
const objectName = (0, helpers_1.getObjectNameByRef)(object.ref);
|
|
374
374
|
if (nonBuildableRefs[objectName]) {
|
|
375
375
|
return this.getObjectCodeBlockAsType(object);
|
|
376
376
|
}
|
|
377
377
|
let response = this.getResponseObjectRef(object.ref);
|
|
378
378
|
if (!response) {
|
|
379
|
-
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);
|
|
380
380
|
return false;
|
|
381
381
|
}
|
|
382
382
|
// VK API JSON Schema specific heuristic
|
|
@@ -397,7 +397,7 @@ class APITypingsGenerator {
|
|
|
397
397
|
case 'array':
|
|
398
398
|
return this.getResponseCodeBlockAsType(object, response);
|
|
399
399
|
default:
|
|
400
|
-
log_1.consoleLogErrorAndExit(response.name, 'unknown type', response.type);
|
|
400
|
+
(0, log_1.consoleLogErrorAndExit)(response.name, 'unknown type', response.type);
|
|
401
401
|
return false;
|
|
402
402
|
}
|
|
403
403
|
}
|
|
@@ -412,13 +412,13 @@ class APITypingsGenerator {
|
|
|
412
412
|
}
|
|
413
413
|
generateMethodParamsAndResponses(method) {
|
|
414
414
|
const { name: methodName } = method;
|
|
415
|
-
const section = helpers_1.getMethodSection(methodName);
|
|
416
|
-
if (!utils_1.isObject(method.responses)) {
|
|
417
|
-
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.`);
|
|
418
418
|
return;
|
|
419
419
|
}
|
|
420
420
|
if (Object.keys(method.responses).length === 0) {
|
|
421
|
-
log_1.consoleLogErrorAndExit(`"${methodName}" "responses" field is empty.`);
|
|
421
|
+
(0, log_1.consoleLogErrorAndExit)(`"${methodName}" "responses" field is empty.`);
|
|
422
422
|
return;
|
|
423
423
|
}
|
|
424
424
|
// Comment with method name for visual sections in file
|
|
@@ -427,7 +427,7 @@ class APITypingsGenerator {
|
|
|
427
427
|
methodNameComment.appendLines(['', method.description]);
|
|
428
428
|
}
|
|
429
429
|
this.appendToFileMap(section, {}, [methodNameComment]);
|
|
430
|
-
const { method: normalizedMethod, parameterRefs } = methods_1.normalizeMethodInfo(method);
|
|
430
|
+
const { method: normalizedMethod, parameterRefs } = (0, methods_1.normalizeMethodInfo)(method);
|
|
431
431
|
method = normalizedMethod;
|
|
432
432
|
this.generateObjectsFromRefs(parameterRefs);
|
|
433
433
|
this.generateMethodParams(new SchemaObject_1.SchemaObject(method.name, method));
|
|
@@ -440,9 +440,9 @@ class APITypingsGenerator {
|
|
|
440
440
|
});
|
|
441
441
|
}
|
|
442
442
|
generateMethods() {
|
|
443
|
-
log_1.consoleLogInfo('creating method params and responses...');
|
|
443
|
+
(0, log_1.consoleLogInfo)('creating method params and responses...');
|
|
444
444
|
this.methodsList.forEach((methodInfo) => {
|
|
445
|
-
if (helpers_1.isMethodNeeded(this.methodsPattern, methodInfo.name)) {
|
|
445
|
+
if ((0, helpers_1.isMethodNeeded)(this.methodsPattern, methodInfo.name)) {
|
|
446
446
|
this.generateMethodParamsAndResponses(methodInfo);
|
|
447
447
|
}
|
|
448
448
|
});
|
|
@@ -453,12 +453,12 @@ class APITypingsGenerator {
|
|
|
453
453
|
this.registerExport(`./methods/${section}`, codeBlock.interfaceName);
|
|
454
454
|
}
|
|
455
455
|
});
|
|
456
|
-
const code = [generator_1.generateImportsBlock(imports, null), ...codeBlocks];
|
|
456
|
+
const code = [(0, generator_1.generateImportsBlock)(imports, null), ...codeBlocks];
|
|
457
457
|
this.registerResultFile(path_1.default.join('methods', `${section}.ts`), code.join(constants_1.newLineChar.repeat(2)));
|
|
458
458
|
});
|
|
459
459
|
}
|
|
460
460
|
generateErrors() {
|
|
461
|
-
log_1.consoleLogInfo('creating errors...');
|
|
461
|
+
(0, log_1.consoleLogInfo)('creating errors...');
|
|
462
462
|
const code = [];
|
|
463
463
|
Object.entries(this.errors)
|
|
464
464
|
.reduce((acc, [name, error]) => {
|
|
@@ -483,14 +483,14 @@ class APITypingsGenerator {
|
|
|
483
483
|
this.registerResultFile(path_1.default.join('common', 'errors.ts'), code.join(constants_1.newLineChar.repeat(2)));
|
|
484
484
|
}
|
|
485
485
|
createCommonTypes() {
|
|
486
|
-
log_1.consoleLogInfo('creating common types...');
|
|
486
|
+
(0, log_1.consoleLogInfo)('creating common types...');
|
|
487
487
|
const code = [];
|
|
488
488
|
const apiVersion = this.methodsDefinitions.version || constants_1.DEFAULT_API_VERSION;
|
|
489
489
|
code.push(`export const API_VERSION = '${apiVersion}'`);
|
|
490
490
|
code.push('export type ValueOf<T> = T[keyof T];');
|
|
491
491
|
code.push(new TypeCodeBlock_1.TypeCodeBlock({
|
|
492
492
|
type: TypeCodeBlock_1.TypeScriptCodeTypes.Interface,
|
|
493
|
-
interfaceName: helpers_1.getInterfaceName(constants_1.baseAPIParamsInterfaceName),
|
|
493
|
+
interfaceName: (0, helpers_1.getInterfaceName)(constants_1.baseAPIParamsInterfaceName),
|
|
494
494
|
needExport: true,
|
|
495
495
|
properties: [
|
|
496
496
|
{
|
|
@@ -514,24 +514,24 @@ class APITypingsGenerator {
|
|
|
514
514
|
],
|
|
515
515
|
}).toString());
|
|
516
516
|
this.registerExport('./common/common', 'API_VERSION');
|
|
517
|
-
this.registerExport('./common/common', helpers_1.getInterfaceName(constants_1.baseAPIParamsInterfaceName));
|
|
517
|
+
this.registerExport('./common/common', (0, helpers_1.getInterfaceName)(constants_1.baseAPIParamsInterfaceName));
|
|
518
518
|
this.registerResultFile(path_1.default.join('common', 'common.ts'), code.join(constants_1.newLineChar.repeat(2)));
|
|
519
519
|
}
|
|
520
520
|
/**
|
|
521
521
|
* This method creates index.ts file with exports of all generated params, responses and objects
|
|
522
522
|
*/
|
|
523
523
|
createIndexExports() {
|
|
524
|
-
log_1.consoleLogInfo('creating index.ts exports...');
|
|
524
|
+
(0, log_1.consoleLogInfo)('creating index.ts exports...');
|
|
525
525
|
const blocks = [];
|
|
526
526
|
let exportedObjects = {};
|
|
527
|
-
utils_1.sortArrayAlphabetically(Object.keys(this.exports)).forEach((path) => {
|
|
527
|
+
(0, utils_1.sortArrayAlphabetically)(Object.keys(this.exports)).forEach((path) => {
|
|
528
528
|
const objects = Object.keys(this.exports[path]);
|
|
529
529
|
if (!objects.length) {
|
|
530
530
|
return;
|
|
531
531
|
}
|
|
532
532
|
const blockLines = [];
|
|
533
533
|
blockLines.push('export {');
|
|
534
|
-
utils_1.sortArrayAlphabetically(objects).forEach((object) => {
|
|
534
|
+
(0, utils_1.sortArrayAlphabetically)(objects).forEach((object) => {
|
|
535
535
|
if (exportedObjects[object]) {
|
|
536
536
|
return;
|
|
537
537
|
}
|
|
@@ -542,21 +542,21 @@ class APITypingsGenerator {
|
|
|
542
542
|
blocks.push(blockLines.join(constants_1.newLineChar));
|
|
543
543
|
});
|
|
544
544
|
this.registerResultFile('index.ts', blocks.join(constants_1.newLineChar.repeat(2)));
|
|
545
|
-
log_1.consoleLogInfo(`${Object.keys(exportedObjects).length} objects successfully generated`);
|
|
545
|
+
(0, log_1.consoleLogInfo)(`${Object.keys(exportedObjects).length} objects successfully generated`);
|
|
546
546
|
}
|
|
547
547
|
generate() {
|
|
548
|
-
log_1.consoleLogInfo('generate');
|
|
548
|
+
(0, log_1.consoleLogInfo)('generate');
|
|
549
549
|
this.generateMethods();
|
|
550
550
|
this.generateErrors();
|
|
551
551
|
if (this.needEmit) {
|
|
552
552
|
this.createCommonTypes();
|
|
553
553
|
this.createIndexExports();
|
|
554
|
-
log_1.consoleLogInfo('prepare out directory');
|
|
555
|
-
helpers_1.prepareBuildDirectory(this.outDirPath);
|
|
556
|
-
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');
|
|
557
557
|
Object.keys(this.resultFiles).forEach((filePath) => {
|
|
558
558
|
const fileContent = this.resultFiles[filePath];
|
|
559
|
-
helpers_1.writeFile(path_1.default.join(this.outDirPath, filePath), fileContent);
|
|
559
|
+
(0, helpers_1.writeFile)(path_1.default.join(this.outDirPath, filePath), fileContent);
|
|
560
560
|
});
|
|
561
561
|
}
|
|
562
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
|
|
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
|
}
|
package/dist/generators/enums.js
CHANGED
|
@@ -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 =
|
|
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 =
|
|
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 =
|
|
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.
|
|
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
|
}
|
package/dist/helpers.test.js
CHANGED
|
@@ -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
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
|
|
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
|
|
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.
|
|
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": "^
|
|
49
|
+
"@types/node": "^18.17.3",
|
|
50
50
|
"@typescript-eslint/eslint-plugin": "5.30.6",
|
|
51
|
-
"@typescript-eslint/parser": "5.
|
|
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.
|
|
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": "
|
|
60
|
+
"typescript": "^5.1.6"
|
|
61
61
|
}
|
|
62
62
|
}
|
package/src/constants.ts
CHANGED
|
@@ -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 =
|
|
254
|
+
imports = mergeImports(imports, newImports);
|
|
257
255
|
codeBlocks = [...codeBlocks, ...newCodeBlocks];
|
|
258
256
|
|
|
259
257
|
codeBlock.addProperty({
|
|
@@ -297,27 +295,27 @@ export class APITypingsGenerator {
|
|
|
297
295
|
|
|
298
296
|
let result: GeneratorResultInterface | false = false;
|
|
299
297
|
|
|
300
|
-
if (
|
|
298
|
+
if (object.ref && object.type === 'object') {
|
|
301
299
|
result = this.getPrimitiveInterfaceCode(object);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
300
|
+
} else {
|
|
301
|
+
switch (object.type) {
|
|
302
|
+
case 'object':
|
|
303
|
+
result = this.getObjectInterfaceCode(object);
|
|
304
|
+
break;
|
|
305
|
+
|
|
306
|
+
case 'string':
|
|
307
|
+
case 'number':
|
|
308
|
+
case 'integer':
|
|
309
|
+
case 'array':
|
|
310
|
+
case 'boolean':
|
|
311
|
+
result = this.getPrimitiveInterfaceCode(object);
|
|
312
|
+
break;
|
|
313
|
+
|
|
314
|
+
default:
|
|
315
|
+
if (!result) {
|
|
316
|
+
consoleLogErrorAndExit(getInterfaceName(object.name), 'Unknown type of object', object);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
321
319
|
}
|
|
322
320
|
|
|
323
321
|
if (!result) {
|
|
@@ -406,7 +404,7 @@ export class APITypingsGenerator {
|
|
|
406
404
|
needEnumNamesConstant: false,
|
|
407
405
|
});
|
|
408
406
|
|
|
409
|
-
imports =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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
|
+
}
|