0.0.2 • Published 4 years ago

@xixilive/jssdk v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

wechat's media platform jssdk utilities

Travis npm bundle size npm version Known Vulnerabilities NPM license

Installation

npm i @xixilvie/jssdk

Usage

// express
const app = require('express')()
const {createSdk, Config} = require('@xixilvie/jssdk')

const config = Config({
  apps: [{key: 'app id', secret: 'app secret'}]
})

const sdk = createSdk(config)

app.post('/config/:appid', (req, res, next) => {
  const {appid} = req.params
  const {key, secret} = config.getApp(appid)
  sdk.createConfig(key, secret).then(data => res.json(data))
})

mock for testing

const {mock} = require('@xixilvie/jssdk')

describe('suite', () => {
  let unmock;
  beforeAll(() => {
    unmock = mock()
  })
  afterAll(() => {
    unmock()
  })

  it('test', () => {
    console.log('all http requests for wechat server will be mocked')
  })
})

mock data:

// when fetch access_token from wechat's server, mock response will be:
{access_token: 'access_token', expires_in: 7200}

// when fetch ticket from wechat's server, mock response will be:
{ticket: 'ticket', expires_in: 7200}

API

interface KeyScretPair {
  key: string;
  secret: string;
}

interface Cache {
  type: 'redis' | 'file' | 'memo';
  options: any;
}

interface ConfigData {
  cors?: string[];
  cache?: any;
  realms?: KeyScretPair;
  apps: KeyScretPair;
}

interface ConfigResponse {
  appId: string;
  timestamp: number;
  nonceStr: string;
  signature: string;
  [property: string]?: any;
}

class Config {
  static load(file:string): Config;
  constructor(data: ConfigData);
  get apps: KeyScretPair[];
  realms: KeyScretPair[];
  cache: Cache;
  getApp(key: string): KeyScretPair;
  getRealm(key: string): KeyScretPair;
  isAllowedOrigin(origin: string): bool;
}

class Sdk {
  constructor(config: Config);
  getAccessToken(key: string, secret: string): Promise<any>;
  getAccessTicket(key: string, secret: string): Promise<any>;
  verifyRealmSign(key: string, payload: string, sign: string): bool;
  createConfig(key: string, secret: string, params: any): Promise<ConfigResponse>;
}
0.0.2

4 years ago

0.0.1

4 years ago