/
gen-type.ts
81 lines (71 loc) · 2.44 KB
/
gen-type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { ReferenceObject, SchemaObject } from 'openapi3-ts';
import { fileName, simpleName, modelClass } from './gen-utils';
import { Import, Imports } from './imports';
import { Options } from './options';
/**
* Base definitions of a generated type
*/
export abstract class GenType {
/** Name of the generated type / class */
public typeName: string;
/** Name of the generated file */
public fileName: string;
/** TypeScript comments for this type */
public tsComments: string;
private _imports: Imports = new Imports();
public imports: Import[];
private _additionalDependencies = new Set<string>();
public additionalDependencies: string[];
constructor(
public name: string,
/** Generation options */
public options: Options) {
}
protected addImport(type: string) {
type = modelClass(type, this.options);
if (type && this.typeName !== type) {
// Don't add an import to this own file
this._imports.add(type, `${this.pathToModels()}${fileName(type)}`);
}
}
protected updateImports() {
this.imports = this._imports.toArray();
this.additionalDependencies = [...this._additionalDependencies];
}
protected collectImports(schema: SchemaObject | ReferenceObject | undefined, additional = false, processOneOf = false): void {
if (!schema) {
return;
} else if (schema.$ref) {
const dep = simpleName(schema.$ref);
if (additional) {
this._additionalDependencies.add(modelClass(dep, this.options));
} else {
this.addImport(dep);
}
} else {
schema = schema as SchemaObject;
(schema.allOf || []).forEach(i => this.collectImports(i, additional));
(schema.anyOf || []).forEach(i => this.collectImports(i, additional));
if (processOneOf) {
(schema.oneOf || []).forEach(i => this.collectImports(i, additional));
}
if (schema.items) {
this.collectImports(schema.items, additional);
}
if (schema.properties) {
const properties = schema.properties;
Object.keys(properties).forEach(p => {
const prop = properties[p];
this.collectImports(prop, additional, true);
});
}
if (typeof schema.additionalProperties === 'object') {
this.collectImports(schema.additionalProperties, additional);
}
}
}
/**
* Must be implemented to return the relative path to the models, ending with `/`
*/
protected abstract pathToModels(): string;
}