-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (141 loc) · 4.69 KB
/
index.js
File metadata and controls
166 lines (141 loc) · 4.69 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Utils
import APIError from "./src/APIError.js";
import axios from "axios";
import { Args } from "./src/utils/utils.js";
// Sections
import Bridge from "./src/sections/bridge.js";
import Bridgelock from "./src/sections/bridgelock.js";
import Experimental from "./src/sections/experimental.js";
import GTW from "./src/sections/gtw.js";
import Inactive from "./src/sections/inactive.js";
import Longpoll from "./src/sections/longpoll.js";
import Minigames from "./src/sections/minigames.js";
import Score from "./src/sections/score.js";
import Server from "./src/sections/server.js";
import Services from "./src/sections/services.js";
import Staff from "./src/sections/staff.js";
import Stats from "./src/sections/stats.js";
import Token from "./src/sections/token.js";
export default class SCFAPIClient {
#provider;
#discord_token;
#scf_token;
#features = {};
// Sections
API = {
bridge: new Bridge(this),
bridgelock: new Bridgelock(this),
experimental: new Experimental(this),
gtw: new GTW(this),
inactive: new Inactive(this),
longpoll: new Longpoll(this),
minigames: new Minigames(this),
score: new Score(this),
server: new Server(this),
services: new Services(this),
staff: new Staff(this),
stats: new Stats(this),
token: new Token(this),
};
/**
*
* @param {String} provider
* @param {?String} discord_token
* @param {?String} scf_token
*/
constructor(provider, discord_token = null, scf_token = null) {
this.#provider = provider;
if (!discord_token && !scf_token) {
throw new APIError("Either Discord Token or SCF Token must be provided.", {
type: "internal",
});
}
this.#discord_token = discord_token;
this.#scf_token = scf_token;
}
async sendAPIRequest(section, method, http_method, params, auth = true) {
try {
const provider = new URL(this.#provider);
provider.searchParams.append("method", `${section}.${method}`);
let config = {
method: http_method,
headers: {
"User-Agent": "SCF-API-Client",
"Content-Type": "application/json",
},
};
for (const param of params) {
if (param.type == Args.GET) {
provider.searchParams.append(param.name, param.value);
}
if (param.type == Args.POST) {
if (!config.data) config.data = {};
config.data[param.name] = param.value;
}
}
config.url = provider.href;
if (auth) {
let token = await this.getToken();
config.headers["Authorization"] = `Bearer ${token}`;
}
if (this.#features.test) {
config.params = Object.fromEntries(provider.searchParams);
this.#features.test(config);
return;
}
let response;
try {
response = await axios(config);
} catch (error) {
throw new APIError("Failed to send API request.", {
type: "request_failed",
axios: error,
});
}
if (!response?.data) {
throw new APIError("API has returned no data.", {
type: "request_failed",
axios: response,
});
}
let data = response.data;
if (data.code && !data.success) {
throw new APIError(data.message, {
type: data.code,
axios: response,
});
}
return data;
} catch (e) {
if ((e instanceof APIError) && this.#features.error) {
this.#features.error(e);
}
throw e;
}
}
async getToken() {
if (!this.#scf_token) {
await this.updateToken();
}
return this.#scf_token;
}
async updateToken() {
if (!this.#discord_token) {
throw new APIError("Discord Token is required to authorize.");
}
try {
let response = await this.API.token.auth(this.#discord_token);
this.#scf_token = response.token;
} catch (e) {
if (e?.type == "APIError") {
console.log(e);
}
}
}
testMode(callback) {
this.#features.test = callback;
}
errorHandler(callback){
this.#features.error = callback;
}
}