From d756595ca0551dbf5f6a9cf0800085308ed8218c Mon Sep 17 00:00:00 2001 From: Alex Figl-Brick Date: Tue, 13 Dec 2022 13:56:22 +0100 Subject: [PATCH] fix: not stringifying data if data logging is disabled (#114) Fixes https://github.com/hg-pyun/axios-logger/issues/102 --- package-lock.json | 4 ++-- src/common/__test__/string-builder.spec.js | 14 ++++++++++++++ src/common/string-builder.ts | 6 ++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index cbe1804..0852605 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "axios-logger", - "version": "2.6.0", + "version": "2.6.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "axios-logger", - "version": "2.6.0", + "version": "2.6.1", "license": "MIT", "dependencies": { "chalk": "^4.1.0", diff --git a/src/common/__test__/string-builder.spec.js b/src/common/__test__/string-builder.spec.js index 2f8425d..cbf329f 100644 --- a/src/common/__test__/string-builder.spec.js +++ b/src/common/__test__/string-builder.spec.js @@ -71,3 +71,17 @@ test('makeStatus should add status', () => { expect(result).toContain('200:OK'); }); + +test('makeData should not stringify data if configured not to', () => { + const config = { + ...getGlobalConfig(), + data: false, + }; + const a = {}; + const b = {}; + a.b = b; + b.a = a; + const sb = new StringBuilder(config); + const result = sb.makeData(a).build(); + expect(result).toEqual(''); +}); diff --git a/src/common/string-builder.ts b/src/common/string-builder.ts index 69fc754..7a92288 100644 --- a/src/common/string-builder.ts +++ b/src/common/string-builder.ts @@ -62,8 +62,10 @@ class StringBuilder { } makeData(data: object) { - const str = typeof data === `string` ? data : JSON.stringify(data); - if(this.config.data && data) this.printQueue.push(str); + if(this.config.data && data) { + const str = typeof data === `string` ? data : JSON.stringify(data); + this.printQueue.push(str); + } return this; }