UNPKG

jinaga

Version:

Data management for web and mobile applications.

65 lines 2.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseLoadMessage = exports.parseSaveMessage = void 0; function parseFactReference(factReference) { if (typeof factReference !== 'object') throw new Error("Expected FactReference to be an object."); if (typeof factReference.type !== 'string') throw new Error("Expected a string 'type' property."); if (typeof factReference.hash !== 'string') throw new Error("Expected a string 'hash' property."); return { type: factReference.type, hash: factReference.hash }; } function parsePredecessor(predecessor) { if (Array.isArray(predecessor)) { return predecessor.map(parseFactReference); } else { return parseFactReference(predecessor); } } function parsePredecessorCollection(predecessors) { if (typeof predecessors !== 'object') throw new Error("Expected PredecessorCollection to be an object."); return Object.keys(predecessors).reduce((result, key) => (Object.assign(Object.assign({}, result), { [key]: parsePredecessor(predecessors[key]) })), {}); } function parseFactRecord(factRecord) { if (typeof factRecord !== 'object') throw new Error("Expected FactRecord to be an object."); if (typeof factRecord.type !== 'string') throw new Error("Expected a string 'type' property."); if (typeof factRecord.hash !== 'string') throw new Error("Expected a string 'hash' property."); if (typeof factRecord.fields !== 'object') throw new Error("Expected an object 'fields' property."); return { type: factRecord.type, hash: factRecord.hash, predecessors: parsePredecessorCollection(factRecord.predecessors), fields: factRecord.fields }; } function parseSaveMessage(message) { if (typeof message !== 'object') throw new Error("Expected an object. Check the content type of the request."); if (!Array.isArray(message.facts)) throw new Error("Expected an array 'facts' property."); return { facts: message.facts.map(parseFactRecord) }; } exports.parseSaveMessage = parseSaveMessage; function parseLoadMessage(message) { if (typeof message !== 'object') throw new Error("Expected an object. Check the content type of the request."); if (!Array.isArray(message.references)) throw new Error("Expected an array 'references' property."); return { references: message.references.map(parseFactReference) }; } exports.parseLoadMessage = parseLoadMessage; //# sourceMappingURL=messageParsers.js.map