Skip to content

Commit

Permalink
add plugin rabbitmq producer for fastify
Browse files Browse the repository at this point in the history
  • Loading branch information
zgid123 committed Apr 7, 2023
1 parent 081fc69 commit 2aad9cf
Show file tree
Hide file tree
Showing 9 changed files with 504 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/fastify/producer/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.turbo
src
tsconfig.json
rollup.config.ts
71 changes: 71 additions & 0 deletions packages/fastify/producer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Fastify's Plugin for RabbitMQ Producer.

# Install

```sh
npm install --save @rabbitmq-ts/fastify-producer

# or

yarn add @rabbitmq-ts/fastify-producer

# or

pnpm add @rabbitmq-ts/fastify-producer
```

# Usage

```ts
import { config } from 'dotenv';
import detect from 'detect-port';
import RabbitMQProducer from '@rabbitmq-ts/fastify-producer';

config({
path: '.env',
});

import { fastify } from 'config/fastify';

import { EXCHANGE, QUEUE, ROUTE } from './constants';

async function bootstrap(): Promise<typeof fastify> {
fastify.register(RabbitMQProducer, {
urls: {
host: process.env.RABBITMQ_HOST,
port: process.env.RABBITMQ_PORT,
username: process.env.RABBITMQ_USERNAME,
password: process.env.RABBITMQ_PASSWORD,
virtualHost: process.env.RABBITMQ_VIRTUAL_HOST,
},
configurations: {
exchanges: [
{
exchange: EXCHANGE,
type: 'topic',
options: {
durable: false,
},
},
],
},
});

fastify.get('/', (_request, reply) => {
fastify.rabbitMQProducer.publish(EXCHANGE, ROUTE, {
text: 'hello from producer',
});

reply.send('Ok');
});

const port = await detect(3_000);
await fastify.listen({
port,
});

return fastify;
}

bootstrap();
```
49 changes: 49 additions & 0 deletions packages/fastify/producer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@rabbitmq-ts/fastify-producer",
"version": "1.0.0",
"license": "MIT",
"directories": {
"lib": "lib"
},
"author": "Alpha",
"description": "Fastify package for producer",
"homepage": "https://github.com/zgid123/rabbitmq-ts",
"keywords": [
"rabbitmq",
"rabbitmq-ts",
"rabbitmq-js",
"rabbitmq-typescript",
"rabbitmq-javascript",
"rabbitmq-nodejs",
"rabbitmq-fastify",
"rabbitmq-producer"
],
"repository": {
"type": "git",
"url": "git+https://github.com/zgid123/rabbitmq-ts"
},
"main": "./lib/index.cjs",
"module": "./lib/index.mjs",
"types": "./lib/index.d.ts",
"exports": {
".": {
"import": "./lib/index.mjs",
"require": "./lib/index.cjs",
"types": "./lib/index.d.ts"
}
},
"scripts": {
"prepublish": "pnpm build",
"build": "rollup --config rollup.config.ts --configPlugin typescript && pnpm append-dts",
"append-dts": "cat src/index.d.ts >> lib/index.d.ts"
},
"dependencies": {
"@rabbitmq-ts/core": "workspace:*",
"fastify-plugin": "^4.5.0"
},
"devDependencies": {
"@types/node": "18.15.11",
"fastify": "^4.15.0",
"fastify-tsconfig": "^1.0.1"
}
}
21 changes: 21 additions & 0 deletions packages/fastify/producer/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig } from 'rollup';
import json from '@rollup/plugin-json';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';

export default defineConfig({
input: 'src/index.ts',
plugins: [json(), resolve(), commonjs(), typescript()],
external: ['fastify-plugin', '@rabbitmq-ts/core'],
output: [
{
file: './lib/index.cjs',
format: 'cjs',
},
{
file: './lib/index.mjs',
format: 'es',
},
],
});
9 changes: 9 additions & 0 deletions packages/fastify/producer/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { TPublishFunc } from './interface';

declare module 'fastify' {
export interface FastifyInstance {
rabbitMQProducer: {
publish: TPublishFunc;
};
}
}
61 changes: 61 additions & 0 deletions packages/fastify/producer/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import fp from 'fastify-plugin';
import {
Connection,
type Channel,
type IProducerProps,
} from '@rabbitmq-ts/core';

import type { FastifyInstance, FastifyPluginAsync } from 'fastify';

import type { TPublishFunc } from './interface';

const rabbitMQProducer: FastifyPluginAsync<IProducerProps> = async (
fastify: FastifyInstance,
{ urls, connectionOptions = {}, configurations = {} }: IProducerProps,
) => {
const { exchanges = [] } = configurations;

const connection = new Connection({
urls,
...connectionOptions,
});

const channelWrapper = connection.createChannel({
setup: function (channel: Channel) {
Promise.all(
exchanges.map(({ exchange, type, options }) => {
return Promise.all([
channel.assertExchange(exchange, type as string, options),
]);
}),
);
},
});

await channelWrapper.waitForConnect();

const publish: TPublishFunc = async (
exchange,
routingKey,
content,
options = {},
) => {
return channelWrapper.publish(exchange, routingKey, content, options);
};

fastify.decorate('rabbitMQProducer', {
publish,
});

fastify.addHook('onClose', onClose);

function onClose() {
channelWrapper.close();
connection.close();
}
};

export default fp(rabbitMQProducer, {
fastify: '>=3',
name: '@@rabbitmq-ts/fastify-producer',
});
8 changes: 8 additions & 0 deletions packages/fastify/producer/src/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { TPublish } from '@rabbitmq-ts/core';

export type TPublishFunc = (
exchange: string,
routingKey: string,
content: Buffer | string | unknown,
options?: TPublish,
) => Promise<boolean>;
14 changes: 14 additions & 0 deletions packages/fastify/producer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"baseUrl": "./src",
"outDir": "./lib",
},
"include": [
"./src"
],
"exclude": [
"./lib",
"./node_modules"
]
}
Loading

0 comments on commit 2aad9cf

Please sign in to comment.