// ╔═══════╗╔═══════╗╔═══════╗╔═══════╗╔═══════╗╔═══════╗╔═══════╗ // ╚══╗ ╔══╝║ ╔╗ ╔╗ ║║ ╔═══╗ ║║ ╔═══╗ ║║ ╔═══╗ ║╚══╗ ╔══╝║ ╔═════╝ // ║ ║ ║ ║║ ║║ ║║ ╚═══╝ ║║ ║ ║ ║║ ╚═══╝ ║ ║ ║ ║ ╚═════╗ // ║ ║ ║ ║║ ║║ ║║ ╔═════╝║ ║ ║ ║║ ╔═╗ ╔═╝ ║ ║ ╚═════╗ ║ // ╔══╝ ╚══╗║ ║║ ║║ ║║ ║ ║ ╚═══╝ ║║ ║ ║ ╚═╗ ║ ║ ╔═════╝ ║ // ╚═══════╝╚═╝╚═╝╚═╝╚═╝ ╚═══════╝╚═╝ ╚═══╝ ╚═╝ ╚═══════╝ import * as connectionValidators from '../validators/connection-validators.js'; import * as connectionTypings from '../typings/connection-typings.js'; import * as tokenModule from '../../token/token-module.js'; import * as userModule from '../../user/user-module.js'; // ╔═══════╗╔═╗ ╔═══════╗╔═══════╗╔═══════╗ // ║ ╔═════╝║ ║ ║ ╔═══╗ ║║ ╔═════╝║ ╔═════╝ // ║ ║ ║ ║ ║ ╚═══╝ ║║ ╚═════╗║ ╚═════╗ // ║ ║ ║ ║ ║ ╔═══╗ ║╚═════╗ ║╚═════╗ ║ // ║ ╚═════╗║ ╚═════╗║ ║ ║ ║╔═════╝ ║╔═════╝ ║ // ╚═══════╝╚═══════╝╚═╝ ╚═╝╚═══════╝╚═══════╝ export class Connection { // ╔═══════╗╔═══════╗╔════╗╔═╗╔═══════╗╔═══════╗╔═══════╗╔═╗ ╔═╗╔═══════╗╔═══════╗╔═══════╗╔═══════╗ // ║ ╔═════╝║ ╔═══╗ ║║ ╔╗ ║║ ║║ ╔═════╝╚══╗ ╔══╝║ ╔═══╗ ║║ ║ ║ ║║ ╔═════╝╚══╗ ╔══╝║ ╔═══╗ ║║ ╔═══╗ ║ // ║ ║ ║ ║ ║ ║║ ║║ ║║ ║║ ╚═════╗ ║ ║ ║ ╚═══╝ ║║ ║ ║ ║║ ║ ║ ║ ║ ║ ║ ║║ ╚═══╝ ║ // ║ ║ ║ ║ ║ ║║ ║║ ║║ ║╚═════╗ ║ ║ ║ ║ ╔═╗ ╔═╝║ ║ ║ ║║ ║ ║ ║ ║ ║ ║ ║║ ╔═╗ ╔═╝ // ║ ╚═════╗║ ╚═══╝ ║║ ║║ ╚╝ ║╔═════╝ ║ ║ ║ ║ ║ ║ ╚═╗║ ╚═══╝ ║║ ╚═════╗ ║ ║ ║ ╚═══╝ ║║ ║ ║ ╚═╗ // ╚═══════╝╚═══════╝╚═╝╚════╝╚═══════╝ ╚═╝ ╚═╝ ╚═══╝╚═══════╝╚═══════╝ ╚═╝ ╚═══════╝╚═╝ ╚═══╝ private readonly _resource: connectionTypings.ConnectionResource; private _token = null as null | string; constructor(resource: connectionTypings.ConnectionResource) { connectionValidators.validateConnectionResource(resource); this._resource = resource; } // ╔═╗ ╔═╗╔═══════╗╔═══════╗╔═══════╗╔═══════╗ // ║ ║ ║ ║║ ╔═════╝║ ╔═════╝║ ╔═══╗ ║║ ╔═════╝ // ║ ║ ║ ║║ ╚═════╗║ ╚═════╗║ ╚═══╝ ║║ ╚═════╗ // ║ ║ ║ ║╚═════╗ ║║ ╔═════╝║ ╔═╗ ╔═╝╚═════╗ ║ // ║ ╚═══╝ ║╔═════╝ ║║ ╚═════╗║ ║ ║ ╚═╗╔═════╝ ║ // ╚═══════╝╚═══════╝╚═══════╝╚═╝ ╚═══╝╚═══════╝ get users() { const that = this; return { async fetchAllUsers() { return await userModule.fetchAllUsers( { token: await that.generateToken(), privateKey: that.getPrivateKey(), hostname: that.getHostname(), }); }, async fetchUserById(userId: string) { return await userModule.fetchUsersById(userId, { token: await that.generateToken(), privateKey: that.getPrivateKey(), hostname: that.getHostname(), }); }, async createUser( userInformation: Parameters[0], ) { return await userModule.createUser(userInformation, { token: await that.generateToken(), privateKey: that.getPrivateKey(), hostname: that.getHostname(), }); }, }; } // ╔═══════╗╔═══════╗╔═╗ ╔═╗ ╔═══════╗╔════╗╔═╗ // ╚══╗ ╔══╝║ ╔═══╗ ║║ ║ ║ ║ ║ ╔═════╝║ ╔╗ ║║ ║ // ║ ║ ║ ║ ║ ║║ ╚═╝ ╚═╗║ ╚═════╗║ ║║ ║║ ║ // ║ ║ ║ ║ ║ ║║ ╔═══╗ ║║ ╔═════╝║ ║║ ║║ ║ // ║ ║ ║ ╚═══╝ ║║ ║ ║ ║║ ╚═════╗║ ║║ ╚╝ ║ // ╚═╝ ╚═══════╝╚═╝ ╚═╝╚═══════╝╚═╝╚════╝ async generateToken() { if (!tokenModule.isValidToken(this._token)) { this._token = await tokenModule.generateToken({ privateKey: this.getPrivateKey(), hostname: this.getHostname(), username: this.getUsername(), password: this.getPassword(), }); } return this._token; } // ╔═══════╗╔═══════╗╔═══════╗╔═══════╗╔═╗ ╔═╗╔═══════╗╔═══════╗╔═══════╗ // ║ ╔═══╗ ║║ ╔═════╝║ ╔═════╝║ ╔═══╗ ║║ ║ ║ ║║ ╔═══╗ ║║ ╔═════╝║ ╔═════╝ // ║ ╚═══╝ ║║ ╚═════╗║ ╚═════╗║ ║ ║ ║║ ║ ║ ║║ ╚═══╝ ║║ ║ ║ ╚═════╗ // ║ ╔═╗ ╔═╝║ ╔═════╝╚═════╗ ║║ ║ ║ ║║ ║ ║ ║║ ╔═╗ ╔═╝║ ║ ║ ╔═════╝ // ║ ║ ║ ╚═╗║ ╚═════╗╔═════╝ ║║ ╚═══╝ ║║ ╚═══╝ ║║ ║ ║ ╚═╗║ ╚═════╗║ ╚═════╗ // ╚═╝ ╚═══╝╚═══════╝╚═══════╝╚═══════╝╚═══════╝╚═╝ ╚═══╝╚═══════╝╚═══════╝ getResource() { return this._resource; } // ╔═══════╗╔═══════╗╔═══════╗╔═══════╗╔═══════╗╔═══════╗╔═══════╗╔═══════╗╔═══════╗╔═══════╗ // ║ ╔═══╗ ║║ ╔═══╗ ║║ ╔═══╗ ║║ ╔═══╗ ║║ ╔═════╝║ ╔═══╗ ║╚══╗ ╔══╝╚══╗ ╔══╝║ ╔═════╝║ ╔═════╝ // ║ ╚═══╝ ║║ ╚═══╝ ║║ ║ ║ ║║ ╚═══╝ ║║ ╚═════╗║ ╚═══╝ ║ ║ ║ ║ ║ ║ ╚═════╗║ ╚═════╗ // ║ ╔═════╝║ ╔═╗ ╔═╝║ ║ ║ ║║ ╔═════╝║ ╔═════╝║ ╔═╗ ╔═╝ ║ ║ ║ ║ ║ ╔═════╝╚═════╗ ║ // ║ ║ ║ ║ ║ ╚═╗║ ╚═══╝ ║║ ║ ║ ╚═════╗║ ║ ║ ╚═╗ ║ ║ ╔══╝ ╚══╗║ ╚═════╗╔═════╝ ║ // ╚═╝ ╚═╝ ╚═══╝╚═══════╝╚═╝ ╚═══════╝╚═╝ ╚═══╝ ╚═╝ ╚═══════╝╚═══════╝╚═══════╝ getPrivateKey() { return this.getResource().privateKey; } getUsername() { return this.getResource().username; } getPassword() { return this.getResource().password; } getHostname() { return this.getResource().hostname; } getLabel() { return this.getResource().label; } }