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

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2015-10-22 20:53:07 +03:00
'use strict';
import { Component, Input, OnInit, ChangeDetectionStrategy } from '@angular/core';
2016-08-22 12:12:13 +03:00
import { BaseComponent, SpecManager } from '../base';
2015-10-27 20:44:08 +03:00
import JsonPointer from '../../utils/JsonPointer';
import { statusCodeType, getJsonLikeSample, getXmlLikeSample } 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.schema || getXmlLikeSample(response.examples) || getJsonLikeSample(response.examples)
}
2016-08-22 12:12:13 +03:00
@Component({
2015-10-22 20:53:07 +03:00
selector: 'responses-samples',
templateUrl: './responses-samples.html',
styleUrls: ['./responses-samples.css'],
changeDetection: ChangeDetectionStrategy.OnPush
2015-10-22 20:53:07 +03:00
})
2016-08-28 21:46:10 +03:00
export class ResponsesSamples extends BaseComponent implements OnInit {
2016-08-22 12:12:13 +03:00
@Input() pointer:string;
data: any;
2016-08-22 12:12:13 +03:00
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;
let hasSuccessResponses = false;
2015-10-22 20:53:07 +03:00
responses = Object.keys(responses).filter(respCode => {
if ((parseInt(respCode) >= 100) && (parseInt(respCode) <=399)) {
hasSuccessResponses = true;
}
2015-10-22 20:53:07 +03:00
// 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;
resp.type = statusCodeType(resp.code, hasSuccessResponses);
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;
}
2016-08-28 21:46:10 +03:00
ngOnInit() {
this.preinit();
}
2015-10-22 20:53:07 +03:00
}