@simplito/privmx-webendpoint
Version:
PrivMX Web Endpoint library
119 lines (118 loc) • 3.63 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PublicConnection = void 0;
const _1 = require(".");
const service_1 = require("../service");
/**
* @class PublicConnection
* @classdesc A client for interacting with the PrivMX Endpoint API as a guest. The scope is limited to sending an entries to inboxes.
* @example
* // Initialize the PrivMX client
* await PrivmxClient.setup('/path/to/privmx/assets');
*
* // Connect to the PrivMX bridge
* const solutionId = 'your-solution-id';
* const bridgeUrl = 'https://your-bridge-url.com';
* const inboxId = 'your-inbox-id'
* const client = await PrivmxClient.connectPublic(solutionId, bridgeUrl);
*
* // Send entry
* const encoder = new TextEncoder();
* const encodedData = encoder.encode(JSON.stringify({ message: 'Hello, PrivMX!' }));
*
* await client.sendEntry(inboxId, {
* data: encodedData
* })
*
* // Disconnect when done
* await client.disconnect();
*/
class PublicConnection {
connection;
threadApi = null;
storeApi = null;
inboxApi = null;
/**
* @constructor
* @param {Connection} connection - The connection object.
*/
constructor(connection) {
this.connection = 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>}
*/
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>}
*/
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>}
*/
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 Disconnects from the PrivMX bridge.
* @returns {Promise<void>}
*/
async disconnect() {
try {
await this.connection.disconnect();
this.threadApi = null;
this.storeApi = null;
this.inboxApi = null;
}
catch (e) {
console.error("Error during disconnection:", e);
}
}
/**
* @description Sends an entry to the specified inbox.
* @param {string} inboxId - The ID of the inbox to send the entry to.
* @param {InboxEntryPayload} payload - The payload of the entry to send.
* @returns {Promise<void>}
*/
async sendEntry(inboxId, payload) {
const inboxApi = await this.getInboxApi();
return await _1.Inboxes.sendEntry(inboxApi, inboxId, payload);
}
}
exports.PublicConnection = PublicConnection;