diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bde606..0997a3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # 变更历史 +## v0.8.9 (2024-02-12) + +- 取消对`axios/lib/utils`的依赖。 + ## v0.8.8 (2024-01-28) - 升级依赖 `axios@0.21.2-0.27`, `xml2js@0.5-0.6`; diff --git a/lib/decorator.js b/lib/decorator.js index 670dcca..73ff7da 100644 --- a/lib/decorator.js +++ b/lib/decorator.js @@ -96,8 +96,8 @@ class Decorator { Accept: 'text/xml, text/plain, application/x-gzip', }, responseType: 'text', - transformRequest: Transformer.request, - transformResponse: Transformer.response, + transformRequest: [].concat(Transformer.request), + transformResponse: [].concat(Transformer.response), })); } @@ -211,7 +211,7 @@ class Decorator { const instance = axios.create(this.withDefaults({ ...config, - transformResponse: [this.responseVerifier(certs), ...axios.defaults.transformResponse], + transformResponse: [].concat(this.responseVerifier(certs), axios.defaults.transformResponse), })); instance.interceptors.request.use(this.requestInterceptor()); diff --git a/lib/utils.js b/lib/utils.js index 2d0cee5..c5747ad 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,22 +1,107 @@ const { AssertionError } = require('assert'); const { format } = require('util'); -// Discussion on axios/axios#3032, the `utils` is not a public API of axios. -// Using directly may not works in that future release. -const utils = require('axios/lib/utils'); const { version: axiosVersion } = require('axios/package.json'); const { name: pkgName, version: pkgVersion } = require('../package.json'); -// Self extends ... -utils.extend(utils, { +const { isArray } = Array; + +const { isBuffer } = Buffer; + +const { toString } = {}; + +function isString(val) { + return typeof val === 'string'; +} + +function isObject(val) { + return val !== null && typeof val === 'object'; +} + +function isNumber(val) { + return typeof val === 'number'; +} + +function isFunction(val) { + return toString.call(val) === '[object Function]'; +} + +function isStream(val) { + return isObject(val) && isFunction(val.pipe); +} + +function isPlainObject(val) { + if (!isObject(val)) { + return false; + } + + const prototype = Object.getPrototypeOf(val); + return prototype === null || prototype === Object.prototype; +} + +function forEach(obj, fn) { + if (obj === null || typeof obj === 'undefined') { + return; + } + + if (typeof obj !== 'object') { + /* eslint no-param-reassign:0 */ + obj = [obj]; + } + + if (isArray(obj)) { + for (let i = 0, l = obj.length; i < l; i += 1) { + fn.call(null, obj[i], i, obj); + } + } else { + Object.keys(obj).forEach((key) => fn.call(null, obj[key], key, obj)); + } +} + +function merge(...args) { + const result = {}; + function assignValue(val, key) { + if (isPlainObject(result[key]) && isPlainObject(val)) { + result[key] = merge(result[key], val); + } else if (isPlainObject(val)) { + result[key] = merge({}, val); + } else if (isArray(val)) { + result[key] = val.slice(); + } else { + result[key] = val; + } + } + + for (let i = 0, l = args.length; i < l; i += 1) { + forEach(args[i], assignValue); + } + return result; +} + +function isProcessEnv() { + return typeof process !== 'undefined' && toString.call(process) === '[object process]'; +} + +function isProcessFormData(val) { + return isProcessEnv() && isObject(val) && toString.call(val) === '[object FormData]'; +} + +module.exports = { + isString, + isNumber, + isArray, + isObject, + isFunction, + isStream, + isBuffer, + merge, + /** * Similar to `isStandardBrowserEnv`, just check it's running in a Node environment * * @return {boolean} Ture on Node, otherwise false */ - isProcessEnv() { - return typeof process !== 'undefined' && toString.call(process) === '[object process]'; - }, + isProcessEnv, /** * Determine if a value is a `form-data` node module @@ -28,10 +113,7 @@ utils.extend(utils, { * * @returns {boolean} True if value is a `form-data` module, otherwise false */ - isProcessFormData(val) { - // @see {@link https://github.com/TheNorthMemory/wechatpay-axios-plugin/issues/22} - return this.isProcessEnv() && this.isObject(val) && toString.call(val) === '[object FormData]'; - }, + isProcessFormData, /** * Compose the `User-Agent` content @@ -63,6 +145,4 @@ utils.extend(utils, { return error; }, -}); - -module.exports = utils; +}; diff --git a/package.json b/package.json index 588952b..c3edd0e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wechatpay-axios-plugin", - "version": "0.8.8", + "version": "0.8.9", "description": "微信支付APIv2及v3 NodeJS SDK,支持CLI模式请求OpenAPI,支持v3证书下载,v2付款码支付、企业付款、退款,企业微信-企业支付-企业红包/向员工付款,v2&v3 Native支付、扫码支付、H5支付、JSAPI/小程序支付、合单支付...", "main": "index.js", "typings": "index.d.ts", diff --git a/tests/lib/utils.test.js b/tests/lib/utils.test.js index 5333932..54cadbc 100644 --- a/tests/lib/utils.test.js +++ b/tests/lib/utils.test.js @@ -38,11 +38,6 @@ describe('lib/utils', () => { should(utils.merge).be.a.Function(); }); - it('should have `extend` property and be a function', () => { - utils.should.have.property('extend'); - should(utils.extend).be.a.Function(); - }); - describe('utils::isProcessEnv', () => { it('method `isProcessEnv` should be function', () => { should(utils.isProcessEnv).be.a.Function();