-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Big refactor to clean the code and improve the tests
- Loading branch information
1 parent
21a921b
commit e37eda4
Showing
12 changed files
with
272 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import SpotifyWrapper from '../src/index'; | ||
|
||
global.fetch = require('node-fetch'); | ||
|
||
const spotify = new SpotifyWrapper({ | ||
token: '', | ||
}); | ||
|
||
const albums = spotify.search.albums('Incubus'); | ||
// console.log(albums); | ||
|
||
albums.then((data) => { | ||
data.albums.items.map(item => console.log(item.name)); | ||
}) | ||
.catch(error => console.log(error)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>Spotify Wrapper Test</title> | ||
</head> | ||
<body> | ||
<div id="albums"></div> | ||
|
||
<script src="../dist/spotify-wrapper.umd.js"></script> | ||
<script> | ||
const spotify = new SpotifyWrapper({ | ||
token: '', | ||
}); | ||
const albums = spotify.search.albums('Incubus'); | ||
const albumsEl = document.getElementById('albums'); | ||
|
||
albums | ||
.then(data => { | ||
markup = data.albums.items.map(item => | ||
`<img src='${item.images[0].url}' alt='${item.name}' />` | ||
).join(''); | ||
|
||
albumsEl.innerHTML = markup; | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
/* global fetch */ | ||
|
||
import API_URL from './config'; | ||
import toJSON from './utils'; | ||
|
||
export const getAlbum = id => fetch(`${API_URL}/albums/${id}`).then(toJSON); | ||
export const getAlbums = id => fetch(`${API_URL}/albums/?ids=${id}`).then(toJSON); | ||
export const getAlbumTracks = id => fetch(`${API_URL}/albums/${id}/tracks`).then(toJSON); | ||
export default function album() { | ||
return { | ||
getAlbum: id => this.request(`${this.apiURL}/albums/${id}`), | ||
getAlbums: id => this.request(`${this.apiURL}/albums/?ids=${id}`), | ||
getTracks: id => this.request(`${this.apiURL}/albums/${id}/tracks`), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,37 @@ | ||
import { | ||
search, | ||
searchArtists, | ||
searchAlbums, | ||
searchPlaylists, | ||
} from './search'; | ||
/* global fetch */ | ||
import search from './search'; | ||
|
||
import { | ||
getAlbum, | ||
getAlbums, | ||
getAlbumTracks, | ||
} from './album'; | ||
import album from './album'; | ||
|
||
module.exports = { | ||
search, | ||
searchArtists, | ||
searchAlbums, | ||
searchPlaylists, | ||
getAlbum, | ||
getAlbums, | ||
getAlbumTracks, | ||
}; | ||
import API_URL from './config'; | ||
import toJSON from './utils'; | ||
|
||
export default class SpotifyWrapper { | ||
constructor(options) { | ||
this.apiURL = options.apiURL || API_URL; | ||
this.token = options.token; | ||
|
||
this.album = album.bind(this)(); | ||
this.search = search.bind(this)(); | ||
} | ||
|
||
/** | ||
* Make the requests to the endpoints | ||
* @param {String} url | ||
*/ | ||
request(url) { | ||
// Regex to verify the URL | ||
const isValidUrl = new RegExp(/^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/); | ||
if (!isValidUrl.test(url)) throw new TypeError('Invalid URL'); | ||
|
||
const headers = { | ||
headers: { | ||
Authorization: `Bearer ${this.token}`, | ||
}, | ||
}; | ||
|
||
return fetch(url, headers) | ||
.then(toJSON) | ||
.catch(error => error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,12 @@ | ||
/* global fetch */ | ||
import API_URL from './config'; | ||
import toJSON from './utils'; | ||
function searcher(type, query) { | ||
return this.request(`${this.apiURL}/search?q=${query}&type=${type}`); | ||
} | ||
|
||
export const search = (query, type) => { | ||
fetch(`${API_URL}/search?q=${query}&type=${type}`) | ||
.then(toJSON); | ||
}; | ||
|
||
export const searchAlbums = (query) => { | ||
search(query, 'album'); | ||
}; | ||
export const searchArtists = (query) => { | ||
search(query, 'artist'); | ||
}; | ||
export const searchTracks = (query) => { | ||
search(query, 'track'); | ||
}; | ||
export const searchPlaylists = (query) => { | ||
search(query, 'playlist'); | ||
}; | ||
export default function search() { | ||
return { | ||
artists: searcher.bind(this, 'artist'), | ||
albums: searcher.bind(this, 'album'), | ||
tracks: searcher.bind(this, 'track'), | ||
playlists: searcher.bind(this, 'playlist'), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
const toJSON = data => data.json(); | ||
const toJSON = (data) => { | ||
if (typeof data === 'undefined') throw new TypeError('data is undefined'); | ||
return data.json(); | ||
}; | ||
export default toJSON; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import chai, { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
|
||
import SpotifyWrapper from '../src/index'; | ||
|
||
chai.use(sinonChai); | ||
|
||
global.fetch = require('node-fetch'); | ||
|
||
describe('SpotifyWrapper Library', () => { | ||
it('should create an instance of SpotifyWrapper', () => { | ||
const spotify = new SpotifyWrapper({}); | ||
expect(spotify).to.be.an.instanceof(SpotifyWrapper); | ||
}); | ||
|
||
it('should receive apiURL as an option', () => { | ||
const spotify = new SpotifyWrapper({ | ||
apiURL: 'bablabla', | ||
}); | ||
expect(spotify.apiURL).to.be.equal('bablabla'); | ||
}); | ||
|
||
it('should use the default apiURL if not provided', () => { | ||
const spotify = new SpotifyWrapper({}); | ||
expect(spotify.apiURL).to.be.equal('https://api.spotify.com/v1'); | ||
}); | ||
|
||
it('should receive token as an option', () => { | ||
const spotify = new SpotifyWrapper({ | ||
token: 'foo', | ||
}); | ||
expect(spotify.token).to.be.equal('foo'); | ||
}); | ||
|
||
describe('request method', () => { | ||
let stubedFetch; | ||
let promise; | ||
|
||
beforeEach(() => { | ||
stubedFetch = sinon.stub(global, 'fetch'); | ||
promise = stubedFetch.returns(Promise.resolve()); | ||
}); | ||
|
||
afterEach(() => { | ||
stubedFetch.restore(); | ||
}); | ||
|
||
it('should have request method', () => { | ||
const spotify = new SpotifyWrapper({}); | ||
expect(spotify.request).to.exist; | ||
}); | ||
|
||
it('should throw TypeError when passed an invalid URL', () => { | ||
const spotify = new SpotifyWrapper({ | ||
token: 'food', | ||
}); | ||
expect(spotify.request.bind(spotify, 'url')).to.throw('Invalid URL'); | ||
}); | ||
|
||
it('should call fetch when request', () => { | ||
const spotify = new SpotifyWrapper({ | ||
token: 'food', | ||
}); | ||
spotify.request('https://api.spotify.com/v1'); | ||
expect(stubedFetch).to.have.been.calledOnce; | ||
}); | ||
|
||
it('should call fetch with the right url passed', () => { | ||
const spotify = new SpotifyWrapper({ | ||
token: 'food', | ||
}); | ||
spotify.request('https://api.spotify.com/v1'); | ||
expect(stubedFetch).to.have.been.calledWith('https://api.spotify.com/v1'); | ||
}); | ||
|
||
it('should call fetch with the right headers passed', () => { | ||
const spotify = new SpotifyWrapper({ | ||
token: 'food', | ||
}); | ||
const headers = { | ||
headers: { | ||
Authorization: 'Bearer food', | ||
}, | ||
}; | ||
spotify.request('https://api.spotify.com/v1'); | ||
expect(stubedFetch).to.have.been.calledWith('https://api.spotify.com/v1', headers); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.