fastify-rabbitmq 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cjs/errors.js CHANGED
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.errors = void 0;
7
- var error_1 = __importDefault(require("@fastify/error"));
7
+ const error_1 = __importDefault(require("@fastify/error"));
8
8
  exports.errors = {
9
9
  /** Error if there is an invalid option used during registration. */
10
10
  FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS: (0, error_1.default)('FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS', 'Invalid options: %s'),
package/lib/cjs/index.js CHANGED
@@ -13,54 +13,18 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
13
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
- return new (P || (P = Promise))(function (resolve, reject) {
19
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
- step((generator = generator.apply(thisArg, _arguments || [])).next());
23
- });
24
- };
25
- var __generator = (this && this.__generator) || function (thisArg, body) {
26
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
27
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
28
- function verb(n) { return function (v) { return step([n, v]); }; }
29
- function step(op) {
30
- if (f) throw new TypeError("Generator is already executing.");
31
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
32
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
33
- if (y = 0, t) op = [op[0] & 2, t.value];
34
- switch (op[0]) {
35
- case 0: case 1: t = op; break;
36
- case 4: _.label++; return { value: op[1], done: false };
37
- case 5: _.label++; y = op[1]; op = [0]; continue;
38
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
39
- default:
40
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
41
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
42
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
43
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
44
- if (t[2]) _.ops.pop();
45
- _.trys.pop(); continue;
46
- }
47
- op = body.call(thisArg, _);
48
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
49
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
50
- }
51
- };
52
16
  var __importDefault = (this && this.__importDefault) || function (mod) {
53
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
54
18
  };
55
19
  Object.defineProperty(exports, "__esModule", { value: true });
56
20
  exports.decorateFastifyInstance = void 0;
57
- var fastify_plugin_1 = __importDefault(require("fastify-plugin"));
58
- var rabbitmq_client_1 = require("rabbitmq-client");
59
- var errors_js_1 = require("./errors.js");
60
- var validation_js_1 = require("./validation.js");
21
+ const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
22
+ const rabbitmq_client_1 = require("rabbitmq-client");
23
+ const errors_js_1 = require("./errors.js");
24
+ const validation_js_1 = require("./validation.js");
61
25
  __exportStar(require("./types.js"), exports);
62
- var decorateFastifyInstance = function (fastify, options, connection) {
63
- var _a = options.namespace, namespace = _a === void 0 ? '' : _a;
26
+ const decorateFastifyInstance = (fastify, options, connection) => {
27
+ const { namespace = '' } = options;
64
28
  if (typeof namespace !== 'undefined' && namespace !== '') {
65
29
  fastify.log.debug('[fastify-rabbitmq] Namespace Attempt: %s', namespace);
66
30
  }
@@ -69,7 +33,7 @@ var decorateFastifyInstance = function (fastify, options, connection) {
69
33
  fastify.decorate('rabbitmq', Object.create(null));
70
34
  }
71
35
  if (typeof fastify.rabbitmq[namespace] !== 'undefined') {
72
- throw new errors_js_1.errors.FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS("Already registered with namespace: ".concat(namespace));
36
+ throw new errors_js_1.errors.FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS(`Already registered with namespace: ${namespace}`);
73
37
  }
74
38
  fastify.log.trace('[fastify-rabbitmq] Decorate Fastify with Namespace: %', namespace);
75
39
  fastify.rabbitmq[namespace] = connection;
@@ -103,18 +67,10 @@ exports.decorateFastifyInstance = decorateFastifyInstance;
103
67
  * @see [https://cody-greene.github.io/node-rabbitmq-client/latest/index.html](https://cody-greene.github.io/node-rabbitmq-client/latest/index.html)
104
68
  *
105
69
  */
106
- var fastifyRabbit = (0, fastify_plugin_1.default)(function (fastify, opts) { return __awaiter(void 0, void 0, void 0, function () {
107
- var connection, c;
108
- return __generator(this, function (_a) {
109
- switch (_a.label) {
110
- case 0: return [4 /*yield*/, (0, validation_js_1.validateOpts)(opts)];
111
- case 1:
112
- _a.sent();
113
- connection = opts.connection;
114
- c = new rabbitmq_client_1.Connection(connection);
115
- decorateFastifyInstance(fastify, opts, c);
116
- return [2 /*return*/];
117
- }
118
- });
119
- }); });
70
+ const fastifyRabbit = (0, fastify_plugin_1.default)(async (fastify, opts) => {
71
+ await (0, validation_js_1.validateOpts)(opts);
72
+ const { connection } = opts;
73
+ const c = new rabbitmq_client_1.Connection(connection);
74
+ decorateFastifyInstance(fastify, opts, c);
75
+ });
120
76
  exports.default = fastifyRabbit;
@@ -1,56 +1,17 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  Object.defineProperty(exports, "__esModule", { value: true });
39
3
  exports.validateOpts = void 0;
40
- var errors_js_1 = require("./errors.js");
41
- var validateOpts = function (options) { return __awaiter(void 0, void 0, void 0, function () {
42
- return __generator(this, function (_a) {
43
- // Mandatory
44
- if (typeof options.connection === 'undefined') {
45
- throw new errors_js_1.errors.FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS('connection or findServers must be defined.');
46
- }
47
- // Mandatory
48
- if (typeof options.connection !== 'undefined') {
49
- if (typeof options.connection !== 'object') {
50
- // we need to do some sort of check here to make sure RabbitMQOptions is "valid"
51
- }
4
+ const errors_js_1 = require("./errors.js");
5
+ const validateOpts = async (options) => {
6
+ // Mandatory
7
+ if (typeof options.connection === 'undefined') {
8
+ throw new errors_js_1.errors.FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS('connection or findServers must be defined.');
9
+ }
10
+ // Mandatory
11
+ if (typeof options.connection !== 'undefined') {
12
+ if (typeof options.connection !== 'object') {
13
+ // we need to do some sort of check here to make sure RabbitMQOptions is "valid"
52
14
  }
53
- return [2 /*return*/];
54
- });
55
- }); };
15
+ }
16
+ };
56
17
  exports.validateOpts = validateOpts;
@@ -0,0 +1,13 @@
1
+ import { ConnectionOptions } from 'rabbitmq-client';
2
+ export interface FastifyRabbitMQOptions {
3
+ /**
4
+ * @since 1.0.0
5
+ * @description Connection String or object pointing to the RabbitMQ Broker Services
6
+ */
7
+ connection: string | ConnectionOptions;
8
+ /**
9
+ * @since 1.0.0
10
+ * @description To set the custom nNamespace within this plugin instance. Used to register this plugin more than one time.
11
+ */
12
+ namespace?: string;
13
+ }
@@ -0,0 +1,14 @@
1
+ export declare const errors: {
2
+ /** Error if there is an invalid option used during registration. */
3
+ FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS: import("@fastify/error").FastifyErrorConstructor<{
4
+ code: "FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS";
5
+ }, [any?, any?, any?]>;
6
+ /** Error if there is an setup error of the plugin itself. */
7
+ FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS: import("@fastify/error").FastifyErrorConstructor<{
8
+ code: "FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS";
9
+ }, [any?, any?, any?]>;
10
+ /** If an invalid usage error was done, this error would pop up. */
11
+ FASTIFY_RABBIT_MQ_ERR_USAGE: import("@fastify/error").FastifyErrorConstructor<{
12
+ code: "FASTIFY_RABBIT_MQ_ERR_USAGE";
13
+ }, [any?, any?, any?]>;
14
+ };
@@ -0,0 +1,26 @@
1
+ import { FastifyInstance } from 'fastify';
2
+ import { ConnectionOptions } from 'rabbitmq-client';
3
+ import { FastifyRabbitMQOptions } from './decorate.js';
4
+ export * from './types.js';
5
+ declare const decorateFastifyInstance: (fastify: FastifyInstance, options: FastifyRabbitMQOptions, connection: any) => void;
6
+ /**
7
+ * Main Function
8
+ * @since 1.0.0
9
+ * @example
10
+ * This is the basics on how to use this plugin:
11
+ * ```js
12
+ * app.register(fastifyRabbit, {
13
+ * connection: 'amqp://guest:guest@localhost'
14
+ * })
15
+ * ```
16
+ * This will allow you to read from your Fastify "object" and
17
+ * use this plugin at the "rabbitmq" level. From there you can execute and maintain
18
+ * the RabbitMQ Connection using the 'rabbitmq-client' package, which is wrapping around
19
+ * this plugin to execute functions it provides.
20
+ *
21
+ * @see [https://cody-greene.github.io/node-rabbitmq-client/latest/index.html](https://cody-greene.github.io/node-rabbitmq-client/latest/index.html)
22
+ *
23
+ */
24
+ declare const fastifyRabbit: import("fastify").FastifyPluginCallback<FastifyRabbitMQOptions, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
25
+ export default fastifyRabbit;
26
+ export { decorateFastifyInstance, FastifyRabbitMQOptions, ConnectionOptions };
@@ -0,0 +1,12 @@
1
+ import { Connection as RabbitMQConnection } from 'rabbitmq-client';
2
+ declare module 'fastify' {
3
+ interface FastifyInstance {
4
+ /** Main Decorator for Fastify **/
5
+ rabbitmq: RabbitMQConnection & fastifyRabbitMQ.FastifyRabbitMQNO;
6
+ }
7
+ }
8
+ export declare namespace fastifyRabbitMQ {
9
+ interface FastifyRabbitMQNO {
10
+ [namespace: string]: RabbitMQConnection;
11
+ }
12
+ }
@@ -0,0 +1,2 @@
1
+ import { FastifyRabbitMQOptions } from './decorate.js';
2
+ export declare const validateOpts: (options: FastifyRabbitMQOptions) => Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify-rabbitmq",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "A Fastify RabbitMQ Plugin Developed in Pure TypeScript.",
5
5
  "module": "./lib/esm/index.js",
6
6
  "main": "./lib/cjs/index.js",
@@ -20,12 +20,12 @@
20
20
  "node": ">=20.0.0"
21
21
  },
22
22
  "scripts": {
23
- "clean": "rm -rf lib coverage",
24
- "build": "tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && tsc -p tsconfig.types.json && ./bin/build-types.sh",
25
- "build:watch": "tsc -p tsconfig.esm.json -w",
23
+ "clean": "rm -rf lib docs coverage",
24
+ "build": "tsc -p src/tsconfig.esm.json && tsc -p src/tsconfig.cjs.json && tsc -p src/tsconfig.types.json && ./bin/build-types.sh",
25
+ "build:watch": "tsc -p src/tsconfig.esm.json -w",
26
26
  "npmPkgJsonLint": "npmPkgJsonLint .",
27
- "lint": "npmPkgJsonLint . && ts-standard | snazzy",
28
- "lint:fix": "npmPkgJsonLint . && ts-standard --fix | snazzy",
27
+ "lint": "npmPkgJsonLint . && ts-standard -p src/tsconfig.esm.json | snazzy",
28
+ "lint:fix": "npmPkgJsonLint . && ts-standard -p src/tsconfig.esm.json --fix | snazzy",
29
29
  "pack": "npm pack",
30
30
  "prepublishOnly": "npm run clean && npm run build && npm run pack",
31
31
  "test": "vitest run",
@@ -57,31 +57,31 @@
57
57
  },
58
58
  "homepage": "https://github.com/Bugs5382/fastify-rabbitmq#readme",
59
59
  "dependencies": {
60
- "@fastify/error": "^3.4.1",
60
+ "@fastify/error": "^4.0.0",
61
61
  "fastify-plugin": "^4.5.1",
62
- "rabbitmq-client": "^4.5.1"
62
+ "rabbitmq-client": "^4.6.0"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@semantic-release/changelog": "^6.0.3",
66
66
  "@semantic-release/git": "^10.0.1",
67
67
  "@the-rabbit-hole/semantic-release-config": "^1.5.0",
68
- "@types/node": "^20.11.24",
69
- "@types/randomstring": "^1.1.12",
70
- "@typescript-eslint/parser": "^7.1.0",
71
- "@vitest/coverage-v8": "^1.3.1",
72
- "@vitest/ui": "^1.3.1",
73
- "fastify": "^4.26.1",
74
- "npm-check-updates": "^16.14.15",
75
- "npm-package-json-lint": "^7.1.0",
68
+ "@types/node": "^20.14.8",
69
+ "@types/randomstring": "^1.3.0",
70
+ "@typescript-eslint/parser": "^7.14.1",
71
+ "@vitest/coverage-v8": "^1.6.0",
72
+ "@vitest/ui": "^1.6.0",
73
+ "fastify": "^4.28.0",
74
+ "npm-check-updates": "^16.14.20",
75
+ "npm-package-json-lint": "^8.0.0",
76
76
  "pre-commit": "^1.2.2",
77
77
  "semantic-release": "^23.0.2",
78
78
  "snazzy": "^9.0.0",
79
79
  "ts-node": "^10.9.2",
80
80
  "ts-standard": "^12.0.2",
81
- "tsd": "^0.30.7",
82
- "typedoc": "^0.25.9",
83
- "typescript": "^5.3.3",
84
- "vitest": "^1.3.1"
81
+ "tsd": "^0.31.1",
82
+ "typedoc": "^0.26.2",
83
+ "typescript": "^5.5.2",
84
+ "vitest": "^1.6.0"
85
85
  },
86
86
  "precommit": [
87
87
  "test",