wispjs 2.4.0 → 3.0.1

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.
@@ -1,22 +0,0 @@
1
- import { WispAPICore } from "./index";
2
-
3
- /**
4
- * Handles the syncing of the FastDL feature
5
- *
6
- * @public
7
- */
8
- export class FastDLAPI {
9
- constructor(private core: WispAPICore) {}
10
-
11
- /**
12
- * Begins a FastDL Sync for the server
13
- *
14
- * @remarks
15
- * ⚠️ If a Sync is already in progress, this function will succeed even though the process will fail.
16
- *
17
- * @public
18
- */
19
- async Sync(): Promise<void> {
20
- await this.core.makeRequest("POST", "fastdl");
21
- }
22
- }
@@ -1,291 +0,0 @@
1
- import { WispAPICore } from "./index";
2
- import type { PaginationData } from "./index";
3
-
4
- // TODO: Handle the 204 vs. 200 with errors better
5
- // TODO: Use the cool `never` thing for all error pieces in here
6
-
7
- export interface DirectoryFile {
8
- object: "file";
9
- attributes: {
10
- type: "file" | "directory";
11
- name: string;
12
- size: number;
13
- mime: string;
14
- symlink: boolean;
15
- created_at: string;
16
- modified_at: string;
17
- }
18
- }
19
-
20
- export interface DirectoryContents {
21
- object: "list";
22
- data: DirectoryFile[]
23
- meta: {
24
- pagination: PaginationData | undefined;
25
- }
26
- }
27
- export type GetDirectoryContentsErrorCode = "generic.daemon_connection_exception";
28
- export interface GetDirectoryContentsError {
29
- code: GetDirectoryContentsErrorCode;
30
- data: {
31
- code: number;
32
- }
33
- }
34
- export interface GetDirectoryContentsFailure {
35
- errors: GetDirectoryContentsError[] | undefined
36
- }
37
-
38
- /**
39
- * The response object for the GetDirectoryContents call
40
- *
41
- * @remarks
42
- * Used in {@link FilesystemAPI.GetDirectoryContents}
43
- *
44
- * @public
45
- */
46
- export type GetDirectoryContentsResponse = DirectoryContents | GetDirectoryContentsFailure;
47
-
48
- export interface FileReadError {
49
- code: string;
50
- data: {
51
- code: number
52
- }
53
- }
54
- export type FileReadResponse =
55
- | { errors: FileReadError[]; content?: never }
56
- | { content: string; errors?: never };
57
-
58
-
59
- export interface FileWriteRequest {
60
- path: string;
61
- content: string;
62
- }
63
-
64
- export interface CopyFileRequest {
65
- path: string;
66
- }
67
-
68
- export interface DownloadFileResponse {
69
- url: string;
70
- }
71
-
72
- export interface RenameFileRequest {
73
- path: string;
74
- to: string;
75
- }
76
-
77
- export interface CompressFilesRequest {
78
- paths: string[];
79
- to: string;
80
- }
81
-
82
- /**
83
- * Handles interaction with the Server's File System
84
- *
85
- * @public
86
- */
87
- export class FilesystemAPI {
88
- constructor(private core: WispAPICore) {}
89
-
90
-
91
- /**
92
- * Get the Contents of the given Directory
93
- *
94
- * @param path The path to list
95
- *
96
- * @throws {@link GetDirectoryContentsErrorCode}
97
- *
98
- * @public
99
- */
100
- async GetDirectoryContents(path: string): Promise<GetDirectoryContentsResponse> {
101
- const response = await this.core.makeRequest("GET", "files/directory", { path: path });
102
- const data: GetDirectoryContentsResponse = await response.json();
103
-
104
- // TODO: Also include the data.code (or handle the 404 case specifically)
105
- if ("errors" in data && data.errors) {
106
- throw new Error(data.errors[0].code);
107
- }
108
-
109
- return data
110
- }
111
-
112
-
113
- /**
114
- * Creates a Directory with the given path
115
- *
116
- * @remarks
117
- * ⚠️ This will silently fail if the given path is present and invalid or unreachable
118
- * ⚠️ This is always relative to the Server's default directory (usually /home/container)
119
- *
120
- * @param path The full path to the new Directory
121
- *
122
- * @public
123
- */
124
- async CreateDirectory(path: string): Promise<void> {
125
- await this.core.makeRequest("POST", "files/directory", { path: path });
126
- }
127
-
128
-
129
- /**
130
- * Retrieves the contents of the File at the given path
131
- *
132
- * @param path The full path to the File to read (After the /home/container/ part)
133
- *
134
- * @throws "Server returned error code: {number}"
135
- * This error is often thrown if the given path doesn't exist, or is not readable. The error code would be 404.
136
- *
137
- * @public
138
- */
139
- async ReadFile(path: string): Promise<string> {
140
- const response = await this.core.makeRequest("GET", "files/read", { path: path });
141
- const data: FileReadResponse = await response.json();
142
-
143
- if (data.errors) {
144
- throw new Error(`Server returned error code: ${data.errors[0].data.code}`);
145
- }
146
-
147
- return data.content;
148
- }
149
-
150
-
151
- /**
152
- * Writes the given content to the File at the given path
153
- *
154
- * @remarks
155
- * ⚠️ Overwrites the file if it already exists
156
- * ⚠️ This function silently fails if the target path is not writeable
157
- *
158
- * @param path The full path to the File
159
- * @param content The full content of the File
160
- *
161
- * @public
162
- */
163
- async WriteFile(path: string, content: string): Promise<void> {
164
- const data: FileWriteRequest = { path: path, content: content };
165
- await this.core.makeRequest("POST", "files/write", data);
166
- }
167
-
168
-
169
- /**
170
- * Copies the File at the given path
171
- *
172
- * @remarks
173
- * ⚠️ New copy will be written to the same directory, with a name such as `test.txt` -> `test.txt copy-1643810941850`
174
- *
175
- * @param path The full path to the File to Copy
176
- *
177
- * @throws "Unexpected response code, Copy may not have succeeded"
178
- * 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.
179
- *
180
- * @public
181
- */
182
- async CopyFile(path: string): Promise<void> {
183
- const requestData: CopyFileRequest = { path: path };
184
- const response = await this.core.makeRequest("POST", "files/copy", requestData);
185
-
186
- if (response.status != 204) {
187
- throw new Error("Unexpected response code, Copy may not have succeeded");
188
- }
189
- }
190
-
191
-
192
- /**
193
- * Deletes the Files at all of the given paths
194
- *
195
- * @param paths An array of File Paths to Delete
196
- *
197
- * @throws "Unexpected response code, Delete may not have succeeded"
198
- * 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.
199
- *
200
- * @public
201
- */
202
- async DeleteFiles(paths: string[]): Promise<void> {
203
- const response = await this.core.makeRequest("POST", "files/delete", { paths: paths });
204
-
205
- if (response.status != 204) {
206
- throw new Error("Unexpected response code, Delete may not have succeeded");
207
- }
208
- }
209
-
210
-
211
- /**
212
- * Retrieves a Download URL for a File
213
- *
214
- * @remarks
215
- * ⚠️ This will return a Download URL even if the given file does not exist
216
- *
217
- * @param path The full path to the File
218
- *
219
- * @public
220
- */
221
- async GetFileDownloadURL(path: string): Promise<string> {
222
- const response = await this.core.makeRequest("GET", "files/download", { path: path });
223
- const data: DownloadFileResponse = await response.json();
224
-
225
- return data.url;
226
- }
227
-
228
-
229
- /**
230
- * Renames (or moves) the given File
231
- *
232
- * @param path The full path to the File
233
- * @param to The new path of the File
234
- *
235
- * @throws "Unexpected response code, Rename may not have succeeded"
236
- * 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
237
- *
238
- * @public
239
- */
240
- async RenameFile(path: string, to: string): Promise<void> {
241
- const requestData: RenameFileRequest = { path: path, to: to };
242
- const response = await this.core.makeRequest("PATCH", "files/rename", requestData);
243
-
244
- if (response.status != 204) {
245
- throw new Error("Unexpected response code, Rename may not have succeeded");
246
- }
247
- }
248
-
249
-
250
- /**
251
- * Compresses the Files at the given paths
252
- *
253
- * @remarks
254
- * ⚠️ The resulting file appears to be unpredictably named?
255
- *
256
- * @param paths An array of Paths to Compress
257
- * @param to The Directory to write the Compressed Files to
258
- *
259
- * @throws "Unexpected response code, Compress may not have succeeded"
260
- * 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
261
- *
262
- * @public
263
- */
264
- async CompressFiles(paths: string[], to: string): Promise<void> {
265
- const requestData: CompressFilesRequest = { paths: paths, to: to };
266
- const response = await this.core.makeRequest("POST", "files/compress", requestData);
267
-
268
- if (response.status != 204) {
269
- throw new Error("Unexpected response code, Compress may not have succeeded");
270
- }
271
- }
272
-
273
-
274
- /**
275
- * Decompresses the File at the given path
276
- *
277
- * @param path The full path to the File to Decompress
278
- *
279
- * @throws "Unexpected response code, Decompress may not have succeeded"
280
- * 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
281
- *
282
- * @public
283
- */
284
- async DecompressFile(path: string): Promise<void> {
285
- const response = await this.core.makeRequest("POST", "files/decompress", { path: path });
286
-
287
- if (response.status != 204) {
288
- throw new Error("Unexpected response code, Decompress may not have succeeded");
289
- }
290
- }
291
- }
@@ -1,135 +0,0 @@
1
- export type RequestTypes = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
2
-
3
- export interface PaginationData {
4
- total: number;
5
- count: number;
6
- perPage: number;
7
- currentPage: number;
8
- totalPages: number;
9
- }
10
-
11
- export interface WispAPICore {
12
- domain: string;
13
- uuid: string;
14
- token: string;
15
- logger: any;
16
- }
17
-
18
- /**
19
- * The Core of the API, handling low-level operations such as making requests and setting the server UUID
20
- *
21
- * @public
22
- */
23
- export class WispAPICore {
24
- constructor(domain: string, uuid: string, token: string, logger: any) {
25
- this.domain = domain;
26
- this.uuid = uuid;
27
- this.token = token;
28
- this.logger = logger;
29
- }
30
-
31
- /**
32
- * Sets the Server UUID
33
- *
34
- * @remarks
35
- * ℹ️ This can be updated at any time, making all future API calls reference the new Server UUID
36
- *
37
- * @public
38
- */
39
- setUUID(uuid: string) {
40
- this.uuid = uuid;
41
- }
42
-
43
- /**
44
- * Generates a URL for the given path
45
- *
46
- * @param path The API path
47
- *
48
- * @internal
49
- */
50
- makeURL(path: string) {
51
- return `https://${this.domain}/api/client/servers/${this.uuid}/${path}`;
52
- }
53
-
54
- // TODO: Handle standard field-level error messages:
55
- // {"errors":[{"code":"required","detail":"The path field is required","source":{"field":"path"}}]}
56
- /**
57
- * Makes a request with the headers and request data set automatically.
58
- *
59
- * @remarks
60
- * The data field is formatted appropriately for whichever request type is given.
61
- *
62
- * @param method A standared request method.
63
- * @param path The API path to send the request to.
64
- * @param data The data to include with the request.
65
- *
66
- * @internal
67
- */
68
- async makeRequest(method: RequestTypes, path: string, data?: any) {
69
- let url = this.makeURL(path);
70
- const headers = new Headers({
71
- "Content-Type": "application/json",
72
- "Accept": "application/vnd.wisp.v1+json",
73
- "Authorization": `Bearer ${this.token}`,
74
- "User-Agent": "WispJS (https://github.com/CFC-Servers/wispjs, 1.0.0)"
75
- });
76
-
77
- const request = async () => {
78
- let response: Response;
79
-
80
- console.log(`${method} -> ${url}`);
81
-
82
- switch(method) {
83
- case "GET":
84
- if (data !== null) {
85
- const params = new URLSearchParams(data);
86
- const uri = new URL(url);
87
-
88
- uri.search = params.toString();
89
- url = uri.toString();
90
- console.log(`Updated GET URL: ${url}`);
91
- }
92
-
93
- response = await fetch(url, { method: "GET", headers: headers });
94
- break;
95
-
96
- case "POST":
97
- data = JSON.stringify(data);
98
- response = await fetch(url, { method: "POST", headers: headers, body: data });
99
- break;
100
-
101
- case "PUT":
102
- data = data ? JSON.stringify(data) : "";
103
- response = await fetch(url, { method: "PUT", headers: headers, body: data });
104
- break;
105
-
106
- case "PATCH":
107
- data = JSON.stringify(data);
108
- response = await fetch(url, { method: "PATCH", headers: headers, body: data });
109
- break;
110
-
111
- case "DELETE":
112
- response = await fetch(url, { method: "DELETE", headers: headers });
113
- break;
114
-
115
- default:
116
- throw new Error(`Invalid method: ${method}`);
117
- }
118
-
119
- if (!response.ok) {
120
- const status = response.status;
121
- const statusText = response.statusText;
122
- this.logger.error(`Request failed: ${method} -> ${url}: ${status} - ${statusText}`);
123
-
124
- const text = await response.text();
125
- this.logger.error(text);
126
-
127
- throw new Error(`Request failed! Status: ${status} - ${statusText}`);
128
- }
129
-
130
- return response;
131
- }
132
-
133
- return await request();
134
- }
135
- }
@@ -1,53 +0,0 @@
1
- import { WispAPICore } from "./index";
2
-
3
- export interface Mod {
4
- object: "mod";
5
- attributes: {
6
- id: number;
7
- name: string;
8
- description: string;
9
- version: string;
10
- category: string;
11
- install_count: number;
12
- server_state: number;
13
- }
14
- }
15
-
16
- export interface GetModsResponse {
17
- object: "list";
18
- data: Mod[];
19
- }
20
-
21
- /**
22
- * Handles Listing and Installating of Mods
23
- *
24
- * @public
25
- */
26
- export class ModsAPI {
27
- constructor(private core: WispAPICore) {}
28
-
29
-
30
- /**
31
- * Lists all Mods available to the Server
32
- *
33
- * @public
34
- */
35
- async List(): Promise<GetModsResponse> {
36
- const response = await this.core.makeRequest("GET", "mods");
37
- const data: GetModsResponse = await response.json();
38
-
39
- return data;
40
- }
41
-
42
-
43
- /**
44
- * Installs or Uninstalls the Mod with the given id
45
- *
46
- * @param id The ID of the Mod to Install/Uninstall
47
- *
48
- * @public
49
- */
50
- async ToggleInstall(id: string): Promise<void> {
51
- await this.core.makeRequest("POST", `mods/${id}`);
52
- }
53
- }