Personal Site
0
fork

Configure Feed

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

Make SpotifyError nicer to work with and move "throws" to /utils

+56 -18
+2 -1
src/components/playing/spotify/access.ts
··· 4 4 SPOTIFY_CLIENT_SECRET, 5 5 SPOTIFY_REDIRECT_URI, 6 6 } from "astro:env/server"; 7 - import { SpotifyError, throws } from "./errors"; 7 + import { SpotifyError } from "./errors"; 8 + import { throws } from "/utils"; 8 9 import { isAuthToken, isRefreshToken } from "./types"; 9 10 10 11 /**
+2 -2
src/components/playing/spotify/api.ts
··· 1 1 import getAccessCode from "./access"; 2 - import { SpotifyError, throws } from "./errors"; 2 + import { SpotifyError } from "./errors"; 3 3 import { isNowPlaying, type nowPlaying } from "./types"; 4 - import { isObj } from "/utils"; 4 + import { isObj, throws } from "/utils"; 5 5 6 6 /** 7 7 * Wrapper for authorizing a spotify API with default headers etc
+42 -13
src/components/playing/spotify/errors.ts
··· 1 + import { isObj } from "/utils"; 2 + 3 + export const spotErrs = [ 4 + "NO_AUTH", 5 + "INVALID_AUTH_RES", 6 + "UNHANDLED_API_ERR", 7 + "RATE_LIMITED", 8 + "NO_CONTENT", 9 + "MALFORMED_SPOTIFY_RES", 10 + ] as const; 11 + 1 12 export class SpotifyError { 2 - code: "NO_AUTH" | "INVALID_AUTH_RES" | "UNHANDLED_API_ERR" | "RATE_LIMITED" | "NO_CONTENT" | "MALFORMED_SPOTIFY_RES"; 13 + code: typeof spotErrs[number]; 3 14 details: unknown; 4 15 human: string; 5 16 17 + constructor(code: SpotifyError["code"], details: unknown, human: string); 18 + constructor(error: SpotifyErrorInit); 6 19 constructor( 7 - code: SpotifyError["code"], 8 - details: unknown, 9 - human: string = code, 20 + code: SpotifyError["code"] | SpotifyErrorInit, 21 + details?: unknown, 22 + human?: string, 10 23 ) { 11 - this.code = code; 12 - this.details = details; 13 - this.human = human; 24 + if (typeof code === "string") { 25 + this.code = code; 26 + this.details = details; 27 + this.human = human ?? code; 28 + return; 29 + } 30 + 31 + this.code = code.code; 32 + this.details = code.details; 33 + this.human = code.human ?? code.code; 14 34 } 15 35 } 16 36 17 - /** 18 - * Throw passed value (for ternaries/etc) 19 - * @param val value to throw 20 - */ 21 - export function throws(val: unknown): never { 22 - throw val; 37 + export interface SpotifyErrorInit { 38 + code: SpotifyError["code"]; 39 + details: unknown; 40 + human?: string; 23 41 } 42 + 43 + export const isSpotifyError = ( 44 + err: unknown, 45 + ): err is SpotifyError | SpotifyErrorInit => 46 + isObj(err) && 47 + "code" in err && 48 + typeof err.code === "string" && 49 + spotErrs.reduce((acc, cur) => acc || cur === err.code, false) && 50 + "details" in err && 51 + ("human" in err ? typeof err.human === "string" : true); 52 +
+10 -2
src/utils.ts
··· 1 - /** 1 + /** 2 2 * via: https://www.totaltypescript.com/concepts/the-prettify-helper 3 3 */ 4 4 export type Prettify<T> = { ··· 7 7 8 8 export function isObj(obj: unknown): obj is object { 9 9 return typeof obj === "object" && obj !== null; 10 - } 10 + } 11 + 12 + /** 13 + * Throw passed value (for ternaries/etc) 14 + * @param val value to throw 15 + */ 16 + export function throws(val: unknown): never { 17 + throw val; 18 + }