From f347eff7b0c7f61ee81eb4598c9f6db31f956303 Mon Sep 17 00:00:00 2001 From: luixlacrux Date: Tue, 20 Jun 2017 19:23:16 -0400 Subject: [PATCH 1/7] [update] Removing Unauthenticated calls to the Spotify API --- lib/client.js | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/lib/client.js b/lib/client.js index 7dead8f..4ee6518 100644 --- a/lib/client.js +++ b/lib/client.js @@ -23,22 +23,6 @@ class Client { } } - /** - * @param {string} path - endpoint to send request - * @param {Object} [params] - querystrings - */ - fetch (path, params) { - path = params ? `${path}?${qs.encode(params)}` : path - - const opts = { - method: 'GET', - uri: path, - json: true - } - - return request(opts) - } - /** * @param {string} path - endpoint to send request * @param {Object} [params] - querystrings @@ -135,7 +119,7 @@ class Client { .asCallback(callback) } - return Promise.resolve(this.fetch(url)) + return Promise.resolve(this.fetchOAuth(url)) } /** @@ -148,11 +132,11 @@ class Client { /* istanbul ignore else */ if (callback) { return Promise - .resolve(this.fetch(this.endpoints.albums, { ids })) + .resolve(this.fetchOAuth(this.endpoints.albums, { ids })) .asCallback(callback) } - return Promise.resolve(this.fetch(this.endpoints.albums, { ids })) + return Promise.resolve(this.fetchOAuth(this.endpoints.albums, { ids })) } /** @@ -185,11 +169,11 @@ class Client { /* istanbul ignore else */ if (callback) { return Promise - .resolve(this.fetch(url, params)) + .resolve(this.fetchOAuth(url, params)) .asCallback(callback) } - return Promise.resolve(this.fetch(url, params)) + return Promise.resolve(this.fetchOAuth(url, params)) } /** @@ -202,11 +186,11 @@ class Client { /* istanbul ignore else */ if (callback) { return Promise - .resolve(this.fetch(this.endpoints.artists, { ids })) + .resolve(this.fetchOAuth(this.endpoints.artists, { ids })) .asCallback(callback) } - return Promise.resolve(this.fetch(this.endpoints.artists, { ids })) + return Promise.resolve(this.fetchOAuth(this.endpoints.artists, { ids })) } /** @@ -219,11 +203,11 @@ class Client { /* istanbul ignore else */ if (callback) { return Promise - .resolve(this.fetch(url)) + .resolve(this.fetchOAuth(url)) .asCallback(callback) } - return Promise.resolve(this.fetch(url)) + return Promise.resolve(this.fetchOAuth(url)) } /** @@ -236,11 +220,11 @@ class Client { /* istanbul ignore else */ if (callback) { return Promise - .resolve(this.fetch(this.endpoints.tracks, { ids })) + .resolve(this.fetchOAuth(this.endpoints.tracks, { ids })) .asCallback(callback) } - return Promise.resolve(this.fetch(this.endpoints.tracks, { ids })) + return Promise.resolve(this.fetchOAuth(this.endpoints.tracks, { ids })) } /** From ddf62ab47ecf4861bccd7c66ad9d3ccaf3bc81e5 Mon Sep 17 00:00:00 2001 From: luixlacrux Date: Wed, 21 Jun 2017 11:04:28 -0400 Subject: [PATCH 2/7] [update] handle error when client crendentials are not provided --- lib/client.js | 60 +++++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/lib/client.js b/lib/client.js index 4ee6518..a4eacaf 100644 --- a/lib/client.js +++ b/lib/client.js @@ -27,21 +27,25 @@ class Client { * @param {string} path - endpoint to send request * @param {Object} [params] - querystrings */ - fetchOAuth (path, params) { + fetch (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) - })) + return new Promise((resolve, reject) => { + this.getToken() + .then((token) => { + const opts = { + method: 'GET', + uri: path, + headers: { + 'Authorization': `Bearer ${token}` + }, + json: true + } + + resolve(request(opts)) + }) + .catch((err) => reject(new Error(err))) + }) } getToken () { @@ -119,7 +123,7 @@ class Client { .asCallback(callback) } - return Promise.resolve(this.fetchOAuth(url)) + return Promise.resolve(this.fetch(url)) } /** @@ -132,11 +136,11 @@ class Client { /* istanbul ignore else */ if (callback) { return Promise - .resolve(this.fetchOAuth(this.endpoints.albums, { ids })) + .resolve(this.fetch(this.endpoints.albums, { ids })) .asCallback(callback) } - return Promise.resolve(this.fetchOAuth(this.endpoints.albums, { ids })) + return Promise.resolve(this.fetch(this.endpoints.albums, { ids })) } /** @@ -169,11 +173,11 @@ class Client { /* istanbul ignore else */ if (callback) { 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)) } /** @@ -186,11 +190,11 @@ class Client { /* istanbul ignore else */ if (callback) { return Promise - .resolve(this.fetchOAuth(this.endpoints.artists, { ids })) + .resolve(this.fetch(this.endpoints.artists, { ids })) .asCallback(callback) } - return Promise.resolve(this.fetchOAuth(this.endpoints.artists, { ids })) + return Promise.resolve(this.fetch(this.endpoints.artists, { ids })) } /** @@ -203,11 +207,11 @@ class Client { /* istanbul ignore else */ if (callback) { return Promise - .resolve(this.fetchOAuth(url)) + .resolve(this.fetch(url)) .asCallback(callback) } - return Promise.resolve(this.fetchOAuth(url)) + return Promise.resolve(this.fetch(url)) } /** @@ -220,11 +224,11 @@ class Client { /* istanbul ignore else */ if (callback) { return Promise - .resolve(this.fetchOAuth(this.endpoints.tracks, { ids })) + .resolve(this.fetch(this.endpoints.tracks, { ids })) .asCallback(callback) } - return Promise.resolve(this.fetchOAuth(this.endpoints.tracks, { ids })) + return Promise.resolve(this.fetch(this.endpoints.tracks, { ids })) } /** @@ -247,11 +251,11 @@ class Client { /* istanbul ignore else */ if (callback) { 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)) } /** @@ -271,11 +275,11 @@ class Client { /* istanbul ignore else */ if (callback) { 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)) } } From ace6c3bb1824a5ee31e6fd49f65da23deadd2dc4 Mon Sep 17 00:00:00 2001 From: luixlacrux Date: Wed, 21 Jun 2017 11:16:51 -0400 Subject: [PATCH 3/7] [add] client config for tests --- test/client-config.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 test/client-config.js diff --git a/test/client-config.js b/test/client-config.js new file mode 100644 index 0000000..6360770 --- /dev/null +++ b/test/client-config.js @@ -0,0 +1,31 @@ +const Spotify = require('../lib/client') + +const config = { + auth: 'https://accounts.spotify.test/api/token', + consumer: { + key: 'NgA6ZcYIixn8bUQ', + secret: 'ixn8bUQNgA6ZcYI' + } +} + +const _extends = function (target) { + for (var i = 1; i < arguments.length; i++) { + var source = arguments[i] + for (var key in source) { + target[key] = source[key] + } + } + + return target +} + +const encode = new Buffer(`${config.consumer.key}:${config.consumer.secret}`).toString('base64') +exports.headers = { 'Authorization': `Basic ${encode}` } + +exports.clientBad = new Spotify({ + auth: 'https://accounts.spotify.test/api/token' +}) + +exports['default'] = new Spotify(config) + +module.exports = _extends(exports['default'], exports) From 675c2e2ece46ffd2831f1a8d8e7d8a55d3a93b9d Mon Sep 17 00:00:00 2001 From: luixlacrux Date: Wed, 21 Jun 2017 11:23:15 -0400 Subject: [PATCH 4/7] [update] oauth test was passed --- test/oauth-test.js | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/test/oauth-test.js b/test/oauth-test.js index 43cbd81..5bbfae1 100644 --- a/test/oauth-test.js +++ b/test/oauth-test.js @@ -2,26 +2,14 @@ const test = require('tape') 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('') .reply(200, response) @@ -32,7 +20,7 @@ test('should return an access token', (t) => { }) test('should fail without client credentials', (t) => { - nock(config.auth, { reqheaders: headers }) + nock(client.authURL, { reqheaders: headers }) .post('/') .reply(200, response) From 8a825da1f983df6ec305b29af83710a7349946cf Mon Sep 17 00:00:00 2001 From: luixlacrux Date: Wed, 21 Jun 2017 12:28:58 -0400 Subject: [PATCH 5/7] [update] all test been success --- test/albums-test.js | 15 ++++++--------- test/artists-test.js | 19 ++++++++----------- test/browse-test.js | 22 +++++----------------- test/category-test.js | 21 ++++----------------- test/client-config.js | 1 + test/search-test.js | 11 ++++------- test/tracks-test.js | 13 +++++-------- 7 files changed, 33 insertions(+), 69 deletions(-) diff --git a/test/albums-test.js b/test/albums-test.js index fa70198..2cf5e7b 100644 --- a/test/albums-test.js +++ b/test/albums-test.js @@ -2,10 +2,7 @@ const test = require('tape') const nock = require('nock') -const Spotify = require('../lib/client') - -const url = 'https://api.spotify.test' -const client = new Spotify({ url }) +const client = require('./client-config') test('should get single an album', (t) => { const albumId = '6Kssm2LosQ0WyLukFZkEG5' @@ -13,7 +10,7 @@ test('should get single an album', (t) => { t.equals(typeof client.getAlbum, 'function', 'should be a function') - nock(url).get(`/albums/${albumId}`) + nock(client.baseURL).get(`/albums/${albumId}`) .reply(200, response) client.getAlbum(albumId, { tracks: false }).then((album) => { @@ -27,7 +24,7 @@ test('should get album tracks', (t) => { const albumId = '6Kssm2LosQ0WyLukFZkEG5' const response = { items: [] } - nock(url).get(`/albums/${albumId}/tracks`) + nock(client.baseURL).get(`/albums/${albumId}/tracks`) .reply(200, response) client.getAlbum(albumId, { tracks: true }).then((tracks) => { @@ -40,7 +37,7 @@ test('should get several albums', (t) => { const ids = ['6Kssm2LosQ0WyLukFZkEG5', '56yYgfX6M5FlpETfyZSHkn'] const response = { albums: [] } - nock(url).get('/albums') + nock(client.baseURL).get('/albums') .query({ ids: ids.toString() }) .reply(200, response) @@ -58,7 +55,7 @@ test('should get single an album and return a callback', (t) => { const albumId = '6Kssm2LosQ0WyLukFZkEG5' const response = { id: albumId, name: 'Demi' } - nock(url).get(`/albums/${albumId}`) + nock(client.baseURL).get(`/albums/${albumId}`) .reply(200, response) client.getAlbum(albumId, { tracks: false }, (err, album) => { @@ -73,7 +70,7 @@ test('should get several albums and return a callback', (t) => { const ids = ['6Kssm2LosQ0WyLukFZkEG5', '56yYgfX6M5FlpETfyZSHkn'] const response = { albums: [] } - nock(url).get('/albums') + nock(client.baseURL).get('/albums') .query({ ids: ids.toString() }) .reply(200, response) diff --git a/test/artists-test.js b/test/artists-test.js index a8ec23f..f6ecc2f 100644 --- a/test/artists-test.js +++ b/test/artists-test.js @@ -2,16 +2,13 @@ const test = require('tape') const nock = require('nock') -const Spotify = require('../lib/client') - -const url = 'https://api.spotify.test' -const client = new Spotify({ url }) +const client = require('./client-config') test('should get single an artist', (t) => { const artistId = '6S2OmqARrzebs0tKUEyXyp' const response = { id: artistId, name: 'Demi Lovato' } - nock(url).get(`/artists/${artistId}`) + nock(client.baseURL).get(`/artists/${artistId}`) .query({ country: 'SE' }) .reply(200, response) @@ -27,7 +24,7 @@ test('should get an artist albums', (t) => { const opts = { country: 'SE', albums: true } const response = { items: [] } - nock(url).get(`/artists/${artistId}/albums`) + nock(client.baseURL).get(`/artists/${artistId}/albums`) .query({ country: 'SE' }) .reply(200, response) @@ -43,7 +40,7 @@ test('should get an artist top tracks', (t) => { const opts = { country: 'SE', topTracks: true } const response = { items: [] } - nock(url).get(`/artists/${artistId}/top-tracks`) + nock(client.baseURL).get(`/artists/${artistId}/top-tracks`) .query({ country: 'SE' }) .reply(200, response) @@ -59,7 +56,7 @@ test('should get related artists with an artist', (t) => { const opts = { country: 'SE', relatedArtists: true } const response = { items: [] } - nock(url).get(`/artists/${artistId}/related-artists`) + nock(client.baseURL).get(`/artists/${artistId}/related-artists`) .query({ country: 'SE' }) .reply(200, response) @@ -74,7 +71,7 @@ test('should get several artists', (t) => { const ids = ['6S2OmqARrzebs0tKUEyXyp', '0C8ZW7ezQVs4URX5aX7Kqx'] const response = { items: [] } - nock(url).get('/artists') + nock(client.baseURL).get('/artists') .query({ ids: ids.toString() }) .reply(200, response) @@ -94,7 +91,7 @@ test('should get single an artist and return a callback', (t) => { const querys = { country: 'SE' } const response = { id: artistId, name: 'Demi Lovato' } - nock(url).get(`/artists/${artistId}`) + nock(client.baseURL).get(`/artists/${artistId}`) .query(querys) .reply(200, response) @@ -110,7 +107,7 @@ test('should get several artists and return al callback', (t) => { const ids = ['6S2OmqARrzebs0tKUEyXyp', '0C8ZW7ezQVs4URX5aX7Kqx'] const response = { items: [] } - nock(url).get('/artists') + nock(client.baseURL).get('/artists') .query({ ids: ids.toString() }) .reply(200, response) diff --git a/test/browse-test.js b/test/browse-test.js index 4b4b695..92f98bf 100644 --- a/test/browse-test.js +++ b/test/browse-test.js @@ -2,25 +2,13 @@ const test = require('tape') const nock = require('nock') -const Spotify = require('../lib/client') - -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}` } +const client = require('./client-config') test('should get new releases', (t) => { const opts = { to: 'new-releases', country: 'SE' } const response = { albums: [] } - nock(config.url, { reqheaders: headers }) + nock(client.baseURL) .get('/browse/new-releases') .query({ country: 'SE' }) .reply(200, response) @@ -36,7 +24,7 @@ test('should get featured playlists', (t) => { const opts = { to: 'featured-playlists', country: 'SE' } const response = { playlists: [] } - nock(config.url, { reqheaders: headers }) + nock(client.baseURL) .get('/browse/featured-playlists') .query({ country: 'SE' }) .reply(200, response) @@ -52,7 +40,7 @@ test('should get categories', (t) => { const opts = { to: 'categories', country: 'SE' } const response = { categories: [] } - nock(config.url, { reqheaders: headers }) + nock(client.baseURL) .get('/browse/categories') .query({ country: 'SE' }) .reply(200, response) @@ -72,7 +60,7 @@ test('should get new releases and return a callback', (t) => { const opts = { to: 'new-releases', country: 'SE' } const response = { albums: [] } - nock(config.url, { reqheaders: headers }) + nock(client.baseURL) .get('/browse/new-releases') .query({ country: 'SE' }) .reply(200, response) diff --git a/test/category-test.js b/test/category-test.js index f91f1a3..474b6f2 100644 --- a/test/category-test.js +++ b/test/category-test.js @@ -2,26 +2,13 @@ const test = require('tape') const nock = require('nock') -const Spotify = require('../lib/client') - -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}` } +const client = require('./client-config') test('should get an category', (t) => { const catId = 'toptracks' const response = { id: catId, name: 'toptracks' } - nock(config.url, { reqheaders: headers }) + nock(client.baseURL) .get(`/browse/categories/${catId}`) .reply(200, response) @@ -36,7 +23,7 @@ test('should get the playlists from a category', (t) => { const catId = 'toptracks' const response = { playlists: [] } - nock(config.url, { reqheaders: headers }) + nock(client.baseURL) .get(`/browse/categories/${catId}/playlists?`) .reply(200, response) @@ -55,7 +42,7 @@ test('should get an category and return a callback', (t) => { const catId = 'toptracks' const response = { id: catId, name: 'toptracks' } - nock(config.url, { reqheaders: headers }) + nock(client.baseURL) .get(`/browse/categories/${catId}?`) .reply(200, response) diff --git a/test/client-config.js b/test/client-config.js index 6360770..285990f 100644 --- a/test/client-config.js +++ b/test/client-config.js @@ -1,6 +1,7 @@ const Spotify = require('../lib/client') const config = { + url: 'https://api.spotify.test', auth: 'https://accounts.spotify.test/api/token', consumer: { key: 'NgA6ZcYIixn8bUQ', diff --git a/test/search-test.js b/test/search-test.js index f737c5e..2516336 100644 --- a/test/search-test.js +++ b/test/search-test.js @@ -2,10 +2,7 @@ const test = require('tape') const nock = require('nock') -const Spotify = require('../lib/client') - -const url = 'https://api.spotify.test' -const client = new Spotify({ url }) +const client = require('./client-config.js') test('should search artist, album, track', (t) => { const querys = { @@ -18,7 +15,7 @@ test('should search artist, album, track', (t) => { t.equals(typeof client.search, 'function', 'should be a function') - nock(url).get('/search') + nock(client.baseURL).get('/search') .query(querys) .reply(200, response) @@ -40,7 +37,7 @@ test('should search only artists', (t) => { const response = { artists: [] } - nock(url).get('/search') + nock(client.baseURL).get('/search') .query(querys) .reply(200, response) @@ -60,7 +57,7 @@ test('should do search and return a callback', (t) => { const response = { artists: [] } - nock(url).get('/search') + nock(client.baseURL).get('/search') .query(querys) .query({ type: 'artist,album,track' }) .reply(200, response) diff --git a/test/tracks-test.js b/test/tracks-test.js index f1cd048..898a3c7 100644 --- a/test/tracks-test.js +++ b/test/tracks-test.js @@ -2,16 +2,13 @@ const test = require('tape') const nock = require('nock') -const Spotify = require('../lib/client') - -const url = 'https://api.spotify.test' -const client = new Spotify({ url }) +const client = require('./client-config') test('should get an track', (t) => { const trackId = 'eGsygTp906u18L0Oimnem' const response = { id: trackId } - nock(url).get(`/tracks/${trackId}`) + nock(client.baseURL).get(`/tracks/${trackId}`) .reply(200, response) client.getTrack(trackId).then((track) => { @@ -24,7 +21,7 @@ test('should get several tracks', (t) => { const ids = ['eGsygTp906u18L0Oimnem', 'dsadeTd465hfdg45hddsd'] const response = { items: [] } - nock(url).get('/tracks') + nock(client.baseURL).get('/tracks') .query({ ids: ids.toString() }) .reply(200, response) @@ -42,7 +39,7 @@ test('should get an track and return a callback', (t) => { const trackId = 'eGsygTp906u18L0Oimnem' const response = { id: trackId } - nock(url).get(`/tracks/${trackId}`) + nock(client.baseURL).get(`/tracks/${trackId}`) .reply(200, response) client.getTrack(trackId, (err, track) => { @@ -56,7 +53,7 @@ test('should get several tracks and return an callback', (t) => { const ids = ['eGsygTp906u18L0Oimnem', 'dsadeTd465hfdg45hddsd'] const response = { items: [] } - nock(url).get('/tracks') + nock(client.baseURL).get('/tracks') .query({ ids: ids.toString() }) .reply(200, response) From fc8f902215d92ff2c56bad4cc56449607a655758 Mon Sep 17 00:00:00 2001 From: luixlacrux Date: Wed, 21 Jun 2017 13:47:55 -0400 Subject: [PATCH 6/7] [update] README.md --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 09c89ef..9b1d866 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,12 @@ $ npm install spotify-finder import Spotify from 'spotify-finder' const client = new Spotify({ 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.2.0 is required + secret: 'YOUR_CLIENT_SECRET' // from v2.2.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/#!/). #### Search for all types ```js From 5ec82e827b3d8b8253966351b76027b10092f82e Mon Sep 17 00:00:00 2001 From: luixlacrux Date: Wed, 21 Jun 2017 14:06:09 -0400 Subject: [PATCH 7/7] 2.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2c60d93..f9c4727 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "spotify-finder", - "version": "2.0.2", + "version": "2.1.0", "description": "A isomorphic Spotify API client", "main": "lib/client.js", "scripts": {