2.1.4 • Published 7 years ago

nats-method-ex v2.1.4

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

Nats Method EX

Extends nats method with enforced protocol.

Features

  • Protocol enforced
  • Default error handler

Protocol

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

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

methodEx.callAndForget

func(name, data, options)

  • options is optional, which may contains:
    • requestId: optional
2.1.4

7 years ago

2.1.3

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago