npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

egg-rpc-like

v1.2.1

Published

This plugin allows developers to access remote servers in the form of RPC

Downloads

30

Readme

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