egg-apiproxy
v1.0.2
Published
接口请求库
Downloads
4
Maintainers
Readme
egg-apiproxy
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.