2015-10-07 12:47:57 +03:00
|
|
|
'use strict';
|
|
|
|
import SwaggerParser from 'swagger-parser';
|
2015-10-09 10:50:02 +03:00
|
|
|
import JsonPointer from './JsonPointer';
|
2015-10-18 19:32:11 +03:00
|
|
|
import {methods as swaggerMethods} from './swagger-defs';
|
2015-10-07 12:47:57 +03:00
|
|
|
|
|
|
|
export class SchemaManager {
|
|
|
|
constructor() {
|
|
|
|
if (SchemaManager.prototype._instance) {
|
|
|
|
return SchemaManager.prototype._instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
SchemaManager.prototype._instance = this;
|
|
|
|
|
|
|
|
this._schema = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
static instance() {
|
|
|
|
return new SchemaManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
load(url) {
|
|
|
|
let promise = new Promise((resolve, reject) => {
|
|
|
|
this._schema = {};
|
|
|
|
|
|
|
|
SwaggerParser.bundle(url)
|
|
|
|
.then(
|
|
|
|
(schema) => {
|
|
|
|
this._schema = schema;
|
|
|
|
resolve(this._schema);
|
|
|
|
},
|
|
|
|
(err) => reject(err)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
get schema() {
|
|
|
|
// TODO: consider returning promise
|
|
|
|
return this._schema;
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:21:51 +03:00
|
|
|
byPointer(pointer) {
|
2015-10-10 16:01:41 +03:00
|
|
|
let res = null;
|
|
|
|
try {
|
|
|
|
res = JsonPointer.get(this._schema, pointer);
|
|
|
|
} catch(e) {/*skip*/ }
|
|
|
|
return res;
|
2015-10-07 12:47:57 +03:00
|
|
|
}
|
2015-10-08 23:21:51 +03:00
|
|
|
|
2015-10-18 19:32:11 +03:00
|
|
|
/* returns ES6 Map */
|
|
|
|
buildMenuTree() {
|
|
|
|
let tag2MethodMapping = new Map();
|
|
|
|
let paths = this._schema.paths;
|
|
|
|
for (let path of Object.keys(paths)) {
|
|
|
|
let methods = Object.keys(paths[path]).filter((k) => swaggerMethods.has(k));
|
|
|
|
for (let method of methods) {
|
|
|
|
let methodInfo = paths[path][method];
|
|
|
|
let tags = methodInfo.tags;
|
|
|
|
|
|
|
|
//TODO: mb need to do something cleverer
|
|
|
|
if (!tags || !tags.length) {
|
|
|
|
tags = ['[Other]'];
|
|
|
|
}
|
|
|
|
let methodPointer = JsonPointer.compile(['paths', path, method]);
|
|
|
|
let methodSummary = methodInfo.summary;
|
|
|
|
for (let tag of tags) {
|
|
|
|
let tagMethods = tag2MethodMapping.get(tag);
|
|
|
|
if (!tagMethods) {
|
|
|
|
tagMethods = [];
|
|
|
|
tag2MethodMapping.set(tag, tagMethods);
|
|
|
|
}
|
|
|
|
|
|
|
|
tagMethods.push({pointer: methodPointer, summary: methodSummary});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tag2MethodMapping;
|
|
|
|
}
|
|
|
|
|
2015-10-07 12:47:57 +03:00
|
|
|
}
|