Skip to content

Commit 1974e17

Browse files
committed
Convert request library from axios to fetch api. Fix some not acceptable methods. Update individual objects when an update is sent. Do test connection on build. Update release for v2.1.0
1 parent bf61ffa commit 1974e17

19 files changed

+765
-250
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pterodactyl.js",
3-
"version": "0.0.1",
3+
"version": "0.0.0",
44
"description": "A wrapper for the Pterodactyl Panel API",
55
"main": "index.js",
66
"types": "src/types/index.d.ts",
@@ -20,10 +20,11 @@
2020
"author": "RedstoneGamez (RedstoneGamez#0001)",
2121
"license": "ISC",
2222
"dependencies": {
23-
"axios": "^0.19.0"
23+
"node-fetch": "^2.6.0"
2424
},
2525
"devDependencies": {
2626
"@types/node": "^12.0.8",
27+
"@types/node-fetch": "^2.5.7",
2728
"typescript": "^3.5.2"
2829
}
2930
}

release/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pterodactyl.js",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "A wrapper for the Pterodactyl Panel API",
55
"main": "index.js",
66
"types": "types/index.d.ts",
@@ -19,6 +19,6 @@
1919
"author": "RedstoneGamez (RedstoneGamez#0001)",
2020
"license": "MIT",
2121
"dependencies": {
22-
"axios": "^0.19.0"
22+
"node-fetch": "^2.6.0"
2323
}
2424
}

release/types/index.d.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
declare module 'pterodactyl.js' {
2-
import { AxiosResponse } from 'axios';
3-
42
export class Builder {
53
constructor(url?: string, apiKey?: string);
64

@@ -27,7 +25,7 @@ declare module 'pterodactyl.js' {
2725

2826
private getHostname(): string;
2927

30-
public call(endpoint: string, method: any, data: any): Promise<AxiosResponse<any>>;
28+
public call(endpoint: string, method: any, data: any): Promise<ResponseData>;
3129

3230
private handleError(error: any): any;
3331
}
@@ -506,10 +504,10 @@ declare module 'pterodactyl.js' {
506504

507505
// Type Defs
508506

509-
interface PterodactylAPIOptions {
510-
url: string;
511-
baseUrl: string;
512-
apiKey: string;
507+
interface ResponseData {
508+
statusCode: number,
509+
data: any,
510+
pagination: PaginationOptionsRaw
513511
}
514512

515513
interface ServerLimits {

src/lib/AdminAPI.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ import { NewNodeOptions } from './models/Node';
1515
class AdminClient extends PterodactylAPI {
1616
constructor(url: string, apiKey: string) {
1717
super(url, apiKey);
18+
19+
this.testConnection()
20+
.catch(error => {
21+
throw error;
22+
});
1823
}
1924

20-
public testConnection(): Promise<any> {
25+
public testConnection(): Promise<void> {
2126
let solutions: any = {
2227
0: 'Most likely hostname is configured wrong causing the request never get executed.',
2328
401: 'Authorization header either missing or not provided.',
@@ -31,15 +36,13 @@ class AdminClient extends PterodactylAPI {
3136
this.call('/application/servers').then(res => {
3237
let error = null;
3338

34-
if (res.status !== 200) {
35-
let statusCode = res.status;
36-
39+
if (res.statusCode !== 200) {
40+
let { statusCode } = res;
3741
error = `Non success status code received: ${statusCode}.\nPossible sulutions: ${solutions[statusCode] !== undefined ? solutions[statusCode] : 'None.'}`
38-
} else {
39-
error = 'Authentication successful, you\'re good to go!';
4042
}
4143

42-
resolve(error);
44+
if (error !== null) return reject(new Error(error));
45+
resolve();
4346
}).catch(error => reject(error));
4447
});
4548
}

src/lib/UserAPI.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ import ClientServer from './client/ClientServer';
55
class UserClient extends PterodactylAPI {
66
constructor(url: string, apiKey: string) {
77
super(url, apiKey);
8+
9+
this.testConnection()
10+
.catch(error => {
11+
throw error;
12+
});
813
}
914

10-
public testConnection(): Promise<any> {
15+
public testConnection(): Promise<void> {
1116
let solutions: any = {
1217
0: 'Most likely hostname is configured wrong causing the request never get executed.',
1318
401: 'Authorization header either missing or not provided.',
@@ -21,15 +26,13 @@ class UserClient extends PterodactylAPI {
2126
this.call('/client').then(res => {
2227
let error = null;
2328

24-
if (res.status !== 200) {
25-
let statusCode = res.status;
26-
29+
if (res.statusCode !== 200) {
30+
let { statusCode } = res;
2731
error = `Non success status code received: ${statusCode}.\nPossible sulutions: ${solutions[statusCode] !== undefined ? solutions[statusCode] : 'None.'}`
28-
} else {
29-
error = 'Authentication successful, you\'re good to go!';
3032
}
3133

32-
resolve(error);
34+
if (error !== null) return reject(new Error(error));
35+
resolve();
3336
}).catch(error => reject(error));
3437
});
3538
}

src/lib/client/ClientServer.ts

+71-21
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ClientServer extends ClientServerModel {
2222
return new Promise(async (resolve, reject) => {
2323
try {
2424
let res = await api.call(`/client?page=${page}`);
25-
resolve(res.data.data.map((value: any) => new ClientServer(api, value.attributes, res.data.meta)));
25+
resolve(res.data.map((value: any) => new ClientServer(api, value.attributes, res.pagination)));
2626
} catch (error) {
2727
reject(error);
2828
}
@@ -41,56 +41,101 @@ class ClientServer extends ClientServerModel {
4141
}
4242

4343
public cpuUsage(): Promise<UtilizationData> {
44-
return new Promise((resolve, reject) => {
45-
this.api.call(`/client/servers/${this.identifier}/utilization`).then(res => resolve({ used: res.data.attributes.cpu.current, total: res.data.attributes.cpu.limit })).catch(error => reject(error));
44+
return new Promise(async (resolve, reject) => {
45+
try {
46+
let res = await this.api.call(`/client/servers/${this.identifier}/utilization`);
47+
resolve({ used: res.data.attributes.cpu.current, total: res.data.attributes.cpu.limit });
48+
} catch (error) {
49+
reject(error);
50+
}
4651
});
4752
}
4853

4954
public diskUsage(): Promise<UtilizationData> {
50-
return new Promise((resolve, reject) => {
51-
this.api.call(`/client/servers/${this.identifier}/utilization`).then(res => resolve({ used: res.data.attributes.disk.current, total: res.data.attributes.disk.limit })).catch(error => reject(error));
55+
return new Promise(async (resolve, reject) => {
56+
try {
57+
let res = await this.api.call(`/client/servers/${this.identifier}/utilization`);
58+
resolve({ used: res.data.attributes.disk.current, total: res.data.attributes.disk.limit });
59+
} catch (error) {
60+
reject(error);
61+
}
5262
});
5363
}
5464

5565
public memoryUsage(): Promise<UtilizationData> {
56-
return new Promise((resolve, reject) => {
57-
this.api.call(`/client/servers/${this.identifier}/utilization`).then(res => resolve({ used: res.data.attributes.memory.current, total: res.data.attributes.memory.limit })).catch(error => reject(error));
66+
return new Promise(async (resolve, reject) => {
67+
try {
68+
let res = await this.api.call(`/client/servers/${this.identifier}/utilization`);
69+
resolve({ used: res.data.attributes.memory.current, total: res.data.attributes.memory.limit });
70+
} catch (error) {
71+
reject(error);
72+
}
5873
});
5974
}
6075

6176
public powerState(): Promise<string> {
62-
return new Promise((resolve, reject) => {
63-
this.api.call(`/client/servers/${this.identifier}/utilization`).then(res => resolve(res.data.attributes.state)).catch(error => reject(error));
77+
return new Promise(async (resolve, reject) => {
78+
try {
79+
let res = await this.api.call(`/client/servers/${this.identifier}/utilization`);
80+
resolve(res.data.attributes.state);
81+
} catch (error) {
82+
reject(error);
83+
}
6484
});
6585
}
6686

6787
public powerAction(signal: 'start' | 'stop' | 'restart' | 'kill'): Promise<void> {
68-
return new Promise((resolve, reject) => {
69-
this.api.call(`/client/servers/${this.identifier}/power`, 'POST', { signal }).then(res => resolve()).catch(error => reject(error));
88+
return new Promise(async (resolve, reject) => {
89+
try {
90+
await this.api.call(`/client/servers/${this.identifier}/power`, 'POST', { signal });
91+
resolve();
92+
} catch (error) {
93+
reject(error);
94+
}
7095
});
7196
}
7297

7398
public start(): Promise<void> {
74-
return new Promise((resolve, reject) => {
75-
this.api.call(`/client/servers/${this.identifier}/power`, 'POST', { signal: 'start' }).then(res => resolve()).catch(error => reject(error));
99+
return new Promise(async (resolve, reject) => {
100+
try {
101+
await this.api.call(`/client/servers/${this.identifier}/power`, 'POST', { signal: 'start' });
102+
resolve();
103+
} catch (error) {
104+
reject(error);
105+
}
76106
});
77107
}
78108

79109
public stop(): Promise<void> {
80-
return new Promise((resolve, reject) => {
81-
this.api.call(`/client/servers/${this.identifier}/power`, 'POST', { signal: 'stop' }).then(res => resolve()).catch(error => reject(error));
110+
return new Promise(async (resolve, reject) => {
111+
try {
112+
await this.api.call(`/client/servers/${this.identifier}/power`, 'POST', { signal: 'stop' });
113+
resolve();
114+
} catch (error) {
115+
reject(error);
116+
}
82117
});
83118
}
84119

85120
public restart(): Promise<void> {
86-
return new Promise((resolve, reject) => {
87-
this.api.call(`/client/servers/${this.identifier}/power`, 'POST', { signal: 'restart' }).then(res => resolve()).catch(error => reject(error));
121+
return new Promise(async (resolve, reject) => {
122+
try {
123+
await this.api.call(`/client/servers/${this.identifier}/power`, 'POST', { signal: 'restart' });
124+
resolve();
125+
} catch (error) {
126+
reject(error);
127+
}
88128
});
89129
}
90130

91131
public kill(): Promise<void> {
92-
return new Promise((resolve, reject) => {
93-
this.api.call(`/client/servers/${this.identifier}/power`, 'POST', { signal: 'kill' }).then(res => resolve()).catch(error => reject(error));
132+
return new Promise(async (resolve, reject) => {
133+
try {
134+
await this.api.call(`/client/servers/${this.identifier}/power`, 'POST', { signal: 'kill' });
135+
resolve();
136+
} catch (error) {
137+
reject(error);
138+
}
94139
});
95140
}
96141

@@ -103,8 +148,13 @@ class ClientServer extends ClientServerModel {
103148
}
104149

105150
public sendCommand(command: string): Promise<void> {
106-
return new Promise((resolve, reject) => {
107-
this.api.call(`/client/servers/${this.identifier}/command`, 'POST', { command }).then(res => resolve()).catch(error => reject(error));
151+
return new Promise(async (resolve, reject) => {
152+
try {
153+
await this.api.call(`/client/servers/${this.identifier}/command`, 'POST', { command });
154+
resolve();
155+
} catch (error) {
156+
reject(error);
157+
}
108158
});
109159
}
110160
}

src/lib/client/Egg.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Egg extends EggModel {
1414
return new Promise(async (resolve, reject) => {
1515
try {
1616
let res = await api.call(`/application/nests/${nest}/eggs`);
17-
resolve(res.data.data.map((value: any) => new Egg(api, value.attributes)));
17+
resolve(res.data.map((value: any) => new Egg(api, value.attributes)));
1818
} catch (error) {
1919
reject(error);
2020
}

src/lib/client/Location.ts

+38-6
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ class Location extends LocationModel {
1414
}
1515

1616
public static create(api: AdminAPI, options: NewLocationOptions): Promise<Location> {
17-
return new Promise((resolve, reject) => {
18-
api.call(`/application/locations`, 'POST', options).then(res => resolve(new Location(api, res.data.attributes))).catch(error => reject(error));
17+
return new Promise(async (resolve, reject) => {
18+
try {
19+
let res = await api.call(`/application/locations`, 'POST', { short: options.shortCode, long: options.description });
20+
resolve(new Location(api, res.data.attributes));
21+
} catch (error) {
22+
reject(error);
23+
}
1924
});
2025
}
2126

2227
public static getAll(api: AdminAPI, page: number = 1): Promise<Location[]> {
2328
return new Promise(async (resolve, reject) => {
2429
try {
2530
let res = await api.call(`/application/locations?page=${page}`);
26-
resolve(res.data.data.map((value: any) => new Location(api, value.attributes, res.data.meta)));
31+
resolve(res.data.map((value: any) => new Location(api, value.attributes, res.pagination)));
2732
} catch (error) {
2833
reject(error);
2934
}
@@ -51,20 +56,47 @@ class Location extends LocationModel {
5156
}
5257

5358
public setShortCode(shortCode: string): Promise<Location> {
59+
this.shortCode = shortCode;
60+
5461
return new Promise((resolve, reject) => {
55-
this.api.call(`/application/locations/${this.id}`, 'PATCH', this.getRequestObject({ short: shortCode })).then(res => resolve(new Location(this.api, res.data.attributes))).catch(error => reject(error));
62+
return new Promise(async (resolve, reject) => {
63+
try {
64+
let res = await this.api.call(`/application/locations`, 'POST', this.getRequestObject({ short: shortCode }));
65+
resolve(new Location(this.api, res.data.attributes));
66+
} catch (error) {
67+
reject(error);
68+
}
69+
});
5670
});
5771
}
5872

5973
public setDescription(description: string): Promise<Location> {
74+
this.description = description;
75+
6076
return new Promise((resolve, reject) => {
61-
this.api.call(`/application/locations/${this.id}`, 'PATCH', this.getRequestObject({ long: description })).then(res => resolve(new Location(this.api, res.data.attributes))).catch(error => reject(error));
77+
return new Promise(async (resolve, reject) => {
78+
try {
79+
let res = await this.api.call(`/application/locations`, 'POST', this.getRequestObject({ long: description }));
80+
resolve(new Location(this.api, res.data.attributes));
81+
} catch (error) {
82+
reject(error);
83+
}
84+
});
6285
});
6386
}
6487

6588
public delete(): Promise<void> {
6689
return new Promise((resolve, reject) => {
67-
this.api.call(`/application/locations/${this.id}`, 'DELETE').then(res => resolve()).catch(error => reject(error));
90+
return new Promise((resolve, reject) => {
91+
return new Promise(async (resolve, reject) => {
92+
try {
93+
await this.api.call(`/application/locations/${this.id}`, 'DELETE');
94+
resolve();
95+
} catch (error) {
96+
reject(error);
97+
}
98+
});
99+
});
68100
});
69101
}
70102
}

src/lib/client/Nest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Nest extends NestModel {
1919
return new Promise(async (resolve, reject) => {
2020
try {
2121
let res = await api.call(`/application/nests?page=${page}`);
22-
resolve(res.data.data.map((value: any) => new Nest(api, value.attributes, res.data.meta)));
22+
resolve(res.data.map((value: any) => new Nest(api, value.attributes, res.pagination)));
2323
} catch (error) {
2424
reject(error);
2525
}

0 commit comments

Comments
 (0)