@pixellot/pxlt-rabbit-handler
Version:
A generic class that handles RabbitMQ connection, consume and produce functionality.
113 lines (92 loc) • 3.68 kB
JavaScript
/* eslint-disable camelcase */
const axios = require('axios');
const config = require('config');
class RabbitMQAPI {
constructor() {
const url = new URL('http://localhost');
url.host = config.get('rmq.host');
url.port = config.get('rmq.httpPort');
url.username = config.get('rmq.username');
url.password = config.get('rmq.password');
this._username = config.get('rmq.username');
this._baseUrl = url.toString();
this._vhost = config.get('rmq.vhost');
}
async _allDataExtractor(callable) {
const request = async (page, pageSize) => {
const { data } = await callable(page, pageSize);
const totalSize = data.total_count;
if (page * pageSize < totalSize) {
return [...data.items, ...(await request(page + 1, pageSize))];
}
return data.items;
};
return request(1, 100);
}
async getAllUserExchangesInVhost(vhost) {
try {
const exchanges = await this._allDataExtractor((page, pageSize) => axios({
method: 'GET',
url: `${this._baseUrl}api/exchanges/${this._vhost}?page=${page}&page_size=${pageSize}`
}));
const userExchanges = exchanges.filter(({ user_who_performed_action }) => user_who_performed_action === this._username);
return userExchanges;
} catch (error) {
throw new Error(`RMQ_HTTP_API_ERROR:: Failed to get all exachnges in vhost ${vhost}: ${error.message}`);
}
}
async deleteExchangeInVhost(vhost, exchange) {
try {
await axios({
method: 'DELETE',
url: `${this._baseUrl}api/exchanges/${vhost}/${exchange}`
});
} catch (error) {
throw new Error(`RMQ_HTTP_API_ERROR:: Failed to delete exchange ${exchange} in vhost ${vhost}: ${error.message}`);
}
}
async deleteAllUserExchangesInVhost(vhost) {
try {
const exchanges = await this.getAllUserExchangesInVhost(vhost);
for (const exchange of exchanges) {
await this.deleteExchangeInVhost(vhost, exchange.name);
}
} catch (error) {
throw new Error(`RMQ_HTTP_API_ERROR:: Failed to delete all exchanges in vhost ${vhost}: ${error.message}`);
}
}
async getAllQueuesInVhost(vhost) {
try {
const queues = await this._allDataExtractor((page, pageSize) => axios({
method: 'GET',
url: `${this._baseUrl}api/queues/${vhost}?page=${page}&page_size=${pageSize}`
}));
return queues;
} catch (error) {
throw new Error(`RMQ_HTTP_API_ERROR:: Failed to get all queues in vhost ${vhost}: ${error.message}`);
}
}
async deleteQueueInVhost(vhost, queue) {
try {
await axios({
method: 'DELETE',
url: `${this._baseUrl}api/queues/${vhost}/${queue}`
});
} catch (error) {
throw new Error(`RMQ_HTTP_API_ERROR:: Failed to delete queue ${queue} in vhost ${vhost}: ${error.message}`);
}
}
async deleteAllQueuesInVhost(vhost) {
try {
const queues = await this.getAllQueuesInVhost(vhost);
for (const queue of queues) {
await this.deleteQueueInVhost(vhost, queue.name);
}
} catch (error) {
throw new Error(`RMQ_HTTP_API_ERROR:: Failed to delete all queues in vhost ${vhost}: ${error.message}`);
}
}
}
module.exports = {
RabbitMQAPI
};