-
Notifications
You must be signed in to change notification settings - Fork 0
/
REST.ts
99 lines (84 loc) · 2.9 KB
/
REST.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import EventEmitter from "events";
import { RequestMethod, RESTEvents } from "./@enum";
import type { InternalRequest, RequestData, RestEvents, RESTOptions, RouteLike } from "./@types";
import { RequestManager } from "./RequestManager";
export class REST extends EventEmitter<RestEvents> {
readonly requestManager: RequestManager;
constructor(options: Partial<RESTOptions> = {}) {
super({ captureRejections: true });
this.requestManager = new RequestManager(options)
.on(RESTEvents.Error, this.emit.bind(this, RESTEvents.Error))
.on(RESTEvents.RateLimited, this.emit.bind(this, RESTEvents.RateLimited));
}
get token() {
return this.requestManager.token;
}
/**
* Sets the authorization token that should be used for requests
*
* @param token - The authorization token to use
*/
setToken(token: string) {
this.requestManager.setToken(token);
return this;
}
/**
* Runs a get request from the api
*
* @param fullRoute - The full route to query
* @param options - Optional request options
*/
get<T = any>(fullRoute: RouteLike, options: RequestData = {}): Promise<T> {
return this.request(Object.assign(options, { fullRoute, method: RequestMethod.Get }));
}
/**
* Runs a delete request from the api
*
* @param fullRoute - The full route to query
* @param options - Optional request options
*/
delete<T = any>(fullRoute: RouteLike, options: RequestData = {}): Promise<T> {
return this.request(Object.assign(options, { fullRoute, method: RequestMethod.Delete }));
}
/**
* Runs a post request from the api
*
* @param fullRoute - The full route to query
* @param options - Optional request options
*/
post<T = any>(fullRoute: RouteLike, options: RequestData = {}): Promise<T> {
return this.request(Object.assign(options, { fullRoute, method: RequestMethod.Post }));
}
/**
* Runs a put request from the api
*
* @param fullRoute - The full route to query
* @param options - Optional request options
*/
put<T = any>(fullRoute: RouteLike, options: RequestData = {}): Promise<T> {
return this.request(Object.assign(options, { fullRoute, method: RequestMethod.Put }));
}
/**
* Runs a request from the api
*
* @param options - Request options
*/
async request<T = any>(options: InternalRequest): Promise<T> {
const res = await this.raw(options);
const contentType = res.headers.get("content-type");
if (contentType?.includes("application/json"))
return res.json() as T;
if (contentType?.includes("text/"))
return res.text() as T;
return res.arrayBuffer() as T;
}
/**
* Runs a request from the API, yielding the raw Response object
*
* @param options - Request options
*/
raw(options: InternalRequest) {
const request = this.requestManager.resolveRequest(options);
return this.requestManager.request(request.url, request.options);
}
}