Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HttpHeaders Link for pagination in Angular project?

The response from api.github has a response header with Link.

How should I use it? it return a string like this

<https://api.github.com/user/repos?per_page=10&page=2>; rel="next", <https://api.github.com/user/repos?per_page=10&page=4>; rel="last"

it need to parse by myselef?

HttpHeaders.Link

like image 575
viola Avatar asked Dec 02 '25 20:12

viola


1 Answers

You need to parse it yourself. Here's how to do it with split, reduce, and a regular expression.

let linkHeaders = '<https://api.github.com/user/repos?per_page=10&page=2>; rel="next", <https://api.github.com/user/repos?per_page=10&page=4>; rel="last"'

let parts = linkHeaders.split(',').reduce((acc, link) => {
   let match = link.match(/<(.*)>; rel="(\w*)"/)
   let url = match[1]
   let rel = match[2]
   acc[rel] = url
   return acc;
}, {})

console.log(parts)
like image 78
hayden Avatar answered Dec 05 '25 10:12

hayden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!