import { Msg, NatsConnection, PublishOptions, RequestOptions, Subscription, SubscriptionOptions } from "nats"; import Base from "../../Base"; import { NatsCallback, NatsMessageType, NatsObject, NatsObjectType, RawMsg } from "../../interfaces/nats"; /** * Connect and authenticate using email and jwt * * to use websocket from within node, forceWebsocket can be used. * A websocket library is required to be pre-installed and needs to be set as global: * import WebSocket from "ws"; * Object.assign(global, { WebSocket: WebSocket }); */ type ConnectParamsJwt = { email: string; jwt: string; servers?: string[]; debug?: boolean; forceWebsocket?: boolean; }; /** * Connect and authenticate using username and password * Can only be used with the CUSTOM_AUTH admin! * * to use websocket from within node, forceWebsocket can be used. * A websocket library is required to be pre-installed and needs to be set as global: * import WebSocket from "ws"; * Object.assign(global, { WebSocket: WebSocket }); */ type ConnectParamsPassword = { username: string; password: string; servers?: string[]; debug?: boolean; forceWebsocket?: boolean; }; declare class Nats extends Base { private natsConnection; private subMap; private connection; connect(params: ConnectParamsJwt | ConnectParamsPassword): Promise; getConnection: () => NatsConnection | undefined; sub(subject: string, callback: NatsCallback, options?: SubscriptionOptions): Promise; unsub(subject: string | Subscription): void; publish(subject: string, type: NatsMessageType, objectType: NatsObjectType, object: NatsObject, options?: PublishOptions): void; request(subject: string, type: NatsMessageType, objectType: NatsObjectType, object: unknown, options?: RequestOptions): void; requestAndWaitForResponse(subject: string, type: NatsMessageType, objectType: NatsObjectType, object: unknown, options?: RequestOptions): Promise; respond(msg: Msg | RawMsg, data: unknown): void; protected getEndpoint(endpoint: string): string; /** * Gracefully closes the NATS connection. * * This method uses 'drain' to ensure that all outgoing messages are sent * and all buffered incoming messages are processed by their respective subscribers before the connection is physically closed * * @returns {Promise} A promise that resolves when the connection is fully drained and closed */ disconnect(): Promise; } export default Nats;