import type { Message, MessageTypesContent } from '../../types/index.ts' import { ConnectionSender, type ConnectionSenderConstructor, type Sender } from '../sender.ts' export class CustomGatewayHttpSender extends ConnectionSender implements Sender { private readonly baseurl: string private readonly token: string constructor(options: ConstructorParameters[0]) { super(options) if (options.authentication.scheme === 'plain') { this.token = CustomGatewayHttpSender.createToken(options.node, btoa(options.authentication.password)) } else if (options.authentication.scheme === 'token') { this.token = options.authentication.token } else { throw new Error('CustomGatewayHttpSender only supports plain or token authentication') } const prefix = options.tenantId ? `${options.tenantId}.` : '' this.baseurl = `https://${prefix}custom.gw.${this.domain}` } public async sendMessage(message: Message): Promise { const response = await fetch(`${this.baseurl}/messages`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Basic ${this.token}`, }, body: JSON.stringify(message), }) if (!response.ok) { throw new Error(`Failed to send message: ${response.statusText}`) } } sendCommand(): Promise { throw new Error('Custom gateway clients cannot send commands') } public static login = ConnectionSender.login }