@barchart/common-js
Version:
Library of common JavaScript utilities
246 lines (240 loc) • 7.82 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var FailureType_exports = {};
__export(FailureType_exports, {
default: () => FailureType
});
module.exports = __toCommonJS(FailureType_exports);
var assert = __toESM(require("./../../lang/assert.js"));
var is = __toESM(require("./../../lang/is.js"));
var import_Enum = __toESM(require("./../../lang/Enum.js"));
class FailureType extends import_Enum.default {
#template;
#severe;
#error;
#verbose;
/**
* @param {string} code - The enumeration code (and description).
* @param {string} template - The template string for formatting human-readable messages.
* @param {boolean=} severe - Indicates if the failure is severe (default is true).
* @param {number=} error - The HTTP error code which should be used as part of an HTTP response.
* @param {boolean=} verbose - Indicates if data object should be included when serialized.
*/
constructor(code, template, severe, error, verbose) {
super(code, code);
assert.argumentIsRequired(template, "template", String);
assert.argumentIsOptional(severe, "severe", Boolean);
assert.argumentIsOptional(error, "error", Number);
assert.argumentIsOptional(verbose, "verbose", Boolean);
this.#template = template;
if (is.boolean(severe)) {
this.#severe = severe;
} else {
this.#severe = true;
}
this.#error = error || null;
this.#verbose = verbose || false;
}
/**
* The template string for formatting human-readable messages.
*
* @public
* @returns {string}
*/
get template() {
return this.#template;
}
/**
* Indicates if the failure is serious.
*
* @public
* @return {boolean}
*/
get severe() {
return this.#severe;
}
/**
* The HTTP error code which should be used as part of an HTTP response.
*
* @public
* @return {number|null}
*/
get error() {
return this.#error;
}
/**
* Indicates if data object should be included when serialized.
*
* @public
* @return {boolean}
*/
get verbose() {
return this.#verbose;
}
/**
* One or more data points is missing.
*
* @public
* @static
* @returns {FailureType}
*/
static get REQUEST_CONSTRUCTION_FAILURE() {
return requestConstructionFailure;
}
/**
* A data point is missing.
*
* @public
* @static
* @returns {FailureType}
*/
static get REQUEST_PARAMETER_MISSING() {
return requestParameterMissing;
}
/**
* A data point is malformed.
*
* @public
* @static
* @returns {FailureType}
*/
static get REQUEST_PARAMETER_MALFORMED() {
return requestParameterMalformed;
}
/**
* User identity could not be determined.
*
* @public
* @static
* @returns {FailureType}
*/
static get REQUEST_IDENTITY_FAILURE() {
return requestIdentifyFailure;
}
/**
* User authorization failed.
*
* @public
* @static
* @returns {FailureType}
*/
static get REQUEST_AUTHORIZATION_FAILURE() {
return requestAuthorizationFailure;
}
/**
* The request data cannot be parsed or interpreted.
*
* @public
* @static
* @returns {FailureType}
*/
static get REQUEST_INPUT_MALFORMED() {
return requestInputMalformed;
}
/**
* The request failed for unspecified reasons.
*
* @public
* @static
* @returns {FailureType}
*/
static get SCHEMA_VALIDATION_FAILURE() {
return schemaValidationFailure;
}
/**
* The request failed for unspecified reasons.
*
* @public
* @static
* @returns {FailureType}
*/
static get REQUEST_GENERAL_FAILURE() {
return requestGeneralFailure;
}
/**
* Insufficient permission level to access the resource.
*
* @public
* @static
* @returns {FailureType}
*/
static get ENTITLEMENTS_FAILED() {
return entitlementsFailed;
}
/**
* Returns an HTTP status code that would be suitable for use with the
* failure type.
*
* @public
* @static
* @param {FailureType} type
* @returns {number}
*/
static getHttpStatusCode(type) {
assert.argumentIsRequired(type, "type", FailureType, "FailureType");
let returnVal;
if (type === FailureType.REQUEST_IDENTITY_FAILURE) {
returnVal = 401;
} else if (type === FailureType.REQUEST_AUTHORIZATION_FAILURE) {
returnVal = 403;
} else {
returnVal = 400;
}
return returnVal;
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return `[FailureType (code=${this.code})]`;
}
}
const requestConstructionFailure = new FailureType("REQUEST_CONSTRUCTION_FAILURE", "An attempt to {L|root.endpoint.description} failed because some required information is missing.");
const requestParameterMissing = new FailureType("REQUEST_PARAMETER_MISSING", 'The "{L|name}" field is required.');
const requestParameterMalformed = new FailureType("REQUEST_PARAMETER_MALFORMED", 'The "{L|name}" field cannot be interpreted.');
const requestIdentifyFailure = new FailureType("REQUEST_IDENTITY_FAILURE", "An attempt to {L|root.endpoint.description} failed because your identity could not be determined.");
const requestAuthorizationFailure = new FailureType("REQUEST_AUTHORIZATION_FAILURE", "An attempt to {L|root.endpoint.description} failed. You are not authorized to perform this action.");
const requestInputMalformed = new FailureType("REQUEST_INPUT_MALFORMED", "An attempt to {L|root.endpoint.description} failed, the data structure is invalid.");
const schemaValidationFailure = new FailureType("SCHEMA_VALIDATION_FAILURE", 'An attempt to read {U|schema} data failed (found "{L|key}" when expecting "{L|name}")');
const requestGeneralFailure = new FailureType("REQUEST_GENERAL_FAILURE", "An attempt to {L|root.endpoint.description} failed for unspecified reason(s).");
const entitlementsFailed = new FailureType("ENTITLEMENTS_FAILED", "Action blocked. The current user requires elevated permissions or the current user has exceeded a quota.", false, 403, true);
{
const cjsExports = module.exports;
const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports;
if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) {
Object.keys(cjsExports).forEach((key) => {
if (key !== 'default' && key !== '__esModule') {
cjsDefaultExport[key] = cjsExports[key];
}
});
}
module.exports = cjsDefaultExport;
}