[Poly > Request] Cleanup some source
This commit is contained in:
		
							parent
							
								
									1694b64247
								
							
						
					
					
						commit
						6775040a37
					
				
					 1 changed files with 10 additions and 30 deletions
				
			
		|  | @ -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; | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue