Skip to content

Commit 1809782

Browse files
Changes for types and templates:
- TemplateConfigModel added - Template config is readed and mapped to templateConfigModel - Types mapping now are readed from template/config.json - Fixs default and empty types
1 parent d73b0cb commit 1809782

File tree

7 files changed

+92
-35
lines changed

7 files changed

+92
-35
lines changed

src/models/api.model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { camel, capital } from 'case';
2+
import { getFixedTypeName } from '../utils/models.util';
23
import { ModelStore } from '../stores/model.store';
34
import { ApiURLModel } from './api-url.model';
45
import { PhysycalFile } from './entities';
@@ -58,7 +59,7 @@ export class ApiModel implements PhysycalFile {
5859
return this.requestBody?.type;
5960
}
6061
get responseType(): string {
61-
return this.response?.type || 'void';
62+
return this.response?.type || getFixedTypeName('empty');
6263
}
6364

6465
get verb(): string {

src/models/config.model.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { existsSync } from 'fs';
22
import { resolve as pathResolve } from 'path';
3+
import { TemplateConfigModel } from './template-config.model';
34

45
export interface ConfigI {
56
outputPath: string;
67
exportPath: string;
78
outputModelsPath: string;
89
outputApisPath: string;
10+
templateConfig: TemplateConfigModel;
911
}
1012

1113
class ConfigModel implements ConfigI {
@@ -16,6 +18,7 @@ class ConfigModel implements ConfigI {
1618

1719
private _template: string;
1820
private _templatePath: string;
21+
private _templateConfig: TemplateConfigModel;
1922

2023
get outputPath(): string {
2124
return this._outputPath;
@@ -43,6 +46,7 @@ class ConfigModel implements ConfigI {
4346
set template(template: string) {
4447
this._template = template;
4548
this._templatePath = null;
49+
this._templateConfig = null;
4650
}
4751

4852
get templatePath(): string {
@@ -68,6 +72,13 @@ class ConfigModel implements ConfigI {
6872
return this._templatePath;
6973
}
7074

75+
get templateConfig(): TemplateConfigModel {
76+
if (!this._templateConfig) {
77+
this._templateConfig = new TemplateConfigModel(pathResolve(this.templatePath, 'config'));
78+
}
79+
return this._templateConfig;
80+
}
81+
7182
constructor() {}
7283

7384
parseYargs(yargs): void {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { fileExtension } from "../utils/files.util";
2+
3+
export interface TypesMapped {
4+
/**
5+
* Type if NO TYPE (literally nothing)
6+
*/
7+
empty: string;
8+
9+
[type: string]: string;
10+
default: string;
11+
boolean: string;
12+
file: string;
13+
integer: string;
14+
number: string;
15+
object: string;
16+
string: string;
17+
}
18+
19+
export interface TemplateConfigI {
20+
// Template files
21+
apiFile: string;
22+
modelFile: string;
23+
enumModelFile: string;
24+
25+
/**
26+
* Types mapping from 'OpenAPI' to the language template
27+
* `default` will be used as default type (if not defined/not in the list)
28+
*/
29+
typesMapped: TypesMapped;
30+
}
31+
32+
export class TemplateConfigModel implements TemplateConfigI {
33+
private configMapped: TemplateConfigI;
34+
35+
get apiFile(): string {
36+
return this.configMapped.apiFile;
37+
}
38+
get apiExtension(): string {
39+
return fileExtension(this.configMapped.apiFile);
40+
}
41+
get modelFile(): string {
42+
return this.configMapped.modelFile;
43+
}
44+
get modelExtension(): string {
45+
return fileExtension(this.configMapped.modelFile);
46+
}
47+
get enumModelFile(): string {
48+
return this.configMapped.enumModelFile;
49+
}
50+
get enumModelExtension(): string {
51+
return fileExtension(this.configMapped.enumModelFile);
52+
}
53+
54+
get typesMapped(): TypesMapped {
55+
return this.configMapped.typesMapped;
56+
}
57+
58+
constructor(templatePath: string) {
59+
this.configMapped = require(templatePath);
60+
}
61+
}

src/services/parsers/open-api-v3/parser-base.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export abstract class ParserBaseService {
125125
console.warn('WARNING: No schema defined! Any will be use instead');
126126
console.warn('TIP: Don\'t fill "content" for responses if void');
127127
const instance = new ModelAttributessModel(null);
128-
instance.typeURI = 'any';
128+
instance.typeURI = 'default';
129129
return instance;
130130
}
131131
if (this.isRefObject(schema)) {
@@ -227,9 +227,9 @@ export abstract class ParserBaseService {
227227
console.warn(
228228
`WARNING: ${attrName} not recognized OpenAPIV3 Schema type ${JSON.stringify(
229229
rawAttribute,
230-
)}. Any will be used instead.`,
230+
)}. DEFAULT will be used instead.`,
231231
);
232-
attribute.typeURI = 'any';
232+
attribute.typeURI = 'default';
233233
}
234234
}
235235
}

src/services/writters/api-writter.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import { generateFileSync, getTemplate, makeDir } from '../../utils/files.util';
1212
export class ApiWritterService {
1313
private mustacheTemplate;
1414

15-
private template = 'api.ts';
16-
private exportExtension = 'ts';
15+
private exportExtension;
1716

1817
constructor(private store: StoreI, private configuration: ConfigI = config) {}
1918

@@ -40,8 +39,9 @@ export class ApiWritterService {
4039
}
4140

4241
private prepareMustacheInstance(): void {
43-
this.mustacheTemplate = getTemplate(this.template);
42+
this.mustacheTemplate = getTemplate(this.configuration.templateConfig.apiFile);
4443
mustache.parse(this.mustacheTemplate);
44+
this.exportExtension = this.configuration.templateConfig.apiExtension;
4545
}
4646

4747
private getGeneratedTemplate(groupName: string, apis: ApiModel[]): string {

src/services/writters/model-writter.service.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ export class ModelWritterService {
1414
private modelMustacheTemplate;
1515
private enumMustacheTemplate;
1616

17-
private modelTemplate = 'model.ts';
18-
private enumTemplate = 'enumModel.ts';
19-
private exportExtension = 'ts';
17+
private exportExtension;
2018

2119
constructor(private store: StoreI, private configuration: ConfigI = config) {}
2220

@@ -40,10 +38,11 @@ export class ModelWritterService {
4038
}
4139

4240
private prepareMustacheInstance(): void {
43-
this.modelMustacheTemplate = getTemplate(this.modelTemplate);
41+
this.modelMustacheTemplate = getTemplate(this.configuration.templateConfig.modelFile);
4442
mustache.parse(this.modelMustacheTemplate);
45-
this.enumMustacheTemplate = getTemplate(this.enumTemplate);
43+
this.enumMustacheTemplate = getTemplate(this.configuration.templateConfig.enumModelFile);
4644
mustache.parse(this.enumMustacheTemplate);
45+
this.exportExtension = this.configuration.templateConfig.modelExtension;
4746
}
4847

4948
private getGeneratedTemplate(model: ModelType): string {

src/utils/models.util.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,30 @@
11
import { capital } from 'case';
22
import { ApiModel } from '../models/api.model';
3+
import { config } from '../models/config.model';
34
import { PhysycalFile } from '../models/entities';
45
import { Store } from '../stores/entities.store';
56

67
export function getFixedTypeName(type: string): string {
7-
let parsedTypes = {
8-
'': 'any',
9-
any: 'any',
10-
array: 'any[]',
11-
bool: 'boolean',
12-
boolean: 'boolean',
13-
double: 'number',
14-
empty: 'void',
15-
file: 'File',
16-
float: 'number',
17-
integer: 'number',
18-
number: 'number',
19-
object: 'any',
20-
string: 'string',
21-
undefined: 'any',
22-
void: 'void',
23-
};
8+
const typesMapped = config.templateConfig.typesMapped;
249

25-
let newType = parsedTypes[type];
10+
let newType = typesMapped[type];
2611
if (newType !== undefined) {
2712
if (newType !== type) {
2813
console.debug('Type', type, 'changed to', newType);
2914
}
30-
return parsedTypes[type];
15+
return typesMapped[type];
3116
}
3217

3318
if (type.indexOf('#/') === 0) {
3419
if (Store.models.getByUri(type)) {
3520
newType = type.substring(type.lastIndexOf('/') + 1);
3621
} else {
37-
newType = 'any';
38-
console.error('ERROR: Type', type, 'not defined. Any will be used.');
22+
newType = typesMapped.default;
23+
console.error(`ERROR: Type ${type}, not defined. ${newType} will be used.`);
3924
}
4025
} else {
41-
newType = 'any';
42-
console.warn('WARNING: Type', type, 'not defined. Any will be used.');
26+
newType = typesMapped.default;
27+
console.warn(`WARNING: Type ${type}, not defined. ${newType} will be used.`);
4328
}
4429

4530
return newType;

0 commit comments

Comments
 (0)