mediateur
Version:
A simple library allowing the implementation of the Mediator design pattern
77 lines (58 loc) • 2.37 kB
Markdown
[](LICENCE)
[](https://github.com/spontoreau/mediateur/actions/workflows/ci.yml)
[](https://gitmoji.dev)
Easy-to-use library for implementing the mediator pattern
Start by installing the **mediateur** library in your project with the following NPM command:
```bash
npm i mediateur
```
Then define a **Message**:
```typescript
// createPerson.ts
import type { Message } from 'mediateur';
export type CreatePerson = Message<'CREATE_PERSON', {
firstName: string;
lastName: string;
email: string;
age: number;
}>
```
When your message is defined, you can create a dedicated async function to handle it using the **MessageHandler** type:
```typescript
// createPerson.handler.ts
import type { MessageHandler } from 'mediateur';
import type { CreatePerson } from './createPerson.ts'
const createPersonHandler: MessageHandler<CreatePerson> = async (message) => {
console.table(message);
return Promise.resolve();
}
```
Finally, add the handler to the meditor with the **register** function, then execute the **send** function by using an instance corresponding to your message:
```typescript
// app.ts
import { mediator } from 'mediateur';
import { CreatePerson } from './createPerson';
import { createPersonHandler } from 'createPerson.handler';
mediator.register('CREATE_PERSON', createPersonHandler);
(async () => {
const message: CreatePerson = {
type: 'CREATE_PERSON',
data: {
firstName: 'Anthony',
lastName: 'Houston',
email: 'anthony.houston@some-email.com',
age: 30
}
}
await mediator.send<CreatePerson>(message);
})();
```
This example is really basic, there is more to discover in the [documentation](https://github.com/spontoreau/mediateur/wiki/documentation).
Before you contribute, please take a few minutes to read the
[](https://github.com/spontoreau/mediateur/wiki/Contribution-guidelines).
This project has adopted the [code of conduct](https://github.com/spontoreau/mediateur/wiki/Code-of-conduct) defined by
the Contributor Covenant.