(header: string | null)
| 6 | type Result = Record<string, ParsedHeader>; |
| 7 | |
| 8 | export function parseLinkHeader(header: string | null): Result { |
| 9 | if (header === null || header === '') { |
| 10 | return {}; |
| 11 | } |
| 12 | |
| 13 | const headerValues = header.split(','); |
| 14 | const links: Record<string, ParsedHeader> = {}; |
| 15 | |
| 16 | headerValues.forEach(val => { |
| 17 | const match = |
| 18 | /<([^>]+)>; rel="([^"]+)"(?:; results="([^"]+)")?(?:; cursor="([^"]+)")?/.exec(val); |
| 19 | if (!match) { |
| 20 | return; |
| 21 | } |
| 22 | const [, href, rel, results, cursor] = match; |
| 23 | const hasResults = results === 'true' ? true : results === 'false' ? false : null; |
| 24 | |
| 25 | links[rel!] = { |
| 26 | href: href!, |
| 27 | results: hasResults, |
| 28 | cursor: cursor!, |
| 29 | }; |
| 30 | }); |
| 31 | |
| 32 | return links; |
| 33 | } |
no test coverage detected