From b30bf9840270b5ea54a90d0cd1d650a7bc17a54e Mon Sep 17 00:00:00 2001 From: Roman Gotsiy Date: Sat, 10 Oct 2015 16:01:41 +0300 Subject: [PATCH] Added simple paramaters list --- lib/components/Method/method.html | 1 + lib/components/Method/method.js | 4 +- lib/components/MethodsList/methods-list.css | 5 ++ lib/components/MethodsList/methods-list.js | 1 + lib/components/ParamsList/params-list.css | 24 +++++++++ lib/components/ParamsList/params-list.html | 18 +++++++ lib/components/ParamsList/params-list.js | 59 +++++++++++++++++++++ lib/utils/JsonPointer.js | 28 +++++++++- lib/utils/SchemaManager.js | 6 ++- 9 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 lib/components/MethodsList/methods-list.css create mode 100644 lib/components/ParamsList/params-list.css create mode 100644 lib/components/ParamsList/params-list.html create mode 100644 lib/components/ParamsList/params-list.js diff --git a/lib/components/Method/method.html b/lib/components/Method/method.html index 79e42085..bf378160 100644 --- a/lib/components/Method/method.html +++ b/lib/components/Method/method.html @@ -6,3 +6,4 @@

{{data.methodInfo.description}}

+ diff --git a/lib/components/Method/method.js b/lib/components/Method/method.js index 780a9b81..58e29f9f 100644 --- a/lib/components/Method/method.js +++ b/lib/components/Method/method.js @@ -2,11 +2,13 @@ import {JsonPointer} from '../../utils/JsonPointer'; import {RedocComponent, BaseComponent} from '../base'; +import {ParamsList} from '../ParamsList/params-list'; @RedocComponent({ selector: 'method', templateUrl: './lib/components/Method/method.html', - styleUrls: ['./lib/components/Method/method.css'] + styleUrls: ['./lib/components/Method/method.css'], + directives: [ParamsList] }) export class Method extends BaseComponent { constructor(schemaMgr) { diff --git a/lib/components/MethodsList/methods-list.css b/lib/components/MethodsList/methods-list.css new file mode 100644 index 00000000..35b9afdf --- /dev/null +++ b/lib/components/MethodsList/methods-list.css @@ -0,0 +1,5 @@ +method { + padding-bottom: 50px; + display: block; + border-bottom: 1px solid silver; +} diff --git a/lib/components/MethodsList/methods-list.js b/lib/components/MethodsList/methods-list.js index 151058b1..60364f56 100644 --- a/lib/components/MethodsList/methods-list.js +++ b/lib/components/MethodsList/methods-list.js @@ -7,6 +7,7 @@ import {Method} from '../Method/method'; @RedocComponent({ selector: 'methods-list', templateUrl: './lib/components/MethodsList/methods-list.html', + styleUrls: ['./lib/components/MethodsList/methods-list.css'], directives: [Method] }) export class MethodsList extends BaseComponent { diff --git a/lib/components/ParamsList/params-list.css b/lib/components/ParamsList/params-list.css new file mode 100644 index 00000000..febf6df3 --- /dev/null +++ b/lib/components/ParamsList/params-list.css @@ -0,0 +1,24 @@ +h4 { + font-size: 16px; + font-weight: 200; + color: black; +} + +.param { + padding: 10px 0; +} + +.param > span { + padding: 5px 10px; + vertical-align: middle; +} + +.param-name { + font-weight: bold; +} + +.param-type { + padding: 2px 10px; + background-color: #EAEAEA; + border: silver 1px solid; +} diff --git a/lib/components/ParamsList/params-list.html b/lib/components/ParamsList/params-list.html new file mode 100644 index 00000000..8750f4a8 --- /dev/null +++ b/lib/components/ParamsList/params-list.html @@ -0,0 +1,18 @@ +

Parameters

+ No parameters +
+
+ {{param.name}} + {{param.type}} + {{param.description}} +
+
+
+ {{data.bodyParam.name}} + {{data.bodyParam.type}} + + {{data.bodyParam.description}} + + Body Schema would be somewhere here (unimplemented yet) +
diff --git a/lib/components/ParamsList/params-list.js b/lib/components/ParamsList/params-list.js new file mode 100644 index 00000000..c4dd89e9 --- /dev/null +++ b/lib/components/ParamsList/params-list.js @@ -0,0 +1,59 @@ +'use strict'; + +import {JsonPointer} from '../../utils/JsonPointer'; +import {RedocComponent, BaseComponent} from '../base'; + +@RedocComponent({ + selector: 'params-list', + templateUrl: './lib/components/ParamsList/params-list.html', + styleUrls: ['./lib/components/ParamsList/params-list.css'] +}) +export class ParamsList extends BaseComponent { + constructor(schemaMgr) { + super(schemaMgr); + } + + prepareModel() { + this.data = {}; + let params = this.componentSchema; + let pathParams = this.getPathParams(); + if (pathParams) params.concat(pathParams); + this.sortParams(params); + + // temporary hanlde body param + if (params.length && params[params.length - 1].in === 'body') { + let bodyParam = params.pop(); + bodyParam.type = bodyParam.schema.type + || `Object(${JsonPointer.baseName(bodyParam.schema.$ref)})`; + this.data.bodyParam = bodyParam; + } + + this.data.noParams = !(params.length || this.data.bodyParam); + this.data.params = params; + } + + getPathParams() { + let ptr = JsonPointer.dirName(this.pointer, 2) + '/parameters'; + let pathParams = this.schemaMgr.byPointer(ptr); + if (Array.isArray(pathParams)) { + return pathParams; + } + if (pathParams && pathParams.$ref) { + return this.schemaMgr.byPointer(pathParams.$ref); + } + + return []; + } + + sortParams(params) { + const sortOrder = { + 'path' : 0, + 'query' : 10, + 'formData' : 20, + 'header': 40, + 'body': 50 + }; + + params.sort((a, b) => sortOrder[a] - sortOrder[b]); + } +} diff --git a/lib/utils/JsonPointer.js b/lib/utils/JsonPointer.js index a6775c37..96337138 100644 --- a/lib/utils/JsonPointer.js +++ b/lib/utils/JsonPointer.js @@ -5,7 +5,6 @@ import JsonPointerLib from 'json-pointer'; * Wrapper for JsonPointer. Provides common operations */ export class JsonPointer extends JsonPointerLib { - /** * returns last JsonPointer token * if level > 1 returns levels last (second last/third last) @@ -19,6 +18,31 @@ export class JsonPointer extends JsonPointerLib { let tokens = JsonPointer.parse(pointer); return tokens[tokens.length - (level)]; } -} + /** + * returns dirname of pointer + * if level > 1 returns corresponding dirname in the hierarchy + * @example + * // returns /path/0 + * JsonPointerHelper.dirName('/path/0/subpath') + * // returns /path + * JsonPointerHelper.dirName('/path/foo/subpath', 2) + */ + static dirName(pointer, level=1) { + let tokens = JsonPointer.parse(pointer); + return JsonPointer.compile(tokens.slice(0, tokens.length - level)); + } + + /** + * overridden JsonPointer original parse to take care of prefixing '#' symbol + * that is not valid JsonPointer + */ + static parse(pointer) { + let ptr = pointer; + if (ptr.charAt(0) === '#') { + ptr = ptr.substring(1); + } + return JsonPointerLib.parse(ptr); + } +} export default JsonPointer; diff --git a/lib/utils/SchemaManager.js b/lib/utils/SchemaManager.js index d1135ab9..33f6ed3c 100644 --- a/lib/utils/SchemaManager.js +++ b/lib/utils/SchemaManager.js @@ -40,7 +40,11 @@ export class SchemaManager { } byPointer(pointer) { - return JsonPointer.get(this._schema, pointer); + let res = null; + try { + res = JsonPointer.get(this._schema, pointer); + } catch(e) {/*skip*/ } + return res; } }