import Validator from 'swagger-model-validator'; /** * GenericProtocol defines a simple passthru handler for messages * This can be extended to handle different schemas * * The assunption is that all messages conform to a single * wrapper schema definition. Different payloads are handled * by a oneOf definitions within the schema determined by a * field value. * * This class can be extended and overridden to handle * different schemas and to implement the client specific * logic related to be executed when invoked for a given * message type. * * By default encode and decode are just passthru stubs * * decode is invoked when receiving a message from the backbone * encode is invoked before delivering a message to the backbone * * The intention is that these allow you to transform the message * and call invoke internal functions and services as required */ export class GenericProtocol { constructor(schema, services) { this.schema = schema; this.validator = new Validator(schema); this.services = services; } /** * Validate that a message meets the reqiured schema * @param {object} message * @returns {object} */ validate(message) { return this.validator.validate( message, this.protocol.schema.components.schemas.Message, this.protocol.schema.components.schemas, false, true ); } /** * Identify the payload type from the message content * @param {object} message * @returns {string} */ getType(message) { return message.message_type; } /** * Invoked on receiving a message from the backbone * @param {string} type * @param {object} message * @returns {*} */ decode(type, message) { return message; } /** * Optionally invoked before delivering a message to the backbone * @param {string} type * @param {*} message * @returns {object} */ encode(type, message) { return message; } }