import ampqlib, { Connection } from 'amqplib' class AmpClient { private _client?: Connection get client () { if (!this._client) throw new Error('Cannot access AMP client before connecting') return this._client } async connect (url: string) { this._client = await ampqlib.connect(url) return new Promise((resolve, reject) => { this.client.on('connect', () => { if (!process.env.NODE_ENV?.includes('test')) console.log('Connected to event bus') resolve() }) this.client.on('error', (err: any) => { console.error(err) reject(err) }) }) } } export const ampClient = new AmpClient() export default ampClient