Skip to content

Commit

Permalink
Merge pull request #2 from luixlacrux/develop
Browse files Browse the repository at this point in the history
Relase v2.1.0
  • Loading branch information
luixlacrux authored Jun 21, 2017
2 parents 0c4d33d + 5ec82e8 commit 4228217
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 123 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 20 additions & 32 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,22 @@ class Client {
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
*/
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)
}))
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 () {
Expand Down Expand Up @@ -263,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))
}

/**
Expand All @@ -287,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))
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
15 changes: 6 additions & 9 deletions test/albums-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

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'
const response = { id: albumId, name: 'Demi' }

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) => {
Expand All @@ -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) => {
Expand All @@ -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)

Expand All @@ -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) => {
Expand All @@ -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)

Expand Down
19 changes: 8 additions & 11 deletions test/artists-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
22 changes: 5 additions & 17 deletions test/browse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
21 changes: 4 additions & 17 deletions test/category-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
32 changes: 32 additions & 0 deletions test/client-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 _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)
Loading

0 comments on commit 4228217

Please sign in to comment.