erium
Version:
Erium is Discord Bot Library made in typescript
63 lines (59 loc) • 2.39 kB
text/typescript
import Client from "../structures/Client";
import axios, { AxiosInstance, Method } from "axios";
import { Endpoints } from "../Endpoints";
export class RequestHandler {
private client: Client;
private readonly instance: AxiosInstance;
// @ts-ignore
private BASE_URL: Endpoints.BASE_URL;
public constructor(client: Client) {
this.client = client;
this.instance = axios.create({
// @ts-ignore
baseURL: this.BASE_URL,
timeout: 1000,
headers: {
"Authorization": client.token
}
});
}
public async request(method: Method, url: string, data?: any): Promise<any> {
const response = await this.instance({
url: url,
method: method,
data: data
}).catch(err => {
switch (err.code) {
case "400":
return new Error("Bad Request (code: 400)" + method + url + data + err.code);
break;
case "401":
return new Error("Unauthorized (code: 401)" + method + url + data + err.code);
break;
case "402":
break;
case "403":
return new Error("Forbidden (code: 403)" + method + url + data + err.code);
break;
case "404":
return new Error("Not Found (code: 404)" + method + url + data + err.code);
break;
case "405":
return new Error("Method not Allowed (code: 405)" + method + url + data + err.code);
break;
case "429":
return new Error("Too many Requests (code: 429)" + method + url + data + err.code);
break;
case "502":
return new Error("Gateway Unavailable (code: 502)" + method + url + data + err.code);
break;
default:
return new Error(`Unknown error Code (code: ${err.code})` + method + url + data + err.code);
break;
}
});
if (response instanceof Error) return response;
if (response) return response.data;
else return new Error("no response from api" + method + url + data);
}
}