# @janiscommerce/event-emitter

> A package for emit events

Latest version **3.0.0** (published 2023-04-25) · ISC license · 0 weekly downloads

## Install

```sh
npm install @janiscommerce/event-emitter
pnpm add @janiscommerce/event-emitter
yarn add @janiscommerce/event-emitter
bun add @janiscommerce/event-emitter
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2023-04-25 |
| First published | 2019-10-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Janis |
| Maintainers | janiscommerce |

## Links

- npm: https://www.npmjs.com/package/@janiscommerce/event-emitter
- Repository: https://github.com/janis-commerce/event-emitter
- Homepage: https://github.com/janis-commerce/event-emitter.git#readme
- Issues: https://github.com/janis-commerce/event-emitter/issues
- npm.io page: https://npm.io/package/@janiscommerce/event-emitter

## Dependencies (1)

- [@janiscommerce/microservice-call](https://npm.io/package/@janiscommerce/microservice-call.md) ^5.0.0

## Recent versions

- 3.0.0 (latest) — 2023-04-25
- 2.2.1 — 2021-04-01
- 2.2.0 — 2021-02-18
- 2.1.0 — 2020-09-17
- 2.0.2 — 2019-10-22
- 2.0.1 — 2019-10-22
- 2.0.0 — 2019-10-21
- 1.0.1 — 2019-10-09
- 1.0.0 — 2019-10-04

## README

# Event Emitter

![Build Status](https://github.com/janis-commerce/event-emitter/workflows/Build%20Status/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/janis-commerce/event-emitter/badge.svg?branch=master)](https://coveralls.io/github/janis-commerce/event-emitter?branch=master)
[![npm version](https://badge.fury.io/js/%40janiscommerce%2Fevent-emitter.svg)](https://www.npmjs.com/package/@janiscommerce/event-emitter)

A package to emit events to JANIS Events

## Installation
```sh
npm install @janiscommerce/event-emitter
```

## Configuration

### ENV variables
**`JANIS_SERVICE_NAME`** (required): The name of the service that will emit the event.

### Important
This package uses [@janiscommerce/microservice-call](https://www.npmjs.com/package/@janiscommerce/microservice-call), it's strongly recommended to read this package docs before continue.

## API

### **`emit(event)`**

Emits an event to janis-events.
Returns an object with a boolean property `result` set to `true` if the operation is successful, `false` otherwise, and a `response` property with the MS Call response.

#### Event parameter

The event parameter is an `[Object]` and have the following structure:
- **`entity [String]`** (required): The name of the entity that is emitting the event.
- **`event [String]`** (required): The event name.
- **`client [String]`** (optional): The client code name.
- **`id [Number|String|Array of Strings|Array of Numbers]`** (optional): The ID or ID's of the entity that is emitting the event.

## Errors

The errors are informed with a `EventEmitterError`.
This object has a code that can be useful for a correct error handling.
The codes are the following:

| Code | Description                    |
|------|--------------------------------|
| 1    | Invalid event                  |
| 2    | Invalid event properties       |
| 3    | Unknown/empty service name     |
| 4    | MicroserviceCall Error         |

## Usage
```js
'use strict';

const EventEmitter = require('@janiscommerce/event-emitter');

process.env.JANIS_SERVICE_NAME = 'my-service';

(async () => {

	try {
		const { result, response } = await EventEmitter.emit({
			entity: 'some-entity',
			event: 'some-event',
			client: 'some-client',
			id: 1
		});

		if(!result)
			console.log(response);

	} catch(err) {
		throw err;
	}

	try {
		const { result, response } = await EventEmitter.emit({
			entity: 'some-entity',
			event: 'some-event',
			client: 'some-client',
			id: [1, 2, 3, 4]
		});

		if(!result)
			console.log(response);

	} catch(err) {
		throw err;
	}

})();
```

---
_Source: https://npm.io/package/@janiscommerce/event-emitter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
