Skip to content

Commit

Permalink
fix: traceId should only appear in debug mode (#113)
Browse files Browse the repository at this point in the history
有些业务的默认字段会带 traceId
  • Loading branch information
troyeagle authored Jun 1, 2024
1 parent c7f236b commit e1d448e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 7 additions & 4 deletions lib/alipay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import { getSNFromPath, getSN, loadPublicKey, loadPublicKeyFromPath } from './an
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require('../package.json');

export function isDebugMode() {
return !!process.env.ALIPAY_SDK_DEBUG;
}
/**
* @interface AlipaySdkConfig SDK 配置
*/
Expand Down Expand Up @@ -250,8 +253,8 @@ class AlipaySdk {
const validateSuccess = option.validateSign ? this.checkResponseSign(body, responseKey) : true;
if (validateSuccess) {
const result = config.camelcase ? camelcaseKeys(data, { deep: true }) : data;
if (result && traceId) {
result.traceId = traceId;
if (result && traceId && method !== 'GET' && isDebugMode()) {
result.__traceId__ = traceId;
}
return result;
}
Expand Down Expand Up @@ -497,8 +500,8 @@ class AlipaySdk {

if (validateSuccess) {
const result = config.camelcase ? camelcaseKeys(data, { deep: true }) : data;
if (result && traceId) {
result.traceId = traceId;
if (result && traceId && isDebugMode()) {
result.__traceId__ = traceId;
}
return resolve(result);
}
Expand Down
8 changes: 6 additions & 2 deletions test/alipay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const GATE_WAY = 'https://openapi-sandbox.dl.alipaydev.com/gateway.do';
describe('sdk', function() {
afterEach(function() {
sandbox.restore();
delete process.env.ALIPAY_SDK_DEBUG;
});

describe('config error', function() {
Expand Down Expand Up @@ -197,6 +198,7 @@ describe('sdk', function() {
});

it('config.camelcase is true', function(done) {
process.env.ALIPAY_SDK_DEBUG = 1;
sandbox.stub(urllib, 'request', function() {
return Promise.resolve({
status: 200,
Expand All @@ -214,7 +216,7 @@ describe('sdk', function() {
},
publicArgs: {},
}, { validateSign: true }).then(function(data){
data.should.eql({ aB: 1, cD: 2, traceId: 'mock-trace-id' });
data.should.eql({ aB: 1, cD: 2, __traceId__: 'mock-trace-id' });
done();
})
});
Expand Down Expand Up @@ -432,8 +434,10 @@ describe('sdk', function() {
form.addField('biz_code', 'openpt_appstore');
form.addFile('file_content', '图片.jpg', filePath);


this.timeout(20000);

process.env.ALIPAY_SDK_DEBUG = 1;
sdk
.exec('alipay.open.file.upload', {
}, { log, formData: form, validateSign: true })
Expand All @@ -449,7 +453,7 @@ describe('sdk', function() {
// include trace_id
infoLog[1].should.match(/,"trace_id":"\w+",/);
errorLog.should.eql([]);
ret.traceId.length.should.eql(29);
ret.__traceId__.length.should.eql(29);

done();
}).catch(done)
Expand Down

0 comments on commit e1d448e

Please sign in to comment.