-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathindex.ts
More file actions
102 lines (93 loc) · 3.94 KB
/
index.ts
File metadata and controls
102 lines (93 loc) · 3.94 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { EOL } from "os";
import * as Api from "./api";
import type * as Types from "./types";
import { generateValidRootSchema } from "./generateValidRootSchema";
export interface Option {
allowOperationIds?: string[];
convertOption: Api.OpenApiTools.ConvertContext.Options;
}
export class CodeGenerator {
private rootSchema: Types.OpenApi.Document;
private resolvedReferenceDocument: Types.OpenApi.Document;
private parser: Api.OpenApiTools.Parser;
constructor(
private readonly entryPointOrDocument: string | Types.OpenApi.Document,
private option?: Option,
) {
if (typeof entryPointOrDocument === "string") {
this.rootSchema = generateValidRootSchema(Api.FileSystem.loadJsonOrYaml(entryPointOrDocument));
this.resolvedReferenceDocument = Api.ResolveReference.resolve(
entryPointOrDocument,
entryPointOrDocument,
JSON.parse(JSON.stringify(this.rootSchema)),
);
} else {
this.rootSchema = entryPointOrDocument;
this.resolvedReferenceDocument = Api.ResolveReference.resolve(".", ".", JSON.parse(JSON.stringify(this.rootSchema)));
}
this.parser = this.createParser();
}
private createParser(): Api.OpenApiTools.Parser {
const entryPoint = typeof this.entryPointOrDocument === "string" ? this.entryPointOrDocument : ".";
return new Api.OpenApiTools.Parser(entryPoint, this.rootSchema, this.resolvedReferenceDocument, this.option?.convertOption);
}
/**
* Validate the OpenAPI Schema
*/
public validateOpenApiSchema(option?: Types.Validator.Option) {
if (!option) {
Api.Validator.validate(this.resolvedReferenceDocument);
} else {
Api.Validator.validate(this.resolvedReferenceDocument, option.logger);
}
}
/**
* Provides TypeScript typedefs generated from OpenAPI Schema.
*
* @param generatorTemplate Template for when you want to change the code following a type definition
* @returns String of generated code
*/
public generateTypeDefinition(generatorTemplates?: Types.CodeGenerator.CustomGenerator<any>[], allowOperationIds?: string[]): string {
const create = () => {
const statements = this.parser.getOpenApiTypeDefinitionStatements();
generatorTemplates?.forEach(generatorTemplate => {
const payload = this.parser.getCodeGeneratorParamsArray(allowOperationIds);
const extraStatements = Api.TsGenerator.Utils.convertIntermediateCodes(generatorTemplate.generator(payload, generatorTemplate.option));
statements.push(...extraStatements);
});
return statements;
};
return [Api.OpenApiTools.Comment.generateLeading(this.resolvedReferenceDocument), Api.TsGenerator.generate(create)].join(EOL + EOL + EOL);
}
/**
* Generate code using a template
*
* @param generatorTemplate
* @returns String of generated code
*/
public generateCode(generatorTemplates: Types.CodeGenerator.CustomGenerator<any>[], allowOperationIds?: string[]): string {
const payload = this.parser.getCodeGeneratorParamsArray(allowOperationIds);
const create = () => {
return generatorTemplates.flatMap(generatorTemplate => {
return Api.TsGenerator.Utils.convertIntermediateCodes(generatorTemplate?.generator(payload, generatorTemplate.option));
});
};
return [Api.OpenApiTools.Comment.generateLeading(this.resolvedReferenceDocument), Api.TsGenerator.generate(create)].join(EOL + EOL + EOL);
}
/**
* Provides parameters extracted from OpenApi Schema
*/
public getCodeGeneratorParamsArray(allowOperationIds?: string[]): Types.CodeGenerator.Params[] {
return this.parser.getCodeGeneratorParamsArray(allowOperationIds);
}
/**
* Provides types for parameters for Templates.FunctionalApiClient.
*
* This API will be moved to Templates in the future.
*/
public getAdditionalTypeDefinitionCustomCodeGenerator(): Types.CodeGenerator.CustomGenerator<undefined> {
return {
generator: () => this.parser.getAdditionalTypeStatements(),
};
}
}