From 490e0689a701bbc70cb1ddb6426e5adfa5ec511d Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Sat, 25 May 2019 20:09:16 +0200 Subject: [PATCH] refactor(host-rules): cleaner got processing --- lib/util/got/host-rules.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/util/got/host-rules.js b/lib/util/got/host-rules.js index 7fb990085c..e52382c97d 100644 --- a/lib/util/got/host-rules.js +++ b/lib/util/got/host-rules.js @@ -1,3 +1,4 @@ +/* eslint-disable no-param-reassign */ const got = require('got'); const hostRules = require('../host-rules'); @@ -7,26 +8,29 @@ const hostRules = require('../host-rules'); module.exports = got.create({ options: {}, handler: (options, next) => { - const { hostType, ...opts } = options; if (!options.hostname) { - return next(opts); + return next(options); } - const { username = '', password, token, timeout } = hostRules.find({ - hostType, + const { username, password, token, timeout } = hostRules.find({ + hostType: options.hostType, url: options.href, }); - if (opts.headers.authorization || opts.auth || opts.token) { - logger.debug('Authorization already set for host: ' + opts.hostname); + if (options.headers.authorization || options.auth || options.token) { + logger.trace('Authorization already set for host: ' + options.hostname); } else if (password) { - logger.debug('Applying Basic authentication for host ' + opts.hostname); - opts.auth = `${username}:${password}`; + logger.trace( + 'Applying Basic authentication for host ' + options.hostname + ); + options.auth = `${username || ''}:${password}`; } else if (token) { - logger.debug('Applying Bearer authentication for host ' + opts.hostname); - opts.token = token; + logger.trace( + 'Applying Bearer authentication for host ' + options.hostname + ); + options.token = token; } if (timeout) { - opts.gotTimeout = { request: timeout }; + options.gotTimeout = { request: timeout }; } - return next(opts); + return next(options); }, }); -- GitLab