Personal Site

Move json parsing to user of generic spotify function to account for empty bodies

vielle.dev a7b517f4 d8cd8fb3

verified
+21 -3
+21 -3
src/components/playing/spotify.ts
··· 145 145 }, 146 146 }) 147 147 .then((res) => (!res.ok ? throws(res) : res)) 148 - .then((res) => res.json() as Promise<Record<string, unknown>>) 149 148 .catch((err) => { 150 149 if (err instanceof Response) { 151 150 if (err.status === 401) ··· 168 167 const res = await getSpotifyApi("/me/player/currently-playing"); 169 168 if (res instanceof Error) return res; 170 169 170 + const output = await Promise.resolve(res) 171 + // send "not modified to catch" 172 + .then((res) => (res.status === 204 ? throws(res) : res)) 173 + .then((res) => res.json() as Promise<Record<string, unknown>>) 174 + // res code is 204 175 + .catch((res) => undefined); 176 + 177 + if (!output) return undefined; 178 + 171 179 // https://developer.spotify.com/documentation/web-api/reference/get-the-users-currently-playing-track 172 - return (res as { item: { id: string } | null }).item?.id ?? null; 180 + return (output as { item: { id: string } | null }).item?.id ?? null; 173 181 } 174 182 175 183 export async function getTrack(id: string) { 176 184 const res = await getSpotifyApi("/tracks/" + id); 177 185 if (res instanceof Error) return res; 178 - return res as { 186 + 187 + const output = await Promise.resolve(res) 188 + // send "not modified to catch" 189 + .then((res) => (res.status === 204 ? throws(res) : res)) 190 + .then((res) => res.json() as Promise<Record<string, unknown>>) 191 + // res code is 204 192 + .catch((res) => undefined); 193 + 194 + if (!output) return undefined; 195 + 196 + return output as { 179 197 external_urls: { 180 198 spotify: string; 181 199 };