mikroevent
Version:
Ultra-lightweight, Node-native way to handle events, both in-process (as EventEmitter events) or across systems via HTTP(S).
121 lines (79 loc) • 3.38 kB
Markdown
in-process (as EventEmitter events) or across systems via HTTP(S)**.
[](https://www.npmjs.com/package/mikroevent)
[](https://bundlephobia.com/package/mikroevent)

[](https://opensource.org/licenses/MIT)
---
- Node.js native solution to work with event-driven architecture
- Easiest possible way to work with events in, or across, Node.js-based systems
- None of the learning curve and overhead of other eventing options
- Tiny (~1.2 KB gzipped)
- Zero dependencies
- High test coverage
## Installation
```bash
npm install mikroevent -S
```
## Usage
### Quick Start
MikroEvent sends _events_ to named _targets_ using HTTP calls and/or internal, i.e. in-process events.
```typescript
// ES5 format
const { MikroEvent } = require('mikroevent');
// ES6 format
import { MikroEvent } from 'mikroevent';
const mikroEvent = new MikroEvent();
mikroEvent.addTarget({
name: 'internal',
events: ['user.created']
});
const handler = () => { console.log('This runs in response to the user.created event') };
mikroEvent.on('user.created', handler);
//mikroEvent.once('user.created', handler); // Run event handler only once
//mikroEvent.off('user.created', handler); // Remove event handler
await mikroEvent.emit('user.created', { id: '123', name: 'Test User' });
```
```typescript
mikroEvent.updateTarget('system_a', { url: 'https://api.mydomain.com/userCreated', events: ['user.updated'] };
```
```typescript
mikroEvent.addEventToTarget('system_a', ['user.updated', 'user.deleted']);
```
```typescript
mikroEvent.removeTarget('system_a');
```
Set up an event listener to react to events.
```typescript
const handler = () => console.log('Run this when user.created is emitted');
mikroEvent.on('user.created', handler);
```
Handle an incoming event arriving over HTTP. Used for server integrations, when you want to manually handle the incoming event payload.
The processing will be async using `process.nextTick()` and running in a non-blocking fashion.
```typescript
await mikroEvent.handleIncomingEvent({
eventName: 'user.created',
data: { id: '123', name: 'Test User' }
});
```
Create middleware for Express-style servers, i.e. using `req` and `res` objects. This is an approach that replaces using `handleIncomingEvent()` manually.
```typescript
const middleware = mikroEvent.createMiddleware();
await middleware(req, res, next);
```
You may also optionally instantiate MikroEvent with a custom error handling function.
```typescript
const errorHandler = () => console.error('Run this on errors');
const mikroEvent = new MikroEvent({ errorHandler });
```
Check out the [integration tests](tests/integration/index.ts) for a bigger, practical example of how MikroEvent works.
MIT. See `LICENSE` file.
**Ultra-lightweight, Node.js-native way to handle events, both