Skip to content

Commit

Permalink
bump to 0.8.9
Browse files Browse the repository at this point in the history
copy-paste some functions from `axios/lib/utils` into `./lib/utils`
  • Loading branch information
TheNorthMemory committed Feb 12, 2024
1 parent 06c87e5 commit 21f42ea
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`;
Expand Down
6 changes: 3 additions & 3 deletions lib/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}));
}

Expand Down Expand Up @@ -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());
Expand Down
110 changes: 95 additions & 15 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -63,6 +145,4 @@ utils.extend(utils, {

return error;
},
});

module.exports = utils;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 0 additions & 5 deletions tests/lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 21f42ea

Please sign in to comment.