-
-
Notifications
You must be signed in to change notification settings - Fork 393
/
request.js
56 lines (45 loc) · 1.32 KB
/
request.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const Got = require('got');
const pkg = require('../../package.json');
const { deep: defaultsDeep } = require('./defaults');
const isAbsoluteUrl = require('./is_absolute_url');
const { HTTP_OPTIONS } = require('./consts');
let DEFAULT_HTTP_OPTIONS;
let got;
const setDefaults = (options) => {
DEFAULT_HTTP_OPTIONS = defaultsDeep({}, options, DEFAULT_HTTP_OPTIONS);
got = Got.extend(DEFAULT_HTTP_OPTIONS);
};
setDefaults({
followRedirect: false,
headers: { 'User-Agent': `${pkg.name}/${pkg.version} (${pkg.homepage})` },
retry: 0,
timeout: 3500,
throwHttpErrors: false,
});
module.exports = async function request(options, { mTLS = false, DPoP } = {}) {
const { url } = options;
isAbsoluteUrl(url);
const optsFn = this[HTTP_OPTIONS];
let opts = options;
if (DPoP && 'dpopProof' in this) {
opts.headers = opts.headers || {};
opts.headers.DPoP = this.dpopProof({
htu: url,
htm: options.method,
}, DPoP);
}
if (optsFn) {
opts = optsFn.call(this, defaultsDeep({}, opts, DEFAULT_HTTP_OPTIONS));
}
if (
mTLS
&& (
(!opts.key || !opts.cert)
&& (!opts.https || !((opts.https.key && opts.https.certificate) || opts.https.pfx))
)
) {
throw new TypeError('mutual-TLS certificate and key not set');
}
return got(opts);
};
module.exports.setDefaults = setDefaults;