2015-10-22 20:53:07 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import {RedocComponent, BaseComponent} from '../base';
|
2015-10-27 20:44:08 +03:00
|
|
|
import JsonPointer from '../../utils/JsonPointer';
|
2015-11-23 22:54:20 +03:00
|
|
|
import {Tabs, Tab} from '../../common/components/Tabs/tabs';
|
2015-10-27 20:44:08 +03:00
|
|
|
import SchemaSample from '../SchemaSample/schema-sample';
|
2015-11-23 23:27:55 +03:00
|
|
|
import {statusCodeType} from '../../utils/helpers';
|
2015-10-22 20:53:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
function isNumeric(n) {
|
|
|
|
return (!isNaN(parseFloat(n)) && isFinite(n));
|
|
|
|
}
|
|
|
|
|
2015-10-22 21:31:12 +03:00
|
|
|
function hasExample(response) {
|
|
|
|
return ((response.examples && response.examples['application/json']) ||
|
|
|
|
response.schema);
|
|
|
|
}
|
|
|
|
|
2015-10-22 20:53:07 +03:00
|
|
|
@RedocComponent({
|
|
|
|
selector: 'responses-samples',
|
|
|
|
templateUrl: './lib/components/ResponsesSamples/responses-samples.html',
|
|
|
|
styleUrls: ['./lib/components/ResponsesSamples/responses-samples.css'],
|
|
|
|
directives: [SchemaSample, Tabs, Tab]
|
|
|
|
})
|
2015-10-27 20:44:08 +03:00
|
|
|
export default class ResponsesSamples extends BaseComponent {
|
2015-10-22 20:53:07 +03:00
|
|
|
constructor(schemaMgr) {
|
|
|
|
super(schemaMgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareModel() {
|
|
|
|
this.data = {};
|
2015-12-19 18:18:48 +03:00
|
|
|
this.data.responses = [];
|
|
|
|
|
2015-10-22 20:53:07 +03:00
|
|
|
let responses = this.componentSchema;
|
2015-12-19 18:18:48 +03:00
|
|
|
if (!responses) return;
|
|
|
|
|
2015-10-22 20:53:07 +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:27:55 +03:00
|
|
|
resp.type = statusCodeType(resp.code);
|
2015-10-22 20:53:07 +03:00
|
|
|
return resp;
|
2015-10-22 21:31:12 +03:00
|
|
|
})
|
|
|
|
.filter(response => hasExample(response));
|
2015-10-22 20:53:07 +03:00
|
|
|
this.data.responses = responses;
|
|
|
|
}
|
|
|
|
}
|