Skip to content

Migration guide for v8

Science Spot edited this page Mar 23, 2021 · 8 revisions

Migration guide for v8

Spotify-api.js v7 had many methods broken and some defects with it! So, incase if you have got habit of v7! Here is our migration changes!

Renaming classes and methods

Here are some importating renaming of classes and methods from v8!

  1. UserPlayer => PlayerManager
  2. Auth.get => Auth.getApiToken
  3. Auth.refresh => Auth.getRefreshToken
  4. CacheManager => Collection
  5. Spotify => Util
  6. Structures are directly exported without using the Structures namspace like in v7
  7. Manager classes ends with Manager on v8

Logging in to the client

With v8 there are many way to login instead of first creating your token through Auth class!

Using a token

await client.login('token')

Using client id and client secret

await client.login('client_id', 'client_secret');

Using current user authenication

const { refreshToken } = await client.login({
    clientID: 'id', // Your app client id
    clientSecret: 'secret', // Your app client secret
    code: 'token or code',  // To get new one, enter the code received by spotify api or to refresh to get a new one, enter the refreshToken!
    redirectURL: 'redirect url' // Redirect url which you used while auth, which is only for verification
}

While loggin in with current user authenication, login method will return a AuthRefresh object!

Error handelling

In v7, error handelling in spotify-api.js is a very big issue, so comming up to v8, here as some easy ways. Lets take a gist of it:

try{
    // Your spotify code
} catch(e) {
   if(e.isSpotifyError){
       console.log(`Spotify responded api with ${e.response.status}!`);
   }
}

You can view the docs for UnexpectedError class of spotify-api.js. There is also one more way to add global error handelling

const Spotify = require('spotify-api.js');

Spotify.handleError = e => {
    if(e.response && e.response.status == 404) return null; // This piece of code is required. If not entered, the package will throw error on 404 too
    if(e.response && e.response.data){
        console.log('Spotify error: ' + e.response.data.message);
    }
    else console.log(e);
}

Exteremely Typed

With v8, typings are extremely typed with interfaces and types. Many typings are removed for some reasons.

Paging

When using v7, Spotify-api.js always returns an array instead of a paging object which leads to loss of many information in the paging object! To prevent v8 has brought up with the Paging object. You can view the new v8 docs to know where and all arrays are replaced with paging objects! Here is an example:

const { items } = await client.playlists.getTracks('id');

In v7 what you were doing was:

const items = await client.playlists.getTracks('id');

Complete Spotify Api

In v8, there will be a covers the whole spotify api with all endpoints!

Cache Events

How you need to do it in v7

const client = new Spotify.Client('token', { cacheCurrentUser: true });

function onReady(){
    console.log('Cache is ready!');
} 

if(!client.madeCache) client.cacheOnReady = onReady;
else onReady()

How you do it in v8

const client = new Spotify.Client('token', { 
    cacheCurrentUser: true,
    ready(){
        console.log('Cache is ready');
    }
});

In v8, ready event wont fire if the token is NO TOKEN! So this will be useful if you login later! For example:

const client = new Spotify.Client('NO TOKEN', { 
    cacheCurrentUser: true,
    ready(){
        console.log('Cache is ready');
    }
});

client.login({
    clientID: 'id', // Your app client id
    clientSecret: 'secret', // Your app client secret
    code: 'token or code',  // To get new one, enter the code received by spotify api or to refresh to get a new one, enter the refreshToken!
    redirectURL: 'redirect url' // Redirect url which you used while auth, which is only for verification
}).then(async ({ refreshToken }) => {
    console.log(`Login successful! Refresh token: ${refreshToken}`);
    console.log(await client.tracks.get('id'));
}); // The ready event will fire now

If your attempt is just using the UserClient for just accessing the current user api then initiating the client is a bad choice, in v8 you can do something like this:

const user = await Spotify.createUser('token');
console.log(`Created a user with spotify id as ${user.id}`);

This method will have the cache prebuilt!