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

63 lines
1.5 KiB
TypeScript
Raw Normal View History

2015-10-22 20:53:07 +03:00
'use strict';
2016-08-28 21:46:10 +03:00
import { Component, Input, OnInit } 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';
2016-05-06 00:48:41 +03:00
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);
}
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'],
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;
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;
}
2016-08-28 21:46:10 +03:00
ngOnInit() {
this.preinit();
}
2015-10-22 20:53:07 +03:00
}