Skip to content

alexandrereyes/nestjs-mediator

 
 

Repository files navigation

Nest Logo

Motivation

NestJS has a lightweight CQRS module that works very well but the problem is we can not inject the other provider with scope-request and that is the reason why this package has existed

Description

The nestjs-mediator supports request/response, command, query, and notification with exposing the ability to inject any scope-request provider

Installation

$ npm install --save nestjs-mediator

or with yarn

$ yarn add nestjs-mediator

Quick Start(or reference in github example)

Request/Response

Define your query or command

import { Request } from "nestjs-mediator"

export class TestCommand extends Request<string>{
	//Your properties
}

Then define your handler

import { RequestHandler, IRequestHandler } from "nestjs-mediator"

@RequestHandler(TestCommand)
export class TestCommandHandler implements IRequestHandler<TestCommand, string> {
	handle(data: TestCommand): Promise<string>{
		//Your logic
	}
}

Don't forget to import your CommandHandler and MediatorModule into your module

import { MediatorModule } from "nestjs-mediator"

@Module({
	imports: [MediatorModule],
	providers: [TestCommandHandler]
})

class TestModule{}

Finally, send your command through the mediator:

import { Mediator } from "nestjs-mediator"

@Controller()
export class TestController {
	constructor(private mediator: Mediator){}

	@Post()
	post(){
		return this.mediator.send(new TestCommand());
	}
}

Notification

Define your notification

import { Notification } from "nestjs-mediator"

export class TestNotification extends Notification{
	//Your properties
}

and then define many handlers that you want to receive this notification

import { NotificationHandler, INotificationHandler } from "nestjs-mediator"

@NotificationHandler(TestNotification)
export class TestNotificationHandler1 implements INotificationHandler<TestNotification>{
	handle(data: TestNotification){
		//Your logic
	}
}

@NotificationHandler(TestNotification)
export class TestNotificationHandler2 implements INotificationHandler<TestNotification>{
	handle(data: TestNotification){
		//Your logic
	}
}

Import MediatorModule and your handler the same as above and finally, publish your message via the mediator

import { Mediator } from "nestjs-mediator"

@Controller()
export class TestController {
	constructor(private mediator: Mediator){}

	@Post()
	post(){
		this.mediator.publish(new TestNotification())
	}
}

Types

Request<T> - where T is the returns value

RequestHandler<T> - the decorator where T is your request

IRequestHandler<T, U> - you must implement this interface, T is your request and U is your return value

Notification

NotificationHandler<T> - the decorator where T is your notification

INotificationHandler<T> - the interface that you have to implement to publish your notification

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 91.0%
  • JavaScript 9.0%