@rapharacing/mw-utils
Version:
Middleware Utils
127 lines • 4.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatAuthorizationToken = exports.getRequestConfig = exports.cognitoErrorResponse = exports.errorResponse = void 0;
const constants_1 = require("./constants");
/**
* Formatted Error
*
* @private
* @param {number} [error.code=500] error.code Error code
* @param {string} error.key Error key
* @param {string} error.message Error message
*/
// const formattedError = ({ code = 500, key, message }: IFormattedError): IFormattedError => ({
// code,
// key,
// message
// });
/**
* Formatted Errors
*
* @private
* @param {number} [error.code=500] error.code Error code
* @param {(Object|string)} error.err Error object / error string
* @param {string} [error.message] Error message
*/
// const formattedErrors = ({ code = 500, err, message }: IFormattedErrors): IFormattedErrorsResponse => {
// if (Array.isArray(err))
// return {
// errors: err.map(e =>
// formattedError({ code, key: e.type, message: e.message })
// )
// };
// return { errors: [formattedError({ code, key: err, message })] };
// };
/**
* Formatted cognito error
*
* @private
* @param {string)} error error string
*/
const formattedCognitoError = (error) => {
return `: ${error}`;
};
/**
* Returns a Error Object
* @param {Object} error Error
* @param {String} customError Custom error to inject into console
* @returns {Object} Error
*/
const errorResponse = async ({ error, customError }) => {
const { response, stack } = error;
if (response && response.data && response.data.errors) {
console.error(`${customError} ${response.status}: ${JSON.stringify(response.data)}`);
return {
statusCode: response.status,
errors: response.data.errors
};
}
if (stack && stack.includes(constants_1.INVALID_VALUE_ERROR)) {
return {
statusCode: 400,
errors: constants_1.MANDATORY_PARAMS_ERROR
};
}
};
exports.errorResponse = errorResponse;
/**
* Returns a Error key
* @param {Object} error Error response from AWS or CC
* @param {string} customMessage Custom error message to overwrite stack errors
* @returns {string} Error key
*/
const cognitoErrorResponse = async ({ error, customMessage }) => {
if (!error && !customMessage)
return formattedCognitoError(constants_1.INTERNAL_FAILURE);
if (!error) {
return formattedCognitoError(customMessage);
}
const { stack, response: ccResponse = {}, code: awsErrorCode } = error;
const { data: ccData = {} } = ccResponse;
const { error: responseError, errors: responseErrors } = ccData;
// Invalid Request
if (stack && stack.includes(constants_1.INVALID_VALUE_ERROR)) {
return formattedCognitoError(constants_1.INCOMPLETE_SIGNATURE);
}
// AWS
if (awsErrorCode) {
return formattedCognitoError(awsErrorCode);
}
// CC Errors
if (ccResponse && ccData) {
// Array of Errors
if (responseErrors)
return formattedCognitoError(responseErrors[0].type);
// Single error
return formattedCognitoError(responseError);
}
// Fallback
return formattedCognitoError(constants_1.INTERNAL_FAILURE);
};
exports.cognitoErrorResponse = cognitoErrorResponse;
/**
* Returns a configuration object for axios
* @param {String} url URL
* @param {String} method HTTP method
* @param {String} token Authorization token
* @param {Object} data Data to be sent as the request body
* @returns {Object} Configuration object
*/
const getRequestConfig = async ({ url, method, token, data }) => ({
url,
method,
data,
headers: {
Authorization: token,
},
});
exports.getRequestConfig = getRequestConfig;
/**
* Format Authorization Token
*
* @param {string} token Token
* @returns {string} Token
*/
const formatAuthorizationToken = (token) => token.startsWith("Bearer ") ? token.slice(7, token.length) : token;
exports.formatAuthorizationToken = formatAuthorizationToken;
//# sourceMappingURL=index.js.map