From 6775040a3757c9fb39fce9f8154b4f6a0863037e Mon Sep 17 00:00:00 2001 From: Oj Date: Sat, 15 Jan 2022 11:53:28 +0000 Subject: [PATCH] [Poly > Request] Cleanup some source --- polyfills/request.js | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/polyfills/request.js b/polyfills/request.js index 0198efa..a646249 100644 --- a/polyfills/request.js +++ b/polyfills/request.js @@ -7,21 +7,9 @@ const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => new Pro let req; try { - req = https.request(fullUrl, { - method, - headers, - timeout - }, async (res) => { + 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, - qs: null, - method, - headers, - timeout, - body, - stream - })); + return resolve(await nodeReq({ url: res.headers.location, method, headers, timeout, body, stream })); } resolve(res); @@ -37,13 +25,11 @@ const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => new Pro req.end(); }); -const request = (...args) => { // Main function - // We have to use ...args because fun fact! request supports both: +const request = (...args) => { + // We have to use ...args because we have to support all of these possible args: // request(url, callback) // request(options, callback) - // ^ These are fine as they have the same number of arguments, however it also supports: // request(url, options, callback) - // ...I know, right let options, callback; switch (args.length) { @@ -67,18 +53,17 @@ const request = (...args) => { // Main function }; } - // log('Polyfill > Request', options.method, options.url); console.log('[OpenAsar Request Polyfill]', options.url); const listeners = {}; - nodeReq(options).then(async (res) => { // No error handling because yes + nodeReq(options).then(async (res) => { const isError = !res.statusCode; if (isError) { console.log('[OpenAsar Request Polyfill] Error:', res); listeners['error']?.(res); - callback?.(res, null, null); // Return null for others? + callback?.(res, null, null); return; } @@ -107,14 +92,9 @@ const request = (...args) => { // Main function return ret; }; -// Method functions -request.get = (url, callback) => request({ url: url, method: 'GET' }, callback); -request.post = (url, callback) => request({ url: url, method: 'POST' }, callback); -request.put = (url, callback) => request({ url: url, method: 'PUT' }, callback); -request.patch = (url, callback) => request({ url: url, method: 'PATCH' }, callback); -request.delete = (url, callback) => request({ url: url, method: 'DELETE' }, callback); -request.del = request.delete; // Random shortened func because request -request.head = (url, callback) => request({ url: url, method: 'HEAD' }, callback); -request.options = (url, callback) => request({ url: url, method: 'OPTIONS' }, callback); +for (const m of [ 'get', 'post', 'put', 'patch', 'delete', 'head', 'options' ]) { + request[m] = (url, callback) => request({ url, method: m.toUpperCase() }, callback); +} +request.del = request.delete; // Special case module.exports = request; \ No newline at end of file