parseLinkHeader.tsx 649 B

1234567891011121314151617181920212223242526
  1. export default function parseLinkHeader(header: string | null): {
  2. [key: string]: {href: string; results: boolean | null; cursor: string};
  3. } {
  4. if (header === null || header === '') {
  5. return {};
  6. }
  7. const header_vals = header.split(',');
  8. const links = {};
  9. header_vals.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. }