redoc/lib/components/ResponsesList/responses-list.ts

74 lines
2.0 KiB
TypeScript
Raw Normal View History

2015-10-21 12:20:14 +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';
2016-05-09 22:55:16 +03:00
import { statusCodeType } from '../../utils/helpers';
2016-05-06 12:46:41 +03:00
import { OptionsService } from '../../services/index';
import { SchemaHelper } from '../../services/schema-helper.service';
2015-10-21 12:20:14 +03:00
function isNumeric(n) {
return (!isNaN(parseFloat(n)) && isFinite(n));
}
2016-08-22 12:12:13 +03:00
@Component({
2015-10-21 12:20:14 +03:00
selector: 'responses-list',
templateUrl: './responses-list.html',
styleUrls: ['./responses-list.css'],
changeDetection: ChangeDetectionStrategy.OnPush
2015-10-21 12:20:14 +03:00
})
2016-08-28 21:46:10 +03:00
export class ResponsesList extends BaseComponent implements OnInit {
2016-08-22 12:12:13 +03:00
@Input() pointer:string;
responses: Array<any>;
options: any;
2016-08-22 12:12:13 +03:00
constructor(specMgr:SpecManager, optionsMgr:OptionsService) {
super(specMgr);
2016-02-16 16:31:12 +03:00
this.options = optionsMgr.options;
2015-10-21 12:20:14 +03:00
}
init() {
this.responses = [];
2015-10-21 12:20:14 +03:00
let responses = this.componentSchema;
if (!responses) return;
2015-10-21 12:20:14 +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-21 12:20:14 +03:00
resp.pointer = ref;
}
resp.empty = !resp.schema;
2015-10-21 12:20:14 +03:00
resp.code = respCode;
2015-11-23 23:57:49 +03:00
resp.type = statusCodeType(resp.code);
2016-06-30 16:42:36 +03:00
if (resp.headers && !(resp.headers instanceof Array)) {
2015-11-26 01:36:07 +03:00
resp.headers = Object.keys(resp.headers).map((k) => {
let respInfo = resp.headers[k];
respInfo.name = k;
return SchemaHelper.preprocess(respInfo, this.pointer, this.pointer);
2015-11-26 01:36:07 +03:00
});
resp.empty = false;
2015-11-26 01:36:07 +03:00
}
resp.extendable = resp.headers || resp.length;
2015-10-21 12:20:14 +03:00
return resp;
});
this.responses = responses;
2015-10-21 12:20:14 +03:00
}
2016-06-30 16:42:36 +03:00
2016-12-02 12:59:29 +03:00
trackByCode(_, el) {
2016-06-30 16:42:36 +03:00
return el.code;
}
2016-08-28 21:46:10 +03:00
ngOnInit() {
this.preinit();
}
2015-10-21 12:20:14 +03:00
}