Keycloak
1export type Links = {2prev?: Record<string, string>;3next?: Record<string, string>;4};5
6export function parseLinks(response: Response): Links {7const linkHeader = response.headers.get("link");8
9if (!linkHeader) {10throw new Error("Attempted to parse links, but no header was found.");11}12
13const links = linkHeader.split(/,\s*</);14return links.reduce<Links>((acc: Links, link: string) => {15const matcher = link.match(/<?([^>]*)>(.*)/);16if (!matcher) return {};17const linkUrl = matcher[1];18const rel = matcher[2].match(/\s*(.+)\s*=\s*"?([^"]+)"?/);19if (rel) {20const link: Record<string, string> = {};21for (const [key, value] of new URL(linkUrl).searchParams.entries()) {22link[key] = value;23}24acc[rel[2] as keyof Links] = link;25}26return acc;27}, {});28}
29