Skip to content

Commit 5336780

Browse files
Inital extension
0 parents  commit 5336780

23 files changed

+17803
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @Sebastian-Iwanczyszyn
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: PR Title check
2+
3+
on:
4+
pull_request:
5+
types: [opened, edited, synchronize]
6+
7+
permissions:
8+
pull-requests: read
9+
contents: read
10+
11+
jobs:
12+
enforce-pr-title:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Ensure PR Title is Conventional Commit
16+
uses: amannn/action-semantic-pull-request@v5
17+
with:
18+
types: |
19+
feat
20+
fix
21+
chore
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/release.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions:
9+
contents: write
10+
issues: write
11+
pull-requests: write
12+
13+
jobs:
14+
release:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
21+
- name: Setup Node.js
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version: 23
25+
registry-url: 'https://registry.npmjs.org/'
26+
27+
- name: Install dependencies
28+
run: npm i
29+
30+
- name: Build library
31+
run: npm run build
32+
33+
- name: Release
34+
env:
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
37+
run: npx semantic-release
38+

.gitignore

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# compiled output
2+
/dist
3+
/node_modules
4+
/lib
5+
/build
6+
7+
# Logs
8+
logs
9+
*.log
10+
npm-debug.log*
11+
pnpm-debug.log*
12+
yarn-debug.log*
13+
yarn-error.log*
14+
lerna-debug.log*
15+
16+
# OS
17+
.DS_Store
18+
19+
# Tests
20+
/coverage
21+
/.nyc_output
22+
23+
# IDEs and editors
24+
/.idea
25+
.project
26+
.classpath
27+
.c9/
28+
*.launch
29+
.settings/
30+
*.sublime-workspace
31+
32+
# IDE - VSCode
33+
.vscode/*
34+
!.vscode/settings.json
35+
!.vscode/tasks.json
36+
!.vscode/launch.json
37+
!.vscode/extensions.json
38+
39+
# dotenv environment variable files
40+
.env
41+
.env.development.local
42+
.env.test.local
43+
.env.production.local
44+
.env.local
45+
46+
# temp directory
47+
.temp
48+
.tmp
49+
50+
# Runtime data
51+
pids
52+
*.pid
53+
*.seed
54+
*.pid.lock
55+
56+
# Diagnostic reports (https://nodejs.org/api/report.html)
57+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<p align="center">
2+
<image src="nestjstools-logo.png" width="400">
3+
</p>
4+
5+
# @nestjstools/messaging-azure-service-bus-extension
6+
7+
A NestJS library for managing asynchronous and synchronous messages with support for buses, handlers, channels, and consumers. This library simplifies building scalable and decoupled applications by facilitating robust message handling pipelines while ensuring flexibility and reliability.
8+
9+
---
10+
11+
## Documentation
12+
13+
https://nestjstools.gitbook.io/nestjstools-messaging-docs
14+
15+
---
16+
17+
## Installation
18+
19+
```bash
20+
npm install @nestjstools/messaging @nestjstools/messaging-azure-service-bus-extension
21+
```
22+
23+
or
24+
25+
```bash
26+
yarn add @nestjstools/messaging @nestjstools/messaging-azure-service-bus-extension
27+
```
28+
## Azure service bus Integration: Messaging Configuration Example
29+
30+
---
31+
32+
```typescript
33+
import { Module } from '@nestjs/common';
34+
import { MessagingModule } from '@nestjstools/messaging';
35+
import { SendMessageHandler } from './handlers/send-message.handler';
36+
import { MessagingAzureServiceBusExtensionModule, AzureServiceBusChannelConfig } from '@nestjstools/messaging-azure-service-bus-extension';
37+
38+
@Module({
39+
imports: [
40+
MessagingAzureServiceBusExtensionModule,
41+
MessagingModule.forRoot({
42+
buses: [
43+
{
44+
name: 'azure.bus',
45+
channels: ['azure-channel'],
46+
},
47+
],
48+
channels: [
49+
new AzureServiceBusChannelConfig({
50+
name: 'azure-channel',
51+
enableConsumer: true,
52+
autoCreate: false, // You need to have admin account to create a queue
53+
fullyQualifiedNamespace: 'Endpoint=...',
54+
queue: 'azure-queue',
55+
}),
56+
],
57+
debug: true, // Optional: Enable debugging for Messaging operations
58+
}),
59+
],
60+
})
61+
export class AppModule {}
62+
```
63+
64+
## Dispatch messages via bus (example)
65+
66+
```typescript
67+
import { Controller, Get } from '@nestjs/common';
68+
import { CreateUser } from './application/command/create-user';
69+
import { IMessageBus, MessageBus, RoutingMessage } from '@nestjstools/messaging';
70+
71+
@Controller()
72+
export class AppController {
73+
constructor(
74+
@MessageBus('azure.bus') private azureMessageBus: IMessageBus,
75+
) {}
76+
77+
@Get('/azure')
78+
createUser(): string {
79+
this.azureMessageBus.dispatch(new RoutingMessage(new CreateUser('John FROM Azure bus'), 'my_app_command.create_user'));
80+
81+
return 'Message sent';
82+
}
83+
}
84+
```
85+
86+
### Handler for your message
87+
88+
```typescript
89+
import { CreateUser } from '../create-user';
90+
import { IMessageBus, IMessageHandler, MessageBus, MessageHandler, RoutingMessage, DenormalizeMessage } from '@nestjstools/messaging';
91+
92+
@MessageHandler('my_app_command.create_user')
93+
export class CreateUserHandler implements IMessageHandler<CreateUser>{
94+
95+
handle(message: CreateUser): Promise<void> {
96+
console.log(message);
97+
// TODO Logic there
98+
}
99+
}
100+
```
101+
102+
---
103+
104+
### Key Features:
105+
106+
* **Azure service bus Integration** Seamlessly send and receive messages using Azure service bus within your NestJS application.
107+
108+
* **Automatic Queue Creation** Automatically creates when autoCreate: true is set in the configuration.
109+
110+
* **Named Buses & Channel Routing** Supports custom-named message buses and routing of messages across multiple channels for event-driven architecture.
111+
112+
---
113+
114+
## 📨 Communicating Beyond a NestJS Application (Cross-Language Messaging)
115+
116+
### To enable communication with a Handler from services written in other languages, follow these steps:
117+
118+
1. **Publish a Message to the queue**
119+
120+
2. **Include the Routing Key Header**
121+
Your message **must** include a header attribute named `messaging-routing-key`.
122+
The value should correspond to the routing key defined in your NestJS message handler:
123+
124+
```ts
125+
@MessageHandler('my_app_command.create_user') // <-- Use this value as the routing key
126+
```
127+
128+
---
129+
130+
## Configuration options
131+
132+
### AzureServiceBusChannel
133+
134+
#### **AzureServiceBusChannelConfig**
135+
136+
| **Property** | **Description** | **Default Value** |
137+
|-------------------------------|-----------------------------------------------------------------------------------------------|-------------------|
138+
| **`name`** | The name of the messaging channel within your app (used for internal routing). | |
139+
| **`fullyQualifiedNamespace`** | Azure service bus credentials (e.g., `Endpoint=sb:...`). | |
140+
| **`enableConsumer`** | Whether to enable message consumption (i.e., subscribing and processing messages from queue). | `true` |
141+
| **`autoCreate`** | Automatically create the queue, but admin permission is required from IAM | `true` |
142+
| **`queue`** | The name of the queue to publish messages and consume. | |
143+
144+
---
145+
146+
## Real world working example with RabbitMQ & Redis - but might be helpful to understand how it works
147+
https://github.com/nestjstools/messaging-rabbitmq-example

eslint.config.mjs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// @ts-check
2+
import eslint from '@eslint/js';
3+
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
4+
import globals from 'globals';
5+
import tseslint from 'typescript-eslint';
6+
7+
export default tseslint.config(
8+
{
9+
ignores: ['eslint.config.mjs'],
10+
},
11+
eslint.configs.recommended,
12+
...tseslint.configs.recommendedTypeChecked,
13+
eslintPluginPrettierRecommended,
14+
{
15+
languageOptions: {
16+
globals: {
17+
...globals.node,
18+
...globals.jest,
19+
},
20+
ecmaVersion: 5,
21+
sourceType: 'module',
22+
parserOptions: {
23+
projectService: true,
24+
tsconfigRootDir: import.meta.dirname,
25+
},
26+
},
27+
},
28+
{
29+
rules: {
30+
'@typescript-eslint/no-explicit-any': 'off',
31+
'@typescript-eslint/no-floating-promises': 'warn',
32+
'@typescript-eslint/no-unsafe-argument': 'warn'
33+
},
34+
},
35+
);

nest-cli.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://json.schemastore.org/nest-cli",
3+
"collection": "@nestjs/schematics",
4+
"sourceRoot": "src",
5+
"compilerOptions": {
6+
"deleteOutDir": true
7+
}
8+
}

nestjstools-logo.png

40.1 KB
Loading

0 commit comments

Comments
 (0)