/**
* Gracefully handles response headers, providing fallbacks for internal tooling
* and logging errors.
*
* @param {Response} response The HTTP response object.
* @param {object} expectedHeaders An object containing the expected header names and their default values.
* @param {string} toolName The name of the internal tooling.
* @returns {object} An object containing the response headers, with fallbacks applied.
*/
function handleResponseHeaders(response, expectedHeaders, toolName) {
const headers = {};
// Iterate through expected headers and apply fallbacks
for (const headerName in expectedHeaders) {
if (expectedHeaders.hasOwnProperty(headerName)) {
const defaultValue = expectedHeaders[headerName];
// Get the header value from the response, or use the default value
headers[headerName] = response.headers.get(headerName) || defaultValue;
}
}
return headers;
}
/**
* Logs an error message to the console.
* @param {string} message The error message.
* @param {string} toolName The name of the internal tooling.
*/
function logError(message, toolName) {
console.error(`[${toolName}] Error: ${message}`);
}
// Example usage:
// Assuming you have a response object called 'response'
// and an object 'expectedHeaders' with expected header names and default values.
// const expectedHeaders = {
// 'Content-Type': 'application/json',
// 'X-Custom-Header': 'default_value',
// 'Authorization': 'no_auth' // Example of handling missing auth
// };
// const toolName = 'myInternalTool';
// const processedHeaders = handleResponseHeaders(response, expectedHeaders, toolName);
// console.log(processedHeaders);
Add your comment