项目作者: yujuiting

项目描述 :
json rpc server
高级语言: TypeScript
项目地址: git://github.com/yujuiting/jtrpc.git
创建时间: 2018-12-07T09:28:48Z
项目社区:https://github.com/yujuiting/jtrpc

开源协议:

下载


jtrpc

A json rpc server framework.

install

  1. $ npm install typedi koa koa-bodyparser @jtrpc/core @jtrpc/koa

quick start

  1. import { Service } from 'typedi';
  2. import * as Koa from 'koa';
  3. import * as bodyparser from 'koa-bodyparser';
  4. import jtrpc from '@jtrpc/koa';
  5. import { MethodInstance } from '@jtrpc/core';
  6. const app = new Koa();
  7. app.use(bodyparser());
  8. app.use(jtrpc({
  9. methodResolver: method => {
  10. switch (method) {
  11. case 'math.sum': return Sum;
  12. }
  13. },
  14. }));
  15. app.listen(3000, () => console.log('server up'));
  16. @Service('ping')
  17. class Ping extends MethodInstance {
  18. execute() {
  19. return 'pong';
  20. }
  21. validateParameters() {
  22. return true;
  23. }
  24. }
  25. @Service()
  26. class Sum extends MethodInstance<number[], number> {
  27. execute(...values: number[]) {
  28. return values.reduce((acc, curr) => acc + curr, 0);
  29. }
  30. validateParameters(...values: unknown[]) {
  31. return values.every(value => typeof value === 'number');
  32. }
  33. }