redoc/lib/components/ParamsList/params-list.ts

76 lines
2.5 KiB
TypeScript
Raw Normal View History

2015-10-10 16:01:41 +03:00
'use strict';
2016-06-22 21:17:48 +03:00
import { RedocComponent, BaseComponent, SpecManager } from '../base';
2016-05-09 22:55:16 +03:00
import { JsonSchema } from '../JsonSchema/json-schema';
2016-06-22 21:17:48 +03:00
import { JsonSchemaLazy } from '../JsonSchema/json-schema-lazy';
import { SchemaHelper } from '../../services/schema-helper.service';
2015-10-17 21:12:46 +03:00
function safePush(obj, prop, item) {
if (!obj[prop]) obj[prop] = [];
obj[prop].push(item);
}
2015-10-10 16:01:41 +03:00
@RedocComponent({
selector: 'params-list',
templateUrl: './params-list.html',
styleUrls: ['./params-list.css'],
2016-02-07 17:11:15 +03:00
directives: [JsonSchema, JsonSchemaLazy]
2015-10-10 16:01:41 +03:00
})
2016-05-06 00:48:41 +03:00
export class ParamsList extends BaseComponent {
2016-06-13 20:54:24 +03:00
data:any;
2016-06-13 20:54:24 +03:00
constructor(specMgr:SpecManager) {
super(specMgr);
2015-10-10 16:01:41 +03:00
}
prepareModel() {
this.data = {};
let paramsList = this.specMgr.getMethodParams(this.pointer, true);
2016-06-22 21:17:48 +03:00
paramsList = paramsList.map(paramSchema => {
let propPointer = paramSchema._pointer;
if (paramSchema.in === 'body') return paramSchema;
2016-06-25 13:02:13 +03:00
paramSchema._name = paramSchema.name;
return SchemaHelper.preprocess(paramSchema,propPointer, this.pointer);
});
2015-10-10 16:01:41 +03:00
let paramsMap = this.orderParams(paramsList);
if (paramsMap.body && paramsMap.body.length) {
let bodyParam = paramsMap.body[0];
2015-10-25 14:26:38 +03:00
bodyParam.pointer = bodyParam._pointer;
2015-10-10 16:01:41 +03:00
this.data.bodyParam = bodyParam;
paramsMap.body = undefined;
2015-10-10 16:01:41 +03:00
}
this.data.noParams = !(Object.keys(paramsMap).length || this.data.bodyParam);
2016-02-04 12:49:00 +03:00
let paramsPlaces = ['path', 'query', 'formData', 'header', 'body'];
let placeHint = {
path: `Used together with Path Templating, where the parameter value is actually part
of the operation's URL. This does not include the host or base path of the API.
For example, in /items/{itemId}, the path parameter is itemId`,
query: `Parameters that are appended to the URL.
For example, in /items?id=###, the query parameter is id`,
formData: `Parameters that are submitted through a form.
application/x-www-form-urlencoded, multipart/form-data or both are usually
used as the content type of the request`,
header: 'Custom headers that are expected as part of the request'
};
let params = [];
paramsPlaces.forEach(place => {
2016-02-23 14:09:31 +03:00
if (paramsMap[place] && paramsMap[place].length) {
params.push({place: place, placeHint: placeHint[place], params: paramsMap[place]});
2016-02-23 14:09:31 +03:00
}
});
2015-10-10 16:01:41 +03:00
this.data.params = params;
}
orderParams(params):any {
let res = {};
params.forEach((param) => safePush(res, param.in, param));
return res;
2015-10-10 16:01:41 +03:00
}
}