wispjs 2.1.3 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/{wisp.ts → dist/wisp.d.ts} +11 -28
- package/dist/wisp.js +34 -0
- package/dist/wisp_api/apis/allocations.d.ts +55 -0
- package/dist/wisp_api/apis/allocations.js +32 -0
- package/dist/wisp_api/apis/audit_log.d.ts +68 -0
- package/dist/wisp_api/apis/audit_log.js +21 -0
- package/{wisp_api/apis/backups.ts → dist/wisp_api/apis/backups.d.ts} +13 -51
- package/dist/wisp_api/apis/backups.js +93 -0
- package/dist/wisp_api/apis/databases.d.ts +61 -0
- package/dist/wisp_api/apis/databases.js +43 -0
- package/dist/wisp_api/apis/fastdl.d.ts +19 -0
- package/dist/wisp_api/apis/fastdl.js +21 -0
- package/dist/wisp_api/apis/filesystem.d.ts +200 -0
- package/dist/wisp_api/apis/filesystem.js +182 -0
- package/dist/wisp_api/apis/index.d.ts +52 -0
- package/dist/wisp_api/apis/index.js +100 -0
- package/dist/wisp_api/apis/mods.d.ts +40 -0
- package/dist/wisp_api/apis/mods.js +30 -0
- package/dist/wisp_api/apis/schedules.d.ts +179 -0
- package/dist/wisp_api/apis/schedules.js +167 -0
- package/dist/wisp_api/apis/servers.d.ts +120 -0
- package/dist/wisp_api/apis/servers.js +76 -0
- package/dist/wisp_api/apis/startup.d.ts +52 -0
- package/dist/wisp_api/apis/startup.js +35 -0
- package/dist/wisp_api/apis/subusers.d.ts +106 -0
- package/dist/wisp_api/apis/subusers.js +87 -0
- package/dist/wisp_api/index.d.ts +39 -0
- package/dist/wisp_api/index.js +41 -0
- package/dist/wisp_socket/index.d.ts +161 -0
- package/{wisp_socket/index.ts → dist/wisp_socket/index.js} +130 -236
- package/dist/wisp_socket/pool.d.ts +183 -0
- package/dist/wisp_socket/pool.js +171 -0
- package/package.json +1 -1
- package/.github/workflows/release.yml +0 -72
- package/tsconfig.json +0 -19
- package/wisp_api/apis/allocations.ts +0 -71
- package/wisp_api/apis/audit_log.ts +0 -81
- package/wisp_api/apis/databases.ts +0 -80
- package/wisp_api/apis/fastdl.ts +0 -22
- package/wisp_api/apis/filesystem.ts +0 -291
- package/wisp_api/apis/index.ts +0 -135
- package/wisp_api/apis/mods.ts +0 -53
- package/wisp_api/apis/schedules.ts +0 -270
- package/wisp_api/apis/servers.ts +0 -155
- package/wisp_api/apis/startup.ts +0 -65
- package/wisp_api/apis/subusers.ts +0 -159
- package/wisp_api/index.ts +0 -57
- package/wisp_socket/pool.ts +0 -387
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { WispAPICore } from "./index";
|
|
2
|
+
import type { PaginationData } from "./index";
|
|
3
|
+
export interface DirectoryFile {
|
|
4
|
+
object: "file";
|
|
5
|
+
attributes: {
|
|
6
|
+
type: "file" | "directory";
|
|
7
|
+
name: string;
|
|
8
|
+
size: number;
|
|
9
|
+
mime: string;
|
|
10
|
+
symlink: boolean;
|
|
11
|
+
created_at: string;
|
|
12
|
+
modified_at: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export interface DirectoryContents {
|
|
16
|
+
object: "list";
|
|
17
|
+
data: DirectoryFile[];
|
|
18
|
+
meta: {
|
|
19
|
+
pagination: PaginationData | undefined;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export type GetDirectoryContentsErrorCode = "generic.daemon_connection_exception";
|
|
23
|
+
export interface GetDirectoryContentsError {
|
|
24
|
+
code: GetDirectoryContentsErrorCode;
|
|
25
|
+
data: {
|
|
26
|
+
code: number;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export interface GetDirectoryContentsFailure {
|
|
30
|
+
errors: GetDirectoryContentsError[] | undefined;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The response object for the GetDirectoryContents call
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* Used in {@link FilesystemAPI.GetDirectoryContents}
|
|
37
|
+
*
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export type GetDirectoryContentsResponse = DirectoryContents | GetDirectoryContentsFailure;
|
|
41
|
+
export interface FileReadError {
|
|
42
|
+
code: string;
|
|
43
|
+
data: {
|
|
44
|
+
code: number;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export type FileReadResponse = {
|
|
48
|
+
errors: FileReadError[];
|
|
49
|
+
content?: never;
|
|
50
|
+
} | {
|
|
51
|
+
content: string;
|
|
52
|
+
errors?: never;
|
|
53
|
+
};
|
|
54
|
+
export interface FileWriteRequest {
|
|
55
|
+
path: string;
|
|
56
|
+
content: string;
|
|
57
|
+
}
|
|
58
|
+
export interface CopyFileRequest {
|
|
59
|
+
path: string;
|
|
60
|
+
}
|
|
61
|
+
export interface DownloadFileResponse {
|
|
62
|
+
url: string;
|
|
63
|
+
}
|
|
64
|
+
export interface RenameFileRequest {
|
|
65
|
+
path: string;
|
|
66
|
+
to: string;
|
|
67
|
+
}
|
|
68
|
+
export interface CompressFilesRequest {
|
|
69
|
+
paths: string[];
|
|
70
|
+
to: string;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Handles interaction with the Server's File System
|
|
74
|
+
*
|
|
75
|
+
* @public
|
|
76
|
+
*/
|
|
77
|
+
export declare class FilesystemAPI {
|
|
78
|
+
private core;
|
|
79
|
+
constructor(core: WispAPICore);
|
|
80
|
+
/**
|
|
81
|
+
* Get the Contents of the given Directory
|
|
82
|
+
*
|
|
83
|
+
* @param path The path to list
|
|
84
|
+
*
|
|
85
|
+
* @throws {@link GetDirectoryContentsErrorCode}
|
|
86
|
+
*
|
|
87
|
+
* @public
|
|
88
|
+
*/
|
|
89
|
+
GetDirectoryContents(path: string): Promise<GetDirectoryContentsResponse>;
|
|
90
|
+
/**
|
|
91
|
+
* Creates a Directory with the given path
|
|
92
|
+
*
|
|
93
|
+
* @remarks
|
|
94
|
+
* ⚠️ This will silently fail if the given path is present and invalid or unreachable
|
|
95
|
+
* ⚠️ This is always relative to the Server's default directory (usually /home/container)
|
|
96
|
+
*
|
|
97
|
+
* @param path The full path to the new Directory
|
|
98
|
+
*
|
|
99
|
+
* @public
|
|
100
|
+
*/
|
|
101
|
+
CreateDirectory(path: string): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* Retrieves the contents of the File at the given path
|
|
104
|
+
*
|
|
105
|
+
* @param path The full path to the File to read (After the /home/container/ part)
|
|
106
|
+
*
|
|
107
|
+
* @throws "Server returned error code: {number}"
|
|
108
|
+
* This error is often thrown if the given path doesn't exist, or is not readable. The error code would be 404.
|
|
109
|
+
*
|
|
110
|
+
* @public
|
|
111
|
+
*/
|
|
112
|
+
ReadFile(path: string): Promise<string>;
|
|
113
|
+
/**
|
|
114
|
+
* Writes the given content to the File at the given path
|
|
115
|
+
*
|
|
116
|
+
* @remarks
|
|
117
|
+
* ⚠️ Overwrites the file if it already exists
|
|
118
|
+
* ⚠️ This function silently fails if the target path is not writeable
|
|
119
|
+
*
|
|
120
|
+
* @param path The full path to the File
|
|
121
|
+
* @param content The full content of the File
|
|
122
|
+
*
|
|
123
|
+
* @public
|
|
124
|
+
*/
|
|
125
|
+
WriteFile(path: string, content: string): Promise<void>;
|
|
126
|
+
/**
|
|
127
|
+
* Copies the File at the given path
|
|
128
|
+
*
|
|
129
|
+
* @remarks
|
|
130
|
+
* ⚠️ New copy will be written to the same directory, with a name such as `test.txt` -> `test.txt copy-1643810941850`
|
|
131
|
+
*
|
|
132
|
+
* @param path The full path to the File to Copy
|
|
133
|
+
*
|
|
134
|
+
* @throws "Unexpected response code, Copy may not have succeeded"
|
|
135
|
+
* If the API returns anything other than a 204, something likely went wrong. Most commonly, this is because the file path didn't exist or wasn't copyable.
|
|
136
|
+
*
|
|
137
|
+
* @public
|
|
138
|
+
*/
|
|
139
|
+
CopyFile(path: string): Promise<void>;
|
|
140
|
+
/**
|
|
141
|
+
* Deletes the Files at all of the given paths
|
|
142
|
+
*
|
|
143
|
+
* @param paths An array of File Paths to Delete
|
|
144
|
+
*
|
|
145
|
+
* @throws "Unexpected response code, Delete may not have succeeded"
|
|
146
|
+
* If the API returns anything other than a 204, something likely went wrong. Most commonly, this is because the file path didn't exist or wasn't deleteable.
|
|
147
|
+
*
|
|
148
|
+
* @public
|
|
149
|
+
*/
|
|
150
|
+
DeleteFiles(paths: string[]): Promise<void>;
|
|
151
|
+
/**
|
|
152
|
+
* Retrieves a Download URL for a File
|
|
153
|
+
*
|
|
154
|
+
* @remarks
|
|
155
|
+
* ⚠️ This will return a Download URL even if the given file does not exist
|
|
156
|
+
*
|
|
157
|
+
* @param path The full path to the File
|
|
158
|
+
*
|
|
159
|
+
* @public
|
|
160
|
+
*/
|
|
161
|
+
GetFileDownloadURL(path: string): Promise<string>;
|
|
162
|
+
/**
|
|
163
|
+
* Renames (or moves) the given File
|
|
164
|
+
*
|
|
165
|
+
* @param path The full path to the File
|
|
166
|
+
* @param to The new path of the File
|
|
167
|
+
*
|
|
168
|
+
* @throws "Unexpected response code, Rename may not have succeeded"
|
|
169
|
+
* If the API returns anything other than a 204, something likely went wrong. Most commonly, this is because the source or destination path did not exist or was not reachable
|
|
170
|
+
*
|
|
171
|
+
* @public
|
|
172
|
+
*/
|
|
173
|
+
RenameFile(path: string, to: string): Promise<void>;
|
|
174
|
+
/**
|
|
175
|
+
* Compresses the Files at the given paths
|
|
176
|
+
*
|
|
177
|
+
* @remarks
|
|
178
|
+
* ⚠️ The resulting file appears to be unpredictably named?
|
|
179
|
+
*
|
|
180
|
+
* @param paths An array of Paths to Compress
|
|
181
|
+
* @param to The Directory to write the Compressed Files to
|
|
182
|
+
*
|
|
183
|
+
* @throws "Unexpected response code, Compress may not have succeeded"
|
|
184
|
+
* If the API returns anything other than a 204, something likely went wrong. Most commonly, this is because the source or destination path did not exist or was not Compressable
|
|
185
|
+
*
|
|
186
|
+
* @public
|
|
187
|
+
*/
|
|
188
|
+
CompressFiles(paths: string[], to: string): Promise<void>;
|
|
189
|
+
/**
|
|
190
|
+
* Decompresses the File at the given path
|
|
191
|
+
*
|
|
192
|
+
* @param path The full path to the File to Decompress
|
|
193
|
+
*
|
|
194
|
+
* @throws "Unexpected response code, Decompress may not have succeeded"
|
|
195
|
+
* If the API returns anything other than a 204, something likely went wrong. Most commonly, this is because the given path did not exist or was not reachable
|
|
196
|
+
*
|
|
197
|
+
* @public
|
|
198
|
+
*/
|
|
199
|
+
DecompressFile(path: string): Promise<void>;
|
|
200
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handles interaction with the Server's File System
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export class FilesystemAPI {
|
|
7
|
+
constructor(core) {
|
|
8
|
+
this.core = core;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Get the Contents of the given Directory
|
|
12
|
+
*
|
|
13
|
+
* @param path The path to list
|
|
14
|
+
*
|
|
15
|
+
* @throws {@link GetDirectoryContentsErrorCode}
|
|
16
|
+
*
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
async GetDirectoryContents(path) {
|
|
20
|
+
const response = await this.core.makeRequest("GET", "files/directory", { path: path });
|
|
21
|
+
const data = await response.json();
|
|
22
|
+
// TODO: Also include the data.code (or handle the 404 case specifically)
|
|
23
|
+
if ("errors" in data && data.errors) {
|
|
24
|
+
throw new Error(data.errors[0].code);
|
|
25
|
+
}
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates a Directory with the given path
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* ⚠️ This will silently fail if the given path is present and invalid or unreachable
|
|
33
|
+
* ⚠️ This is always relative to the Server's default directory (usually /home/container)
|
|
34
|
+
*
|
|
35
|
+
* @param path The full path to the new Directory
|
|
36
|
+
*
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
async CreateDirectory(path) {
|
|
40
|
+
await this.core.makeRequest("POST", "files/directory", { path: path });
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Retrieves the contents of the File at the given path
|
|
44
|
+
*
|
|
45
|
+
* @param path The full path to the File to read (After the /home/container/ part)
|
|
46
|
+
*
|
|
47
|
+
* @throws "Server returned error code: {number}"
|
|
48
|
+
* This error is often thrown if the given path doesn't exist, or is not readable. The error code would be 404.
|
|
49
|
+
*
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
async ReadFile(path) {
|
|
53
|
+
const response = await this.core.makeRequest("GET", "files/read", { path: path });
|
|
54
|
+
const data = await response.json();
|
|
55
|
+
if (data.errors) {
|
|
56
|
+
throw new Error(`Server returned error code: ${data.errors[0].data.code}`);
|
|
57
|
+
}
|
|
58
|
+
return data.content;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Writes the given content to the File at the given path
|
|
62
|
+
*
|
|
63
|
+
* @remarks
|
|
64
|
+
* ⚠️ Overwrites the file if it already exists
|
|
65
|
+
* ⚠️ This function silently fails if the target path is not writeable
|
|
66
|
+
*
|
|
67
|
+
* @param path The full path to the File
|
|
68
|
+
* @param content The full content of the File
|
|
69
|
+
*
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
async WriteFile(path, content) {
|
|
73
|
+
const data = { path: path, content: content };
|
|
74
|
+
await this.core.makeRequest("POST", "files/write", data);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Copies the File at the given path
|
|
78
|
+
*
|
|
79
|
+
* @remarks
|
|
80
|
+
* ⚠️ New copy will be written to the same directory, with a name such as `test.txt` -> `test.txt copy-1643810941850`
|
|
81
|
+
*
|
|
82
|
+
* @param path The full path to the File to Copy
|
|
83
|
+
*
|
|
84
|
+
* @throws "Unexpected response code, Copy may not have succeeded"
|
|
85
|
+
* If the API returns anything other than a 204, something likely went wrong. Most commonly, this is because the file path didn't exist or wasn't copyable.
|
|
86
|
+
*
|
|
87
|
+
* @public
|
|
88
|
+
*/
|
|
89
|
+
async CopyFile(path) {
|
|
90
|
+
const requestData = { path: path };
|
|
91
|
+
const response = await this.core.makeRequest("POST", "files/copy", requestData);
|
|
92
|
+
if (response.status != 204) {
|
|
93
|
+
throw new Error("Unexpected response code, Copy may not have succeeded");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Deletes the Files at all of the given paths
|
|
98
|
+
*
|
|
99
|
+
* @param paths An array of File Paths to Delete
|
|
100
|
+
*
|
|
101
|
+
* @throws "Unexpected response code, Delete may not have succeeded"
|
|
102
|
+
* If the API returns anything other than a 204, something likely went wrong. Most commonly, this is because the file path didn't exist or wasn't deleteable.
|
|
103
|
+
*
|
|
104
|
+
* @public
|
|
105
|
+
*/
|
|
106
|
+
async DeleteFiles(paths) {
|
|
107
|
+
const response = await this.core.makeRequest("POST", "files/delete", { paths: paths });
|
|
108
|
+
if (response.status != 204) {
|
|
109
|
+
throw new Error("Unexpected response code, Delete may not have succeeded");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Retrieves a Download URL for a File
|
|
114
|
+
*
|
|
115
|
+
* @remarks
|
|
116
|
+
* ⚠️ This will return a Download URL even if the given file does not exist
|
|
117
|
+
*
|
|
118
|
+
* @param path The full path to the File
|
|
119
|
+
*
|
|
120
|
+
* @public
|
|
121
|
+
*/
|
|
122
|
+
async GetFileDownloadURL(path) {
|
|
123
|
+
const response = await this.core.makeRequest("GET", "files/download", { path: path });
|
|
124
|
+
const data = await response.json();
|
|
125
|
+
return data.url;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Renames (or moves) the given File
|
|
129
|
+
*
|
|
130
|
+
* @param path The full path to the File
|
|
131
|
+
* @param to The new path of the File
|
|
132
|
+
*
|
|
133
|
+
* @throws "Unexpected response code, Rename may not have succeeded"
|
|
134
|
+
* If the API returns anything other than a 204, something likely went wrong. Most commonly, this is because the source or destination path did not exist or was not reachable
|
|
135
|
+
*
|
|
136
|
+
* @public
|
|
137
|
+
*/
|
|
138
|
+
async RenameFile(path, to) {
|
|
139
|
+
const requestData = { path: path, to: to };
|
|
140
|
+
const response = await this.core.makeRequest("PATCH", "files/rename", requestData);
|
|
141
|
+
if (response.status != 204) {
|
|
142
|
+
throw new Error("Unexpected response code, Rename may not have succeeded");
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Compresses the Files at the given paths
|
|
147
|
+
*
|
|
148
|
+
* @remarks
|
|
149
|
+
* ⚠️ The resulting file appears to be unpredictably named?
|
|
150
|
+
*
|
|
151
|
+
* @param paths An array of Paths to Compress
|
|
152
|
+
* @param to The Directory to write the Compressed Files to
|
|
153
|
+
*
|
|
154
|
+
* @throws "Unexpected response code, Compress may not have succeeded"
|
|
155
|
+
* If the API returns anything other than a 204, something likely went wrong. Most commonly, this is because the source or destination path did not exist or was not Compressable
|
|
156
|
+
*
|
|
157
|
+
* @public
|
|
158
|
+
*/
|
|
159
|
+
async CompressFiles(paths, to) {
|
|
160
|
+
const requestData = { paths: paths, to: to };
|
|
161
|
+
const response = await this.core.makeRequest("POST", "files/compress", requestData);
|
|
162
|
+
if (response.status != 204) {
|
|
163
|
+
throw new Error("Unexpected response code, Compress may not have succeeded");
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Decompresses the File at the given path
|
|
168
|
+
*
|
|
169
|
+
* @param path The full path to the File to Decompress
|
|
170
|
+
*
|
|
171
|
+
* @throws "Unexpected response code, Decompress may not have succeeded"
|
|
172
|
+
* If the API returns anything other than a 204, something likely went wrong. Most commonly, this is because the given path did not exist or was not reachable
|
|
173
|
+
*
|
|
174
|
+
* @public
|
|
175
|
+
*/
|
|
176
|
+
async DecompressFile(path) {
|
|
177
|
+
const response = await this.core.makeRequest("POST", "files/decompress", { path: path });
|
|
178
|
+
if (response.status != 204) {
|
|
179
|
+
throw new Error("Unexpected response code, Decompress may not have succeeded");
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export type RequestTypes = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
2
|
+
export interface PaginationData {
|
|
3
|
+
total: number;
|
|
4
|
+
count: number;
|
|
5
|
+
perPage: number;
|
|
6
|
+
currentPage: number;
|
|
7
|
+
totalPages: number;
|
|
8
|
+
}
|
|
9
|
+
export interface WispAPICore {
|
|
10
|
+
domain: string;
|
|
11
|
+
uuid: string;
|
|
12
|
+
token: string;
|
|
13
|
+
logger: any;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The Core of the API, handling low-level operations such as making requests and setting the server UUID
|
|
17
|
+
*
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export declare class WispAPICore {
|
|
21
|
+
constructor(domain: string, uuid: string, token: string, logger: any);
|
|
22
|
+
/**
|
|
23
|
+
* Sets the Server UUID
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* ℹ️ This can be updated at any time, making all future API calls reference the new Server UUID
|
|
27
|
+
*
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
setUUID(uuid: string): void;
|
|
31
|
+
/**
|
|
32
|
+
* Generates a URL for the given path
|
|
33
|
+
*
|
|
34
|
+
* @param path The API path
|
|
35
|
+
*
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
makeURL(path: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Makes a request with the headers and request data set automatically.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* The data field is formatted appropriately for whichever request type is given.
|
|
44
|
+
*
|
|
45
|
+
* @param method A standared request method.
|
|
46
|
+
* @param path The API path to send the request to.
|
|
47
|
+
* @param data The data to include with the request.
|
|
48
|
+
*
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
makeRequest(method: RequestTypes, path: string, data?: any): Promise<Response>;
|
|
52
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Core of the API, handling low-level operations such as making requests and setting the server UUID
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export class WispAPICore {
|
|
7
|
+
constructor(domain, uuid, token, logger) {
|
|
8
|
+
this.domain = domain;
|
|
9
|
+
this.uuid = uuid;
|
|
10
|
+
this.token = token;
|
|
11
|
+
this.logger = logger;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Sets the Server UUID
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* ℹ️ This can be updated at any time, making all future API calls reference the new Server UUID
|
|
18
|
+
*
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
setUUID(uuid) {
|
|
22
|
+
this.uuid = uuid;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Generates a URL for the given path
|
|
26
|
+
*
|
|
27
|
+
* @param path The API path
|
|
28
|
+
*
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
makeURL(path) {
|
|
32
|
+
return `https://${this.domain}/api/client/servers/${this.uuid}/${path}`;
|
|
33
|
+
}
|
|
34
|
+
// TODO: Handle standard field-level error messages:
|
|
35
|
+
// {"errors":[{"code":"required","detail":"The path field is required","source":{"field":"path"}}]}
|
|
36
|
+
/**
|
|
37
|
+
* Makes a request with the headers and request data set automatically.
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* The data field is formatted appropriately for whichever request type is given.
|
|
41
|
+
*
|
|
42
|
+
* @param method A standared request method.
|
|
43
|
+
* @param path The API path to send the request to.
|
|
44
|
+
* @param data The data to include with the request.
|
|
45
|
+
*
|
|
46
|
+
* @internal
|
|
47
|
+
*/
|
|
48
|
+
async makeRequest(method, path, data) {
|
|
49
|
+
let url = this.makeURL(path);
|
|
50
|
+
const headers = new Headers({
|
|
51
|
+
"Content-Type": "application/json",
|
|
52
|
+
"Accept": "application/vnd.wisp.v1+json",
|
|
53
|
+
"Authorization": `Bearer ${this.token}`,
|
|
54
|
+
"User-Agent": "WispJS (https://github.com/CFC-Servers/wispjs, 1.0.0)"
|
|
55
|
+
});
|
|
56
|
+
const request = async () => {
|
|
57
|
+
let response;
|
|
58
|
+
console.log(`${method} -> ${url}`);
|
|
59
|
+
switch (method) {
|
|
60
|
+
case "GET":
|
|
61
|
+
if (data !== null) {
|
|
62
|
+
const params = new URLSearchParams(data);
|
|
63
|
+
const uri = new URL(url);
|
|
64
|
+
uri.search = params.toString();
|
|
65
|
+
url = uri.toString();
|
|
66
|
+
console.log(`Updated GET URL: ${url}`);
|
|
67
|
+
}
|
|
68
|
+
response = await fetch(url, { method: "GET", headers: headers });
|
|
69
|
+
break;
|
|
70
|
+
case "POST":
|
|
71
|
+
data = JSON.stringify(data);
|
|
72
|
+
response = await fetch(url, { method: "POST", headers: headers, body: data });
|
|
73
|
+
break;
|
|
74
|
+
case "PUT":
|
|
75
|
+
data = data ? JSON.stringify(data) : "";
|
|
76
|
+
response = await fetch(url, { method: "PUT", headers: headers, body: data });
|
|
77
|
+
break;
|
|
78
|
+
case "PATCH":
|
|
79
|
+
data = JSON.stringify(data);
|
|
80
|
+
response = await fetch(url, { method: "PATCH", headers: headers, body: data });
|
|
81
|
+
break;
|
|
82
|
+
case "DELETE":
|
|
83
|
+
response = await fetch(url, { method: "DELETE", headers: headers });
|
|
84
|
+
break;
|
|
85
|
+
default:
|
|
86
|
+
throw new Error(`Invalid method: ${method}`);
|
|
87
|
+
}
|
|
88
|
+
if (!response.ok) {
|
|
89
|
+
const status = response.status;
|
|
90
|
+
const statusText = response.statusText;
|
|
91
|
+
this.logger.error(`Request failed: ${method} -> ${url}: ${status} - ${statusText}`);
|
|
92
|
+
const text = await response.text();
|
|
93
|
+
this.logger.error(text);
|
|
94
|
+
throw new Error(`Request failed! Status: ${status} - ${statusText}`);
|
|
95
|
+
}
|
|
96
|
+
return response;
|
|
97
|
+
};
|
|
98
|
+
return await request();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { WispAPICore } from "./index";
|
|
2
|
+
export interface Mod {
|
|
3
|
+
object: "mod";
|
|
4
|
+
attributes: {
|
|
5
|
+
id: number;
|
|
6
|
+
name: string;
|
|
7
|
+
description: string;
|
|
8
|
+
version: string;
|
|
9
|
+
category: string;
|
|
10
|
+
install_count: number;
|
|
11
|
+
server_state: number;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export interface GetModsResponse {
|
|
15
|
+
object: "list";
|
|
16
|
+
data: Mod[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Handles Listing and Installating of Mods
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
export declare class ModsAPI {
|
|
24
|
+
private core;
|
|
25
|
+
constructor(core: WispAPICore);
|
|
26
|
+
/**
|
|
27
|
+
* Lists all Mods available to the Server
|
|
28
|
+
*
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
List(): Promise<GetModsResponse>;
|
|
32
|
+
/**
|
|
33
|
+
* Installs or Uninstalls the Mod with the given id
|
|
34
|
+
*
|
|
35
|
+
* @param id The ID of the Mod to Install/Uninstall
|
|
36
|
+
*
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
ToggleInstall(id: string): Promise<void>;
|
|
40
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handles Listing and Installating of Mods
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export class ModsAPI {
|
|
7
|
+
constructor(core) {
|
|
8
|
+
this.core = core;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Lists all Mods available to the Server
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
async List() {
|
|
16
|
+
const response = await this.core.makeRequest("GET", "mods");
|
|
17
|
+
const data = await response.json();
|
|
18
|
+
return data;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Installs or Uninstalls the Mod with the given id
|
|
22
|
+
*
|
|
23
|
+
* @param id The ID of the Mod to Install/Uninstall
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
async ToggleInstall(id) {
|
|
28
|
+
await this.core.makeRequest("POST", `mods/${id}`);
|
|
29
|
+
}
|
|
30
|
+
}
|