spotify-finder
Advanced tools
Comparing version 2.0.2 to 2.1.0
@@ -33,32 +33,20 @@ 'use strict' | ||
const opts = { | ||
method: 'GET', | ||
uri: path, | ||
json: true | ||
} | ||
return new Promise((resolve, reject) => { | ||
this.getToken() | ||
.then((token) => { | ||
const opts = { | ||
method: 'GET', | ||
uri: path, | ||
headers: { | ||
'Authorization': `Bearer ${token}` | ||
}, | ||
json: true | ||
} | ||
return request(opts) | ||
resolve(request(opts)) | ||
}) | ||
.catch((err) => reject(new Error(err))) | ||
}) | ||
} | ||
/** | ||
* @param {string} path - endpoint to send request | ||
* @param {Object} [params] - querystrings | ||
*/ | ||
fetchOAuth (path, params) { | ||
path = params ? `${path}?${qs.encode(params)}` : path | ||
return Promise.resolve(this.getToken().then((token) => { | ||
const opts = { | ||
method: 'GET', | ||
uri: path, | ||
headers: { | ||
'Authorization': `Bearer ${token}` | ||
}, | ||
json: true | ||
} | ||
return request(opts) | ||
})) | ||
} | ||
getToken () { | ||
@@ -267,7 +255,7 @@ /* istanbul ignore else */ | ||
return Promise | ||
.resolve(this.fetchOAuth(url, params)) | ||
.resolve(this.fetch(url, params)) | ||
.asCallback(callback) | ||
} | ||
return Promise.resolve(this.fetchOAuth(url, params)) | ||
return Promise.resolve(this.fetch(url, params)) | ||
} | ||
@@ -292,7 +280,7 @@ | ||
return Promise | ||
.resolve(this.fetchOAuth(url, opts)) | ||
.resolve(this.fetch(url, opts)) | ||
.asCallback(callback) | ||
} | ||
return Promise.resolve(this.fetchOAuth(url, opts)) | ||
return Promise.resolve(this.fetch(url, opts)) | ||
} | ||
@@ -299,0 +287,0 @@ } |
{ | ||
"name": "spotify-finder", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "A isomorphic Spotify API client", | ||
@@ -5,0 +5,0 @@ "main": "lib/client.js", |
@@ -16,9 +16,8 @@ # spotify-finder | ||
consumer: { | ||
key: 'YOUR_CLIENT_ID', // if your not have an app in spotify ignore this options | ||
secret: 'YOUR_CLIENT_SECRET' // if your not have an app in spotify ignore this options | ||
key: 'YOUR_CLIENT_ID', // from v2.1.0 is required | ||
secret: 'YOUR_CLIENT_SECRET' // from v2.1.0 is required | ||
} | ||
}) | ||
``` | ||
Note: if you do not Provide the client credentials, some features that require authentication will not be available. | ||
To create an application in Spotify. [click here](https://developer.spotify.com/my-applications/#!/) | ||
> Note: you have that provide the client credentials because from 29th May 2017 was removed unauthenticated calls to the Spotify Web API [more info](https://developer.spotify.com/news-stories/2017/01/27/removing-unauthenticated-calls-to-the-web-api/). Create an application in Spotify [click here](https://developer.spotify.com/my-applications/#!/). | ||
@@ -25,0 +24,0 @@ #### Search for all types |
@@ -5,7 +5,4 @@ 'use strict' | ||
const nock = require('nock') | ||
const Spotify = require('../lib/client') | ||
const client = require('./client-config') | ||
const url = 'https://api.spotify.test' | ||
const client = new Spotify({ url }) | ||
test('should get single an album', (t) => { | ||
@@ -17,3 +14,3 @@ const albumId = '6Kssm2LosQ0WyLukFZkEG5' | ||
nock(url).get(`/albums/${albumId}`) | ||
nock(client.baseURL).get(`/albums/${albumId}`) | ||
.reply(200, response) | ||
@@ -32,3 +29,3 @@ | ||
nock(url).get(`/albums/${albumId}/tracks`) | ||
nock(client.baseURL).get(`/albums/${albumId}/tracks`) | ||
.reply(200, response) | ||
@@ -46,3 +43,3 @@ | ||
nock(url).get('/albums') | ||
nock(client.baseURL).get('/albums') | ||
.query({ ids: ids.toString() }) | ||
@@ -65,3 +62,3 @@ .reply(200, response) | ||
nock(url).get(`/albums/${albumId}`) | ||
nock(client.baseURL).get(`/albums/${albumId}`) | ||
.reply(200, response) | ||
@@ -81,3 +78,3 @@ | ||
nock(url).get('/albums') | ||
nock(client.baseURL).get('/albums') | ||
.query({ ids: ids.toString() }) | ||
@@ -84,0 +81,0 @@ .reply(200, response) |
@@ -5,7 +5,4 @@ 'use strict' | ||
const nock = require('nock') | ||
const Spotify = require('../lib/client') | ||
const client = require('./client-config') | ||
const url = 'https://api.spotify.test' | ||
const client = new Spotify({ url }) | ||
test('should get single an artist', (t) => { | ||
@@ -15,3 +12,3 @@ const artistId = '6S2OmqARrzebs0tKUEyXyp' | ||
nock(url).get(`/artists/${artistId}`) | ||
nock(client.baseURL).get(`/artists/${artistId}`) | ||
.query({ country: 'SE' }) | ||
@@ -32,3 +29,3 @@ .reply(200, response) | ||
nock(url).get(`/artists/${artistId}/albums`) | ||
nock(client.baseURL).get(`/artists/${artistId}/albums`) | ||
.query({ country: 'SE' }) | ||
@@ -49,3 +46,3 @@ .reply(200, response) | ||
nock(url).get(`/artists/${artistId}/top-tracks`) | ||
nock(client.baseURL).get(`/artists/${artistId}/top-tracks`) | ||
.query({ country: 'SE' }) | ||
@@ -66,3 +63,3 @@ .reply(200, response) | ||
nock(url).get(`/artists/${artistId}/related-artists`) | ||
nock(client.baseURL).get(`/artists/${artistId}/related-artists`) | ||
.query({ country: 'SE' }) | ||
@@ -82,3 +79,3 @@ .reply(200, response) | ||
nock(url).get('/artists') | ||
nock(client.baseURL).get('/artists') | ||
.query({ ids: ids.toString() }) | ||
@@ -103,3 +100,3 @@ .reply(200, response) | ||
nock(url).get(`/artists/${artistId}`) | ||
nock(client.baseURL).get(`/artists/${artistId}`) | ||
.query(querys) | ||
@@ -120,3 +117,3 @@ .reply(200, response) | ||
nock(url).get('/artists') | ||
nock(client.baseURL).get('/artists') | ||
.query({ ids: ids.toString() }) | ||
@@ -123,0 +120,0 @@ .reply(200, response) |
@@ -5,16 +5,4 @@ 'use strict' | ||
const nock = require('nock') | ||
const Spotify = require('../lib/client') | ||
const client = require('./client-config') | ||
const config = { | ||
url: 'https://api.spotify.test', | ||
auth: 'https://accounts.spotify.test/api/token', | ||
consumer: { | ||
key: 'NgA6ZcYIixn8bUQ', | ||
secret: 'ixn8bUQNgA6ZcYI' | ||
} | ||
} | ||
const client = new Spotify(config) | ||
const token = 'xxx-xxxx-xxx' | ||
const headers = { 'Authorization': `Bearer ${token}` } | ||
test('should get new releases', (t) => { | ||
@@ -24,3 +12,3 @@ const opts = { to: 'new-releases', country: 'SE' } | ||
nock(config.url, { reqheaders: headers }) | ||
nock(client.baseURL) | ||
.get('/browse/new-releases') | ||
@@ -41,3 +29,3 @@ .query({ country: 'SE' }) | ||
nock(config.url, { reqheaders: headers }) | ||
nock(client.baseURL) | ||
.get('/browse/featured-playlists') | ||
@@ -58,3 +46,3 @@ .query({ country: 'SE' }) | ||
nock(config.url, { reqheaders: headers }) | ||
nock(client.baseURL) | ||
.get('/browse/categories') | ||
@@ -79,3 +67,3 @@ .query({ country: 'SE' }) | ||
nock(config.url, { reqheaders: headers }) | ||
nock(client.baseURL) | ||
.get('/browse/new-releases') | ||
@@ -82,0 +70,0 @@ .query({ country: 'SE' }) |
@@ -5,17 +5,4 @@ 'use strict' | ||
const nock = require('nock') | ||
const Spotify = require('../lib/client') | ||
const client = require('./client-config') | ||
const config = { | ||
url: 'https://api.spotify.test', | ||
auth: 'https://accounts.spotify.test/api/token', | ||
consumer: { | ||
key: 'NgA6ZcYIixn8bUQ', | ||
secret: 'ixn8bUQNgA6ZcYI' | ||
} | ||
} | ||
const client = new Spotify(config) | ||
const token = 'xxx-xxxx-xxx' | ||
const headers = { 'Authorization': `Bearer ${token}` } | ||
test('should get an category', (t) => { | ||
@@ -25,3 +12,3 @@ const catId = 'toptracks' | ||
nock(config.url, { reqheaders: headers }) | ||
nock(client.baseURL) | ||
.get(`/browse/categories/${catId}`) | ||
@@ -41,3 +28,3 @@ .reply(200, response) | ||
nock(config.url, { reqheaders: headers }) | ||
nock(client.baseURL) | ||
.get(`/browse/categories/${catId}/playlists?`) | ||
@@ -61,3 +48,3 @@ .reply(200, response) | ||
nock(config.url, { reqheaders: headers }) | ||
nock(client.baseURL) | ||
.get(`/browse/categories/${catId}?`) | ||
@@ -64,0 +51,0 @@ .reply(200, response) |
@@ -5,22 +5,10 @@ 'use strict' | ||
const nock = require('nock') | ||
const Spotify = require('../lib/client') | ||
const client = require('./client-config.js') | ||
const headers = require('./client-config.js').headers | ||
const clientBad = require('./client-config.js').clientBad | ||
const config = { | ||
auth: 'https://accounts.spotify.test/api/token', | ||
consumer: { | ||
key: 'NgA6ZcYIixn8bUQ', | ||
secret: 'ixn8bUQNgA6ZcYI' | ||
} | ||
} | ||
const encode = new Buffer(`${config.consumer.key}:${config.consumer.secret}`).toString('base64') | ||
const headers = { 'Authorization': `Basic ${encode}` } | ||
const response = { access_token: 'xxx-xxxx-xxx' } | ||
const client = new Spotify(config) | ||
const clientBad = new Spotify({ auth: 'https://accounts.spotify.test/api/token' }) | ||
test('should return an access token', (t) => { | ||
nock(config.auth, { reqheaders: headers }) | ||
nock(client.authURL, { reqheaders: headers }) | ||
.post('') | ||
@@ -36,3 +24,3 @@ .reply(200, response) | ||
test('should fail without client credentials', (t) => { | ||
nock(config.auth, { reqheaders: headers }) | ||
nock(client.authURL, { reqheaders: headers }) | ||
.post('/') | ||
@@ -39,0 +27,0 @@ .reply(200, response) |
@@ -5,7 +5,4 @@ 'use strict' | ||
const nock = require('nock') | ||
const Spotify = require('../lib/client') | ||
const client = require('./client-config.js') | ||
const url = 'https://api.spotify.test' | ||
const client = new Spotify({ url }) | ||
test('should search artist, album, track', (t) => { | ||
@@ -22,3 +19,3 @@ const querys = { | ||
nock(url).get('/search') | ||
nock(client.baseURL).get('/search') | ||
.query(querys) | ||
@@ -45,3 +42,3 @@ .reply(200, response) | ||
nock(url).get('/search') | ||
nock(client.baseURL).get('/search') | ||
.query(querys) | ||
@@ -66,3 +63,3 @@ .reply(200, response) | ||
nock(url).get('/search') | ||
nock(client.baseURL).get('/search') | ||
.query(querys) | ||
@@ -69,0 +66,0 @@ .query({ type: 'artist,album,track' }) |
@@ -5,7 +5,4 @@ 'use strict' | ||
const nock = require('nock') | ||
const Spotify = require('../lib/client') | ||
const client = require('./client-config') | ||
const url = 'https://api.spotify.test' | ||
const client = new Spotify({ url }) | ||
test('should get an track', (t) => { | ||
@@ -15,3 +12,3 @@ const trackId = 'eGsygTp906u18L0Oimnem' | ||
nock(url).get(`/tracks/${trackId}`) | ||
nock(client.baseURL).get(`/tracks/${trackId}`) | ||
.reply(200, response) | ||
@@ -29,3 +26,3 @@ | ||
nock(url).get('/tracks') | ||
nock(client.baseURL).get('/tracks') | ||
.query({ ids: ids.toString() }) | ||
@@ -48,3 +45,3 @@ .reply(200, response) | ||
nock(url).get(`/tracks/${trackId}`) | ||
nock(client.baseURL).get(`/tracks/${trackId}`) | ||
.reply(200, response) | ||
@@ -63,3 +60,3 @@ | ||
nock(url).get('/tracks') | ||
nock(client.baseURL).get('/tracks') | ||
.query({ ids: ids.toString() }) | ||
@@ -66,0 +63,0 @@ .reply(200, response) |
17
30796
718
179