import fetch from 'isomorphic-unfetch' ; import { stringify } from 'querystring' ; const { SPOTIFY_CLIENT_ID : client_id , SPOTIFY_CLIENT_SECRET : client_secret , SPOTIFY_REFRESH_TOKEN : refresh_token , } = process . env ; const basic = Buffer . from ( `${ client_id }:${ client_secret }` ) . toString ( 'base64' ) ; const Authorization = `Basic ${ basic }` ; returns { Promise < string > } async function getAuthorizationToken ( ) { const url = new URL ( 'https://accounts.spotify.com/api/token' ) ; const body = stringify ( { grant_type : 'refresh_token' , refresh_token , } ) ; const response = await fetch ( `${ url }` , { method : 'POST' , headers : { Authorization , 'Content-Type' : 'application/x-www-form-urlencoded' , } , body , } ) . then ( ( r ) => r . json ( ) ) ; return `Bearer ${ response . access_token }` ; } returns { Promise < ICurrentlyPlayingResponse | object > } export async function nowPlaying ( ) { const Authorization = await getAuthorizationToken ( ) ; const response = await fetch ( 'https://api.spotify.com/v1/me/player/currently-playing' , { headers : { Authorization , } , } ) ; const { status } = response ; if ( status === 200 ) { const data = await response . json ( ) ; data . Authorization = Authorization ; return data ; } else { return { context : null , timestamp : 0 , progress_ms : 0 , is_playing : false , item : null , currently_playing_type : '' , actions : { } , Authorization , } ; } } ; returns { Promise < ICursorBasedPagingObject | object > } export async function lastPlayed ( Authorization ) { const response = await fetch ( 'https://api.spotify.com/v1/me/player/recently-played?limit=1' , { headers : { Authorization , } , } ) ; const { status } = response ; if ( status === 200 ) { const data = await response . json ( ) ; const trackResponse = await fetch ( `https://api.spotify.com/v1/tracks/${ data . items [ 0 ] . track . id }` , { headers : { Authorization , } , } ) ; const track = await trackResponse . json ( ) ; return { context : null , timestamp : 1 , progress_ms : 0 , is_playing : false , item : track , currently_playing_type : 'track' , actions : { } , Authorization , } ; } return { context : null , timestamp : 0 , progress_ms : 0 , is_playing : false , item : null , currently_playing_type : '' , actions : { } , Authorization , } ; } ; returns { Promise < IAudioFeaturesResponse | object > } export async function trackAudioFeatures ( id , Authorization ) { const response = await fetch ( `https://api.spotify.com/v1/audio-features/${ id }` , { headers : { Authorization , } , } ) ; const { status } = response ; if ( status === 200 ) { return await response . json ( ) ; } else { return { } ; } } ; param { string } timeRange returns { Promise < Array < ITrackObject > > } export async function topPlayed ( timeRange ) { const Authorization = await getAuthorizationToken ( ) ; const response = await fetch ( `https://api.spotify.com/v1/me/top/tracks?limit=5&time_range=${ timeRange }` , { headers : { Authorization , } , } ) ; const { status } = response ; if ( status === 200 ) { const data = await response . json ( ) ; return data . items ; } else { return [ ] ; } } ;