parseLinkHeader.tsx 668 B

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