项目作者: unicreators

项目描述 :
extend api.
高级语言: JavaScript
项目地址: git://github.com/unicreators/extend-api.git
创建时间: 2017-09-12T08:05:57Z
项目社区:https://github.com/unicreators/extend-api

开源协议:MIT License

下载


extend-api

extend api.

Install

  1. $ npm install extend-api

Usage

Custom api

  1. const { Api, ApiExtend } = require('extend-api');
  2. let CustomApi = class customApi extends Api {
  3. constructor(token) { super(); this.token = token; }
  4. // override
  5. async buildApiReqOpts(extendInvokeOpts) {
  6. // attach 'token'
  7. extendInvokeOpts.qs = Object.assign({ token: this.token }, extendInvokeOpts.qs);
  8. return extendInvokeOpts;
  9. }
  10. };

Custom extend

  1. let MessageExtend = class MessageExtend extends ApiExtend {
  2. async send(to, content) {
  3. return await this.invoke(
  4. 'https://api.weixin.qq.com/cgi-bin/message/custom/send',
  5. // request opts.
  6. // (see: https://github.com/request/request#requestoptions-callback)
  7. {
  8. body: {
  9. touser: to, msgtype: 'text',
  10. text: { content }
  11. }
  12. }, 'POST');
  13. }
  14. };

Register extend

  1. let api = new CustomApi('token..');
  2. // register
  3. api.extend('message', MessageExtend);

Use

  1. api.message.send('to', 'content')
  2. .then(function (result) {
  3. // ..
  4. }).catch(function (err) {
  5. // ..
  6. });
  7. // or
  8. // await api.message.send('openId', 'content');

License

MIT