-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add plugin rabbitmq producer for fastify
- Loading branch information
Showing
9 changed files
with
504 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.turbo | ||
src | ||
tsconfig.json | ||
rollup.config.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
Oops, something went wrong.