Skip to content

Migration guide for v8

Science Spot edited this page Mar 21, 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!

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!

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!

Clone this wiki locally