egg-rpc-like
v1.2.1
Published
This plugin allows developers to access remote servers in the form of RPC
Downloads
8
Maintainers
Readme
egg-rpc-like
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