1+ import { http as Http , HttpHeader , HttpRequest , HttpRequestMethod , HttpResponse , HttpClient as httpClient } from "mojang-net" ;
2+
3+ class authoration {
4+ static authorized : boolean = false ;
5+ static url : string = "http://localhost:14589" ;
6+ static readonly PERMISSION_DENIED = "Permission Denied: This request is not authorized." ;
7+ } ;
8+
9+ export class HttpClient extends httpClient {
10+ /**
11+ * @remarks
12+ * Performs a simple HTTP get request.
13+ * @param uri
14+ * URL to make an HTTP Request to.
15+ * @returns
16+ * An awaitable promise that contains the HTTP response.
17+ */
18+ async get ( uri : string ) : Promise < HttpResponse > {
19+ if ( authoration . authorized !== true ) throw Error ( authoration . PERMISSION_DENIED ) ;
20+ return await Http . get ( uri ) ;
21+ } ;
22+ /**
23+ * @remarks
24+ * Performs an HTTP request.
25+ * @param config
26+ * Contains an HTTP Request object with configuration data on
27+ * the HTTP request.
28+ * @returns
29+ * An awaitable promise that contains the HTTP response.
30+ */
31+ async request ( config : HttpRequest ) : Promise < HttpResponse > {
32+ if ( authoration . authorized !== true ) throw Error ( authoration . PERMISSION_DENIED ) ;
33+ return await Http . request ( config ) ;
34+ } ;
35+ constructor ( ) {
36+ super ( )
37+ } ;
38+ } ;
39+
40+ export async function auth ( token : string ) : Promise < boolean > {
41+ const localAuthRequest = new HttpRequest ( authoration . url ) ;
42+ localAuthRequest . setMethod ( HttpRequestMethod . POST ) ;
43+ localAuthRequest . setHeaders ( [ new HttpHeader ( "token" , token ) ] ) ;
44+
45+ const response = await Http . request ( localAuthRequest ) ;
46+
47+ if ( response . status === 200 ) {
48+ authoration . authorized = true ;
49+ return true ;
50+ }
51+ else {
52+ authoration . authorized = false ;
53+ return false ;
54+ } ;
55+ } ;
56+
57+ export const http = new HttpClient ( ) ;
0 commit comments