spotify-web-api-node icon indicating copy to clipboard operation
spotify-web-api-node copied to clipboard

Add missing get Queue API Endpoint

Open NoelBank opened this issue 2 years ago • 1 comments

Here is a new API Endpoint in current Spotify's API Docs: https://developer.spotify.com/documentation/web-api/reference/#/operations/get-queue

I added it to the Lib.

NoelBank avatar Sep 15 '22 06:09 NoelBank

In the mean-time for others who are waiting for this PR to be merged, you could also use the default node https module to make the request:

import https from "https";
import SpotifyWebApi from "spotify-web-api-node";

type MyQueueResponse = {
  currently_playing: SpotifyApi.TrackObjectFull | null;
  queue: SpotifyApi.TrackObjectFull[];
};

// TODO wait for: https://github.com/thelinmichael/spotify-web-api-node/pull/465
export const getMyQueue = async (
  accessToken: string,
  attempt = 0
): Promise<MyQueueResponse> => {
  const request = https.get({
    hostname: "api.spotify.com",
    path: "/v1/me/player/queue",
    method: "GET",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${accessToken}`,
    },
  });

  try {
    return new Promise((resolve, reject) => {
      let data = "";
      request
        .on("response", (response) => {
          if (response.statusCode !== 200) reject(response.statusCode);

          response.resume();
          response.on("data", (chunk) => (data += chunk));
          response.on("end", () => resolve(JSON.parse(data) as ClearQueueResponse));
        })
        .on("end", reject)
        .on("error", reject);
    });
  } catch (error) {
   console.error(`${getMyQueue.name}(${attempt}): ${JSON.stringify(error)}`);
  }
};

xiduzo avatar Oct 27 '22 12:10 xiduzo