@barchart/common-js
Version:
Library of common JavaScript utilities
173 lines (167 loc) • 5.36 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 ErrorInterceptor_exports = {};
__export(ErrorInterceptor_exports, {
default: () => ErrorInterceptor
});
module.exports = __toCommonJS(ErrorInterceptor_exports);
var assert = __toESM(require("./../../../lang/assert.js"));
var is = __toESM(require("./../../../lang/is.js"));
var import_FailureReason = __toESM(require("./../../failures/FailureReason.js"));
var import_FailureType = __toESM(require("./../../failures/FailureType.js"));
class ErrorInterceptor {
constructor() {
}
/**
* Adjusts incoming error before the response is forwarded
* back to the original caller.
*
* @public
* @async
* @param {object} error
* @param {Endpoint} endpoint - The endpoint which is originating the request.
* @returns {Promise<*>}
*/
async process(error, endpoint) {
return this._onProcess(error, endpoint);
}
/**
* @protected
* @async
* @param {object} error
* @param {Endpoint} endpoint
* @returns {Promise<*>}
*/
async _onProcess(error, endpoint) {
throw error;
}
/**
* A no-op error interceptor which rejects using raw response data.
*
* @public
* @static
* @returns {ErrorInterceptor}
*/
static get EMPTY() {
return errorInterceptorEmpty;
}
/**
* An error interceptor that handles most server-side issues and rejects
* using formatted {@link FailureReason} when an error is detected.
*
* @public
* @static
* @returns {ErrorInterceptor}
*/
static get GENERAL() {
return errorInterceptorGeneral;
}
/**
* Returns a new {@link ErrorInterceptor} which delegates its work to another function.
*
* @public
* @static
* @param {Function} delegate
* @returns {ErrorInterceptor}
*/
static fromDelegate(delegate) {
return new DelegateErrorInterceptor(delegate);
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return "[ErrorInterceptor]";
}
}
class DelegateErrorInterceptor extends ErrorInterceptor {
#delegate;
/**
* @param {Function} delegate
*/
constructor(delegate) {
super();
assert.argumentIsRequired(delegate, "delegate", Function);
this.#delegate = delegate;
}
/**
* @protected
* @override
* @param {object} error
* @param {Endpoint} endpoint
* @returns {*}
*/
_onProcess(error, endpoint) {
return this.#delegate(error, endpoint);
}
toString() {
return "[DelegateErrorInterceptor]";
}
}
const errorInterceptorEmpty = new ErrorInterceptor();
const errorInterceptorGeneral = new DelegateErrorInterceptor(async (error, endpoint) => {
const response = error.response;
let rejection = null;
if (is.object(response) && is.object(response.headers) && response.headers["content-type"] === "application/json") {
let deserialized = null;
if (is.object(response.data)) {
deserialized = response.data;
} else {
try {
deserialized = JSON.parse(response.data);
} catch {
deserialized = null;
}
}
if (deserialized !== null) {
rejection = deserialized;
}
}
if (rejection === null && is.undef(response) && error.message === "Network Error") {
rejection = import_FailureReason.default.forRequest({ endpoint }).addItem(import_FailureType.default.REQUEST_AUTHORIZATION_FAILURE).format();
}
if (rejection === null) {
rejection = import_FailureReason.default.forRequest({ endpoint }).addItem(import_FailureType.default.REQUEST_GENERAL_FAILURE).format();
}
throw rejection;
});
{
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;
}