[Poly > Request] Further cleanup tweaks

This commit is contained in:
Ducko 2022-01-29 10:09:55 +00:00
parent 197f785edf
commit 5949633d03

View file

@ -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)