UNPKG

@coolio/http

Version:
42 lines 1.36 kB
import isNil from 'lodash/isNil'; export const getHeader = (headers, header) => { if (!headers) { return undefined; } const lowercaseHeader = header.toLowerCase(); const foundKey = Object.keys(headers).find(key => key.toLowerCase() === lowercaseHeader); return foundKey ? String(headers[foundKey]) : undefined; }; export const parseHeaders = (headers) => { const result = {}; headers.forEach((value, key) => result[key.toLowerCase()] = value); return result; }; export const sanitizeHeaders = (...multipleHeaders) => { const result = {}; const keys = {}; for (const headers of multipleHeaders) { if (!headers) { continue; } for (const key in headers) { if (headers.hasOwnProperty(key)) { const matchKey = key.toLowerCase(); const originalKey = keys[matchKey]; const value = headers[key]; if (originalKey) { delete result[originalKey]; } if (isNil(value)) { delete keys[matchKey]; } else { keys[matchKey] = key; result[key] = value.toString(); } } } } return result; }; //# sourceMappingURL=headers.helper.js.map