From b55ce9154e24cd0bd70ec7b9a21fa6129d1c9d10 Mon Sep 17 00:00:00 2001 From: ahmetonurslmz Date: Sun, 14 Nov 2021 18:58:16 +0300 Subject: [PATCH] converts result JSON, if isJson option is true --- index.js | 9 +++++++-- package.json | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 141959c..ece70a3 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ const pathChecker = (path) => { }; -const request = async ({ hostname, path = '/', method = 'GET', payload = null }) => { +const request = async ({ hostname, path = '/', method = 'GET', payload = null, isJson = true }) => { const isHttps = hostname.startsWith('https://'); const lib = isHttps ? https : http; @@ -40,7 +40,12 @@ const request = async ({ hostname, path = '/', method = 'GET', payload = null }) data.push(chunk); }); - res.on('end', () => resolve(Buffer.concat(data).toString())); + res.on('end', () => resolve((() => { + if (isJson) { + return JSON.parse(Buffer.concat(data).toString()); + } + return Buffer.concat(data).toString(); + })())); }); req.on('error', reject); diff --git a/package.json b/package.json index 8ebd463..962ab9e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "smoothly-request", - "version": "1.0.8", + "version": "1.0.9", "description": "Perform request with promise based client on NodeJS", "main": "index.js", "scripts": {