youzanyun-sdk-nodejs
Advanced tools
Comparing version 2.0.0-beta.17 to 2.0.0-beta.18
@@ -15,4 +15,2 @@ "use strict"; | ||
const Breakdown = require('./breakdown'); | ||
const { | ||
@@ -45,3 +43,2 @@ OPEN_HOST | ||
this._tokenCache = new TokenCache(); | ||
this._breakdown = new Breakdown(); | ||
@@ -69,4 +66,5 @@ const { | ||
}; | ||
} | ||
} // 获取token | ||
async getToken(kdtId) { | ||
@@ -82,5 +80,4 @@ if (!kdtId) { | ||
const tokenInfo = this._tokenCache.get(CLIENT_ID, kdtId, this.logger); | ||
const tokenInfo = this._tokenCache.get(CLIENT_ID, kdtId); | ||
this.logger.info('dktest-缓存中tokenInfo', tokenInfo); | ||
const { | ||
@@ -92,3 +89,2 @@ expires, | ||
if (token && +new Date() < expires) { | ||
this.logger.info('dktest-取用缓存token', token); | ||
return token; | ||
@@ -101,3 +97,3 @@ } // 重新获取token | ||
type: TYPE.CLOUD_FUNCTION | ||
}, this.logger); // 缓存token | ||
}); // 缓存token | ||
@@ -121,4 +117,3 @@ this._tokenCache.set(CLIENT_ID, kdtId, data); | ||
callApi(apiParams, axiosConfig, logger) { | ||
this.logger = logger; | ||
callApi(apiParams, axiosConfig) { | ||
return new Promise((resolve, reject) => { | ||
@@ -136,6 +131,4 @@ const config = { | ||
config.success = response => { | ||
this.logger.info('dktest-success', response); | ||
const { | ||
code, | ||
data, | ||
gw_err_resp | ||
@@ -148,3 +141,3 @@ } = response.data || {}; | ||
if (CODE_SUCCESS.includes(+code)) { | ||
resolve(data); | ||
resolve(response.data); | ||
} else if (CODE_INVALID.includes(+err_code) && config.retryCount <= MAX_RETRIES.TOKEN_INVALID) { | ||
@@ -158,6 +151,3 @@ // 重试请求 | ||
config.fail = error => { | ||
this.logger.info('dktest-fail', error.response); | ||
reject(error); | ||
}; // 初次发送请求 | ||
config.fail = error => reject(error); // 初次发送请求 | ||
@@ -171,3 +161,2 @@ | ||
async invoke(config = {}) { | ||
this.logger.info('dktest-invoke-请求进来了', config.retryCount); | ||
const { | ||
@@ -201,4 +190,3 @@ apiParams = {}, | ||
} catch (error) { | ||
this.logger.info('dktest-invoke-结束请求', error); // 结束请求 | ||
// 结束请求 | ||
return fail(error); | ||
@@ -215,3 +203,3 @@ } | ||
}, axiosConfig) { | ||
const url = `https://${this._openHost}/api/${api}/${version}?access_token=0a2f29d15fe39ca1558ba333e3bc11e`; | ||
const url = `https://${this._openHost}/api/${api}/${version}?access_token=${token}`; | ||
@@ -233,3 +221,3 @@ if (files && Object.keys(files).length > 0) { | ||
} | ||
}), this.logger); | ||
})); | ||
return response; | ||
@@ -259,3 +247,3 @@ } | ||
}, form.getHeaders()) | ||
}), this.logger); | ||
})); | ||
return response; | ||
@@ -262,0 +250,0 @@ } |
@@ -54,4 +54,3 @@ "use strict"; | ||
request(options, logger) { | ||
this.logger = logger; | ||
request(options) { | ||
let { | ||
@@ -65,6 +64,4 @@ url, | ||
if (!this.enableProxy || this.ignoreHostSet.has(requestUrl.hostname)) { | ||
this.logger.info('dktest-不代理'); | ||
res = this._doRequest(options); | ||
} else { | ||
this.logger.info('dktest-代理'); | ||
let scheme = requestUrl.protocol.substr(0, requestUrl.protocol.length - 1); | ||
@@ -95,3 +92,2 @@ let headers = Object.assign(options.headers || {}, { | ||
console.log('config', config); | ||
this.logger.info('dktest-request请求配置', config); | ||
let res = axios.request(config); | ||
@@ -98,0 +94,0 @@ return res; |
@@ -23,6 +23,5 @@ "use strict"; | ||
get(clientId, kdtId, logger) { | ||
get(clientId, kdtId) { | ||
const currentTokenInfoName = getTokenCacheName(clientId, kdtId); | ||
if (!currentTokenInfoName) return false; | ||
logger.info('dktest-缓存中所有token信息', this.store); | ||
@@ -29,0 +28,0 @@ if (this.store[currentTokenInfoName]) { |
@@ -81,3 +81,3 @@ "use strict"; | ||
refreshToken | ||
}, logger, _axiosConfig = {}) { | ||
}, _axiosConfig = {}) { | ||
let params = { | ||
@@ -119,7 +119,5 @@ client_id: this.clientId, | ||
console.log('getToken requestParams', requestParams, _axiosConfig); | ||
logger.info('dktest-getToken请求参数', requestParams); | ||
let response = await this.httpClient.request(requestParams, logger); | ||
let response = await this.httpClient.request(requestParams); | ||
if (response.data.success) { | ||
logger.info('dktest-getToken请求响应', response.data.data); | ||
return response.data.data; | ||
@@ -126,0 +124,0 @@ } else { |
{ | ||
"name": "youzanyun-sdk-nodejs", | ||
"version": "2.0.0-beta.17", | ||
"version": "2.0.0-beta.18", | ||
"description": "有赞云 nodejs sdk", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
const TokenService = require('./token'); | ||
const FormData = require('form-data'); | ||
const TokenCache = require('./token-cache'); | ||
const Breakdown = require('./breakdown'); | ||
const { OPEN_HOST } = require('./config'); | ||
@@ -16,3 +15,2 @@ const packageJson = require('../package.json'); | ||
this._tokenCache = new TokenCache(); | ||
this._breakdown = new Breakdown(); | ||
const { CLIENT_ID, CLIENT_SECRET } = this._getCurrentSecret(); | ||
@@ -34,2 +32,3 @@ this._tokenService = new TokenService({ | ||
// 获取token | ||
async getToken(kdtId) { | ||
@@ -43,9 +42,5 @@ if (!kdtId) { | ||
// 从缓存中获取token | ||
const tokenInfo = this._tokenCache.get(CLIENT_ID, kdtId, this.logger); | ||
this.logger.info('dktest-缓存中tokenInfo', tokenInfo); | ||
const tokenInfo = this._tokenCache.get(CLIENT_ID, kdtId); | ||
const { expires, token } = tokenInfo || {}; | ||
if (token && +new Date() < expires) { | ||
this.logger.info('dktest-取用缓存token', token); | ||
return token; | ||
@@ -55,6 +50,3 @@ } | ||
// 重新获取token | ||
const data = await this._tokenService.getToken( | ||
{ kdtId, type: TYPE.CLOUD_FUNCTION }, | ||
this.logger, | ||
); | ||
const data = await this._tokenService.getToken({ kdtId, type: TYPE.CLOUD_FUNCTION }); | ||
// 缓存token | ||
@@ -76,4 +68,3 @@ this._tokenCache.set(CLIENT_ID, kdtId, data); | ||
*/ | ||
callApi(apiParams, axiosConfig, logger) { | ||
this.logger = logger; | ||
callApi(apiParams, axiosConfig) { | ||
return new Promise((resolve, reject) => { | ||
@@ -87,8 +78,7 @@ const config = { apiParams, axiosConfig }; | ||
config.success = response => { | ||
this.logger.info('dktest-success', response); | ||
const { code, data, gw_err_resp } = response.data || {}; | ||
const { code, gw_err_resp } = response.data || {}; | ||
const { err_code } = gw_err_resp || {}; | ||
if (CODE_SUCCESS.includes(+code)) { | ||
resolve(data); | ||
resolve(response.data); | ||
} else if ( | ||
@@ -104,8 +94,4 @@ CODE_INVALID.includes(+err_code) && | ||
}; | ||
config.fail = error => { | ||
this.logger.info('dktest-fail', error.response); | ||
config.fail = error => reject(error); | ||
reject(error); | ||
}; | ||
// 初次发送请求 | ||
@@ -118,4 +104,2 @@ request(); | ||
async invoke(config = {}) { | ||
this.logger.info('dktest-invoke-请求进来了', config.retryCount); | ||
const { apiParams = {}, axiosConfig = {}, success, fail } = config; | ||
@@ -142,4 +126,2 @@ const { api, version, params, files, kdtId } = apiParams; | ||
} catch (error) { | ||
this.logger.info('dktest-invoke-结束请求', error); | ||
// 结束请求 | ||
@@ -151,3 +133,3 @@ return fail(error); | ||
async _call({ api, version, params, files, token }, axiosConfig) { | ||
const url = `https://${this._openHost}/api/${api}/${version}?access_token=0a2f29d15fe39ca1558ba333e3bc11e`; | ||
const url = `https://${this._openHost}/api/${api}/${version}?access_token=${token}`; | ||
@@ -162,14 +144,11 @@ if (files && Object.keys(files).length > 0) { | ||
async _requestJson(url, data, axiosConfig = {}) { | ||
let response = await this._httpClient.request( | ||
{ | ||
...axiosConfig, | ||
url, | ||
method: 'post', | ||
data, | ||
headers: { | ||
'User-Agent': `youzanyun-sdk-nodejs ${packageJson.version}-Node`, | ||
}, | ||
let response = await this._httpClient.request({ | ||
...axiosConfig, | ||
url, | ||
method: 'post', | ||
data, | ||
headers: { | ||
'User-Agent': `youzanyun-sdk-nodejs ${packageJson.version}-Node`, | ||
}, | ||
this.logger, | ||
); | ||
}); | ||
return response; | ||
@@ -189,18 +168,15 @@ } | ||
let response = await this._httpClient.request( | ||
{ | ||
...axiosConfig, | ||
url, | ||
method: 'post', | ||
data: form, | ||
headers: Object.assign( | ||
{}, | ||
{ | ||
'User-Agent': `youzanyun-sdk-nodejs ${packageJson.version}-NodeJs`, | ||
}, | ||
form.getHeaders(), | ||
), | ||
}, | ||
this.logger, | ||
); | ||
let response = await this._httpClient.request({ | ||
...axiosConfig, | ||
url, | ||
method: 'post', | ||
data: form, | ||
headers: Object.assign( | ||
{}, | ||
{ | ||
'User-Agent': `youzanyun-sdk-nodejs ${packageJson.version}-NodeJs`, | ||
}, | ||
form.getHeaders(), | ||
), | ||
}); | ||
return response; | ||
@@ -207,0 +183,0 @@ } |
@@ -50,4 +50,3 @@ const https = require('https'); | ||
*/ | ||
request(options, logger) { | ||
this.logger = logger; | ||
request(options) { | ||
let { url, baseURL } = options; | ||
@@ -59,8 +58,4 @@ | ||
if (!this.enableProxy || this.ignoreHostSet.has(requestUrl.hostname)) { | ||
this.logger.info('dktest-不代理'); | ||
res = this._doRequest(options); | ||
} else { | ||
this.logger.info('dktest-代理'); | ||
let scheme = requestUrl.protocol.substr(0, requestUrl.protocol.length - 1); | ||
@@ -89,4 +84,2 @@ let headers = Object.assign(options.headers || {}, { | ||
console.log('config', config); | ||
this.logger.info('dktest-request请求配置', config); | ||
let res = axios.request(config); | ||
@@ -93,0 +86,0 @@ return res; |
@@ -17,6 +17,5 @@ function getTokenCacheName(clientId, kdtId) { | ||
get(clientId, kdtId, logger) { | ||
get(clientId, kdtId) { | ||
const currentTokenInfoName = getTokenCacheName(clientId, kdtId); | ||
if (!currentTokenInfoName) return false; | ||
logger.info('dktest-缓存中所有token信息', this.store); | ||
@@ -23,0 +22,0 @@ if (this.store[currentTokenInfoName]) { |
@@ -72,3 +72,2 @@ const config = require('./config'); | ||
}, | ||
logger, | ||
_axiosConfig = {}, | ||
@@ -109,8 +108,4 @@ ) { | ||
console.log('getToken requestParams', requestParams, _axiosConfig); | ||
logger.info('dktest-getToken请求参数', requestParams); | ||
let response = await this.httpClient.request(requestParams, logger); | ||
let response = await this.httpClient.request(requestParams); | ||
if (response.data.success) { | ||
logger.info('dktest-getToken请求响应', response.data.data); | ||
return response.data.data; | ||
@@ -117,0 +112,0 @@ } else { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31572
25
985