0.1.1 • Published 4 years ago

@axolo/node-dingtalk v0.1.1

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

node-dingtalk

Dingtalk OpenAPI Node.js SDK.

Install

yarn add @axolo/node-dingtalk

API

constructor(config)

params

corp app

configrequireddefaultdescription
appModecorpcorp = corp internal app, isv = isv app
appTypeeappeapp = mini app, h5 = web app
agentIdtrueagentId
appKeytrueappKey
appSecrettrueappSecret

isv app

configrequireddefaultdescription
appModeisvcorpcorp = corp internal app, isv = isv app
appTypeeappeapp = mini app, h5 = web app
appIdtrueappId
suiteIdtruedingtalk cloud push set suiteTicket
suiteKeytruesuiteKey
suiteSecrettruesuiteSecret
eventTokentruehttp event callback encrypt token
eventAesKeytruehttp event callback encrypt aesKey

builtin config

configdescription
axiosHTTP Client, use axios
cacheManagercache, use cache-manager
cachecache setting
baseUrlbase url of Dingtalk OpenAPI
corpAppAuthTokenUrlget access token url of corp app
isvAppAuthTokenUrlget access token url of isv app
isvAppAuthInfoUrlget auth info url
isvAppAgentUrlget agent info url

return

A instance of Dingtalk OpenAPI Node.js SDK.

execute(api, request = {}, scope = {})

more request options see axios.

params

parmasdescription
apiquerystring, Dingtalk OpenAPI
request.methodHTTP Method
request.paramsHTTP querystring as Object by GET
request.bodyHTTP body as Object by POST/PATCH/PUT
scope.corpIdto get accessToken per corp of isv app

return

Get data or throw dingtalk error from Dingtalk OpenAPI.

callback({ signature, timestamp, nonce, encrypt })

See http event callback for help. This method use as middleware usualy.

params

paramsdescription
signaturesignature string
timestamptimestamp string
noncenonce string
encryptencrypt string

return

event decrypted of callback. and response is encrypted response for callback success.

Example

request

const DingtalkSdk = require('@axolo/node-dingtalk');

const config = {
  agentId: 'AGENT_ID',
  appKey: 'APP_KEY',
  appSecret: 'APP_SECRET',
};
const dingtalkSdk = new DingtalkSdk(config);

const request = {
  url: '/user/getuserinfo',
  body: { code: 'authcode' },
};
dingtalk
  .execute(request)
  .catch(err => console.log(err))
  .then(res => console.log(res));

Test

yarn test ./test/config.test.js   # test config
yarn test ./test/execute.test.js  # test execute

TIP: Please create .env and .env.test in project root before test.

.env

for corp app

agentId = AGENT_ID
appKey = APP_KEY
appSecret = APP_SECRET

for isv app

appMode = isv
appType = h5
appId = APP_ID
# suiteId is required of Dingtalk Cloud Push
suiteId = SUITE_ID
suiteKey = SUITE_KEY
suiteSecret = SUITE_SECRET
# eventToken and eventAesKey is required of HTTP Event Callback
eventToken = EVENT_TOKEN
eventAesKey = EVENT_AES_KEY

.env.test

## http server for http event callback
httpPort = 7001

## mysql rds for dingtalk cloud push
rdsHost = your.mysql.host
rdsPort = 3306
rdsUser = user
rdsPassword = password
rdsDatabase = ding_cloud_push

# get corpId and appId from dingtalk-jsapi or querystring
corpId = CROP_ID

TODO

  • test: Assertion Testing with Mocha or Jest.
  • cache: class DingtalkSdkCache, support memory, redis, mysql, etc.
  • props: DingtalkSdk.event, DingtalkSdk.error.

Thanks

Yueming Fang