magmastream 2.10.1 → 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
  /**
@@ -106,16 +96,20 @@ class Rest {
106
96
  timeout: this.node.options.apiRequestTimeoutMs,
107
97
  validateStatus: () => true,
108
98
  };
109
- let response;
99
+ let response = null;
110
100
  try {
111
101
  response = await (0, axios_1.default)(config);
112
102
  }
113
- catch (err) {
114
- const message = err instanceof Error ? err.message : "Unknown error";
115
- throw new MagmastreamError_1.MagmaStreamError({
103
+ catch (e) {
104
+ const isTimeout = axios_1.default.isAxiosError(e) && e.code === "ECONNABORTED";
105
+ const message = e instanceof Error ? e.message : "Unknown error";
106
+ const error = new MagmastreamError_1.MagmaStreamError({
116
107
  code: Enums_1.MagmaStreamErrorCode.REST_REQUEST_FAILED,
117
- message: `No response from node ${this.node.options.identifier}: ${message}`,
108
+ message: `${isTimeout ? "Timeout" : "Network error"} on ${method} ${endpoint}: ${message}`,
118
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);
112
+ return null;
119
113
  }
120
114
  const { status, data } = response;
121
115
  if (status >= 200 && status < 300) {
@@ -125,81 +119,64 @@ class Rest {
125
119
  if (status === 404 && data?.message === "Guild not found") {
126
120
  return [];
127
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);
128
127
  if (status === 401) {
129
128
  throw new MagmastreamError_1.MagmaStreamError({
130
129
  code: Enums_1.MagmaStreamErrorCode.REST_UNAUTHORIZED,
131
130
  message: `Unauthorized access to node ${this.node.options.identifier}`,
132
131
  });
133
132
  }
134
- if (status >= 400 && status < 500) {
135
- const message = typeof data === "string"
136
- ? data
137
- : typeof data === "object" && data !== null && "message" in data && typeof data.message === "string"
138
- ? data.message
139
- : "Unknown client error";
140
- return {
141
- status,
142
- error: true,
143
- message,
144
- data,
145
- };
146
- }
147
- const safeMessage = typeof data === "string"
148
- ? data
149
- : typeof data === "object" && data !== null && "message" in data && typeof data.message === "string"
150
- ? data.message
151
- : Utils_1.JSONUtils.safe(data, 2);
152
133
  throw new MagmastreamError_1.MagmaStreamError({
153
134
  code: Enums_1.MagmaStreamErrorCode.REST_REQUEST_FAILED,
154
- message: `Request to node ${this.node.options.identifier} failed (${status}): ${safeMessage}`,
135
+ message: `Request to node ${this.node.options.identifier} failed (${status}): ${message}`,
155
136
  });
156
137
  }
157
138
  /**
158
139
  * Sends a GET request to the specified endpoint and returns the response data.
159
140
  * @param {string} endpoint The endpoint to send the GET request to.
160
- * @returns {Promise<unknown>} The response data of the GET request.
141
+ * @returns {Promise<T>} The response data of the GET request.
161
142
  */
162
143
  async get(endpoint) {
163
- // Send a GET request to the specified endpoint and return the response data.
164
- return await this.request("GET", endpoint);
144
+ return (await this.request("GET", endpoint));
165
145
  }
166
146
  /**
167
147
  * Sends a PATCH request to the specified endpoint and returns the response data.
168
148
  * @param {string} endpoint The endpoint to send the PATCH request to.
169
149
  * @param {unknown} body The data to send in the request body.
170
- * @returns {Promise<unknown>} The response data of the PATCH request.
150
+ * @returns {Promise<T>} The response data of the PATCH request.
171
151
  */
172
152
  async patch(endpoint, body) {
173
- // Send a PATCH request to the specified endpoint and return the response data.
174
- return await this.request("PATCH", endpoint, body);
153
+ return (await this.request("PATCH", endpoint, body));
175
154
  }
176
155
  /**
177
156
  * Sends a POST request to the specified endpoint and returns the response data.
178
157
  * @param {string} endpoint The endpoint to send the POST request to.
179
158
  * @param {unknown} body The data to send in the request body.
180
- * @returns {Promise<unknown>} The response data of the POST request.
159
+ * @returns {Promise<T>} The response data of the POST request.
181
160
  */
182
161
  async post(endpoint, body) {
183
- return await this.request("POST", endpoint, body);
162
+ return (await this.request("POST", endpoint, body));
184
163
  }
185
164
  /**
186
165
  * Sends a PUT request to the specified endpoint and returns the response data.
187
166
  * @param {string} endpoint The endpoint to send the PUT request to.
188
167
  * @param {unknown} body The data to send in the request body.
189
- * @returns {Promise<unknown>} The response data of the PUT request.
168
+ * @returns {Promise<T>} The response data of the PUT request.
190
169
  */
191
170
  async put(endpoint, body) {
192
- // Send a PUT request to the specified endpoint and return the response data.
193
- return await this.request("PUT", endpoint, body);
171
+ return (await this.request("PUT", endpoint, body));
194
172
  }
195
173
  /**
196
174
  * Sends a DELETE request to the specified endpoint.
197
175
  * @param {string} endpoint - The endpoint to send the DELETE request to.
198
- * @returns {Promise<unknown>} The response data of the DELETE request.
176
+ * @returns {Promise<void>} Void.
199
177
  */
200
178
  async delete(endpoint) {
201
- // Send a DELETE request using the request method and return the response data.
202
- return await this.request("DELETE", endpoint);
179
+ await this.request("DELETE", endpoint);
203
180
  }
204
181
  }
205
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.1",
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",
@@ -16,7 +16,8 @@
16
16
  "lint": "eslint \"src/**/*.{ts,js}\"",
17
17
  "lint:fix": "eslint --fix \"src/**/*.{ts,js}\"",
18
18
  "ci": "run-s format:check lint build types",
19
- "release:dev": "npm run format && npm run lint:fix && npm run ci && npm version prerelease --preid=dev && git push && npm publish --tag dev"
19
+ "release:alpha": "npm run format && npm run lint:fix && npm run ci && npm version prerelease --preid=alpha && npm publish --tag alpha && git push && git push --tags",
20
+ "release:dev": "npm run format && npm run lint:fix && npm run ci && npm version prerelease --preid=dev && npm publish --tag dev && git push && git push --tags"
20
21
  },
21
22
  "devDependencies": {
22
23
  "@favware/rollup-type-bundler": "^4.0.0",