From e3798e56e4b1f11b42372252aab3e670fb73db89 Mon Sep 17 00:00:00 2001 From: TheNorthMemory Date: Wed, 27 Mar 2024 16:04:43 +0800 Subject: [PATCH] bump to 0.8.13 APIv3s responseVerifier no need verification anymore while the HTTP status is not 20X code. --- CHANGELOG.md | 3 +++ lib/decorator.js | 5 ++++- package.json | 2 +- .../issue-28-ERR_ASSERTION-SYSTEM_ERROR.test.js | 16 +++++++++------- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7ad009..fda87e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # 变更历史 +## v0.8.13 (2024-03-27) +- 针对`APIv3`返回的HTTP status code非20x场景,不再尝试去验签,异常类型从`AssertionError`回退为`AxiosError`; + ## v0.8.12 (2024-03-09) - 修正 `Transformer.toObject` 类型标注错误; - [动态`uri_template`参数](https://github.com/TheNorthMemory/wechatpay-axios-plugin/issues/57)类型标注,感谢 @taoliujun 报告此问题; diff --git a/lib/decorator.js b/lib/decorator.js index 73ff7da..6173c16 100644 --- a/lib/decorator.js +++ b/lib/decorator.js @@ -141,7 +141,10 @@ class Decorator { * @return {function} Named as `verifier` function */ static responseVerifier(certs = {}) { - return function verifier(data, headers) { + return function verifier(data, headers, status) { + /** @since v0.8.13 no need verification anymore while the HTTP status is not 20X code. */ + if (status && this.validateStatus && !this.validateStatus(status)) { return data; } + const { 'wechatpay-timestamp': timestamp, 'wechatpay-nonce': nonce, 'wechatpay-serial': serial, 'wechatpay-signature': signature, } = headers; diff --git a/package.json b/package.json index 162c119..be42758 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wechatpay-axios-plugin", - "version": "0.8.12", + "version": "0.8.13", "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/openapi/issue-28-ERR_ASSERTION-SYSTEM_ERROR.test.js b/tests/openapi/issue-28-ERR_ASSERTION-SYSTEM_ERROR.test.js index d97c3b2..e444754 100644 --- a/tests/openapi/issue-28-ERR_ASSERTION-SYSTEM_ERROR.test.js +++ b/tests/openapi/issue-28-ERR_ASSERTION-SYSTEM_ERROR.test.js @@ -1,6 +1,6 @@ const { readFileSync } = require('fs'); const { join } = require('path'); -const { AssertionError } = require('assert'); +const { AxiosError } = require('axios'); require('should'); const nock = require('nock'); @@ -11,10 +11,12 @@ describe('Issue #28 jsapi 请求路径有误', () => { let scope; let instance; - const mocks = () => JSON.stringify({ + const message = { code: 'SYSTEM_ERROR', message: '系统繁忙,请稍后重试', - }); + }; + + const mocks = () => JSON.stringify(message); beforeEach(() => { // {@link ../../fixtures/README.md} @@ -40,10 +42,10 @@ describe('Issue #28 jsapi 请求路径有误', () => { it('post onto the `/v3/combine-transactions/jsapi` got a `500` SYSTEM_ERROR', async () => { scope.post('/v3/combine-transactions/jsapi').reply(500, mocks); await instance.v3.combineTransactions.jsapi.post({}).catch((resp) => { - resp.should.instanceOf(AssertionError); + resp.should.instanceOf(AxiosError); resp.response.should.be.Object().and.have.keys('headers', 'data'); resp.response.headers.should.be.Object().and.not.have.keys('Wechatpay-Timestamp'); - resp.response.data.should.be.String().and.match(/"SYSTEM_ERROR"/); + resp.response.data.should.be.eql(message); }); }); }); @@ -52,10 +54,10 @@ describe('Issue #28 jsapi 请求路径有误', () => { it('post onto the `/v3/pay/transactions/jsapi` got a `500` SYSTEM_ERROR', async () => { scope.post('/v3/pay/transactions/jsapi').reply(500, mocks); await instance.v3.pay.transactions.jsapi.post({}).catch((resp) => { - resp.should.instanceOf(AssertionError); + resp.should.instanceOf(AxiosError); resp.response.should.be.Object().and.have.keys('headers', 'data'); resp.response.headers.should.be.Object().and.not.have.keys('Wechatpay-Timestamp'); - resp.response.data.should.be.String().and.match(/"SYSTEM_ERROR"/); + resp.response.data.should.be.eql(message); }); }); });