UNPKG

@tree-house/serializer

Version:
35 lines (34 loc) 1.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorSerializer = void 0; const lodash_1 = require("lodash"); function constructError(data) { const requiredProps = ['title', 'status']; const optionalProps = ['id', 'code', 'detail', 'url', 'meta']; requiredProps.forEach((prop) => { if (!Object.keys(data).includes(prop)) { throw new Error(`ErrorSerializer requires property ${prop}`); } }); return { ...(0, lodash_1.pick)(data, requiredProps), ...(0, lodash_1.pick)(data, optionalProps), }; } function constructErrors(data) { if ((0, lodash_1.isPlainObject)(data)) { return [constructError(data)]; } return data.map(constructError); } class ErrorSerializer { constructor() { throw new Error('ErrorSerializer instances are not implemented'); } static serialize(data) { return { errors: constructErrors(data), }; } } exports.ErrorSerializer = ErrorSerializer;