magmastream 2.10.2-alpha.0 → 2.10.2-alpha.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,6 +1,6 @@
1
1
  import { Node } from "./Node";
2
2
  import { Manager } from "./Manager";
3
- import { LavaPlayer, RestPlayOptions } from "./Types";
3
+ import { LavalinkSession, LavaPlayer, RestPlayOptions } from "./Types";
4
4
  /** Handles the requests sent to the Lavalink REST API. */
5
5
  export declare class Rest {
6
6
  /** The Node that this Rest instance is connected to. */
@@ -24,31 +24,30 @@ export declare class Rest {
24
24
  */
25
25
  setSessionId(sessionId: string): string;
26
26
  /**
27
- * Retrieves one the player that is currently running on the node.
28
- * @returns {Promise<unknown>} Returns the result of the GET request.
27
+ * Retrieves the player that is currently running on the node.
28
+ * @param {string} guildId The guild ID of the player to retrieve.
29
+ * @returns {Promise<LavaPlayer>} Returns the player.
29
30
  */
30
31
  getPlayer(guildId: string): Promise<LavaPlayer>;
31
32
  /**
32
33
  * Sends a PATCH request to update player related data.
33
34
  * @param {RestPlayOptions} options The options to update the player with.
34
- * @returns {Promise<unknown>} Returns the result of the PATCH request.
35
+ * @returns {Promise<LavaPlayer>} Returns the updated player.
35
36
  */
36
- updatePlayer(options: RestPlayOptions): Promise<unknown>;
37
+ updatePlayer(options: RestPlayOptions): Promise<LavaPlayer>;
37
38
  /**
38
39
  * Sends a DELETE request to the server to destroy the player.
39
40
  * @param {string} guildId The guild ID of the player to destroy.
40
- * @returns {Promise<unknown>} Returns the result of the DELETE request.
41
+ * @returns {Promise<void>} Returns void (204 No Content).
41
42
  */
42
- destroyPlayer(guildId: string): Promise<unknown>;
43
+ destroyPlayer(guildId: string): Promise<void>;
43
44
  /**
44
45
  * Updates the session status for resuming.
45
- * This method sends a PATCH request to update the session's resuming status and timeout.
46
- *
47
46
  * @param {boolean} resuming - Indicates whether the session should be set to resuming.
48
47
  * @param {number} timeout - The timeout duration for the session resume.
49
- * @returns {Promise<unknown>} The result of the PATCH request.
48
+ * @returns {Promise<LavalinkSession>} The updated session.
50
49
  */
51
- updateSession(resuming: boolean, timeout: number): Promise<unknown>;
50
+ updateSession(resuming: boolean, timeout: number): Promise<LavalinkSession>;
52
51
  /**
53
52
  * Sends a request to the specified endpoint and returns the response data.
54
53
  * @param {string} method The HTTP method to use for the request.
@@ -60,34 +59,34 @@ export declare class Rest {
60
59
  /**
61
60
  * Sends a GET request to the specified endpoint and returns the response data.
62
61
  * @param {string} endpoint The endpoint to send the GET request to.
63
- * @returns {Promise<unknown>} The response data of the GET request.
62
+ * @returns {Promise<T>} The response data of the GET request.
64
63
  */
65
- get(endpoint: string): Promise<unknown>;
64
+ get<T>(endpoint: string): Promise<T>;
66
65
  /**
67
66
  * Sends a PATCH request to the specified endpoint and returns the response data.
68
67
  * @param {string} endpoint The endpoint to send the PATCH request to.
69
68
  * @param {unknown} body The data to send in the request body.
70
- * @returns {Promise<unknown>} The response data of the PATCH request.
69
+ * @returns {Promise<T>} The response data of the PATCH request.
71
70
  */
72
- patch(endpoint: string, body: unknown): Promise<unknown>;
71
+ patch<T>(endpoint: string, body: unknown): Promise<T>;
73
72
  /**
74
73
  * Sends a POST request to the specified endpoint and returns the response data.
75
74
  * @param {string} endpoint The endpoint to send the POST request to.
76
75
  * @param {unknown} body The data to send in the request body.
77
- * @returns {Promise<unknown>} The response data of the POST request.
76
+ * @returns {Promise<T>} The response data of the POST request.
78
77
  */
79
- post(endpoint: string, body: unknown): Promise<unknown>;
78
+ post<T>(endpoint: string, body: unknown): Promise<T>;
80
79
  /**
81
80
  * Sends a PUT request to the specified endpoint and returns the response data.
82
81
  * @param {string} endpoint The endpoint to send the PUT request to.
83
82
  * @param {unknown} body The data to send in the request body.
84
- * @returns {Promise<unknown>} The response data of the PUT request.
83
+ * @returns {Promise<T>} The response data of the PUT request.
85
84
  */
86
- put(endpoint: string, body: unknown): Promise<unknown>;
85
+ put<T>(endpoint: string, body: unknown): Promise<T>;
87
86
  /**
88
87
  * Sends a DELETE request to the specified endpoint.
89
88
  * @param {string} endpoint - The endpoint to send the DELETE request to.
90
- * @returns {Promise<unknown>} The response data of the DELETE request.
89
+ * @returns {Promise<void>} Void.
91
90
  */
92
- delete(endpoint: string): Promise<unknown>;
91
+ delete(endpoint: string): Promise<void>;
93
92
  }
@@ -39,51 +39,41 @@ class Rest {
39
39
  return this.sessionId;
40
40
  }
41
41
  /**
42
- * Retrieves one the player that is currently running on the node.
43
- * @returns {Promise<unknown>} Returns the result of the GET request.
42
+ * Retrieves the player that is currently running on the node.
43
+ * @param {string} guildId The guild ID of the player to retrieve.
44
+ * @returns {Promise<LavaPlayer>} Returns the player.
44
45
  */
45
46
  async getPlayer(guildId) {
46
- // Send a GET request to the Lavalink Node to retrieve all the players.
47
- const result = (await this.get(`/v4/sessions/${this.sessionId}/players/${guildId}`));
48
- // Log the result of the request.
49
- this.manager.emit(Enums_1.ManagerEventTypes.Debug, `[REST] Getting all players on node: ${this.node.options.identifier} : ${Utils_1.JSONUtils.safe(result, 2)}`);
50
- // Return the result of the request.
47
+ const result = await this.get(`/v4/sessions/${this.sessionId}/players/${guildId}`);
48
+ this.manager.emit(Enums_1.ManagerEventTypes.Debug, `[REST] Getting player on node: ${this.node.options.identifier} : ${Utils_1.JSONUtils.safe(result, 2)}`);
51
49
  return result;
52
50
  }
53
51
  /**
54
52
  * Sends a PATCH request to update player related data.
55
53
  * @param {RestPlayOptions} options The options to update the player with.
56
- * @returns {Promise<unknown>} Returns the result of the PATCH request.
54
+ * @returns {Promise<LavaPlayer>} Returns the updated player.
57
55
  */
58
56
  async updatePlayer(options) {
59
- // Log the request.
60
57
  this.manager.emit(Enums_1.ManagerEventTypes.Debug, `[REST] Updating player: ${options.guildId}: ${Utils_1.JSONUtils.safe(options, 2)}`);
61
- // Send the PATCH request.
62
- return await this.patch(`/v4/sessions/${this.sessionId}/players/${options.guildId}?noReplace=false`, options.data);
58
+ return await this.patch(`/v4/sessions/${this.sessionId}/players/${options.guildId}?noReplace=${options.noReplace ?? false}`, options.data);
63
59
  }
64
60
  /**
65
61
  * Sends a DELETE request to the server to destroy the player.
66
62
  * @param {string} guildId The guild ID of the player to destroy.
67
- * @returns {Promise<unknown>} Returns the result of the DELETE request.
63
+ * @returns {Promise<void>} Returns void (204 No Content).
68
64
  */
69
65
  async destroyPlayer(guildId) {
70
- // Log the request.
71
66
  this.manager.emit(Enums_1.ManagerEventTypes.Debug, `[REST] Destroying player: ${guildId}`);
72
- // Send the DELETE request.
73
67
  return await this.delete(`/v4/sessions/${this.sessionId}/players/${guildId}`);
74
68
  }
75
69
  /**
76
70
  * Updates the session status for resuming.
77
- * This method sends a PATCH request to update the session's resuming status and timeout.
78
- *
79
71
  * @param {boolean} resuming - Indicates whether the session should be set to resuming.
80
72
  * @param {number} timeout - The timeout duration for the session resume.
81
- * @returns {Promise<unknown>} The result of the PATCH request.
73
+ * @returns {Promise<LavalinkSession>} The updated session.
82
74
  */
83
75
  async updateSession(resuming, timeout) {
84
- // Emit a debug event with information about the session being updated
85
76
  this.manager.emit(Enums_1.ManagerEventTypes.Debug, `[REST] Updating session: ${this.sessionId}`);
86
- // Send a PATCH request to update the session with the provided resuming status and timeout
87
77
  return await this.patch(`/v4/sessions/${this.sessionId}`, { resuming, timeout });
88
78
  }
89
79
  /**
@@ -113,7 +103,12 @@ class Rest {
113
103
  catch (e) {
114
104
  const isTimeout = axios_1.default.isAxiosError(e) && e.code === "ECONNABORTED";
115
105
  const message = e instanceof Error ? e.message : "Unknown error";
116
- this.manager.emit(Enums_1.ManagerEventTypes.NodeError, this.node, new Error(`${isTimeout ? "Timeout" : "Network error"} on ${method} ${endpoint}: ${message}`));
106
+ const error = new MagmastreamError_1.MagmaStreamError({
107
+ code: Enums_1.MagmaStreamErrorCode.REST_REQUEST_FAILED,
108
+ message: `${isTimeout ? "Timeout" : "Network error"} on ${method} ${endpoint}: ${message}`,
109
+ });
110
+ // Emit so the node manager can react (e.g. trigger reconnection logic)
111
+ this.manager.emit(Enums_1.ManagerEventTypes.NodeError, this.node, error);
117
112
  return null;
118
113
  }
119
114
  const { status, data } = response;
@@ -124,81 +119,64 @@ class Rest {
124
119
  if (status === 404 && data?.message === "Guild not found") {
125
120
  return [];
126
121
  }
122
+ const message = typeof data === "string"
123
+ ? data
124
+ : typeof data === "object" && data !== null && "message" in data && typeof data.message === "string"
125
+ ? data.message
126
+ : Utils_1.JSONUtils.safe(data, 2);
127
127
  if (status === 401) {
128
128
  throw new MagmastreamError_1.MagmaStreamError({
129
129
  code: Enums_1.MagmaStreamErrorCode.REST_UNAUTHORIZED,
130
130
  message: `Unauthorized access to node ${this.node.options.identifier}`,
131
131
  });
132
132
  }
133
- if (status >= 400 && status < 500) {
134
- const message = typeof data === "string"
135
- ? data
136
- : typeof data === "object" && data !== null && "message" in data && typeof data.message === "string"
137
- ? data.message
138
- : "Unknown client error";
139
- return {
140
- status,
141
- error: true,
142
- message,
143
- data,
144
- };
145
- }
146
- const safeMessage = typeof data === "string"
147
- ? data
148
- : typeof data === "object" && data !== null && "message" in data && typeof data.message === "string"
149
- ? data.message
150
- : Utils_1.JSONUtils.safe(data, 2);
151
133
  throw new MagmastreamError_1.MagmaStreamError({
152
134
  code: Enums_1.MagmaStreamErrorCode.REST_REQUEST_FAILED,
153
- message: `Request to node ${this.node.options.identifier} failed (${status}): ${safeMessage}`,
135
+ message: `Request to node ${this.node.options.identifier} failed (${status}): ${message}`,
154
136
  });
155
137
  }
156
138
  /**
157
139
  * Sends a GET request to the specified endpoint and returns the response data.
158
140
  * @param {string} endpoint The endpoint to send the GET request to.
159
- * @returns {Promise<unknown>} The response data of the GET request.
141
+ * @returns {Promise<T>} The response data of the GET request.
160
142
  */
161
143
  async get(endpoint) {
162
- // Send a GET request to the specified endpoint and return the response data.
163
- return await this.request("GET", endpoint);
144
+ return (await this.request("GET", endpoint));
164
145
  }
165
146
  /**
166
147
  * Sends a PATCH request to the specified endpoint and returns the response data.
167
148
  * @param {string} endpoint The endpoint to send the PATCH request to.
168
149
  * @param {unknown} body The data to send in the request body.
169
- * @returns {Promise<unknown>} The response data of the PATCH request.
150
+ * @returns {Promise<T>} The response data of the PATCH request.
170
151
  */
171
152
  async patch(endpoint, body) {
172
- // Send a PATCH request to the specified endpoint and return the response data.
173
- return await this.request("PATCH", endpoint, body);
153
+ return (await this.request("PATCH", endpoint, body));
174
154
  }
175
155
  /**
176
156
  * Sends a POST request to the specified endpoint and returns the response data.
177
157
  * @param {string} endpoint The endpoint to send the POST request to.
178
158
  * @param {unknown} body The data to send in the request body.
179
- * @returns {Promise<unknown>} The response data of the POST request.
159
+ * @returns {Promise<T>} The response data of the POST request.
180
160
  */
181
161
  async post(endpoint, body) {
182
- return await this.request("POST", endpoint, body);
162
+ return (await this.request("POST", endpoint, body));
183
163
  }
184
164
  /**
185
165
  * Sends a PUT request to the specified endpoint and returns the response data.
186
166
  * @param {string} endpoint The endpoint to send the PUT request to.
187
167
  * @param {unknown} body The data to send in the request body.
188
- * @returns {Promise<unknown>} The response data of the PUT request.
168
+ * @returns {Promise<T>} The response data of the PUT request.
189
169
  */
190
170
  async put(endpoint, body) {
191
- // Send a PUT request to the specified endpoint and return the response data.
192
- return await this.request("PUT", endpoint, body);
171
+ return (await this.request("PUT", endpoint, body));
193
172
  }
194
173
  /**
195
174
  * Sends a DELETE request to the specified endpoint.
196
175
  * @param {string} endpoint - The endpoint to send the DELETE request to.
197
- * @returns {Promise<unknown>} The response data of the DELETE request.
176
+ * @returns {Promise<void>} Void.
198
177
  */
199
178
  async delete(endpoint) {
200
- // Send a DELETE request using the request method and return the response data.
201
- return await this.request("DELETE", endpoint);
179
+ await this.request("DELETE", endpoint);
202
180
  }
203
181
  }
204
182
  exports.Rest = Rest;
@@ -1169,6 +1169,13 @@ export type LavalinkVoiceStateUpdate = {
1169
1169
  sessionId: string;
1170
1170
  channelId: string;
1171
1171
  };
1172
+ /**
1173
+ * Lavalink session
1174
+ */
1175
+ export interface LavalinkSession {
1176
+ resuming?: boolean;
1177
+ timeout?: number;
1178
+ }
1172
1179
  /**
1173
1180
  * ManagerInitOptions interface
1174
1181
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magmastream",
3
- "version": "2.10.2-alpha.0",
3
+ "version": "2.10.2-alpha.1",
4
4
  "description": "A user-friendly Lavalink client designed for NodeJS.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",