@shopify/shopify-api
Version:
Shopify API Library for Node - accelerate development with support for authentication, graphql proxy, webhooks
1 lines • 8.04 kB
Source Map (JSON)
{"version":3,"file":"headers.mjs","sources":["../../../../../../runtime/http/headers.ts"],"sourcesContent":["import type {Headers} from './types';\n\n/**\n * Canonicalizes a header name by capitalizing each segment and ensuring consistent hyphenation.\n *\n * @param hdr - The header name to canonicalize.\n * @returns The canonicalized header name.\n */\nexport function canonicalizeHeaderName(hdr: string): string {\n return hdr.replace(\n /(^|-)(\\w+)/g,\n (_fullMatch, start, letters) =>\n start +\n letters.slice(0, 1).toUpperCase() +\n letters.slice(1).toLowerCase(),\n );\n}\n\n/**\n * Retrieves all values associated with a canonicalized header name from the headers object.\n *\n * @param headers - The headers object or undefined.\n * @param needle_ - The header name to search for.\n * @returns An array of header values associated with the canonicalized header name.\n */\nexport function getHeaders(\n headers: Headers | undefined,\n needle_: string,\n): string[] {\n const result: string[] = [];\n if (!headers) return result;\n const needle = canonicalizeHeaderName(needle_);\n for (const [key, values] of Object.entries(headers)) {\n if (canonicalizeHeaderName(key) !== needle) continue;\n if (Array.isArray(values)) {\n result.push(...values);\n } else {\n result.push(values);\n }\n }\n return result;\n}\n\n/**\n * Retrieves the first value associated with a canonicalized header name from the headers object.\n *\n * @param headers - The headers object or undefined.\n * @param needle - The header name to search for.\n * @returns The first value associated with the canonicalized header name, or undefined if not found.\n */\nexport function getHeader(\n headers: Headers | undefined,\n needle: string,\n): string | undefined {\n if (!headers) return undefined;\n return getHeaders(headers, needle)?.[0];\n}\n/**\n * Sets a header to a single value, canonicalizing the header name.\n *\n * @param headers - The headers object.\n * @param key - The header name to set.\n * @param value - The value to assign to the header.\n */\nexport function setHeader(headers: Headers, key: string, value: string) {\n canonicalizeHeaders(headers);\n headers[canonicalizeHeaderName(key)] = [value];\n}\n/**\n * Adds a value to an existing header, creating a new array if necessary, and canonicalizing the header name.\n *\n * @param headers - The headers object.\n * @param key - The header name to add to.\n * @param value - The value to add.\n */\nexport function addHeader(headers: Headers, key: string, value: string) {\n canonicalizeHeaders(headers);\n const canonKey = canonicalizeHeaderName(key);\n let list = headers[canonKey];\n if (!list) {\n list = [];\n } else if (!Array.isArray(list)) {\n list = [list];\n }\n headers[canonKey] = list;\n list.push(value);\n}\n\n/**\n * Canonicalizes a header value, converting numbers to strings.\n *\n * @param value - The value to canonicalize.\n * @returns The canonicalized value as a string.\n */\nfunction canonicalizeValue(value: any): any {\n if (typeof value === 'number') return value.toString();\n return value;\n}\n\n/**\n * Canonicalizes all headers in the headers object by ensuring consistent header names and values.\n *\n * @param hdr - The headers object to canonicalize.\n * @returns The headers object with canonicalized header names and values.\n */\nexport function canonicalizeHeaders(hdr: Headers): Headers {\n for (const [key, values] of Object.entries(hdr)) {\n const canonKey = canonicalizeHeaderName(key);\n if (!hdr[canonKey]) hdr[canonKey] = [];\n if (!Array.isArray(hdr[canonKey]))\n hdr[canonKey] = [canonicalizeValue(hdr[canonKey])];\n if (key === canonKey) continue;\n delete hdr[key];\n (hdr[canonKey] as any).push(\n ...[values].flat().map((value) => canonicalizeValue(value)),\n );\n }\n return hdr;\n}\n\n/**\n * Removes a header from the headers object.\n *\n * @param headers - The headers object.\n * @param needle - The header name to remove.\n */\nexport function removeHeader(headers: Headers, needle: string) {\n canonicalizeHeaders(headers);\n const canonKey = canonicalizeHeaderName(needle);\n delete headers[canonKey];\n}\n\n/**\n * Converts a headers object into an array of tuples, where each tuple represents a header name and value.\n *\n * @param {Object|string[][]} headers - The headers object or undefined/null.\n * @returns {string[][]} An array of tuples where each tuple contains a header name and its corresponding value.\n *\n * @example\n * // Example headers object\n * const headers = {\n * 'Set-Cookie': 'a=b',\n * 'Set-Cookie': 'x=y'\n * };\n *\n * // Converted to an array of tuples\n * const result = convertHeadersToTuples(headers);\n * console.log(result);\n * // Output: [\n * // [\"Set-Cookie\", \"a=b\"],\n * // [\"Set-Cookie\", \"x=y\"]\n * // ]\n */\nexport function flatHeaders(\n headers: Headers | undefined | null,\n): [string, string][] {\n if (!headers) return [];\n\n return Object.entries(headers).flatMap(([header, values]) =>\n Array.isArray(values)\n ? values.map((value): [string, string] => [header, value])\n : [[header, values]],\n );\n}\n"],"names":[],"mappings":"AAEA;;;;;AAKG;AACG,SAAU,sBAAsB,CAAC,GAAW,EAAA;AAChD,IAAA,OAAO,GAAG,CAAC,OAAO,CAChB,aAAa,EACb,CAAC,UAAU,EAAE,KAAK,EAAE,OAAO,KACzB,KAAK;QACL,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE;QACjC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CACjC;AACH;AAEA;;;;;;AAMG;AACG,SAAU,UAAU,CACxB,OAA4B,EAC5B,OAAe,EAAA;IAEf,MAAM,MAAM,GAAa,EAAE;AAC3B,IAAA,IAAI,CAAC,OAAO;AAAE,QAAA,OAAO,MAAM;AAC3B,IAAA,MAAM,MAAM,GAAG,sBAAsB,CAAC,OAAO,CAAC;AAC9C,IAAA,KAAK,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;AACnD,QAAA,IAAI,sBAAsB,CAAC,GAAG,CAAC,KAAK,MAAM;YAAE;AAC5C,QAAA,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;AACzB,YAAA,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC;QACxB;aAAO;AACL,YAAA,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;QACrB;IACF;AACA,IAAA,OAAO,MAAM;AACf;AAEA;;;;;;AAMG;AACG,SAAU,SAAS,CACvB,OAA4B,EAC5B,MAAc,EAAA;AAEd,IAAA,IAAI,CAAC,OAAO;AAAE,QAAA,OAAO,SAAS;IAC9B,OAAO,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;AACzC;AACA;;;;;;AAMG;SACa,SAAS,CAA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