support swagger for microservice definition
[sdc/sdc-workflow-designer.git] / sdc-workflow-designer-ui / src / app / model / swagger.ts
1 /*******************************************************************************
2  * Copyright (c) 2017 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  *******************************************************************************/
12
13 export class SwaggerParameter {
14     public description: string;
15     public position: string;  // in path, query, header, body, form
16     public name: string;
17     public required: boolean;
18     public type: string;
19
20     // if position is body
21     public schema: SwaggerSchemaObject;
22
23     constructor(options: any) {
24         this.description = options.description;
25         this.position = options.in;
26         this.name = options.name;
27         this.required = options.required;
28         this.type = options.type;
29         if (this.position === 'body') {
30             this.schema = getSchemaObject(options.schema);
31         }
32     }
33 }
34
35 export class SwaggerHeader {
36     public description: string;
37
38     constructor(options: any) {
39         this.description = options.description;
40     }
41 }
42
43 export class SwaggerResponse {
44     public description: string;
45     public schema: SwaggerSchemaObject;
46     public headers: any;
47
48     constructor({description, schema, headers}) {
49         this.description = description;
50
51         if (schema) {
52             this.schema = getSchemaObject(schema);
53         }
54
55         if (headers) {
56             this.headers = {};
57             for (const key in headers) {
58                 this.headers[key] = new SwaggerHeader(headers[key]);
59             }
60         }
61     }
62 }
63
64 export class SwaggerMethod {
65     public consumes: string[];
66     public description: string;
67     public operationId: string;
68     public parameters: SwaggerParameter[];
69     public produces: string[];
70     public responses: any;
71     public summary: string;
72     public tags: string[];
73
74     constructor({ consumes, description, operationId, parameters, produces, responses, summary, tags }) {
75         this.consumes = consumes;
76         this.description = description;
77         this.operationId = operationId;
78         this.parameters = parameters.map(param => new SwaggerParameter(param));
79         this.produces = produces;
80         this.responses = this.initResponses(responses);
81         this.summary = summary;
82         this.tags = tags;
83     }
84
85     private initResponses(responses: any): any {
86         const responseObjs = {};
87         for (const key in responses) {
88             responseObjs[key] = new SwaggerResponse(responses[key]);
89         }
90
91         return responseObjs;
92     }
93 }
94
95 export class SwaggerInfo {
96     public title: string;
97     public version: string;
98
99     constructor({ title, version }) {
100         this.title = title;
101         this.version = version;
102     }
103 }
104
105 export class SwaggerTag {
106     public name: string;
107
108     constructor({name}) {
109         this.name = name;
110     }
111 }
112
113 export class Swagger {
114     public basePath: string;
115     public definitions: any;
116     public info: SwaggerInfo;
117     public paths: any;
118     public swagger: string;
119     public tags: SwaggerTag[];
120
121     constructor({basePath, definitions, info, paths, swagger, tags}) {
122         this.basePath = basePath;
123         this.definitions = this.initDefinitions(definitions);
124         this.info = new SwaggerInfo(info);
125         this.paths = this.initPaths(paths);
126         this.swagger = swagger;
127         this.tags = tags.map(tag => new SwaggerTag(tag));
128     }
129
130     private initPaths(paths: any): any {
131         const pathObjs = {};
132         for (const key in paths) {
133             pathObjs[key] = this.initPath(paths[key]);
134         }
135         return pathObjs;
136     }
137
138     private initPath(path: any): any {
139         const pathObj = {};
140
141         for (const key in path) {
142             pathObj[key] = new SwaggerMethod(path[key]);
143         }
144
145         return pathObj;
146     }
147
148     private initDefinitions(definitions: any): any {
149         const definitionObjs = {};
150         for (const key in definitions) {
151             definitionObjs[key] = getSchemaObject(definitions[key]);
152         }
153         return definitionObjs;
154     }
155 }
156
157 export function getSchemaObject(definition: any) {
158     if (definition.$ref) {
159         return new SwaggerReferenceObject(definition);
160     } else if (definition.type === 'array') {
161         return new SwaggerModelArray(definition);
162     } else if (definition.type === 'object') {
163         if (definition.properties) {
164             return new SwaggerModelSimple(definition);
165         } else if (definition.additionalProperties) {
166             return new SwaggerModelMap(definition);
167         } else {
168             return new SwaggerModel();
169         }
170     } else {
171         return new SwaggerPrimitiveObject(definition);
172     }
173 }
174
175 export class SwaggerSchemaObject {
176
177 }
178
179 export class SwaggerReferenceObject extends SwaggerSchemaObject {
180     public $ref: string;
181
182     constructor({ $ref }) {
183         super();
184         this.$ref = $ref;
185     }
186 }
187
188 export class SwaggerPrimitiveObject extends SwaggerSchemaObject {
189     public collectionFormat: string;
190     public defaultValue: any;
191     public enumValues: any[];
192     public exclusiveMaximum: boolean;
193     public exclusiveMinimum: boolean;
194     public format: string;
195     public maximum: number;
196     public maxLength: number;
197     public minimum: number;
198     public minLength: number;
199     public multipleOf: number;
200     public pattern: string;
201     public type: string;
202
203     constructor(options: any) {
204         super();
205         this.collectionFormat = options.collectionFormat;
206         this.defaultValue = options.default;
207         this.enumValues = options.enum;
208         this.exclusiveMaximum = options.exclusiveMaximum;
209         this.exclusiveMinimum = options.exclusiveMinimum;
210         this.format = options.format;
211         this.maximum = options.maximum;
212         this.maxLength = options.maxLength;
213         this.minimum = options.minimum;
214         this.minLength = options.minLength;
215         this.multipleOf = options.multipleOf;
216         this.pattern = options.pattern;
217         this.type = options.type;
218     }
219 }
220
221 export class SwaggerModel extends SwaggerSchemaObject {
222     public type = 'object';
223 }
224
225 export class SwaggerModelSimple extends SwaggerModel {
226     public properties = {};
227     public required = [];
228
229     constructor(options: any) {
230         super();
231         this.required = options.required;
232         for (const key in options.properties) {
233             this.properties[key] = getSchemaObject(options.properties[key]);
234         }
235     }
236 }
237
238 export class SwaggerModelMap extends SwaggerModel {
239     public additionalProperties: SwaggerSchemaObject;
240
241     constructor(options: any) {
242         super();
243         this.additionalProperties = getSchemaObject(options.additionalProperties);
244     }
245 }
246
247 export class SwaggerModelArray extends SwaggerSchemaObject {
248     public type = 'array';
249     public items: SwaggerSchemaObject;
250
251     constructor(options: any) {
252         super();
253         this.items = getSchemaObject(options.items);
254     }
255 }