redoc/lib/components/ResponsesSamples/responses-samples.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

2015-10-22 20:53:07 +03:00
'use strict';
2016-05-06 00:48:41 +03:00
import { forwardRef } from '@angular/core';
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-06 00:48:41 +03:00
import { Tabs, Tab } from '../../shared/components/index';
import { SchemaSample } from '../index';
import { statusCodeType } from '../../utils/helpers';
2015-10-22 20:53:07 +03:00
function isNumeric(n) {
return (!isNaN(parseFloat(n)) && isFinite(n));
}
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: './responses-samples.html',
styleUrls: ['./responses-samples.css'],
2016-05-06 00:48:41 +03:00
directives: [forwardRef( ()=> SchemaSample), Tabs, Tab]
2015-10-22 20:53:07 +03:00
})
2016-05-06 00:48:41 +03:00
export class ResponsesSamples extends BaseComponent {
data: any;
constructor(specMgr:SpecManager) {
super(specMgr);
2015-10-22 20:53:07 +03:00
}
init() {
2015-10-22 20:53:07 +03:00
this.data = {};
this.data.responses = [];
2015-10-22 20:53:07 +03:00
let responses = this.componentSchema;
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.specMgr.byPointer(resp.$ref);
2015-10-22 20:53:07 +03:00
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;
})
.filter(response => hasExample(response));
2015-10-22 20:53:07 +03:00
this.data.responses = responses;
}
}