-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.js
36 lines (32 loc) · 1003 Bytes
/
helpers.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
const config = require('config');
const axios = require('axios');
const qs = require('qs');
// In my local dev / testing I don't like having to update the values in index.js
const getConfig = (key, specified) => {
if(specified && ! specified.startsWith('REPLACE_WITH_')){
return specified;
}
else if(config.has(key)) {
return config.get(key);
}
else {
throw `Error, tried to get a config value with key=${key} and a value of ${specified} and the value was missing`;
}
};
const fetchAPIToken = async (apiTokenUrl, client_id, client_secret) => {
const {data} = await axios({
method: 'POST',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: qs.stringify({
client_id,
client_secret
}),
url: apiTokenUrl,
params: {
'grant_type': 'client_credentials'
}
});
// data will contain access_token and expires_in (seconds that the token will expire)
return data;
}
module.exports = { getConfig, fetchAPIToken };