@@ -16,27 +16,33 @@ interface ArunaEvents {
1616}
1717
1818/**
19- * Main class for the api client
20- * @class ArunaClient
21- * @extends {EventEmitter }
19+ * The main WebSocket client for connecting to an ArunaCore server.
20+ * Handles connection, authentication, messaging, and event management.
21+ *
22+ * @remarks
23+ * This client supports secure and shard modes, emits events for connection lifecycle and message handling,
24+ * and provides methods for sending messages, pinging, and closing the connection.
25+ *
26+ * @extends EventEmitter
27+ *
2228 * @example
23- * const { ArunaClient } = require( 'aruna-api') ;
29+ * import { ArunaClient } from 'aruna-api';
2430 * const client = new ArunaClient({
25- * host: 'localhost',
26- * port: 3000,
27- * secureMode: false,
28- * shardMode: false,
29- * secureKey: null,
30- * logger: null,
31- * id: 'client'
31+ * host: 'localhost',
32+ * port: 3000,
33+ * secureMode: false,
34+ * shardMode: false,
35+ * secureKey: null,
36+ * logger: null,
37+ * id: 'client'
3238 * });
3339 * client.on('ready', () => {
34- * console.log('Client is ready!');
40+ * console.log('Client is ready!');
3541 * });
3642 * client.on('message', (message) => {
37- * console.log(message);
43+ * console.log(message);
3844 * });
39- * client.connect(); // optional: secureKey
45+ * await client.connect(); // Optionally pass secureKey
4046 */
4147export class ArunaClient extends EventEmitter < ArunaEvents > {
4248 private finishTimeout : ReturnType < typeof setTimeout > | null = null ;
@@ -69,15 +75,18 @@ export class ArunaClient extends EventEmitter<ArunaEvents> {
6975 }
7076
7177 /**
72- * Connects to the ArunaCore server
73- * @param {string? } [secureKey] Secure key for secure mode
74- * @returns {Promise<void> }
78+ * Establishes a WebSocket connection to the ArunaCore server.
79+ *
80+ * @param {string? } [secureKey] Optional secure key for authentication in secure mode. If provided, enables secure mode.
81+ * @returns {Promise<void> } Promise that resolves when the connection is established, or rejects on error.
82+ * @throws Error if secure mode is enabled but no secure key is provided, or if shard mode is enabled without secure mode.
7583 */
7684 public async connect ( secureKey ?: string ) : Promise < void > {
7785 if ( secureKey ) {
7886 this . secureKey = secureKey ;
7987 this . secureMode = true ;
8088 }
89+
8190 this . ws = new ws ( `ws://${ this . host } :${ this . port } ` , {
8291 headers : {
8392 'Authorization' : this . secureKey ?? '' ,
@@ -96,12 +105,12 @@ export class ArunaClient extends EventEmitter<ArunaEvents> {
96105 this . ws ! . on ( 'open' , ( ) => {
97106 this . logger . debug ( 'Connected to server!' ) ;
98107 this . emit ( 'ready' ) ;
99- resolve ( ) ;
108+ return resolve ( ) ;
100109 } ) ;
101110
102111 this . ws ! . on ( 'error' , ( err ) => {
103112 this . logger . error ( err ) ;
104- reject ( err ) ;
113+ return reject ( err ) ;
105114 } ) ;
106115
107116 this . ws ! . on ( 'unexpected-response' , ( req , res ) => {
@@ -113,34 +122,34 @@ export class ArunaClient extends EventEmitter<ArunaEvents> {
113122 this . logger . warn ( 'Conflict: Client ID already connected! Randomizing a new one...' ) ;
114123 this . id = this . id + utils . randomString ( 5 ) ;
115124 this . connect ( ) . then ( ( ) => {
116- resolve ( ) ;
125+ return resolve ( ) ;
117126 } ) . catch ( ( err ) => {
118127 reject ( err ) ;
119128 } ) ;
120129 } else if ( res . statusCode === 412 ) {
121130 this . logger . error ( 'Precondition Failed: Unsupported API version!' ) ;
122131 this . ws ?. close ( ) ;
123- reject ( new Error ( 'Precondition Failed: Unsupported API version!' ) ) ;
132+ return reject ( new Error ( 'Precondition Failed: Unsupported API version!' ) ) ;
124133 } else {
125134 this . logger . error ( `Unexpected response: ${ res . statusCode } ` ) ;
126135 this . ws ?. close ( ) ;
127- reject ( new Error ( `Unexpected response: ${ res . statusCode } ` ) ) ;
136+ return reject ( new Error ( `Unexpected response: ${ res . statusCode } ` ) ) ;
128137 }
129138 } ) ;
130139 } ) ;
131140 }
132141
133142 /**
134143 * Sends a message to the ArunaCore server.
135- *
136- * @param {any } content - Content of the message, can be a string or a serializable object.
137- * @param {Object } options - Options for the message .
138- * @param {string } [options.type] - Type of the client or message.
139- * @param {string } [options.command] - Command code for the message.
140- * @param {{ id: string, key?: string } } [options.target] - Target of the message (id and optional key).
141- * @param {string[] } [options.args] - Arguments of the message.
142- * @returns {Promise<void> } Resolves when the message is sent.
143- *
144+ *
145+ * @param {unknown } content The message content , can be a string or a serializable object.
146+ * @param {Object } options Message options .
147+ * @param {string } [options.type] Type of the client or message.
148+ * @param {string } [options.command] Command code for the message.
149+ * @param {{ id: string, key?: string } } [options.target] Target recipient (id and optional key).
150+ * @param {string[] } [options.args] Arguments for the message.
151+ * @returns {Promise<void> } Promise that resolves when the message is sent, or rejects if the connection is not open or secure key is missing in secure mode .
152+ *
144153 * @example
145154 * await client.send('100', {
146155 * args: ['Hello World!'],
@@ -149,7 +158,7 @@ export class ArunaClient extends EventEmitter<ArunaEvents> {
149158 * command: 'someCommand'
150159 * });
151160 */
152- public async send ( content : any , { type, command, target, args } : { type ?: string , command ?: string , target ?: { id : string , key ?: string } , args ?: string [ ] } ) : Promise < void > {
161+ public async send ( content : unknown , { type, command, target, args } : { type ?: string , command ?: string , target ?: { id : string , key ?: string } , args ?: string [ ] } ) : Promise < void > {
153162 return new Promise ( ( resolve , reject ) => {
154163 if ( this . ws ?. readyState !== ws . OPEN ) return reject ( new Error ( 'Connection is not open!' ) ) ;
155164
@@ -172,9 +181,10 @@ export class ArunaClient extends EventEmitter<ArunaEvents> {
172181 }
173182
174183 /**
175- * Called when a message is received from the ArunaCore server
176- * Responsable for parsing the message and emitting the events
177- * @param {string } message Message received
184+ * Handles incoming messages from the ArunaCore server.
185+ * Parses the message and emits the appropriate events.
186+ *
187+ * @param {string } message The raw message string received from the server.
178188 * @returns {void }
179189 * @private
180190 */
@@ -208,12 +218,13 @@ export class ArunaClient extends EventEmitter<ArunaEvents> {
208218 }
209219
210220 /**
211- * Pings the ArunaCore server
212- * @returns {Promise<boolean> }
221+ * Sends a ping frame to the ArunaCore server to check connectivity.
222+ *
223+ * @returns {Promise<boolean> } Promise that resolves to true if the ping succeeds, or rejects on error.
224+ *
213225 * @example
214- * client.ping().then(() => {
215- * console.log('Pong!');
216- * });
226+ * await client.ping();
227+ * // => Pong!
217228 */
218229 public async ping ( ) : Promise < boolean > {
219230 return new Promise ( ( resolve , reject ) => {
@@ -230,20 +241,23 @@ export class ArunaClient extends EventEmitter<ArunaEvents> {
230241 }
231242
232243 /**
233- * Returns the client id
234- * @returns {string }
244+ * Gets the current client ID.
245+ *
246+ * @returns {string } The client ID string.
235247 */
236248 public getID ( ) : string {
237249 return this . id ;
238250 }
239251
240252 /**
241- * Closes the connection to the ArunaCore server
242- * @returns {Promise<void> }
253+ * Gracefully closes the connection to the ArunaCore server.
254+ * Sends an unregister request and waits for confirmation or times out after 5 seconds.
255+ *
256+ * @returns {Promise<void> } Promise that resolves when the connection is closed.
257+ *
243258 * @example
244- * client.finish().then(() => {
245- * console.log('Connection closed!');
246- * });
259+ * await client.finish();
260+ * // => Connection closed!
247261 */
248262 public async finish ( ) : Promise < void > {
249263 return new Promise ( async ( resolve ) => {
0 commit comments