diff --git a/.travis.yml b/.travis.yml index 8798947..d81cf1d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,10 @@ language: node_js node_js: - - '4.2.2' + - 'v6.17.0' before_script: - npm install - + script: "npm test" after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" diff --git a/README.md b/README.md index 920ebc3..202b68b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # yhsd-api-node -友好速搭 API SDK for Node(v4.9.1) +友好速搭 API SDK for Node(>= v6) 支持`Promise`依赖[bluebird](https://github.com/petkaantonov/bluebird)。 diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..aae52f5 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,58 @@ +interface ApiOption { + host?: string; + protocol?: string; + getRequestCount?: (token: string) => any; + saveRequestCount?: (token: string) => void; +} + +interface AuthOption { + appKey: string; + appSecret: string; + callbackUrl?: string; + redirectUrl?: string; + scope?: string[]; + private?: boolean; +} + +declare module 'yhsd-api' { + class Auth { + constructor(options: AuthOption); + + // 验证 Hmac + verifyHmac(queryObj: object): boolean; + + // 获取应用授权页面地址,用于开放应用 + getAuthorizeUrl(shopKey: string, state: string): string; + + // 获取 token + getToken(code?: string): Promise; + } + + class Api { + + constructor(token: string, option?: ApiOption); + + // 发送 GET 请求 + get(path: string, query?: object): Promise; + + // 发送 PUT 请求 + put(path: string, data?: object): Promise; + + // 发送 POST 请求 + post(path: string, data?: object): Promise; + + // 发送 DELETE 请求 + delete(path: string): Promise; + + // 请求函数 + request(method: string, path: string, params?: object): Promise + } + + class WebHook { + constructor(webHookToken: string); + + // 验证WebHook Hmac + verifyHmac(hmac: string, bodyData: string): boolean; + } +} + diff --git a/lib/Auth.js b/lib/Auth.js index 4bb112f..9e2e119 100644 --- a/lib/Auth.js +++ b/lib/Auth.js @@ -89,7 +89,7 @@ Auth.prototype = { if (this.private) { - headers.Authorization = 'Basic ' + new Buffer(this.app_key + ':' + this.app_secret).toString('base64'); + headers.Authorization = 'Basic ' + Buffer.from(this.app_key + ':' + this.app_secret).toString('base64'); params = { grant_type: 'client_credentials' }; diff --git a/package.json b/package.json index 112cd12..d9fe9ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "yhsd-api", - "version": "2.0.0", + "version": "2.1.0", "description": "Youhaosuda API SDK for node.", "main": "index.js", "repository": { @@ -25,6 +25,9 @@ "mocha": "^2.3.4", "should": "^8.0.2" }, + "engines": { + "node": ">= 6.0.0" + }, "dependencies": { "bluebird": "^3.5.1" }