nats-method-ex
v2.1.4
Published
Extends nats method with enforced protocol.
Downloads
9
Readme
Nats Method EX
Extends nats method with enforced protocol.
Features
- Protocol enforced
- Default error handler
Protocol
Usage
Install package:
npm install nats-method-ex
Import an use:
const methodEx = require('nats-method-ex')('nats://localhost:4222')
Basic Usage:
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:
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:
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, and that module is based on 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
- an nats url string for single server, such as
methodEx.define
func(name, handler)
handler
: async (data, input, subject) => outputoutput
could be any data or standard Output instance built bymodule.exports.ok
ormodule.exports.fail
methodEx.call
async func(name, data, options) => response
options
is optional, which may contains:- timeout: optional
- requestId: optional
response
: see protocol response
methodEx.callAndForget
func(name, data, options)
options
is optional, which may contains:- requestId: optional