UNPKG

@pokt-network/pocket-js

Version:

Pocket-js core package with the main functionalities to interact with the Pocket Network.

73 lines 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Event = void 0; /** * * * @class Event */ var Event = /** @class */ (function () { /** * Creates a Event. * @constructor * @param {string} type - * @param {IKVPair} attributes - */ function Event(type, attributes) { this.type = type; this.attributes = attributes; if (!this.isValid()) { throw new TypeError("Invalid Event properties."); } } /** * * Creates a Event object using a JSON string * @param {String} json - JSON string. * @returns {Event} - Event object. * @memberof Event */ Event.fromJSON = function (json) { try { var jsonObject = JSON.parse(json); var attributesArray_1 = []; if (jsonObject.attributes) { jsonObject.attributes.forEach(function (attribute) { var key = Buffer.from(attribute.key, 'base64').toString('ascii'); var value = Buffer.from(attribute.value, 'base64').toString('ascii'); var kvPair = { "key": key, "value": value }; attributesArray_1.push(kvPair); }); } return new Event(jsonObject.type, attributesArray_1); } catch (error) { throw error; } }; /** * * Creates a JSON object with the Event properties * @returns {JSON} - JSON Object. * @memberof Event */ Event.prototype.toJSON = function () { return { type: this.type, attributes: this.attributes, }; }; /** * * Verify if all properties are valid * @returns {boolean} - True or false. * @memberof Event */ Event.prototype.isValid = function () { return this.type.length > 0 && this.attributes !== undefined; }; return Event; }()); exports.Event = Event; //# sourceMappingURL=event.js.map