nano2 requires node v8.0.0 or higher
$ npm install nano2
import nano2 from 'nano2';
// create service
const service = nano2();
Middleware works same as koa js middleware and internally it uses koa-compose
service.use((ctx, next) => {
ctx.meta.user = {
name: 'user',
};
return next();
});
// add ping action
service.action('ping', () => 'pong');
// add math.add action
service.action('math.add', ctx => ctx.params.a + ctx.params.b);
// add math.multiply using action spec
service.action({
name: 'math.multiply',
version: '1.0',
description: 'Math multiplication',
type: 'service',
handler: ctx => ctx.params.a * ctx.params.b;
});
await service.start()
// call ping action
const pingResponse = await service.call('ping');
// call math.add action
const addResponse = await service.call('math.add', { a: 5, b: 3 });
// call math.multiply action
const multiplyResponse = await service.call('math.multiply', { a: 5, b: 3 });
TODO
Please see types.ts
See the LICENSE file for license rights and limitations (MIT).