@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/.eslintrc.json +15 -2
- package/.github/CODEOWNERS +2 -0
- package/.prettierrc.js +10 -0
- package/dist/constants.js +1 -1
- package/dist/generators/APITypingsGenerator.js +58 -42
- package/dist/generators/CommentCodeBlock.js +3 -9
- package/dist/generators/SchemaObject.js +19 -1
- package/dist/generators/TypeCodeBlock.js +31 -18
- package/dist/generators/enums.js +2 -5
- package/dist/generators/methods.js +4 -3
- package/dist/helpers.js +34 -20
- package/dist/helpers.test.js +12 -0
- package/dist/index.js +4 -3
- package/jest.config.js +5 -0
- package/package.json +17 -11
- package/src/constants.ts +1 -1
- package/src/generator.ts +5 -1
- package/src/generators/APITypingsGenerator.ts +84 -61
- 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 +4 -3
- package/src/generators/typeString.ts +4 -1
- package/src/helpers.test.ts +12 -0
- package/src/helpers.ts +38 -24
- package/src/index.ts +15 -5
- package/src/types/schema.ts +7 -0
package/src/constants.ts
CHANGED
package/src/generator.ts
CHANGED
|
@@ -3,7 +3,11 @@ import { getInterfaceName, getSectionFromObjectName } from './helpers';
|
|
|
3
3
|
import { Dictionary, ObjectType, RefsDictionary, RefsDictionaryType } from './types';
|
|
4
4
|
import { sortArrayAlphabetically, uniqueArray } from './utils';
|
|
5
5
|
|
|
6
|
-
export function generateImportsBlock(
|
|
6
|
+
export function generateImportsBlock(
|
|
7
|
+
refs: RefsDictionary,
|
|
8
|
+
section: string | null,
|
|
9
|
+
type?: ObjectType,
|
|
10
|
+
): string {
|
|
7
11
|
let importRefs = Object.entries(refs)
|
|
8
12
|
.filter(([, type]) => type === RefsDictionaryType.GenerateAndImport)
|
|
9
13
|
.map(([key]) => key);
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import * as Schema from '../types/schema';
|
|
2
|
-
import {
|
|
3
|
-
Dictionary,
|
|
4
|
-
ObjectType,
|
|
5
|
-
RefsDictionary,
|
|
6
|
-
} from '../types';
|
|
2
|
+
import { Dictionary, ObjectType, RefsDictionary } from '../types';
|
|
7
3
|
import { generateEnumAsUnionType } from './enums';
|
|
8
4
|
import { normalizeMethodInfo } from './methods';
|
|
9
5
|
import { SchemaObject } from './SchemaObject';
|
|
10
6
|
import {
|
|
11
|
-
getInterfaceName,
|
|
12
|
-
|
|
7
|
+
getInterfaceName,
|
|
8
|
+
getMethodSection,
|
|
9
|
+
getObjectNameByRef,
|
|
10
|
+
getSectionFromObjectName,
|
|
11
|
+
isMethodNeeded,
|
|
12
|
+
isPatternProperty,
|
|
13
|
+
prepareBuildDirectory,
|
|
13
14
|
prepareMethodsPattern,
|
|
14
15
|
writeFile,
|
|
15
16
|
} from '../helpers';
|
|
@@ -20,7 +21,8 @@ import {
|
|
|
20
21
|
baseAPIParamsInterfaceName,
|
|
21
22
|
baseBoolIntRef,
|
|
22
23
|
baseOkResponseRef,
|
|
23
|
-
basePropertyExistsRef,
|
|
24
|
+
basePropertyExistsRef,
|
|
25
|
+
DEFAULT_API_VERSION,
|
|
24
26
|
newLineChar,
|
|
25
27
|
} from '../constants';
|
|
26
28
|
import path from 'path';
|
|
@@ -28,6 +30,7 @@ import { CommentCodeBlock } from './CommentCodeBlock';
|
|
|
28
30
|
import { consoleLogError, consoleLogErrorAndExit, consoleLogInfo } from '../log';
|
|
29
31
|
import { generateImportsBlock } from '../generator';
|
|
30
32
|
import { generateTypeString } from './typeString';
|
|
33
|
+
import { ErrorInterface } from '../types/schema';
|
|
31
34
|
|
|
32
35
|
interface APITypingsGeneratorOptions {
|
|
33
36
|
needEmit: boolean;
|
|
@@ -47,6 +50,7 @@ interface APITypingsGeneratorOptions {
|
|
|
47
50
|
methodsDefinitions: Schema.API;
|
|
48
51
|
objects: Dictionary<SchemaObject>;
|
|
49
52
|
responses: Dictionary<SchemaObject>;
|
|
53
|
+
errors: Dictionary<ErrorInterface>;
|
|
50
54
|
}
|
|
51
55
|
|
|
52
56
|
export class APITypingsGenerator {
|
|
@@ -59,6 +63,7 @@ export class APITypingsGenerator {
|
|
|
59
63
|
this.methodsList = options.methodsDefinitions.methods || [];
|
|
60
64
|
this.objects = this.convertJSONSchemaDictionary(options.objects);
|
|
61
65
|
this.responses = this.convertJSONSchemaDictionary(options.responses);
|
|
66
|
+
this.errors = options.errors;
|
|
62
67
|
|
|
63
68
|
this.visitedRefs = {};
|
|
64
69
|
this.generatedObjects = {};
|
|
@@ -68,7 +73,7 @@ export class APITypingsGenerator {
|
|
|
68
73
|
|
|
69
74
|
this.ignoredResponses = {
|
|
70
75
|
'storage.get': {
|
|
71
|
-
|
|
76
|
+
keysResponse: true,
|
|
72
77
|
},
|
|
73
78
|
};
|
|
74
79
|
|
|
@@ -83,6 +88,7 @@ export class APITypingsGenerator {
|
|
|
83
88
|
methodsList!: NonNullable<Schema.API['methods']>;
|
|
84
89
|
objects!: Dictionary<SchemaObject>;
|
|
85
90
|
responses!: Dictionary<SchemaObject>;
|
|
91
|
+
errors!: Dictionary<ErrorInterface>;
|
|
86
92
|
|
|
87
93
|
visitedRefs!: Dictionary<boolean>;
|
|
88
94
|
generatedObjects!: Dictionary<boolean>;
|
|
@@ -129,10 +135,7 @@ export class APITypingsGenerator {
|
|
|
129
135
|
...methodFile.imports,
|
|
130
136
|
...imports,
|
|
131
137
|
},
|
|
132
|
-
codeBlocks: [
|
|
133
|
-
...methodFile.codeBlocks,
|
|
134
|
-
...codeBlocks,
|
|
135
|
-
],
|
|
138
|
+
codeBlocks: [...methodFile.codeBlocks, ...codeBlocks],
|
|
136
139
|
};
|
|
137
140
|
}
|
|
138
141
|
|
|
@@ -192,10 +195,7 @@ export class APITypingsGenerator {
|
|
|
192
195
|
}
|
|
193
196
|
|
|
194
197
|
if (additionalProperties.length) {
|
|
195
|
-
properties = [
|
|
196
|
-
...properties,
|
|
197
|
-
...additionalProperties,
|
|
198
|
-
];
|
|
198
|
+
properties = [...properties, ...additionalProperties];
|
|
199
199
|
}
|
|
200
200
|
});
|
|
201
201
|
}
|
|
@@ -265,10 +265,7 @@ export class APITypingsGenerator {
|
|
|
265
265
|
});
|
|
266
266
|
|
|
267
267
|
return {
|
|
268
|
-
codeBlocks: [
|
|
269
|
-
...codeBlocks,
|
|
270
|
-
codeBlock,
|
|
271
|
-
],
|
|
268
|
+
codeBlocks: [...codeBlocks, codeBlock],
|
|
272
269
|
imports,
|
|
273
270
|
value: '',
|
|
274
271
|
};
|
|
@@ -338,12 +335,18 @@ export class APITypingsGenerator {
|
|
|
338
335
|
|
|
339
336
|
if (stringCodeBlocks.length > 0) {
|
|
340
337
|
const code = stringCodeBlocks.join(newLineChar.repeat(2));
|
|
341
|
-
this.registerResultFile(
|
|
338
|
+
this.registerResultFile(
|
|
339
|
+
path.join('objects', section, `${getInterfaceName(object.name)}.ts`),
|
|
340
|
+
code,
|
|
341
|
+
);
|
|
342
342
|
}
|
|
343
343
|
|
|
344
344
|
codeBlocks.forEach((codeBlock) => {
|
|
345
345
|
if (codeBlock instanceof TypeCodeBlock && codeBlock.needExport && codeBlock.interfaceName) {
|
|
346
|
-
this.registerExport(
|
|
346
|
+
this.registerExport(
|
|
347
|
+
`./objects/${section}/${getInterfaceName(object.name)}.ts`,
|
|
348
|
+
codeBlock.interfaceName,
|
|
349
|
+
);
|
|
347
350
|
}
|
|
348
351
|
});
|
|
349
352
|
|
|
@@ -399,7 +402,9 @@ export class APITypingsGenerator {
|
|
|
399
402
|
imports: newImports,
|
|
400
403
|
value,
|
|
401
404
|
codeBlocks: newCodeBlocks,
|
|
402
|
-
} = generateTypeString(property, this.objects, {
|
|
405
|
+
} = generateTypeString(property, this.objects, {
|
|
406
|
+
needEnumNamesConstant: false,
|
|
407
|
+
});
|
|
403
408
|
|
|
404
409
|
imports = { ...imports, ...newImports };
|
|
405
410
|
codeBlocks = [...codeBlocks, ...newCodeBlocks];
|
|
@@ -432,11 +437,13 @@ export class APITypingsGenerator {
|
|
|
432
437
|
|
|
433
438
|
if (object.enum) {
|
|
434
439
|
const { codeBlocks: newCodeBlocks } = generateEnumAsUnionType(object);
|
|
435
|
-
codeBlocks = [
|
|
436
|
-
...newCodeBlocks,
|
|
437
|
-
];
|
|
440
|
+
codeBlocks = [...newCodeBlocks];
|
|
438
441
|
} else {
|
|
439
|
-
const {
|
|
442
|
+
const {
|
|
443
|
+
imports: newImports,
|
|
444
|
+
value,
|
|
445
|
+
codeBlocks: newCodeBlocks,
|
|
446
|
+
} = generateTypeString(object, this.objects);
|
|
440
447
|
const codeBlock = new TypeCodeBlock({
|
|
441
448
|
type: TypeScriptCodeTypes.Type,
|
|
442
449
|
refName: object.name,
|
|
@@ -448,11 +455,7 @@ export class APITypingsGenerator {
|
|
|
448
455
|
});
|
|
449
456
|
|
|
450
457
|
imports = newImports;
|
|
451
|
-
codeBlocks = [
|
|
452
|
-
...codeBlocks,
|
|
453
|
-
...newCodeBlocks,
|
|
454
|
-
codeBlock,
|
|
455
|
-
];
|
|
458
|
+
codeBlocks = [...codeBlocks, ...newCodeBlocks, codeBlock];
|
|
456
459
|
}
|
|
457
460
|
|
|
458
461
|
return {
|
|
@@ -462,13 +465,11 @@ export class APITypingsGenerator {
|
|
|
462
465
|
};
|
|
463
466
|
}
|
|
464
467
|
|
|
465
|
-
private getResponseCodeBlockAsType(
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
description,
|
|
471
|
-
} = generateTypeString(response, this.objects, {
|
|
468
|
+
private getResponseCodeBlockAsType(
|
|
469
|
+
object: SchemaObject,
|
|
470
|
+
response: SchemaObject,
|
|
471
|
+
): GeneratorResultInterface | false {
|
|
472
|
+
const { imports, value, codeBlocks, description } = generateTypeString(response, this.objects, {
|
|
472
473
|
objectParentName: ' ', // TODO: Refactor
|
|
473
474
|
});
|
|
474
475
|
|
|
@@ -476,20 +477,14 @@ export class APITypingsGenerator {
|
|
|
476
477
|
type: TypeScriptCodeTypes.Type,
|
|
477
478
|
refName: object.name,
|
|
478
479
|
interfaceName: getInterfaceName(object.name),
|
|
479
|
-
description: [
|
|
480
|
-
object.description,
|
|
481
|
-
description || '',
|
|
482
|
-
].join(newLineChar),
|
|
480
|
+
description: [object.description, description || ''].join(newLineChar),
|
|
483
481
|
needExport: true,
|
|
484
482
|
properties: [],
|
|
485
483
|
value,
|
|
486
484
|
});
|
|
487
485
|
|
|
488
486
|
return {
|
|
489
|
-
codeBlocks: [
|
|
490
|
-
...codeBlocks,
|
|
491
|
-
codeBlock,
|
|
492
|
-
],
|
|
487
|
+
codeBlocks: [...codeBlocks, codeBlock],
|
|
493
488
|
imports,
|
|
494
489
|
value: '',
|
|
495
490
|
description,
|
|
@@ -576,17 +571,11 @@ export class APITypingsGenerator {
|
|
|
576
571
|
// Comment with method name for visual sections in file
|
|
577
572
|
const methodNameComment = new CommentCodeBlock([methodName]);
|
|
578
573
|
if (method.description) {
|
|
579
|
-
methodNameComment.appendLines([
|
|
580
|
-
'',
|
|
581
|
-
method.description,
|
|
582
|
-
]);
|
|
574
|
+
methodNameComment.appendLines(['', method.description]);
|
|
583
575
|
}
|
|
584
576
|
this.appendToFileMap(section, {}, [methodNameComment]);
|
|
585
577
|
|
|
586
|
-
const {
|
|
587
|
-
method: normalizedMethod,
|
|
588
|
-
parameterRefs,
|
|
589
|
-
} = normalizeMethodInfo(method);
|
|
578
|
+
const { method: normalizedMethod, parameterRefs } = normalizeMethodInfo(method);
|
|
590
579
|
|
|
591
580
|
method = normalizedMethod;
|
|
592
581
|
this.generateObjectsFromRefs(parameterRefs);
|
|
@@ -619,15 +608,48 @@ export class APITypingsGenerator {
|
|
|
619
608
|
this.registerExport(`./methods/${section}`, codeBlock.interfaceName);
|
|
620
609
|
}
|
|
621
610
|
});
|
|
622
|
-
const code = [
|
|
623
|
-
generateImportsBlock(imports, null),
|
|
624
|
-
...codeBlocks,
|
|
625
|
-
];
|
|
611
|
+
const code = [generateImportsBlock(imports, null), ...codeBlocks];
|
|
626
612
|
|
|
627
|
-
this.registerResultFile(
|
|
613
|
+
this.registerResultFile(
|
|
614
|
+
path.join('methods', `${section}.ts`),
|
|
615
|
+
code.join(newLineChar.repeat(2)),
|
|
616
|
+
);
|
|
628
617
|
});
|
|
629
618
|
}
|
|
630
619
|
|
|
620
|
+
private generateErrors() {
|
|
621
|
+
consoleLogInfo('creating errors...');
|
|
622
|
+
|
|
623
|
+
const code: string[] = [];
|
|
624
|
+
|
|
625
|
+
Object.entries(this.errors)
|
|
626
|
+
.reduce<Array<ErrorInterface & { name: string }>>((acc, [name, error]) => {
|
|
627
|
+
acc.push({ name, ...error });
|
|
628
|
+
return acc;
|
|
629
|
+
}, [])
|
|
630
|
+
.sort((errorA, errorB) => {
|
|
631
|
+
return errorA.code - errorB.code;
|
|
632
|
+
})
|
|
633
|
+
.forEach((error) => {
|
|
634
|
+
const errorConstantName = error.name.toUpperCase();
|
|
635
|
+
|
|
636
|
+
code.push(
|
|
637
|
+
new TypeCodeBlock({
|
|
638
|
+
type: TypeScriptCodeTypes.Const,
|
|
639
|
+
interfaceName: errorConstantName,
|
|
640
|
+
needExport: true,
|
|
641
|
+
value: String(error.code),
|
|
642
|
+
properties: [],
|
|
643
|
+
description: [error.description, error.$comment || ''].join(newLineChar.repeat(2)),
|
|
644
|
+
}).toString(),
|
|
645
|
+
);
|
|
646
|
+
|
|
647
|
+
this.registerExport('./common/errors', errorConstantName);
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
this.registerResultFile(path.join('common', 'errors.ts'), code.join(newLineChar.repeat(2)));
|
|
651
|
+
}
|
|
652
|
+
|
|
631
653
|
private createCommonTypes() {
|
|
632
654
|
consoleLogInfo('creating common types...');
|
|
633
655
|
const code: string[] = [];
|
|
@@ -708,6 +730,7 @@ export class APITypingsGenerator {
|
|
|
708
730
|
consoleLogInfo('generate');
|
|
709
731
|
|
|
710
732
|
this.generateMethods();
|
|
733
|
+
this.generateErrors();
|
|
711
734
|
|
|
712
735
|
if (this.needEmit) {
|
|
713
736
|
this.createCommonTypes();
|
|
@@ -10,19 +10,12 @@ export class CommentCodeBlock extends BaseCodeBlock {
|
|
|
10
10
|
lines: string[];
|
|
11
11
|
|
|
12
12
|
appendLines(lines: string[]) {
|
|
13
|
-
this.lines = [
|
|
14
|
-
...this.lines,
|
|
15
|
-
...lines,
|
|
16
|
-
];
|
|
13
|
+
this.lines = [...this.lines, ...lines];
|
|
17
14
|
}
|
|
18
15
|
|
|
19
16
|
toString(): string {
|
|
20
17
|
const inner = this.lines.map((line) => spaceChar + `* ${line}`.trim());
|
|
21
18
|
|
|
22
|
-
return [
|
|
23
|
-
'/**',
|
|
24
|
-
...inner,
|
|
25
|
-
' */',
|
|
26
|
-
].join(newLineChar);
|
|
19
|
+
return ['/**', ...inner, ' */'].join(newLineChar);
|
|
27
20
|
}
|
|
28
21
|
}
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { EnumLikeArray } from '../types';
|
|
2
2
|
import { isObject, isString } from '../utils';
|
|
3
|
-
import {
|
|
4
|
-
transformPatternPropertyName,
|
|
5
|
-
} from '../helpers';
|
|
3
|
+
import { transformPatternPropertyName } from '../helpers';
|
|
6
4
|
import { consoleLogErrorAndExit } from '../log';
|
|
7
5
|
|
|
8
6
|
export class SchemaObject {
|
|
9
7
|
constructor(name: string, object: any, parentName?: string) {
|
|
10
8
|
if (!isObject(object)) {
|
|
11
|
-
consoleLogErrorAndExit(`[SchemaObject] "${name}" is not an object.`, {
|
|
9
|
+
consoleLogErrorAndExit(`[SchemaObject] "${name}" is not an object.`, {
|
|
10
|
+
name,
|
|
11
|
+
object,
|
|
12
|
+
parentName,
|
|
13
|
+
});
|
|
12
14
|
return;
|
|
13
15
|
}
|
|
14
16
|
|
|
@@ -67,9 +69,13 @@ export class SchemaObject {
|
|
|
67
69
|
}
|
|
68
70
|
|
|
69
71
|
if (object.patternProperties) {
|
|
70
|
-
Object.entries(object.patternProperties).forEach(
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
Object.entries(object.patternProperties).forEach(
|
|
73
|
+
([propertyName, property]: [string, any]) => {
|
|
74
|
+
this.properties.push(
|
|
75
|
+
new SchemaObject(transformPatternPropertyName(propertyName), property, name),
|
|
76
|
+
);
|
|
77
|
+
},
|
|
78
|
+
);
|
|
73
79
|
}
|
|
74
80
|
|
|
75
81
|
if (isObject(object.items)) {
|
|
@@ -112,6 +118,9 @@ export class SchemaObject {
|
|
|
112
118
|
}
|
|
113
119
|
|
|
114
120
|
public clone() {
|
|
115
|
-
return Object.assign(
|
|
121
|
+
return Object.assign(
|
|
122
|
+
Object.create(Object.getPrototypeOf(this)),
|
|
123
|
+
this,
|
|
124
|
+
) as NonNullable<SchemaObject>;
|
|
116
125
|
}
|
|
117
126
|
}
|
|
@@ -9,6 +9,7 @@ export enum TypeScriptCodeTypes {
|
|
|
9
9
|
Enum = 'enum',
|
|
10
10
|
ConstantObject = 'constant_object',
|
|
11
11
|
Type = 'type',
|
|
12
|
+
Const = 'const',
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
export interface TypeCodeBlockProperty {
|
|
@@ -63,41 +64,45 @@ export class TypeCodeBlock extends BaseCodeBlock {
|
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
private getPropertiesCode() {
|
|
66
|
-
const quoteChar = this.properties.some((property) => areQuotesNeededForProperty(property.name))
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
67
|
+
const quoteChar = this.properties.some((property) => areQuotesNeededForProperty(property.name))
|
|
68
|
+
? "'"
|
|
69
|
+
: '';
|
|
70
|
+
|
|
71
|
+
return this.properties
|
|
72
|
+
.map((property) => {
|
|
73
|
+
let divider = '';
|
|
74
|
+
let lineEnd = '';
|
|
75
|
+
|
|
76
|
+
switch (this.type) {
|
|
77
|
+
case TypeScriptCodeTypes.Interface:
|
|
78
|
+
divider = property.isRequired ? ':' : '?:';
|
|
79
|
+
lineEnd = ';';
|
|
80
|
+
break;
|
|
81
|
+
case TypeScriptCodeTypes.ConstantObject:
|
|
82
|
+
divider = ':';
|
|
83
|
+
lineEnd = ',';
|
|
84
|
+
break;
|
|
85
|
+
case TypeScriptCodeTypes.Enum:
|
|
86
|
+
divider = ' =';
|
|
87
|
+
lineEnd = ',';
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
86
90
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
+
let value = property.wrapValue ? quoteJavaScriptValue(property.value) : property.value;
|
|
92
|
+
let propertyCode = [
|
|
93
|
+
` ${quoteChar}${property.name}${quoteChar}${divider} ${value}${lineEnd}`,
|
|
94
|
+
];
|
|
91
95
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
+
if (property.description) {
|
|
97
|
+
const commentLines = joinCommentLines(2, property.description);
|
|
98
|
+
if (commentLines.length) {
|
|
99
|
+
propertyCode.unshift(commentLines.join(newLineChar));
|
|
100
|
+
}
|
|
96
101
|
}
|
|
97
|
-
}
|
|
98
102
|
|
|
99
|
-
|
|
100
|
-
|
|
103
|
+
return propertyCode.join(newLineChar);
|
|
104
|
+
})
|
|
105
|
+
.join(newLineChar);
|
|
101
106
|
}
|
|
102
107
|
|
|
103
108
|
toString(): string {
|
|
@@ -113,10 +118,7 @@ export class TypeCodeBlock extends BaseCodeBlock {
|
|
|
113
118
|
}
|
|
114
119
|
|
|
115
120
|
if (this.description) {
|
|
116
|
-
before = [
|
|
117
|
-
...before,
|
|
118
|
-
...joinCommentLines(0, this.description),
|
|
119
|
-
];
|
|
121
|
+
before = [...before, ...joinCommentLines(0, this.description)];
|
|
120
122
|
}
|
|
121
123
|
|
|
122
124
|
switch (this.type) {
|
|
@@ -125,19 +127,19 @@ export class TypeCodeBlock extends BaseCodeBlock {
|
|
|
125
127
|
if (this.options.allowEmptyInterface) {
|
|
126
128
|
propertiesCode = '';
|
|
127
129
|
} else {
|
|
128
|
-
propertiesCode = [
|
|
129
|
-
' // empty interface',
|
|
130
|
-
' [key: string]: any;',
|
|
131
|
-
].join(newLineChar);
|
|
130
|
+
propertiesCode = [' // empty interface', ' [key: string]: any;'].join(newLineChar);
|
|
132
131
|
}
|
|
133
132
|
}
|
|
134
133
|
|
|
135
|
-
const extendsInterfaces =
|
|
136
|
-
this.extendsInterfaces
|
|
137
|
-
|
|
134
|
+
const extendsInterfaces =
|
|
135
|
+
Array.isArray(this.extendsInterfaces) && this.extendsInterfaces.length
|
|
136
|
+
? this.extendsInterfaces.join(', ')
|
|
137
|
+
: '';
|
|
138
138
|
|
|
139
139
|
code = [
|
|
140
|
-
trimStringDoubleSpaces(
|
|
140
|
+
trimStringDoubleSpaces(
|
|
141
|
+
`${exportKeyword} interface ${this.interfaceName} ${extendsInterfaces} {`,
|
|
142
|
+
),
|
|
141
143
|
propertiesCode,
|
|
142
144
|
'}',
|
|
143
145
|
].join(propertiesCode.length ? newLineChar : '');
|
|
@@ -169,11 +171,18 @@ export class TypeCodeBlock extends BaseCodeBlock {
|
|
|
169
171
|
trimStringDoubleSpaces(`${exportKeyword} type ${this.interfaceName} = ${this.value};`),
|
|
170
172
|
].join(newLineChar);
|
|
171
173
|
break;
|
|
174
|
+
|
|
175
|
+
case TypeScriptCodeTypes.Const:
|
|
176
|
+
if (!this.value) {
|
|
177
|
+
consoleLogErrorAndExit(`"${this.interfaceName}" type has empty value`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
code = [
|
|
181
|
+
trimStringDoubleSpaces(`${exportKeyword} const ${this.interfaceName} = ${this.value};`),
|
|
182
|
+
].join(newLineChar);
|
|
183
|
+
break;
|
|
172
184
|
}
|
|
173
185
|
|
|
174
|
-
return [
|
|
175
|
-
before.join(newLineChar),
|
|
176
|
-
code,
|
|
177
|
-
].join(newLineChar).trim();
|
|
186
|
+
return [before.join(newLineChar), code].join(newLineChar).trim();
|
|
178
187
|
}
|
|
179
188
|
}
|
package/src/generators/enums.ts
CHANGED
|
@@ -18,7 +18,11 @@ export function getEnumNamesIdentifier(name: string) {
|
|
|
18
18
|
return `${name} enumNames`.trim();
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
export function generateEnumConstantObject(
|
|
21
|
+
export function generateEnumConstantObject(
|
|
22
|
+
object: SchemaObject,
|
|
23
|
+
objectName: string,
|
|
24
|
+
enumNames: Array<string | number>,
|
|
25
|
+
) {
|
|
22
26
|
const enumInterfaceName = getInterfaceName(objectName);
|
|
23
27
|
|
|
24
28
|
const codeBlock = new TypeCodeBlock({
|
|
@@ -52,10 +56,7 @@ export function generateEnumAsUnionType(object: SchemaObject): GeneratorResultIn
|
|
|
52
56
|
type: TypeScriptCodeTypes.Type,
|
|
53
57
|
refName: object.name,
|
|
54
58
|
interfaceName: getInterfaceName(object.name),
|
|
55
|
-
description: [
|
|
56
|
-
object.description,
|
|
57
|
-
description,
|
|
58
|
-
].join(newLineChar),
|
|
59
|
+
description: [object.description, description].join(newLineChar),
|
|
59
60
|
needExport: true,
|
|
60
61
|
properties: [],
|
|
61
62
|
value,
|
|
@@ -97,12 +98,11 @@ interface GenerateInlineEnumOptions {
|
|
|
97
98
|
refName?: string;
|
|
98
99
|
}
|
|
99
100
|
|
|
100
|
-
export function generateInlineEnum(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
} = getEnumNames(object);
|
|
101
|
+
export function generateInlineEnum(
|
|
102
|
+
object: SchemaObject,
|
|
103
|
+
options: GenerateInlineEnumOptions = {},
|
|
104
|
+
): GeneratorResultInterface {
|
|
105
|
+
const { isNumericEnum, enumNames, needEnumNamesDescription } = getEnumNames(object);
|
|
106
106
|
|
|
107
107
|
options = {
|
|
108
108
|
needEnumNamesConstant: isNumericEnum,
|
|
@@ -18,6 +18,7 @@ export function normalizeMethodInfo(method: Schema.Method): NormalizeMethodInfoR
|
|
|
18
18
|
// For method params "boolean" type means 1 or 0
|
|
19
19
|
// Real "false" boolean value will be detected by API as true
|
|
20
20
|
if (parameter.type === 'boolean') {
|
|
21
|
+
// @ts-expect-error
|
|
21
22
|
delete parameter.type;
|
|
22
23
|
parameter.$ref = baseBoolIntRef;
|
|
23
24
|
}
|
|
@@ -36,9 +37,9 @@ export function normalizeMethodInfo(method: Schema.Method): NormalizeMethodInfoR
|
|
|
36
37
|
const ref = parameter.items?.$ref;
|
|
37
38
|
parameterRefs[ref] = RefsDictionaryType.Generate;
|
|
38
39
|
|
|
39
|
-
parameter.description +=
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
parameter.description +=
|
|
41
|
+
newLineChar.repeat(2) +
|
|
42
|
+
[`@see ${getInterfaceName(getObjectNameByRef(ref))} (${ref})`].join(newLineChar);
|
|
42
43
|
}
|
|
43
44
|
});
|
|
44
45
|
|
|
@@ -28,7 +28,10 @@ interface GenerateTypeStringOptions {
|
|
|
28
28
|
needEnumNamesConstant?: boolean;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
function generateBaseType(
|
|
31
|
+
function generateBaseType(
|
|
32
|
+
object: SchemaObject,
|
|
33
|
+
options: GenerateTypeStringOptions,
|
|
34
|
+
): GeneratorResultInterface {
|
|
32
35
|
let codeBlocks: CodeBlocksArray = [];
|
|
33
36
|
let typeString = 'any /* default type */';
|
|
34
37
|
let imports: RefsDictionary = {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { areQuotesNeededForProperty } from './helpers';
|
|
2
|
+
|
|
3
|
+
test('areQuotesNeededForProperty', () => {
|
|
4
|
+
expect(areQuotesNeededForProperty('user_id')).toBe(false);
|
|
5
|
+
expect(areQuotesNeededForProperty('uuid4')).toBe(false);
|
|
6
|
+
expect(areQuotesNeededForProperty('_foo')).toBe(false);
|
|
7
|
+
|
|
8
|
+
expect(areQuotesNeededForProperty('4uuid')).toBe(true);
|
|
9
|
+
expect(areQuotesNeededForProperty('user-id')).toBe(true);
|
|
10
|
+
expect(areQuotesNeededForProperty('user&id')).toBe(true);
|
|
11
|
+
expect(areQuotesNeededForProperty('идентификатор')).toBe(true);
|
|
12
|
+
});
|