fastify-rabbitmq
Version:
A Fastify RabbitMQ Plugin Developed in Pure TypeScript.
83 lines (79 loc) • 2.54 kB
JavaScript
// src/index.ts
import fp from "fastify-plugin";
import {
Connection as RabbitMQConnection
} from "rabbitmq-client";
// src/errors.ts
import createError from "@fastify/error";
var errors = {
/** Error if there is an invalid option used during registration. */
FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS: createError(
"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: createError(
"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: createError(
"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 = fp(async (fastify, opts) => {
await validateOpts(opts);
const { connection } = opts;
const c = new RabbitMQConnection(connection);
decorateFastifyInstance(fastify, opts, c);
});
var index_default = fastifyRabbit;
export {
decorateFastifyInstance,
index_default as default
};