0

I am using Google Apps Script to make a series of HTTP Requests. The endpoint I have been using just switched to cursor-based pagination.

The response looks like this.

{...
 Link=
  <https://x.shopify.com/admin/api/2019-10/inventory_levels.json?limit=250&page_info=abc>;rel="previous",
  <https://x.shopify.com/admin/api/2019-10/inventory_levels.json?limit=250&page_info=def>;rel="next"
}

I can use response['Link'] to get it down to

<https://x.shopify.com/admin/api/2019-10/inventory_levels.json?limit=250&page_info=abc>;rel="previous",
<https://x.shopify.com/admin/api/2019-10/inventory_levels.json?limit=250&page_info=def>;rel="next"

Is there a way to extract page_info reliably from the "next" URL without regular expression? I am fine resorting to regular expression but I wondered if there was specific method for getting it.

Thanks in advance for your help. I dabble and get that I still have a ton to learn.

enano2054
  • 329
  • 6
  • 19
  • Split by `?`, then by `&` and `=`. – TheMaster Dec 14 '19 at 20:28
  • I did try that one and it works fine. I was just hoping there was a "prettier" method. Oh well as long as the result is the same. I wish they would have used an array or another object. – enano2054 Dec 14 '19 at 22:24
  • Does this answer your question? [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Diego Dec 15 '19 at 07:08

1 Answers1

0

You can use a regex to extract the URL and whether the link is the next or previous page.

/<(.*)>; rel=\"(.*)\"/

To use this against your code you you could do something like this:

const urls = headers.links.map(link => {
  const linkContents = link.match(/<(.*)>; rel=\"(.*)\"/)

  const url = linkContents[1]
  const type = linkContents[2] // next or previous

  return { url, type }
})
Stretch0
  • 8,362
  • 13
  • 71
  • 133