# nats-method-ex

> Extends nats method with enforced protocol.

Latest version **2.1.4** (published 2017-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install nats-method-ex
pnpm add nats-method-ex
yarn add nats-method-ex
bun add nats-method-ex
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.4 |
| Published | 2017-09-28 |
| First published | 2017-09-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | zhaoyao91 |
| Maintainers | zhaoyao91 |

## Links

- npm: https://www.npmjs.com/package/nats-method-ex
- Repository: https://github.com/zhaoyao91/method-nats-ex
- Homepage: https://github.com/zhaoyao91/method-nats-ex#readme
- Issues: https://github.com/zhaoyao91/method-nats-ex/issues
- npm.io page: https://npm.io/package/nats-method-ex

## Dependencies (3)

- [uuid](https://npm.io/package/uuid.md) ^3.1.0
- [nats-method](https://npm.io/package/nats-method.md) ^1.3.1
- [simple-json-logger](https://npm.io/package/simple-json-logger.md) ^1.2.0

## Recent versions

- 2.1.4 (latest) — 2017-09-28
- 2.1.3 — 2017-09-15
- 2.1.2 — 2017-09-15
- 2.1.1 — 2017-09-15
- 2.1.0 — 2017-09-15
- 2.0.0 — 2017-09-15
- 1.0.1 — 2017-09-15
- 1.0.0 — 2017-09-14

## README

# Nats Method EX

Extends nats method with enforced protocol.

## Features

- Protocol enforced
- Default error handler

## Protocol

[protocol](docs/protocol.md)

## Usage

Install package:

```
npm install nats-method-ex
```

Import an use:

```ecmascript 6
const methodEx = require('nats-method-ex')('nats://localhost:4222')
```

Basic Usage:

```ecmascript 6
methodEx.define('sum', async ({a, b}) => a + b)

const output = await methodEx.call('sum', {a: 1, b: 2})
// output: {requestId: ..., ok: true, data: 3}
```

Failed case:

```ecmascript 6
const {fail} = require('nats-method-ex')

methodEx.define('sum', async ({a, b}) => {
  if (typeof a !== 'number' || typeof b !== number) {
    return fail('invalid-args', 'a and b must be numbers', {a, b})
  }
  else return a + b
})

const output = await methodEx.call('sum', {a: '1', b: '2'})
// output: {
//   requestId: ..., 
//   ok: false,
//   error: 'invalid-args',
//   message: 'a and b must be numbers',
//   details: {a: '1', b: '2'}
// }
```

Default error handler:

```ecmascript 6
methodEx.define('error', async () => {throw new Error('Wops!')})

const output = await methodEx.call('error')
// output: {
//   requestId: ..., 
//   ok: false,
//   error: 'internal-method-error',
//   message: 'Error: Wops!'
// }
```

## API

This module is based on [nats-method](https://github.com/zhaoyao91/nats-method),
and that module is based on [node-nats](https://github.com/nats-io/node-nats).
Please check their docs for more detailed apis.

#### module.exports

> func(options) => methodEx

- `options` could be:
  - an nats url string for single server, such as `nats://localhost:4222`
  - an nats url string for cluster servers, such as `nats://192.168.0.1:4222,nats://192.168.0.2:4222`
  - [nats connect options](https://github.com/nats-io/node-nats)
  
#### methodEx.define

> `func(name, handler)`

- `handler`: async (data, input, subject) => output
  - `output` could be any data or standard Output instance built by `module.exports.ok` or `module.exports.fail`

#### methodEx.call

> `async func(name, data, options) => response`

- `options` is optional, which may contains:
  - timeout: optional
  - requestId: optional
- `response`: see [protocol response](docs/protocol.md#response)
  
#### methodEx.callAndForget

> `func(name, data, options)`

- `options` is optional, which may contains:
  - requestId: optional

---
_Source: https://npm.io/package/nats-method-ex · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
