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

66 lines
1.9 KiB
TypeScript
Raw Normal View History

2015-10-21 12:20:14 +03:00
'use strict';
2016-06-22 21:17:48 +03:00
import {RedocComponent, BaseComponent, SpecManager} from '../base';
2015-10-27 20:44:08 +03:00
import JsonPointer from '../../utils/JsonPointer';
2016-05-09 22:55:16 +03:00
import { JsonSchema } from '../JsonSchema/json-schema';
import { JsonSchemaLazy } from '../JsonSchema/json-schema-lazy';
import { Zippy } from '../../shared/components/index';
import { statusCodeType } from '../../utils/helpers';
2016-05-06 12:46:41 +03:00
import { OptionsService } from '../../services/index';
2015-10-21 12:20:14 +03:00
function isNumeric(n) {
return (!isNaN(parseFloat(n)) && isFinite(n));
}
@RedocComponent({
selector: 'responses-list',
templateUrl: './responses-list.html',
styleUrls: ['./responses-list.css'],
2016-05-18 16:59:54 +03:00
directives: [JsonSchema, Zippy, JsonSchemaLazy],
detect: true
2015-10-21 12:20:14 +03:00
})
2016-05-06 00:48:41 +03:00
export class ResponsesList extends BaseComponent {
data: any;
options: any;
constructor(specMgr:SpecManager, optionsMgr:OptionsService) {
super(specMgr);
2016-02-16 16:31:12 +03:00
this.options = optionsMgr.options;
2015-10-21 12:20:14 +03:00
}
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.specMgr.byPointer(resp.$ref);
2015-10-21 12:20:14 +03:00
resp.pointer = ref;
}
resp.empty = !resp.schema;
2015-10-21 12:20:14 +03:00
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.empty = false;
2015-11-26 01:36:07 +03:00
}
resp.extendable = resp.headers || resp.length;
2015-10-21 12:20:14 +03:00
return resp;
});
this.data.responses = responses;
}
}