micron-client

3.0.3 • Public • Published

Micron Client

npm Dependencies Join the chat at https://gitter.im/johnhof/micron-client

Client for interaction between micron based microservices.

The goal of micron is to simplify construction and communication between micro-services, regardless of the communication method. The client and service currently support communication over REST/HTTP, ØMQ, and others.

$ npm install --save micron-client

Key

Usage

Spot

  • should be used in a shared fashion to prevent constant socket connection overhead
let micron = require('micron-client');
 
let client = micron({
  userService : {
    micron: 'zeromq', // specify zeromq as the communication method
    // OR
    micron: 'http', // specify HTTP as the communication method
 
    prefix: 'v1', // prefix all request url's
    host: '127.0.0.1', /// host of the service
    port: 8001 // port of the service
  }
});
 
let result = yield client.userService.post('user/create', {ß
  email: 'test@tester.com',
  password: 'Tester@1'
});

Middleware

  • Allows sockets to stay open for server duration
let micron = require('micron-client');
let config = require('./services.json');
 
// koa
 
let koa = require('koa');
let koaApp = koa();
 
koaApp.use(micron.middleware.koa());
koaApp.listen(8000)
 
 
// express
 
 express = require('express');
let expressApp = express();
 
expressApp.use(micron.middleware.express());
expressApp.listen(8000)

Operations

.request

  • Follows the object structure of the request module's base request builder
    • The only major change is that the url and host are pulled from the config. The opts.path method should be used instead
  • All other operations are simply wrappers of this function
  • Important notes
    • opts.path should be used instead of opts.url (the host and port are added from the resource config)
    • No opts.method defaults to GET
    • the opts.path string can use templates using keys on from the opts.parameters object
    • form is aliased as body
    • All requests default to type JSON
yield client.someMicronService.request({
  method: 'post',
  path: '/foo/{foo_id}',
  parameters: {
    foo_id: 'FooId12345'
  },
  body: {
    foo_property: 'bar'
  },
  qs: {
    foo_query: 'bar'
  }
});

.create/.post

  • Wraps .request
  • Takes arguments (path, opts)
    • path
      • Prefixed with the host, port, and prefix from the resource config
      • Supports templating with {KEY} against opts.parameters
    • opts
      • If no parameters, body, qs, or headers param exists, the object will be set as the body/form
yield client.someMicronService.post('/foo/FooId12345', {
  foo_property: 'bar'
});
 
// OR
 
yield client.someMicronService.post('/foo/{foo_id}', {
  parameters: {
    foo_id: 'FooId12345'
  },
  body: {
    foo_property: 'bar'
  }
});

.read/.get

  • Wraps .request
  • Takes arguments (path, opts)
    • path
      • Prefixed with the host, port, and prefix from the resource config
      • Supports templating with {KEY} against opts.parameters
    • opts
      • If no parameters, body, qs, or headers param exists, the object will be set as the body/form
yield client.someMicronService.get('/foo/FooId12345?foo_query=bar');
 
// OR
 
yield client.someMicronService.get('/foo/{foo_id}', {
  parameters: {
    foo_id: 'FooId12345'
  },
  qs: {
    foo_query: 'bar'
  }
});

.update/.put

  • Wraps .request
  • Takes arguments (path, opts)
    • path
      • Prefixed with the host, port, and prefix from the resource config
      • Supports templating with {KEY} against opts.parameters
    • opts
      • If no parameters, body, qs, or headers param exists, the object will be set as the body/form
yield client.someMicronService.put('/foo/FooId12345', {
  foo_property: 'bar'
});
 
// OR
 
yield client.someMicronService.put('/foo/{foo_id}', {
  parameters: {
    foo_id: 'FooId12345'
  },
  body: {
    foo_property: 'bar'
  }
});

.destroy/.delete

  • Wraps .request
  • Takes arguments (path, opts)
    • path
      • Prefixed with the host, port, and prefix from the resource config
      • Supports templating with {KEY} against opts.parameters
    • opts
      • If no parameters, body, qs, or headers param exists, the object will be set as the body/form
yield client.someMicronService.delete('/foo/FooId12345', {
  foo_property: 'bar'
});
 
// OR
 
yield client.someMicronService.delete('/foo/{foo_id}', {
  parameters: {
    foo_id: 'FooId12345'
  },
  body: {
    foo_property: 'bar'
  }
});

.status

  • Performs a GET /status on the service
  • The service will perform a GET /status on all of its dependent services
  • All ternary dependencies will be ignored to prevent a loop back
  • Calling Status directly on micron-client will get the status of all registered services
  • Takes arguments [opts]
    • opts.timeout
      • timeout in milliseconds for each status request
// singular status
yield client.someMicronService.status();
 
// all status's
yield client.status();

Contributing

Add new clients to the ./lib/clients directory as an independant file. Each client should support all operations above

Requirements

  • the opts patameter of each request should follow the request module structure
    • rather than url, a path parameter is expected
    • the path parameter should be used to map functionality of the service client being written
  • the path parameter should template properties of opts.pathOpts matching{OPT_NAME}

Authors

Readme

Keywords

Package Sidebar

Install

npm i micron-client

Weekly Downloads

4

Version

3.0.3

License

MIT

Last publish

Collaborators

  • johnhof