Skip to content
Snippets Groups Projects
Commit 490e0689 authored by Rhys Arkins's avatar Rhys Arkins
Browse files

refactor(host-rules): cleaner got processing

parent 11c77d07
No related branches found
No related tags found
No related merge requests found
/* 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);
},
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment