Files
2024-02-15 17:01:50 +00:00

28 lines
709 B
TypeScript

export type ParsedHeader = {cursor: string; href: string; results: boolean | null};
type Result = Record<string, ParsedHeader>;
export default function parseLinkHeader(header: string | null): Result {
if (header === null || header === '') {
return {};
}
const headerValues = header.split(',');
const links = {};
headerValues.forEach(val => {
const match =
/<([^>]+)>; rel="([^"]+)"(?:; results="([^"]+)")?(?:; cursor="([^"]+)")?/g.exec(
val
);
const hasResults = match![3] === 'true' ? true : match![3] === 'false' ? false : null;
links[match![2]] = {
href: match![1],
results: hasResults,
cursor: match![4],
};
});
return links;
}