forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (63 loc) · 1.8 KB
/
index.js
File metadata and controls
63 lines (63 loc) · 1.8 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
// Script example for ScriptAPI
// Author: JaylyMC <https://github.com/JaylyDev>
// Project: https://github.com/JaylyDev/ScriptAPI
import { http as __http, HttpHeader, HttpRequest, HttpRequestMethod } from "@minecraft/server-net";
class authoration {
}
authoration.authorized = false;
authoration.url = "http://localhost:14589";
authoration.PERMISSION_DENIED = "Permission Denied: This request is not authorized.";
;
export class HttpClient {
/**
* @remarks
* Performs a simple HTTP get request.
* @param uri
* URL to make an HTTP Request to.
* @returns
* An awaitable promise that contains the HTTP response.
*/
async get(uri) {
if (authoration.authorized !== true)
throw Error(authoration.PERMISSION_DENIED);
return await __http.get(uri);
}
;
/**
* @remarks
* Performs an HTTP request.
* @param config
* Contains an HTTP Request object with configuration data on
* the HTTP request.
* @returns
* An awaitable promise that contains the HTTP response.
*/
async request(config) {
if (authoration.authorized !== true)
throw Error(authoration.PERMISSION_DENIED);
return await __http.request(config);
}
;
cancelAll(reason) {
http.cancelAll(reason);
}
;
}
;
export async function auth(token) {
const localAuthRequest = new HttpRequest(authoration.url);
localAuthRequest.setMethod(HttpRequestMethod.Post);
localAuthRequest.setHeaders([new HttpHeader("token", token)]);
const response = await __http.request(localAuthRequest);
if (response.status === 200) {
authoration.authorized = true;
return true;
}
else {
authoration.authorized = false;
return false;
}
;
}
;
export const http = new HttpClient();