Skip to content

Instantly share code, notes, and snippets.

@borispoehland
Created August 23, 2023 20:08
Show Gist options
  • Save borispoehland/fc25a15f677cb79016c920c5b97238fe to your computer and use it in GitHub Desktop.
Save borispoehland/fc25a15f677cb79016c920c5b97238fe to your computer and use it in GitHub Desktop.
async function getAccessToken() {
return Effect.runPromise(
fetchFresh<{ access_token: string }>(
null,
'https://accounts.spotify.com/api/token',
{
method: 'POST',
headers: {
Authorization: `Basic ${Buffer.from(
`${process.env.SPOTIFY_CLIENT_ID}:${process.env.SPOTIFY_CLIENT_SECRET}`
).toString('base64')}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: String(process.env.SPOTIFY_REFRESH_TOKEN),
}),
}
)
)
}
export async function getCurrentTrack() {
const { access_token } = await getAccessToken()
const program = fetchFresh<{ item: ISong }>(
null,
'https://api.spotify.com/v1/me/player/currently-playing',
{
headers: {
Authorization: `Bearer ${access_token}`,
},
}
).pipe(
Effect.map(({ item }) => item),
Effect.orElseSucceed(() => null)
)
return Effect.runPromise(program)
}
export default async function Page() {
const currentTrack = await getCurrentTrack()
if (!currentTrack) {
return <></>
}
return JSON.stringify(currentTrack)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment