Personal Site
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Create simple util for handling generic spotify API calls

+31
+31
src/components/playing/spotify.ts
··· 132 132 }) 133 133 ); 134 134 } 135 + 136 + export async function getSpotifyApi(url: string) { 137 + const accessToken = await getAccessCode(); 138 + if (!accessToken) 139 + return new Error( 140 + "Failed to get access code. try using src/pages/_callback", 141 + ); 142 + const res = await fetch("https://api.spotify.com/v1" + url, { 143 + headers: { 144 + Authorization: `Bearer ${accessToken}`, 145 + }, 146 + }) 147 + .then((res) => (!res.ok ? throws(res) : res)) 148 + .then((res) => res.json() as Promise<Record<string, unknown>>) 149 + .catch((err) => { 150 + if (err instanceof Response) { 151 + if (err.status === 401) 152 + return new Error("Bad token. Try using /_callback"); 153 + if (err.status === 403) 154 + return new Error("Bad OAuth. Cry about it (???)"); 155 + if (err.status === 429) return new Error("Rate limited. Cry about it"); 156 + console.error(err); 157 + return new Error("Unexpected status code"); 158 + } 159 + if (err instanceof Error) return err; 160 + console.log("Unexpected exception."); 161 + throw err; 162 + }); 163 + 164 + return res; 165 + }