2015-10-22 20:53:07 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-08-30 17:07:54 +03:00
|
|
|
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';
|
2017-05-12 11:03:14 +03:00
|
|
|
import { statusCodeType, getJsonLikeSample } 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) {
|
2017-05-12 11:03:14 +03:00
|
|
|
return ((response.examples && getJsonLikeSample(response.examples)) ||
|
2015-10-22 21:31:12 +03:00
|
|
|
response.schema);
|
|
|
|
}
|
|
|
|
|
2016-08-22 12:12:13 +03:00
|
|
|
@Component({
|
2015-10-22 20:53:07 +03:00
|
|
|
selector: 'responses-samples',
|
2016-05-25 18:34:31 +03:00
|
|
|
templateUrl: './responses-samples.html',
|
|
|
|
styleUrls: ['./responses-samples.css'],
|
2016-08-30 17:07:54 +03:00
|
|
|
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;
|
|
|
|
|
2016-05-25 18:34:31 +03:00
|
|
|
data: any;
|
2016-08-22 12:12:13 +03:00
|
|
|
|
2016-06-23 17:36:38 +03:00
|
|
|
constructor(specMgr:SpecManager) {
|
|
|
|
super(specMgr);
|
2015-10-22 20:53:07 +03:00
|
|
|
}
|
|
|
|
|
2016-07-20 11:07:08 +03:00
|
|
|
init() {
|
2015-10-22 20:53:07 +03:00
|
|
|
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;
|
|
|
|
|
2017-02-26 01:37:57 +03:00
|
|
|
let hasSuccessResponses = false;
|
2015-10-22 20:53:07 +03:00
|
|
|
responses = Object.keys(responses).filter(respCode => {
|
2017-02-26 01:37:57 +03:00
|
|
|
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;
|
2016-06-23 17:36:38 +03:00
|
|
|
resp = this.specMgr.byPointer(resp.$ref);
|
2015-10-22 20:53:07 +03:00
|
|
|
resp.pointer = ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.code = respCode;
|
2017-02-26 01:37:57 +03:00
|
|
|
resp.type = statusCodeType(resp.code, hasSuccessResponses);
|
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;
|
|
|
|
}
|
2016-08-28 21:46:10 +03:00
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.preinit();
|
|
|
|
}
|
2015-10-22 20:53:07 +03:00
|
|
|
}
|