fastify-rabbitmq
Version:
A Fastify RabbitMQ Plugin Developed in Pure TypeScript.
58 lines (57 loc) • 2.34 kB
JavaScript
import fp from "fastify-plugin";
import { Connection as RabbitMQConnection, } from "rabbitmq-client";
import { errors } from "./errors.js";
import { validateOpts } from "./validation.js";
export * from "./types.js";
/* eslint-disable @typescript-eslint/no-explicit-any */
const decorateFastifyInstance = (fastify, options, connection) => {
const { namespace = "" } = options;
if (typeof namespace !== "undefined" && namespace !== "") {
fastify.log.debug("[fastify-rabbitmq] Namespace Attempt: %s", namespace);
}
if (typeof namespace !== "undefined" && namespace !== "") {
if (typeof fastify.rabbitmq === "undefined") {
fastify.decorate("rabbitmq", Object.create(null));
}
if (typeof fastify.rabbitmq[namespace] !== "undefined") {
throw new errors.FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS(`Already registered with namespace: ${namespace}`);
}
fastify.log.trace("[fastify-rabbitmq] Decorate Fastify with Namespace: %", namespace);
fastify.rabbitmq[namespace] = connection;
}
else {
if (typeof fastify.rabbitmq !== "undefined") {
throw new errors.FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS("Already registered.");
}
}
if (typeof fastify.rabbitmq === "undefined") {
fastify.log.trace("[fastify-rabbitmq] Decorate Fastify");
fastify.decorate("rabbitmq", connection);
}
};
/**
* Main Function
* @since 1.0.0
* @example
* This is the basics on how to use this plugin:
* ```js
* app.register(fastifyRabbit, {
* connection: 'amqp://guest:guest@localhost'
* })
* ```
* This will allow you to read from your Fastify "object" and
* use this plugin at the "rabbitmq" level. From there you can execute and maintain
* the RabbitMQ Connection using the 'rabbitmq-client' package, which is wrapping around
* this plugin to execute functions it provides.
*
* @see [https://cody-greene.github.io/node-rabbitmq-client/latest/index.html](https://cody-greene.github.io/node-rabbitmq-client/latest/index.html)
*
*/
const fastifyRabbit = fp(async (fastify, opts) => {
await validateOpts(opts);
const { connection } = opts;
const c = new RabbitMQConnection(connection);
decorateFastifyInstance(fastify, opts, c);
});
export default fastifyRabbit;
export { decorateFastifyInstance };