1- import { AUTH_LOG_IN , AUTH_LOG_OUT , GUILDS } from "./routes" ;
1+ import { AUTH_LOG_IN , AUTH_LOG_OUT , GUILD_CONFIG , GUILDS } from "./routes" ;
22
3- type HttpMethod = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" ;
3+ type HTTPMethod = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" ;
4+
5+ function extractMessage ( body : unknown ) {
6+ if ( typeof body === "string" )
7+ return body ;
8+
9+ if ( typeof body === "object" && body !== null && "error" in body )
10+ return body . error ;
11+
12+ return "No message" ;
13+ }
14+
15+ export class RESTError extends Error {
16+ body : unknown ;
17+
18+ constructor ( status : number , body : unknown ) {
19+ super ( `${ status } - ${ extractMessage ( body ) } ` ) ;
20+ this . name = "RESTError" ;
21+ this . body = body ;
22+ }
23+ }
424
525export interface LogInResponse {
626 token : string ;
@@ -20,9 +40,11 @@ export interface GuildResponse {
2040 ownerID : string ;
2141}
2242
23- async function requestJSON < T > ( route : string , method : HttpMethod , token ?: string , body ?: any ) : Promise < T | ErrorResponse > {
43+ async function request < T > ( route : string , method : HTTPMethod , token ?: string , body ?: any ) : Promise < T > {
2444 const headers = new Headers ;
25- headers . set ( "Content-Type" , "application/json" ) ;
45+
46+ if ( body != null )
47+ headers . set ( "Content-Type" , "application/json" ) ;
2648
2749 if ( token !== undefined )
2850 headers . set ( "Authorization" , token ) ;
@@ -33,22 +55,30 @@ async function requestJSON<T>(route: string, method: HttpMethod, token?: string,
3355 headers,
3456 } ) ;
3557
36- const text = await response . text ( ) ;
37-
38- if ( text . length === 0 )
58+ if ( response . status === 204 )
3959 return null as T ;
4060
41- return JSON . parse ( text ) ;
61+ const isJson = response . headers . get ( "Content-Type" ) === "application/json" ;
62+ const responseBody = isJson ? await response . json ( ) : await response . text ( ) ;
63+
64+ if ( ! response . ok )
65+ throw new RESTError ( response . status , responseBody ) ;
66+
67+ return responseBody ;
4268}
4369
4470export function logIn ( code : string ) {
45- return requestJSON < LogInResponse > ( AUTH_LOG_IN , "POST" , undefined , { code } ) ;
71+ return request < LogInResponse > ( AUTH_LOG_IN , "POST" , undefined , { code } ) ;
4672}
4773
4874export function logOut ( token : string ) {
49- return requestJSON < null > ( AUTH_LOG_OUT , "GET" , token ) ;
75+ return request < null > ( AUTH_LOG_OUT , "GET" , token ) ;
5076}
5177
5278export function getGuilds ( token : string ) {
53- return requestJSON < GuildResponse [ ] > ( GUILDS , "GET" , token ) ;
79+ return request < GuildResponse [ ] > ( GUILDS , "GET" , token ) ;
80+ }
81+
82+ export async function getGuildConfig ( token : string , guildID : string , config : string ) {
83+ return request < string > ( GUILD_CONFIG ( guildID , config ) , "GET" , token ) ;
5484}
0 commit comments