-
Notifications
You must be signed in to change notification settings - Fork 59
/
swagger.builder.ts
executable file
·213 lines (187 loc) · 5.14 KB
/
swagger.builder.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import {
ISwaggerInfo,
ISwaggerDefinition,
ISwaggerDefinitionProperty,
ISwaggerExternalDocs,
ISwaggerOperationResponse,
} from './i-swagger';
import * as assert from 'assert';
import { SwaggerService } from './swagger.service';
import { SwaggerDefinitionConstant } from './swagger-definition.constant';
import {
IApiOperationArgsBaseParameter,
IApiOperationArgsBaseResponse,
} from './i-api-operation-args.base';
export interface ISwaggerBuildDefinitionModelPropertyType {
type?: string | ISwaggerBuildDefinitionModelPropertyType;
}
export interface ISwaggerBuildDefinitionModelProperty {
/**
* Define type of property. Example: SwaggerDefinitionConstant.Definition.Property.Type.STRING
* Optional.
*/
type?: string;
/**
* Define format of property. Example: SwaggerDefinitionConstant.Definition.Property.Format.INT_64
* Optional.
*/
format?: string;
/**
* Define if property is required.
* Optional. Default is false.
*/
required?: boolean;
/**
* Define model.
* Optional.
*/
model?: string;
/**
* Define enum;
* Optional.
*/
enum?: string[];
/**
* Define description.
* Optional.
*/
description?: string;
/**
* Define type of item. Example: SwaggerDefinitionConstant.Definition.Property.Type.STRING
* Optional.
*/
itemType?: string;
/**
* Define example.
*/
example?: any[];
}
export interface ISwaggerBuildDefinitionModel {
/**
* Define description.
*/
description?: string;
/**
* Define all properties of model.
*/
properties: { [key: string]: ISwaggerBuildDefinitionModelProperty };
}
export interface ISwaggerSecurityDefinition {
/**
* Define type of security.
*/
type: string;
/**
* Define where security set.
* Optional.
*/
in?: string;
/**
* Define name of security.
* Optional.
*/
name?: string;
}
export interface ISwaggerBuildDefinition {
/**
* Base URL for all API.
* Optional. Default is "/".
*/
basePath?: string;
/**
* Version Open API
* Optional.
*/
openapi?: string;
/**
* Metadata.
*/
info: ISwaggerInfo;
/**
* Define the MIME types supported by the API for consumes. The root-level definition can be overridden in individual operations.
* Optional. Default is SwaggerDefinition.Consume.JSON = "application/json".
*/
consumes?: string[];
/**
* Define the MIME types supported by the API for produces. The root-level definition can be overridden in individual operations.
* Optional. Default is SwaggerDefinition.Produce.JSON = "application/json".
*/
produces?: string[];
/**
* Define schemes.
* Optional. Default is SwaggerDefinition.Scheme.HTTP = "http".
*/
schemes?: string[];
/**
* Define host.
* Optional.
*/
host?: string;
/**
* Define All Definitions.
* Optional.
*/
models?: { [key: string]: ISwaggerBuildDefinitionModel };
/**
* Define external doc
* Optional.
*/
externalDocs?: ISwaggerExternalDocs;
/**
* Define security definitions list.
* Optional.
*/
securityDefinitions?: { [key: string]: ISwaggerSecurityDefinition };
/**
* Define global responses.
* Optional.
*/
responses?: { [key: string]: IApiOperationArgsBaseResponse };
}
export function build(buildDefinition: ISwaggerBuildDefinition): void {
assert.ok(buildDefinition, 'Definition are required.');
assert.ok(
buildDefinition.info,
'Informations are required. Base is { title: "Title of my API", version: "1.0.0"}'
);
if (buildDefinition.basePath) {
SwaggerService.getInstance().setBasePath(buildDefinition.basePath);
}
if (buildDefinition.openapi) {
SwaggerService.getInstance().setOpenapi(buildDefinition.openapi);
}
if (buildDefinition.info) {
SwaggerService.getInstance().setInfo(buildDefinition.info);
}
if (buildDefinition.schemes) {
SwaggerService.getInstance().setSchemes(buildDefinition.schemes);
}
if (buildDefinition.produces) {
SwaggerService.getInstance().setProduces(buildDefinition.produces);
}
if (buildDefinition.consumes) {
SwaggerService.getInstance().setConsumes(buildDefinition.consumes);
}
if (buildDefinition.host) {
SwaggerService.getInstance().setHost(buildDefinition.host);
}
if (buildDefinition.externalDocs) {
SwaggerService.getInstance().setExternalDocs(
buildDefinition.externalDocs
);
}
if (buildDefinition.securityDefinitions) {
SwaggerService.getInstance().addSecurityDefinitions(
buildDefinition.securityDefinitions
);
}
if (buildDefinition.models) {
SwaggerService.getInstance().setDefinitions(buildDefinition.models);
}
if (buildDefinition.responses) {
SwaggerService.getInstance().setGlobalResponses(
buildDefinition.responses
);
}
SwaggerService.getInstance().buildSwagger();
}