UNPKG

fastify-rabbitmq

Version:

A Fastify RabbitMQ Plugin Developed in Pure TypeScript.

116 lines (111 loc) 4.3 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { decorateFastifyInstance: () => decorateFastifyInstance, default: () => index_default }); module.exports = __toCommonJS(index_exports); var import_fastify_plugin = __toESM(require("fastify-plugin")); var import_rabbitmq_client = require("rabbitmq-client"); // src/errors.ts var import_error = __toESM(require("@fastify/error")); var errors = { /** Error if there is an invalid option used during registration. */ FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS: (0, import_error.default)( "FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS", "Invalid options: %s" ), /** Error if there is an setup error of the plugin itself. */ FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS: (0, import_error.default)( "FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS", "Setup error: %s" ), /** If an invalid usage error was done, this error would pop up. */ FASTIFY_RABBIT_MQ_ERR_USAGE: (0, import_error.default)( "FASTIFY_RABBIT_MQ_ERR_USAGE", "Usage error: %s" ) }; // src/validation.ts var validateOpts = async (options) => { if (typeof options.connection === "undefined") { throw new errors.FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS( "connection or findServers must be defined." ); } if (typeof options.connection !== "undefined") { if (typeof options.connection !== "object") { } } }; // src/index.ts var 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", /* @__PURE__ */ 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); } }; var fastifyRabbit = (0, import_fastify_plugin.default)(async (fastify, opts) => { await validateOpts(opts); const { connection } = opts; const c = new import_rabbitmq_client.Connection(connection); decorateFastifyInstance(fastify, opts, c); }); var index_default = fastifyRabbit; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { decorateFastifyInstance });