'How to manage pagination and filter by popularity with Spotify API in Nodejs?

I am working with React.js and Node.js and making the calls to the Spotify API(with axios) from the back. In the front I pass an artist name, and the back responses the Albums of the first artists it founds. I want to obtain all the albums, no only the first 50. I am not sure how can I handle pagination in the spotify API, I've been reading for days now and I am still where I began. Here is the documentation. Also I want to filter them by popularity, is there a library I can use to sort arrays by their own properties value?

And here's my code:

API Controller

async function searchForArtist(req, res) {
  try {
    const searchArtist = await axios.get(
      `${apiURL}/search?query=${req.body.artist}&type=artist&market=us&limit=1`,
      {
        headers: {
          Authorization: `Bearer ${await token}`,
          "Content-Type": "application/x-www-form-urlencoded",
        },
      },
    );

    const artistId = searchArtist.data.artists.items[0].id;

    const includeAlbums = await axios.get(
      `${apiURL}/artists/${artistId}/albums?market=UY&limit=50&offset=0`,
      {
        headers: {
          Authorization: `Bearer ${await token}`,
          "Content-Type": "application/x-www-form-urlencoded",
        },
      },
    );

    res.json({ artist_info: searchArtist.data, albums_artist: includeAlbums.data });
  } catch (error) {
    console.error(error);
  }
}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source