1.2.1 • Published 3 years ago

egg-rpc-like v1.2.1

Weekly downloads
4
License
MIT
Repository
github
Last release
3 years ago

egg-rpc-like

NPM version build status Test coverage David deps Known Vulnerabilities npm download

This plugin allows developers to access remote servers in the form of RPC, whether it is an http request, a database operation or use another RPC.

中文说明

Install

$ npm i egg-rpc-like --save

Usage

// {app_root}/config/plugin.js
exports.rpcLike = {
  enable: true,
  package: 'egg-rpc-like',
};
// config/config.default.js
const generators = require('egg-rpc-like');

exports.rpcLike = {
  default: {
    $generator: generator.simpleCurl(data=>data), // or generator.functionCurl
  },
  clients: {
    serviceA: { // object name
      host: 'http://your.site.com',
      $members: [
        'test',
        { $key:'test2', api:'sample'},
      ],
    }
  },
};
//use it in your controller or service
let data = await ctx.rpcl.serviceA.test({id:1}); // get data from http://your.site.com/test?id=1
let data = await app.rpcl.serviceA.test2(); // get data from http://your.site.com/sample

Configuration

  • The generation of rpcl objects is based on obj-gen-9
  • Each child of rpcl will have an additional member app, which can be accessed inside this function via this.app. Reference here
  • $generator is used to generate your rpcLike object. You can use the default simpleCurl or functionCurl, or you can write your own generator.
  • The format of the configuration depends on $generator, and different generators require different configuration formats.
  • More detailed configuration can refer to config.default.js

simpleCurl

  • simpleCurl needs to pass a function to process the data from curl() to the result the user wants, in the format function(data}{return data;}
  • The function generated by simpleCurl is in the form of async function(params){}, where params is options.data in curl().
  • When using simpleCurl, you must configure host, otherwise an error will be thrown.
  • Member can be in two formats: {$key:'name', api:'', options:''} and 'name', where the latter will be parsed as {$key:'name', api: 'name' }
  • The default options for simpleCurl are {method:'GET',dataType:'json'}, which can also be specified in member or config
  • The configuration can use the onFail function to handle request failures.

functionCurl

  • functionCurl requires that the returned data be a JSON and the format must be {err:-1,msg:'msg'} or {data:data}
  • The function generated by functionCurl is in the form of async function(...argments){}, where argments will be processed as an array and stringed as a parameter arg.
  • When using functionCurl, you must configure host, otherwise an error will be thrown.
  • Member can be in two formats: {$key:'name', api:'', options:''} and 'name', where the latter will be parsed as {$key:'name', api: 'name' }
  • The default options for functionCurl are {method:'GET',dataType:'json'}, which can also be specified in member or config
  • The configuration can use the onFail function to handle request failures, and the onError function to handle RPC method errors.

Custom generator

Reference here

Server example of functionCurl

// app/controller/sample.js
const Controller = require('egg').Controller;
class SampleController extends Controller {
  async sample() {
    const { ctx, service } = this;
    try {
        const args = JSON.parse(ctx.request.query.arg);
        const result = await service.sample(...args);
        ctx.body = { data: result };
      } catch (e) {
        ctx.body = {
          err: -1,
          msg: e.message,
        };
      }
  }
}
module.exports = SampleController;
// app/service/sample.js
const Service = require('egg').Service;

class SampleService extends Service {
  async sample(a, b, c = 0) {
    return a + b + c;
  }
}

module.exports = SampleService;

Unit tests

npm test

License

MIT This README was translate by google

1.2.0

3 years ago

1.2.1

3 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago