@opra/common
Version:
Opra common package
47 lines (46 loc) • 1.61 kB
JavaScript
import { omitUndefined } from '@jsopen/objects';
import { HttpMediaType } from './http-media-type.js';
import { HttpStatusRange } from './http-status-range.js';
/**
* @class HttpOperationResponse
*/
export class HttpOperationResponse extends HttpMediaType {
statusCode;
parameters;
partial;
constructor(owner, init) {
super(owner, init);
this.parameters = [];
this.statusCode = (Array.isArray(init.statusCode) ? init.statusCode : [init.statusCode]).map(x => typeof x === 'object'
? new HttpStatusRange(x.start, x.end)
: new HttpStatusRange(x));
this.partial = init.partial;
}
findParameter(paramName, location) {
paramName = paramName.toLowerCase();
for (const prm of this.parameters) {
if ((!location || location === prm.location) &&
((prm.name instanceof RegExp && prm.name.test(paramName)) ||
prm.name === paramName)) {
return prm;
}
}
}
toJSON(options) {
const statusCode = this.statusCode.map(x => x.toJSON());
const out = omitUndefined({
...super.toJSON(options),
statusCode: statusCode.length === 1 && typeof statusCode[0] === 'number'
? statusCode[0]
: statusCode,
partial: this.partial,
});
if (this.parameters.length) {
out.parameters = [];
for (const prm of this.parameters) {
out.parameters.push(prm.toJSON(options));
}
}
return out;
}
}