/
rabbit-mq-connection-factory.ts
45 lines (39 loc) · 1.26 KB
/
rabbit-mq-connection-factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import * as amqp from 'amqplib';
import { RabbitMqConnectionError } from '../errors/rabbitmq-connection.error';
import { RabbitMqConnection } from '../models/rabbitmq-connection';
/**
* Factory to build RabbitMqConnection instances
*/
export class RabbitMqConnectionFactory {
private uri: string;
private options: amqp.Options.Connect;
constructor() {}
/**
* Sets uri to connect to
* @param uri connection string
*/
public setUri(uri: string) {
this.uri = uri;
}
/**
* Sets options of amqplib to build a connection
* @param options connection options
*/
public setOptions(options: amqp.Options.Connect) {
this.options = options;
}
/**
* Builds an instance of RabbitMqConnection, prefers options object over connection string
* @throws RabbitMqConnectionError
*/
public async newConnection() {
try {
if (!this.options && !this.uri)
throw new Error('Either options or connection uri must be provided');
const amqpConnection = await amqp.connect(this.options || this.uri);
return new RabbitMqConnection(amqpConnection, this.options || this.uri);
} catch (error) {
throw new RabbitMqConnectionError(`Error while building connection to RabbitMQ: ${error.message}`, error.code);
}
}
}