Migrating from Request library

tags:  javascript, nodejs

Context

The library for NodeJS request is deprecated since 2019 but it's still being used by a lot of my own libraries.

There's the needing to migrate from request to the most updated libraries or maybe use the standard library itself with the latest NodeJS versions.

Solution

Taking this code as an example from Spotify playlist

function getTracks() {
  return new Promise((resolve /*, reject */) => {
    request.post(auth_options, async function (err, resp, body) {
      if (!err && resp.statusCode === 200) {
        ...

        resolve(body.items);
      }
    });
  });
}

A proposal to migrate could be:

async function getAllTracks() {
  const items = [];

  try {
    const response = await fetch(
      url,
      auth_options
    );

    ...
  } catch (e) {
    console.error('There was an error when connecting with the API\n');
    console.error(e);
  }

  return items;
}

References

TODO