ts-japi
Version:
A highly-modular (typescript-friendly)-framework agnostic library for serializing data to the JSON:API specification
137 lines • 4.74 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const error_model_1 = __importDefault(require("../models/error.model"));
const merge_1 = __importDefault(require("../utils/merge"));
/**
* The {@link ErrorSerializer} class is used to serialize errors.
*
* Example:
* ```typescript
* [[include:error-serializer.example.ts]]
* ```
*/
class ErrorSerializer {
/**
* Default options. Can be edited to change default options globally.
*/
static defaultOptions = {
version: '1.0',
attributes: {
id: 'id',
status: 'code',
code: 'name',
title: 'reason',
detail: 'message',
source: {
pointer: 'location',
parameter: undefined,
header: undefined,
},
},
metaizers: {},
linkers: {},
};
/**
* The set of options for the serializer.
*/
options;
/**
* Creates a {@link Serializer}.
*
* @param collectionName - The name of the collection of objects.
* @param options - Options for the serializer.
*/
constructor(options = {}) {
// Setting default options.
this.options = (0, merge_1.default)({}, ErrorSerializer.defaultOptions, options);
}
/**
* The actual serialization function.
*
* @param errors - Errors to serialize.
* @param options - Options to use at runtime.
*/
serialize(errors, options) {
// Get options.
let o = this.options;
if (options)
o = (0, merge_1.default)({}, this.options, options);
const attributes = o.attributes;
const linkers = o.linkers;
const metaizers = o.metaizers;
const version = o.version;
const document = { errors: [] };
// Normalize error input
if (!Array.isArray(errors)) {
errors = [errors];
}
document.errors = errors.map((e) => {
if (e instanceof error_model_1.default)
return e;
const eo = {};
if (attributes.id && e[attributes.id]) {
eo.id = String(e[attributes.id]);
}
if (attributes.status && e[attributes.status]) {
eo.status = String(e[attributes.status]);
}
if (attributes.code && e[attributes.code]) {
eo.code = String(e[attributes.code]);
}
if (attributes.title && e[attributes.title]) {
eo.title = String(e[attributes.title]);
}
if (attributes.detail && e[attributes.detail]) {
eo.detail = String(e[attributes.detail]);
}
if (attributes.source) {
eo.source = {};
if (attributes.source.pointer && e[attributes.source.pointer]) {
eo.source.pointer = String(e[attributes.source.pointer]);
}
if (attributes.source.parameter && e[attributes.source.parameter]) {
eo.source.parameter = String(e[attributes.source.parameter]);
}
if (attributes.source.header && e[attributes.source.header]) {
eo.source.header = String(e[attributes.source.header]);
}
if (Object.keys(eo.source).length === 0) {
delete eo.source;
}
}
return new error_model_1.default(eo);
});
// Constructing base document.
if (version) {
document.jsonapi = { version };
}
// Handling document metadata.
if (metaizers.jsonapi) {
document.jsonapi = { ...document.jsonapi, meta: metaizers.jsonapi.metaize() };
}
if (metaizers.document) {
document.meta = metaizers.document.metaize(document.errors);
}
if (metaizers.error) {
for (const error of document.errors) {
error.meta = metaizers.error.metaize(error);
}
}
// Handling links.
if (Object.keys(linkers).length > 0) {
for (const [key, linker] of Object.entries(linkers)) {
if (linker) {
for (const error of document.errors) {
error.links = { ...error.links, [key]: linker.link(error) };
}
}
}
}
return document;
}
}
exports.default = ErrorSerializer;
//# sourceMappingURL=error-serializer.js.map