stringiful
Version:
Easy to use stringify function with built-in configurable formatters
44 lines • 2.66 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAxiosErrorFormatter = exports.isAxiosError = void 0;
const lodash_1 = __importDefault(require("lodash"));
const config_1 = __importDefault(require("../../config"));
const isAxiosError = (obj) => {
return Boolean(obj === null || obj === void 0 ? void 0 : obj.isAxiosError);
};
exports.isAxiosError = isAxiosError;
const getAxiosErrorFormatter = (formatterConfig) => {
var _a;
const { formattersDefaultParams: { axiosError: { maxResponseDataLength, maxRequestDataLength, allowedProperties }, }, } = config_1.default;
const baseAxiosErrorFormatter = {
matches: exports.isAxiosError,
};
const defaultFormatFunction = (axiosError) => {
var _a, _b, _c, _d, _e, _f, _g, _h;
const selectedMaxResponseDataLength = (_b = (_a = formatterConfig.params) === null || _a === void 0 ? void 0 : _a.maxResponseDataLength) !== null && _b !== void 0 ? _b : maxResponseDataLength;
const selectedMaxRequestDataLength = (_d = (_c = formatterConfig.params) === null || _c === void 0 ? void 0 : _c.maxRequestDataLength) !== null && _d !== void 0 ? _d : maxRequestDataLength;
const formattedRequestData = (_f = (_e = lodash_1.default.get(axiosError, 'config.data')) === null || _e === void 0 ? void 0 : _e.substring) === null || _f === void 0 ? void 0 : _f.call(_e, 0, selectedMaxResponseDataLength);
if (formattedRequestData) {
lodash_1.default.set(axiosError, 'config.data', formattedRequestData);
}
const formattedResponseData = (_h = (_g = lodash_1.default.get(axiosError, 'response.data')) === null || _g === void 0 ? void 0 : _g.substring) === null || _h === void 0 ? void 0 : _h.call(_g, 0, selectedMaxRequestDataLength);
if (formattedResponseData) {
lodash_1.default.set(axiosError, 'response.data', formattedResponseData);
}
return axiosError;
};
baseAxiosErrorFormatter.format = (_a = formatterConfig.format) !== null && _a !== void 0 ? _a : defaultFormatFunction;
const { fieldsBlacklist, fieldsWhitelist } = formatterConfig;
if (fieldsWhitelist) {
return { ...baseAxiosErrorFormatter, fieldsWhitelist };
}
if (fieldsBlacklist) {
return { ...baseAxiosErrorFormatter, fieldsBlacklist };
}
return { ...baseAxiosErrorFormatter, fieldsWhitelist: allowedProperties };
};
exports.getAxiosErrorFormatter = getAxiosErrorFormatter;
//# sourceMappingURL=axiosError.js.map
;