# wechat-component

> Wechat component Node.js SDK

Latest version **2.0.1** (published 2018-02-13) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install wechat-component
pnpm add wechat-component
yarn add wechat-component
bun add wechat-component
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2018-02-13 |
| First published | 2015-07-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 13.9 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Lin Lou |
| Maintainers | loulin |
| Keywords | wechat, component, wechat-api |

## Links

- npm: https://www.npmjs.com/package/wechat-component
- Repository: https://github.com/loulin/wechat-component
- Homepage: https://github.com/loulin/wechat-component#readme
- Issues: https://github.com/loulin/wechat-component/issues
- npm.io page: https://npm.io/package/wechat-component

## Dependencies (2)

- [request](https://npm.io/package/request.md) ^2.83.0
- [request-promise](https://npm.io/package/request-promise.md) ^4.2.2

## Recent versions

- 2.0.1 (latest) — 2018-02-13
- 2.0.0 — 2017-12-24
- 0.0.7 — 2016-10-27
- 0.0.6 — 2015-12-08
- 0.0.5 — 2015-11-24
- 0.0.4 — 2015-11-23
- 0.0.2 — 2015-09-18
- 0.0.1 — 2015-07-29

## README

# wechat-component

## Usage

### Basic
```js
const WechatComponent = require('wechat-component');

const component = new WechatComponent({
  appid: '<WECHAT_COMPONENT_APPID>',
  appsecret: '<WECHAT_COMPONENT_APPSECRET>',
  getComponentTicketAsync: async () => ... // get component ticket from cache/database;
  getTokenAsync: async () => ... // get access token from cache/database;
  saveTokenAsync: async token => ... // save access token to cache/database;
});

// Step 1
const authorizeURL = await component.getAuthorizeURL(redirectURI);
=> redirect to authorizeURL

// Step 2
const result = await component.queryAuth(code);
=> result.authorization_info.authorizer_access_token

// Step 3
const authorizerAppid = result.authorization_info.authorizer_appid;
=> await component.getAuthorizerInfo(authorizerAppid);
```

### Wrap wechat-api
```js
const util = require('util');
const API = require('wechat-api');
const api = new API(authorizerAppid, null, getToken, saveToken);

api.getAccessToken = async (callback) => {
  try {
    api.getTokenAsync = util.promisify(api.getToken);
    api.saveTokenAsync = util.promisify(api.saveToken);

    const refresh = await api.getTokenAsync();
    const token = await service.getAuthorizerToken(authorizerAppid, refresh.refreshToken);
    // service should use a Central Control Server

    await api.saveTokenAsync(token);

    callback(null, token);
  } catch (e) {
    callback(e);
  }
};
```

### Wrap wechat-oauth
```js
const OAuth = require('wechat-oauth');
const oauth = new OAuth(authorizerAppid, null, getToken, saveToken);

oauth.getAccessToken = async (code, callback) => {
  const token = await component.getOAuthAccessToken(authorizerAppid, code);

  oauth.saveToken(token.openid, token, err => callback(err, { data: token }));
};

oauth.refreshAccessToken = async (refreshToken, callback) => {
  const token = await component.refreshOAuthAccessToken(authorizerAppid, refreshToken);

  oauth.saveToken(token.openid, token, err => callback(err, { data: token }));
};

oauth.getAuthorizeURL = (redirect, state, scope) => component
  .getOAuthAuthorizeURL(authorizerAppid, redirect, scope, state);
```

---
_Source: https://npm.io/package/wechat-component · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
