UNPKG

@opra/common

Version:
63 lines (62 loc) 2.16 kB
import { omitUndefined } from '@jsopen/objects'; import { DocumentElement } from '../common/document-element.js'; import { DataType } from '../data-type/data-type.js'; /** * @class RpcOperationResponse */ export class RpcOperationResponse extends DocumentElement { constructor(owner, initArgs) { super(owner); this.headers = []; this.channel = initArgs?.channel; this.description = initArgs?.description; if (initArgs?.payloadType) { this.payloadType = initArgs?.payloadType instanceof DataType ? initArgs.payloadType : this.owner.node.getDataType(initArgs.payloadType); } else this.payloadType = this.owner.node.getDataType('any'); if (initArgs?.keyType) { this.keyType = initArgs?.keyType instanceof DataType ? initArgs.keyType : this.owner.node.getDataType(initArgs.keyType); } } findHeader(paramName) { const paramNameLower = paramName.toLowerCase(); let prm; for (prm of this.headers) { if (typeof prm.name === 'string') { prm._nameLower = prm._nameLower || prm.name.toLowerCase(); if (prm._nameLower === paramNameLower) return prm; } if (prm.name instanceof RegExp && prm.name.test(paramName)) return prm; } } toJSON() { const out = omitUndefined({ description: this.description, channel: this.channel, payloadType: this.payloadType.name ? this.payloadType.name : this.payloadType.toJSON(), keyType: this.keyType ? this.keyType.name ? this.keyType.name : this.keyType.toJSON() : undefined, }); if (this.headers.length) { out.headers = []; for (const prm of this.headers) { out.headers.push(prm.toJSON()); } } return out; } }