-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwrapper.js
53 lines (44 loc) · 1.15 KB
/
wrapper.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
const request = require('./index');
const _ = require('lodash');
module.exports = (config, cache) => {
const audience = _.get(config, 'resourceServer', 'https://api.cimpress.io/');
if (cache) {
request.setCredentialCache(cache);
}
return (method, url, body, headers, accessToken) => {
let customHeaders = headers;
if (customHeaders) {
if (!customHeaders['content-type']) {
customHeaders['content-type'] = 'application/json';
}
} else {
customHeaders = {
'content-type': 'application/json',
};
}
const options = {
method,
url,
headers: customHeaders,
auth: {
audience,
clientId: config.clientId,
clientSecret: config.clientSecret,
refreshToken: config.refreshToken,
},
timesToRetry: 2,
};
if (body) {
// setting a null body in options can cause issues
options.body = body;
}
if (accessToken) {
options.auth.bearer = accessToken;
}
if (config.keyGen) {
options.keyGen = config.keyGen;
}
return request(options);
};
};
module.exports.requestEmitter = request.requestEmitter;