Socket
Book a DemoInstallSign in
Socket

koa-weixin-jssdk

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-weixin-jssdk

Koa weixin jssdk middleware

latest
Source
npmnpm
Version
2.0.1
Version published
Maintainers
1
Created
Source

koa-weixin-jssdk

Build Status

koa weixin jssdk middleware

Quick Start

import koa from 'koa';
import weixinJSSDK from '../src';

const port = process.env.PORT || 3000;
const app = koa();

app.use(weixinJSSDK({
    appId: '<YOUR_APP_ID>', // [required] weixin-jssdk app id
    
    secret: '<YOUR_SECRET>', // weixin-jssdk secret
 
    pathName: '/jssdk', // [optional] eg: http://imyourfather.com/jssdk

    onError: (err, ctx, next) => {
        console.error(err);
        ctx.body = 'error';
    },
}));

app.listen(port);

Advanced Usage

Custom store access_token and ticket

By default, it would cache access_token and ticket in runtime memory, but you can store them in somewhere else.

app.use(weixinJSSDK({
    appId: '<YOUR_APP_ID>',
    secret: '<YOUR_SECRET>',
    async onGetToken(url) {
        return redis.getAsync('ACCESS_TOKEN'); // this is an example
    },
    async onSetToken(token, expiresIn) {
        return redis.setSync('ACCESS_TOKEN', token, { expiresIn });
    },
    async onGetTicket(url) {
        return redis.getAsync('TICKET');
    },
    async onSetTicket(ticket, expiresIn) {
        return redis.setSync('TICKET', ticket, { expiresIn });
    },
    // other configs...
}));
Third-party weixin service

Maybe you already have a Third-party weixin service and have a access token, you could use custom fetchTicket or fetchToken function instead of secret.

app.use(weixinJSSDK({
    appId: '<YOUR_APP_ID>', // [required]
    async fetchToken() {
        // await fetch(...)
        return { access_token: '<MY_ACCESS_TOKEN>', expires_in: 7200 };
    },
    // other configs...
}));

Or fetchTicket

app.use(weixinJSSDK({
    appId: '<YOUR_APP_ID>', // [required]
    async fetchTicket() {
        // await fetch(...)
        return { ticket: 'TICKET', expires_in: 7200 };
    },
    // other configs...
}));

Installation

Using npm:

$ npm install koa-weixin-jssdk --save

License

MIT

Keywords

weixin

FAQs

Package last updated on 01 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts