[Poly > Request] Cleanup source a bit
This commit is contained in:
parent
6214ae0a24
commit
bdfef85bb0
|
@ -2,12 +2,11 @@ const https = require('https');
|
||||||
|
|
||||||
// Generic polyfill for "request" npm package, wrapper for https
|
// Generic polyfill for "request" npm package, wrapper for https
|
||||||
const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => new Promise((resolve) => {
|
const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => new Promise((resolve) => {
|
||||||
const fullUrl = `${url}${qs != null ? `?${(new URLSearchParams(qs)).toString()}` : ''}`; // With query string
|
|
||||||
|
|
||||||
let req;
|
let req;
|
||||||
try {
|
try {
|
||||||
req = https.request(fullUrl, { method, headers, timeout }, async (res) => {
|
req = https.request(url + (qs != null ? `?${(new URLSearchParams(qs)).toString()}` : ''), { method, headers, timeout }, async (res) => {
|
||||||
if (res.statusCode === 301 || res.statusCode === 302) return resolve(await nodeReq({ url: res.headers.location, method, headers, timeout, body, stream }));
|
const loc = res.headers.location;
|
||||||
|
if (loc) return resolve(await nodeReq({ url: loc, method, headers, timeout, body, stream }));
|
||||||
|
|
||||||
resolve(res);
|
resolve(res);
|
||||||
});
|
});
|
||||||
|
@ -50,9 +49,7 @@ const request = (...args) => {
|
||||||
nodeReq(options).then(async (res) => {
|
nodeReq(options).then(async (res) => {
|
||||||
if (!res.statusCode) {
|
if (!res.statusCode) {
|
||||||
listeners['error']?.(res);
|
listeners['error']?.(res);
|
||||||
callback?.(res, null, null);
|
return callback?.(res, null, null);
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
listeners['response']?.(res);
|
listeners['response']?.(res);
|
||||||
|
|
Loading…
Reference in New Issue