[Polyfill > Request] Pass body properly, add get method func
This commit is contained in:
parent
79a36dfb4b
commit
4c23ecac71
1 changed files with 16 additions and 4 deletions
|
@ -31,7 +31,7 @@ const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => {
|
|||
});
|
||||
};
|
||||
|
||||
module.exports = (options, callback) => {
|
||||
const request = (options, callback) => { // Main function
|
||||
if (typeof options === 'string') {
|
||||
options = {
|
||||
url: options
|
||||
|
@ -42,8 +42,15 @@ module.exports = (options, callback) => {
|
|||
|
||||
const listener = {};
|
||||
|
||||
nodeReq(options).then((res) => { // No error handling because yes
|
||||
if (callback) callback(undefined, res, res.body);
|
||||
nodeReq(options).then(async (res) => { // No error handling because yes
|
||||
let body = '';
|
||||
res.setEncoding('utf8');
|
||||
|
||||
res.on('data', (chunk) => body += chunk);
|
||||
|
||||
await new Promise((resolve) => res.on('end', resolve)); // Wait to read full body
|
||||
|
||||
if (callback) callback(undefined, res, body);
|
||||
if (listener['response']) listener['response'](res);
|
||||
});
|
||||
|
||||
|
@ -52,4 +59,9 @@ module.exports = (options, callback) => {
|
|||
listener[type] = handler;
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Method functions
|
||||
request.get = (url, callback) => request({ url: url, method: 'GET' }, callback);
|
||||
|
||||
module.exports = request;
|
Loading…
Reference in a new issue