parseLinkHeader.tsx 709 B

123456789101112131415161718192021222324252627
  1. export type ParsedHeader = {cursor: string; href: string; results: boolean | null};
  2. type Result = Record<string, ParsedHeader>;
  3. export default function parseLinkHeader(header: string | null): Result {
  4. if (header === null || header === '') {
  5. return {};
  6. }
  7. const headerValues = header.split(',');
  8. const links = {};
  9. headerValues.forEach(val => {
  10. const match =
  11. /<([^>]+)>; rel="([^"]+)"(?:; results="([^"]+)")?(?:; cursor="([^"]+)")?/g.exec(
  12. val
  13. );
  14. const hasResults = match![3] === 'true' ? true : match![3] === 'false' ? false : null;
  15. links[match![2]] = {
  16. href: match![1],
  17. results: hasResults,
  18. cursor: match![4],
  19. };
  20. });
  21. return links;
  22. }