Skip to content

Commit

Permalink
fix: httpOptions helper (#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtadevos authored Jun 13, 2022
1 parent c6b284f commit 80fe961
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions lib/helpers/request.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
const { Agent: HttpAgent } = require('http');
const { Agent: HttpsAgent } = require('https');

const got = require('got');
const CacheableLookup = require('cacheable-lookup');
const QuickLRU = require('quick-lru');

const omitBy = require('./_/omit_by');
const pickBy = require('./_/pick_by');
const instance = require('./weak_cache');

const cacheable = new CacheableLookup({
Expand All @@ -20,21 +17,18 @@ module.exports = async function request(options) {
// eslint-disable-next-line no-param-reassign
options.headers['user-agent'] = undefined;
const { timeout, agent, lookup } = instance(this).configuration('httpOptions')(new URL(options.url));
const helperOptions = omitBy({ timeout, agent, lookup }, Boolean);
const helperOptions = pickBy({ timeout, agent, lookup }, Boolean);

if (helperOptions.timeout !== undefined && typeof helperOptions.timeout !== 'number') {
throw new TypeError('"timeout" http request option must be a number');
}

if (helperOptions.agent !== undefined && typeof helperOptions.agent !== 'number') {
if (!(agent instanceof HttpsAgent) && !(agent instanceof HttpAgent)) {
throw new TypeError('"agent" http request option must be an instance of https.Agent or http.Agent depending on the protocol used');
}
helperOptions.agent = { [options.url.protocol]: helperOptions.agent };
helperOptions.agent = { [options.url.protocol.slice(0, -1)]: helperOptions.agent };
}

if (helperOptions.lookup !== undefined && typeof helperOptions.lookup !== 'function') {
throw new TypeError('"agent" http request option must be a function');
throw new TypeError('"lookup" http request option must be a function');
}

return got({
Expand Down

0 comments on commit 80fe961

Please sign in to comment.