-
Notifications
You must be signed in to change notification settings - Fork 9
/
RestRequestManager.ts
113 lines (96 loc) · 3.17 KB
/
RestRequestManager.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import fetch from 'node-fetch';
import { EventEmitter } from 'events';
import type { BaseManager } from '../structures/BaseManager';
import { APIErrorResponse, PteroAPIError, RequestError } from '../structures/Errors';
import { FetchOptions } from '../common';
import { buildQuery } from '../util/query';
import { version } from '../../package.json';
export type Method =
| 'GET'
| 'POST'
| 'PATCH'
| 'PUT'
| 'DELETE';
export class RestRequestManager extends EventEmitter {
private type: string;
public domain: string;
public auth: string;
public ping: number;
constructor(type: string, domain: string, auth: string) {
super();
this.type = type;
this.domain = domain;
this.auth = auth;
this.ping = -1;
}
getHeaders(): { [key: string]: string } {
return {
'User-Agent': `${this.type} PteroJS v${version}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${this.auth}`
}
}
private debug(message: string): void {
//super.emit('debug', message);
}
async _make(
path: string,
params: object | undefined,
method: Method
): Promise<object | Buffer | void> {
let body = undefined;
if (params !== undefined) {
if ('raw' in params) body = (params as { raw: string }).raw;
else body = JSON.stringify(params);
}
const start = Date.now();
const res = await fetch(
`${this.domain}/api/${this.type.toLowerCase()}${path}`,
{
method,
body,
headers: this.getHeaders()
}
);
this.ping = Date.now() - start;
this.debug(`received status: ${res.status} (${this.ping}ms)`);
if ([202, 204].includes(res.status)) return;
let data: object | Buffer;
if (res.headers.get('content-type') === 'application/json') {
data = await res.json().catch(null);
} else {
data = await res.buffer().catch(null);
}
if (data) {
//super.emit('reveive', data);
if (res.ok) return data;
if (res.status >= 400 && res.status < 500)
throw new PteroAPIError(data as APIErrorResponse);
}
throw new RequestError(
'Pterodactyl API returned an invalid or unacceptable response '+
`(status: ${res.status})`
);
}
async get(
path: string,
params: FetchOptions,
cls: BaseManager
): Promise<object | Buffer> {
const query = buildQuery(params, cls.getQueryOptions());
return this._make(path + query, undefined, 'GET') as object | Buffer;
}
async post(path: string, data: object = {}) {
return this._make(path, data, 'POST');
}
async patch(path: string, data: object = {}) {
return this._make(path, data, 'PATCH');
}
async put(path: string, data: object = {}) {
return this._make(path, data, 'PUT');
}
async delete(path: string) {
return this._make(path, undefined, 'DELETE');
}
}