utils.js

"use strict";
/**
 * @module utils
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.logError = exports.logFailedResponse = exports.removeEnclosingDQuotes = exports.ensure0x = exports.remove0x = void 0;
/**
 * @description Removes leading "0x" pefix from a string if it has any.
 *
 * @param {string} val The input string.
 * @returns {string} The string without 0x prefix.
 */
function remove0x(val) {
    if (!val)
        return '';
    return val.startsWith('0x') ? val.slice(2) : val;
}
exports.remove0x = remove0x;
/**
 * @description Adds a leading "0x" prefix to a string if it doesn't have already.
 *
 * @param {string} str The string
 */
function ensure0x(str) {
    return str.startsWith('0x') ? str : `0x${str}`;
}
exports.ensure0x = ensure0x;
/**
 * @description Removes enclosing double quotes from a string. Used for formatting messages coming from the Secret Store nodes.
 *
 * @param {string} str The string to format.
 * @returns {string} The string without enclosing double quotes.
 */
function removeEnclosingDQuotes(str) {
    return str.replace(/^"(.*)"$/, '$1');
}
exports.removeEnclosingDQuotes = removeEnclosingDQuotes;
/**
 * @description Logs relevant information from a failed http response.
 *
 * @param {function(any[]): void} logFunction Log function to use with the error message.
 * @param {AxiosResponse} response The response object.
 */
function logFailedResponse(logFunction, response) {
    logFunction('Request failed');
    logFunction(`StatusCode: ${response.status}`);
    logFunction(`StatusMessage:  ${response.statusText}`);
    logFunction(`Body: ${response.data}`);
    logFunction(`Request options: ${JSON.stringify(response.config)}`);
}
exports.logFailedResponse = logFailedResponse;
/**
 * @description Logs the error object. Only stringifies the input.
 *
 * @param {function(any[]): void} logFunction Log function to use with the error message.
 * @param {any} e Error object or message.
 */
function logError(logFunction, e) {
    logFunction('Error:');
    logFunction(JSON.stringify(e));
}
exports.logError = logError;
//# sourceMappingURL=utils.js.map