@simplito/privmx-webendpoint
Version:
PrivMX Web Endpoint library
344 lines (343 loc) • 11.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrivmxClient = void 0;
const service_1 = require("../service");
const PublicConnection_1 = require("./PublicConnection");
const events_1 = require("./events");
/**
* @class PrivmxClient
* @classdesc A client for interacting with the PrivMX Endpoint API.
* @example
* // Initialize the PrivMX client
* await PrivmxClient.setup('/path/to/privmx/assets');
*
* // Connect to the PrivMX bridge
* const privateKey = 'your-private-key';
* const solutionId = 'your-solution-id';
* const contextId = 'your-context-id';
* const bridgeUrl = 'https://your-bridge-url.com';
* const client = await PrivmxClient.connect(privateKey, solutionId, bridgeUrl);
*
* // Get the Thread API and list threads
* const threadApi = await client.getThreadApi();
* const threads = await threadApi.listThreads(contextId, {
* skip: 0,
* limit: 100,
* sort: 'desc'
* })
*
* // Disconnect when done
* await client.disconnect();
*/
class PrivmxClient {
connection;
static cryptoApi = null;
static eventQueue = null;
static isSetup = false;
static eventManager = null;
threadApi = null;
storeApi = null;
inboxApi = null;
kvdbApi = null;
eventApi = null;
connectionEventManager = null;
userEventManager = null;
threadEventManager = null;
storeEventManager = null;
inboxEventManager = null;
customEventsManager = null;
kvdbEventsManager = null;
/**
* @constructor
* @param {Connection} connection - The connection object.
*/
constructor(connection) {
this.connection = connection;
}
/**
* @description Sets up the PrivMX endpoint if it hasn't been set up yet.
* @param {string} folderPath - The path to the folder where PrivMX assets are stored.
* @returns {Promise<void>}
*/
static async setup(folderPath) {
if (!PrivmxClient.isSetup) {
await service_1.EndpointFactory.setup(folderPath);
PrivmxClient.isSetup = true;
}
}
static checkSetup() {
if (!this.isSetup) {
throw new Error("Endpoint not initialized, use PrivMXClient.setup(folderPath).");
}
}
/**
* @description Gets the Crypto API.
* @returns {Promise<CryptoApi>}
*/
static async getCryptoApi() {
if (this.cryptoApi) {
return this.cryptoApi;
}
this.checkSetup();
this.cryptoApi = (async () => {
return service_1.EndpointFactory.createCryptoApi();
})();
return this.cryptoApi;
}
/**
* @description Gets the Event Queue.
* @returns {Promise<EventQueue>}
*/
static async getEventQueue() {
if (this.eventQueue) {
return this.eventQueue;
}
this.checkSetup();
this.eventQueue = (async () => {
return service_1.EndpointFactory.getEventQueue();
})();
return this.eventQueue;
}
/**
* @description Gets the Event Manager.
* @returns {Promise<EventManager>}
*/
static async getEventManager() {
if (this.eventManager) {
return this.eventManager;
}
this.checkSetup();
this.eventManager = (async () => {
const eventQueue = await PrivmxClient.getEventQueue();
return events_1.EventManager.startEventLoop(eventQueue);
})();
return await this.eventManager;
}
/**
* @description Connects to the PrivMX bridge.
* @param {string} privateKey user's private key
* @param {string} solutionId ID of the Solution
* @param {string} bridgeUrl the Bridge Server URL
* @returns {Promise<PrivmxClient>}
* @throws {Error} If the connection to the bridge fails.
*/
static async connect(privateKey, solutionId, bridgeUrl) {
this.checkSetup();
const connection = await service_1.EndpointFactory.connect(privateKey, solutionId, bridgeUrl);
if (!connection) {
throw new Error("ERROR: Could not connect to bridge");
}
return new PrivmxClient(connection);
}
/**
* Connects to the Platform backend as a guest user.
*
* @param {string} solutionId ID of the Solution
* @param {string} bridgeUrl the Bridge Server URL
*
* @returns {Promise<PublicConnection>} Promised instance of Connection
*/
static async connectPublic(solutionId, bridgeUrl) {
this.checkSetup();
const connection = await service_1.EndpointFactory.connectPublic(solutionId, bridgeUrl);
if (!connection) {
throw new Error("ERROR: Could not connect to bridge");
}
return new PublicConnection_1.PublicConnection(connection);
}
/**
* @description Gets the connection object.
* @returns {Connection}
* @throws {Error} If there is no active connection.
*/
getConnection() {
if (!this.connection) {
throw new Error("No active connection");
}
return this.connection;
}
/**
* @description Gets the Thread API.
* @returns {Promise<ThreadApi>}
*/
async getThreadApi() {
if (!this.threadApi) {
this.threadApi = (() => {
const connection = this.getConnection();
return service_1.EndpointFactory.createThreadApi(connection);
})();
}
return this.threadApi;
}
/**
* @description Gets the Store API.
* @returns {Promise<StoreApi>}
*/
async getStoreApi() {
if (!this.storeApi) {
this.storeApi = (async () => {
const connection = this.getConnection();
return service_1.EndpointFactory.createStoreApi(connection);
})();
}
return this.storeApi;
}
/**
* @description Gets the Inbox API.
* @returns {Promise<InboxApi>}
*/
async getInboxApi() {
if (!this.inboxApi) {
this.inboxApi = (async () => {
const connection = this.getConnection();
return service_1.EndpointFactory.createInboxApi(connection, await this.getThreadApi(), await this.getStoreApi());
})();
}
return this.inboxApi;
}
/**
* @description Gets the Kvdb API.
* @returns {Promise<KvdbApi>}
*/
async getKvdbApi() {
if (!this.kvdbApi) {
this.kvdbApi = (async () => {
const connection = this.getConnection();
return service_1.EndpointFactory.createKvdbApi(connection);
})();
}
return this.kvdbApi;
}
/**
* @description Gets the Event API.
* @returns {Promise<EventApi>}
*/
async getEventApi() {
if (!this.eventApi) {
this.eventApi = (async () => {
const connection = this.getConnection();
return service_1.EndpointFactory.createEventApi(connection);
})();
}
return this.eventApi;
}
/**
* @description Gets the Connection Event Manager.
* @returns {Promise<ConnectionEventsManager>}
*/
async getConnectionEventManager() {
if (this.connectionEventManager) {
return this.connectionEventManager;
}
this.connectionEventManager = (async () => {
const eventManager = await PrivmxClient.getEventManager();
const connection = this.getConnection();
const connectionId = await connection.getConnectionId();
return eventManager.getConnectionEventManager(`${connectionId}`);
})();
return this.connectionEventManager;
}
/**
* @description Gets the User Event Manager.
* @returns {Promise<UserEventsManager>}
*/
async getUserEventsManager() {
if (this.userEventManager) {
return this.userEventManager;
}
this.userEventManager = (async () => {
const eventManager = await PrivmxClient.getEventManager();
return eventManager.getUserEventsManager(this.getConnection());
})();
return this.userEventManager;
}
/**
* @description Gets the Thread Event Manager.
* @returns {Promise<ThreadEventsManager>}
*/
async getThreadEventManager() {
if (this.threadEventManager) {
return this.threadEventManager;
}
this.threadEventManager = (async () => {
const eventManager = await PrivmxClient.getEventManager();
return eventManager.getThreadEventManager(await this.getThreadApi());
})();
return this.threadEventManager;
}
/**
* @description Gets the Store Event Manager.
* @returns {Promise<StoreEventsManager>}
*/
async getStoreEventManager() {
if (this.storeEventManager) {
return this.storeEventManager;
}
this.storeEventManager = (async () => {
const eventManager = await PrivmxClient.getEventManager();
return eventManager.getStoreEventManager(await this.getStoreApi());
})();
return this.storeEventManager;
}
/**
* @description Gets the Inbox Event Manager.
* @returns {Promise<InboxEventsManager>}
*/
async getInboxEventManager() {
if (this.inboxEventManager) {
return this.inboxEventManager;
}
this.inboxEventManager = (async () => {
const eventManager = await PrivmxClient.getEventManager();
return eventManager.getInboxEventManager(await this.getInboxApi());
})();
return this.inboxEventManager;
}
/**
* @description Gets the Custom Events Manager.
* @returns {Promise<CustomEventsManager>}
*/
async getCustomEventsManager() {
if (this.customEventsManager) {
return this.customEventsManager;
}
this.customEventsManager = (async () => {
const eventManager = await PrivmxClient.getEventManager();
return eventManager.getCustomEventsManager(await this.getEventApi());
})();
return this.customEventsManager;
}
async getKvdbEventsManager() {
if (this.kvdbEventsManager) {
return this.kvdbEventsManager;
}
this.kvdbEventsManager = (async () => {
const eventManager = await PrivmxClient.getEventManager();
return eventManager.getKvdbEventManager(await this.getKvdbApi());
})();
return this.kvdbEventsManager;
}
/**
* @description Disconnects from the PrivMX bridge.
* @returns {Promise<void>}
*/
async disconnect() {
try {
await this.connection.disconnect();
this.threadApi = null;
this.storeApi = null;
this.inboxApi = null;
this.connectionEventManager = null;
this.customEventsManager = null;
this.userEventManager = null;
this.threadEventManager = null;
this.storeEventManager = null;
this.inboxEventManager = null;
this.kvdbEventsManager = null;
}
catch (e) {
console.error("Error during disconnection:", e);
}
}
}
exports.PrivmxClient = PrivmxClient;