UNPKG

fastify-rabbitmq

Version:

A Fastify RabbitMQ Plugin Developed in Pure TypeScript.

78 lines (77 loc) 3.43 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.decorateFastifyInstance = void 0; const fastify_plugin_1 = __importDefault(require("fastify-plugin")); const rabbitmq_client_1 = require("rabbitmq-client"); const errors_js_1 = require("./errors.js"); const validation_js_1 = require("./validation.js"); __exportStar(require("./types.js"), exports); /* 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_js_1.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_js_1.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); } }; exports.decorateFastifyInstance = decorateFastifyInstance; /** * 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 = (0, fastify_plugin_1.default)(async (fastify, opts) => { await (0, validation_js_1.validateOpts)(opts); const { connection } = opts; const c = new rabbitmq_client_1.Connection(connection); decorateFastifyInstance(fastify, opts, c); }); exports.default = fastifyRabbit;