egg-apiproxy

1.0.2 • Public • Published

egg-apiproxy

npm npm node

egg Request lib

Installation

npm i -S egg-apiproxy

Usages

  • 开启插件
// config/plugin.js
exports.apiproxy = {
  enable: true,
  package: 'egg-apiproxy'
};
  • 使用方式
// config/config.default.js
apiproxy: {
  defaults: {
    key: 'key-和后端约定',
    client: {
      // dataKey: 'data',
      caller: 'caller-和后端约定',
    },
    options: {
      timeout: 10000,
    },
  },
  passHeaderKeys: [ 'Cookie', 'token' ],
},
// config/config.${env}.js
apiproxy: {
  server: {
    test: {
      host: 'http://10.1.1.36:8888',
    },
  },
},
 
// app/service/Test.js
module.exports = app => {
  class TestProxy extends app.CommonProxy {
    constructor(ctx) {
      super(ctx);
      this.hostname = 'test'; // 对应 config/config.${env}.js 中 apiproxy.server.test的配置
    }
 
    // _transformResponse(result, config, response) {
    //   result = super._transformResponse(result, config, response);
    //   return newResult;
    // }
 
    async getTestData(data, page) {
      const opts = {
        method: 'GET', // 默认 POST
        service: '/sys/user/info',
        data,
        page,
        // client: {},
        // headers: {},
      };
      return await this.request(opts);
    }
  }
 
  return TestProxy;
};
 
// app/controller/test.js
const result = await service.test.getTestData(data, page);

Issues

Submit the issues if you find any bug or have any suggestion.

Contribution

Fork the repository and submit pull requests.

Release Notes

CHANGELOG

License

npm

Dependents (0)

Package Sidebar

Install

npm i egg-apiproxy

Weekly Downloads

2

Version

1.0.2

License

MIT

Unpacked Size

33.5 kB

Total Files

10

Last publish

Collaborators

  • bigang.ybg