UNPKG

itunes-info

Version:
37 lines (29 loc) 849 B
import request from 'request'; import osLocale from 'os-locale'; export const FILTER = { ALL: '', SONG: '&entity=song', }; const URL = 'https://itunes.apple.com/search?term='; const suffix = '&country=' class ITunes { constructor() { this.filter = FILTER.ALL; } currentCountry() { const locale = osLocale.sync(); const country = locale.split('_')[1]; return country; } fetch(query, country = this.currentCountry()) { return new Promise((resolve, reject) => { const destination = `${URL}${encodeURIComponent(query)}${suffix}${country}${this.filter}`; request(destination, (error, response, body) => { if (!error && response.statusCode == 200) { resolve(JSON.parse(body)); } else { reject(error); } }); }); } } export const iTunes = new ITunes();;