UNPKG

ts-japi

Version:

A highly-modular (typescript-friendly)-framework agnostic library for serializing data to the JSON:API specification

94 lines 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const is_object_1 = require("../utils/is-object"); class JapiError { /** * Tests whether `error` has similar attributes to a JapiError * * @param error - An unknown object */ static isLikeJapiError(error) { if (!(0, is_object_1.isObject)(error)) { return false; } const hasErrorKeys = [ "id", "status", "code", "title", "detail", "source", "links", "meta", ].some((attrName) => attrName in error); const expectedStringKeys = ["id", "status", "code", "title", "detail"].every((attrName) => !(attrName in error) || error[attrName] === undefined || typeof error[attrName] === "string"); const expectedObjectKeys = ["source", "links", "meta"].every((attrName) => !(attrName in error) || error[attrName] === undefined || (0, is_object_1.isObject)(error[attrName])); return (hasErrorKeys && [expectedStringKeys, expectedObjectKeys].every((v) => v)); } /** @internal */ stack; /** * A unique identifier for this particular occurrence of the problem. */ id; /** * The HTTP status code applicable to this problem, expressed as a string * value. */ status; /** * An application-specific error code, expressed as a string value. */ code; /** * A short, human-readable summary of the problem that SHOULD NOT change from * occurrence to occurrence of the problem, except for purposes of * localization. */ title; /** * A human-readable explanation specific to this occurrence of the problem. * Like title, this field’s value can be localized. */ detail; /** * An object containing references to the source of the error, optionally * including any of the following members. */ source; /** * A links object */ links; /** * A meta object containing non-standard meta information about the error. */ meta; constructor(options = {}) { if (options.id) { this.id = options.id; } if (options.status) { this.status = options.status.toString(); } if (options.code) { this.code = options.code; } if (options.title) { this.title = options.title; } if (options.detail) { this.detail = options.detail; } if (options.source) { this.source = options.source; } Error.captureStackTrace(this, Error); } } exports.default = JapiError; //# sourceMappingURL=error.model.js.map