Skip to content

Commit

Permalink
converts result JSON, if isJson option is true
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetonurslmz committed Nov 14, 2021
1 parent 4891287 commit b55ce91
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit b55ce91

Please sign in to comment.