[Poly > Request] Further cleanup tweaks
This commit is contained in:
parent
197f785edf
commit
5949633d03
1 changed files with 2 additions and 9 deletions
|
@ -2,15 +2,13 @@ const https = require('https');
|
|||
const querystring = require("querystring");
|
||||
|
||||
// Generic polyfill for "request" npm package, wrapper for https
|
||||
const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => new Promise((resolve, reject) => {
|
||||
const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => new Promise((resolve) => {
|
||||
const fullUrl = `${url}${qs != null ? `?${querystring.stringify(qs)}` : ''}`; // With query string
|
||||
|
||||
let req;
|
||||
try {
|
||||
req = https.request(fullUrl, { method, headers, timeout }, async (res) => {
|
||||
if (res.statusCode === 301 || res.statusCode === 302) { // Redirect, recall function
|
||||
return resolve(await nodeReq({ url: res.headers.location, method, headers, timeout, body, stream }));
|
||||
}
|
||||
if (res.statusCode === 301 || res.statusCode === 302) return resolve(await nodeReq({ url: res.headers.location, method, headers, timeout, body, stream }));
|
||||
|
||||
resolve(res);
|
||||
});
|
||||
|
@ -26,11 +24,6 @@ const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => new Pro
|
|||
});
|
||||
|
||||
const request = (...args) => {
|
||||
// We have to use ...args because we have to support all of these possible args:
|
||||
// request(url, callback)
|
||||
// request(options, callback)
|
||||
// request(url, options, callback)
|
||||
|
||||
let options, callback;
|
||||
switch (args.length) {
|
||||
case 3: // request(url, options, callback)
|
||||
|
|
Loading…
Reference in a new issue