Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async ping() {
const options = {
headers: {
'User-Agent': `Simplenote/${app.getVersion()}`,
},
};
try {
const releaseResp = await fetch(this.apiUrl, options);
if (releaseResp.status !== 200) {
this.emit('error');
console.log(releaseResp); // eslint-disable-line no-console
return;
}
const releaseBody = await releaseResp.json();
const releaseAsset = releaseBody.assets.find(
release => release.name === 'latest.yml'
);
if (releaseAsset) {
const configResp = await fetch(
releaseAsset.browser_download_url,
options
checkUpdates( version ) {
return fetch( this.rawRepo )
.then(res => res.json() )
.then( json => {
if( json.version > version ) {
this.updateWindow = new BrowserWindow( { width: 400, height: 200 } )
this.updateWindow.loadFile( `${ __dirname }/../src/update.html` )
this.window.on( 'closed', f => this.updateWindow = null )
if( process.env.debug ) this.updateWindow.webContents.openDevTools( )
}
} )
}
exports.getToken = function(body) {
body.append('client_id', SPOTIFY_CLIENT_ID);
body.append('client_secret', SPOTIFY_CLIENT_SECRET);
return fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
body: body.toString(),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
.then(res => res.json());
};
exports.getLatestVersion = function() {
return fetch('https://api.github.com/repos/davicorreiajr/spotify-now-playing/releases/latest', {
method: 'GET',
headers: { 'Accept': 'application/vnd.github.v3+json' }
})
.then(res => res.json())
.then(res => ({
version: res.name,
dmgDownloadUrl: res.assets[0].browser_download_url
}));
};
exports.pause = function(accessToken) {
return fetch('https://api.spotify.com/v1/me/player/pause', {
method: 'PUT',
headers: { 'Authorization': `Bearer ${accessToken}` }
});
};
return Promise.all(endpoints.map(endpoint => fetch(endpoint, fetchOptions).then(res => res.json())))
.then(data => data.map(res => res.items).reduce((result, item) => result.concat(item), []))
exports.addTrackToPlaylist = function(accessToken, playlistId, uri) {
return fetch(`https://api.spotify.com/v1/playlists/${playlistId}/tracks?uris=${encodeURIComponent(uri)}`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${accessToken}` }
})
.then(res => res.json());
};
exports.getPlaylists = function(accessToken) {
const limit = 50;
const fetchOptions = {
method: 'GET',
headers: { 'Authorization': `Bearer ${accessToken}` }
};
return fetch(`https://api.spotify.com/v1/me/playlists?limit=${limit}`, fetchOptions)
.then(res => res.json())
.then(json => {
const numberOfRequests = Math.ceil(json.total/limit);
if(numberOfRequests === 1) return json.items;
const endpoints = [...Array(numberOfRequests)].map((_, request) => `https://api.spotify.com/v1/me/playlists?offset=${limit * request}&limit=${limit}`);
return Promise.all(endpoints.map(endpoint => fetch(endpoint, fetchOptions).then(res => res.json())))
.then(data => data.map(res => res.items).reduce((result, item) => result.concat(item), []));
})
.then(data => data.filter(playlist => playlist.collaborative || isPlaylistFromCurrentUser(playlist)));
};
exports.nextTrack = function(accessToken) {
return fetch('https://api.spotify.com/v1/me/player/next', {
method: 'POST',
headers: { 'Authorization': `Bearer ${accessToken}` }
});
};