redoc/lib/components/ResponsesList/responses-list.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2015-10-21 12:20:14 +03:00
'use strict';
import {RedocComponent, BaseComponent} from '../base';
2015-10-27 20:44:08 +03:00
import JsonPointer from '../../utils/JsonPointer';
2015-11-24 01:08:11 +03:00
import JsonSchema from '../JsonSchema/json-schema';
2015-12-19 15:16:17 +03:00
import Zippy from '../../common/components/Zippy/zippy';
2015-11-23 23:57:49 +03:00
import {statusCodeType} from '../../utils/helpers';
2015-10-21 12:20:14 +03:00
function isNumeric(n) {
return (!isNaN(parseFloat(n)) && isFinite(n));
}
@RedocComponent({
selector: 'responses-list',
templateUrl: './lib/components/ResponsesList/responses-list.html',
styleUrls: ['./lib/components/ResponsesList/responses-list.css'],
2015-11-24 01:08:11 +03:00
directives: [JsonSchema, Zippy]
2015-10-21 12:20:14 +03:00
})
2015-10-27 20:44:08 +03:00
export default class ResponsesList extends BaseComponent {
2015-10-21 12:20:14 +03:00
constructor(schemaMgr) {
super(schemaMgr);
}
prepareModel() {
this.data = {};
this.data.responses = [];
2015-10-21 12:20:14 +03:00
let responses = this.componentSchema;
if (!responses) return;
2015-10-21 12:20:14 +03:00
responses = Object.keys(responses).filter(respCode => {
// only response-codes and "default"
return ( isNumeric(respCode) || (respCode === 'default'));
}).map(respCode => {
let resp = responses[respCode];
resp.pointer = JsonPointer.join(this.pointer, respCode);
if (resp.$ref) {
let ref = resp.$ref;
resp = this.schemaMgr.byPointer(resp.$ref);
resp.pointer = ref;
}
resp.code = respCode;
2015-11-23 23:57:49 +03:00
resp.type = statusCodeType(resp.code);
2015-11-26 01:36:07 +03:00
if (resp.headers) {
resp.headers = Object.keys(resp.headers).map((k) => {
let respInfo = resp.headers[k];
respInfo.name = k;
return respInfo;
});
}
resp.extendable = resp.headers || resp.length;
2015-10-21 12:20:14 +03:00
return resp;
});
this.data.responses = responses;
}
}