Skip to content

Instantly share code, notes, and snippets.

@ashaffah
Last active June 6, 2025 16:39
Show Gist options
  • Save ashaffah/bc17e27689a63ddffab02044d0dedbd7 to your computer and use it in GitHub Desktop.
Save ashaffah/bc17e27689a63ddffab02044d0dedbd7 to your computer and use it in GitHub Desktop.
http headers combinator
/**
* @typedef {(
* "text/plain" |
* "text/html" |
* "text/css" |
* "text/javascript" |
* "application/javascript" |
* "application/json" |
* "application/xml" |
* "application/x-www-form-urlencoded" |
* "application/octet-stream" |
* "multipart/form-data" |
* "image/jpeg" |
* "image/png" |
* "image/gif" |
* "image/webp" |
* "image/svg+xml" |
* "audio/mpeg" |
* "video/mp4" |
* "video/webm" |
* "font/woff" |
* "font/woff2" |
* "font/ttf" |
* "application/pdf" |
* "application/zip" |
* "application/vnd.api+json" |
* string
* )} ContentType
*/
/**
* Returns an object with the `Content-Type` header.
* @param {ContentType} type
* @returns {{ "Content-Type": string }}
*/
export function setContentType(type) {
return { "Content-Type": type };
}
/**
* Returns an object with the `Authorization` header.
* @param {string} token
* @returns {{ Authorization: string }}
*/
export function setAuthorization(token) {
return { Authorization: `Bearer ${token}` };
}
/**
* Returns an object with the `Accept-Language` header.
* @param {string} lang
* @returns {{ "Accept-Language": string }}
*/
export function setAcceptLanguage(lang) {
return { "Accept-Language": lang };
}
/**
* Merges multiple header objects into one.
*
* Useful for combining results of `setAuthorization`, `setContentType`, etc.
*
* @example
* const headers = mergeHeaders(
* setAuthorization("abc"),
* setContentType("application/json"),
* { "X-Custom": "test" }
* );
*
* // {
* // Authorization: "Bearer abc",
* // "Content-Type": "application/json",
* // "X-Custom": "test"
* // }
*
* @param {...Object<string, string>} parts
* @returns {Object<string, string>}
*/
export function mergeHeaders(...parts) {
return Object.assign({}, ...parts);
}
/**
* List of supported Content-Type values.
*/
export type ContentType =
| "text/plain"
| "text/html"
| "text/css"
| "text/javascript"
| "application/javascript"
| "application/json"
| "application/xml"
| "application/x-www-form-urlencoded"
| "application/octet-stream"
| "multipart/form-data"
| "image/jpeg"
| "image/png"
| "image/gif"
| "image/webp"
| "image/svg+xml"
| "audio/mpeg"
| "video/mp4"
| "video/webm"
| "font/woff"
| "font/woff2"
| "font/ttf"
| "application/pdf"
| "application/zip"
| "application/vnd.api+json";
/**
* Returns a header object containing the given Content-Type.
*
* @param {ContentType} type - The content type to use.
* @returns {Record<string, string>} A headers object with the Content-Type set.
*/
export function setContentType(type: ContentType): Record<string, string> {
return { "Content-Type": type };
}
/**
* Returns a header object with a Bearer Authorization token.
*
* @param {string} token - The bearer token to use.
* @returns {Record<string, string>} A headers object with the Authorization set.
*/
export function setAuthorization(token: string): Record<string, string> {
return { Authorization: `Bearer ${token}` };
}
/**
* Returns a header object with the specified Accept-Language.
*
* @param {string} lang - The language code to use (e.g., "en-US").
* @returns {Record<string, string>} A headers object with Accept-Language set.
*/
export function setAcceptLanguage(lang: string): Record<string, string> {
return { "Accept-Language": lang };
}
/**
* Combines multiple header objects into a single headers object.
*
* This function is useful for merging the results of helper functions like
* `setContentType`, `setAuthorization`, `setAcceptLanguage`, and other
* custom headers into one object that can be passed to Axios or fetch requests.
*
* @example
* const headers = mergeHeaders(
* setAuthorization(token),
* setContentType("application/json"),
* { "X-Custom-Header": "value" }
* );
*
* // Result:
* // {
* // "Authorization": "Bearer <token>",
* // "Content-Type": "application/json",
* // "X-Custom-Header": "value"
* // }
*
* @param {...Record<string, string>} parts - One or more header objects to merge.
* @returns {Record<string, string>} A single merged headers object.
*/
export function mergeHeaders(
...parts: Array<Record<string, string>>
): Record<string, string> {
return Object.assign({}, ...parts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment