-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
67 lines (56 loc) · 1.45 KB
/
utils.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
57
58
59
60
61
62
63
64
65
66
67
import axios from 'axios';
import fs from 'fs';
import yaml from 'js-yaml';
import { ClientCredentials } from 'simple-oauth2';
import config from './config/config.js';
const { VGS_CLIENT_ID, VGS_CLIENT_SECRET } = process.env;
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
export function makeHeaders(accessToken, tenantId) {
const headers = {
'Content-Type': 'application/vnd.api+json',
Accept: 'application/vnd.api+json',
Authorization: `Bearer ${accessToken}`,
};
if (tenantId) {
headers['VGS-Tenant'] = tenantId;
}
return headers;
}
export async function fetchJSONApi(url, options) {
const params = {
url,
method: options.method,
data: options.data,
headers: options.headers,
};
return axios.request(params);
}
export async function getAccessToken() {
const oauthConfig = {
client: {
id: VGS_CLIENT_ID,
secret: VGS_CLIENT_SECRET
},
auth: config.auth,
options: {
authorizationMethod: 'body'
}
};
const client = new ClientCredentials(oauthConfig);
const tokenParams = {
scope: ['routes:write'],
};
try {
const response = await client.getToken(tokenParams, {json: true});
return response.token.access_token;
} catch (error) {
throw new Error(error);
}
}
export function getRouteConfig(path = './vgs/routes.yaml') {
if (fs.existsSync(path)) {
return yaml.load(fs.readFileSync(path, 'utf8'));
} else {
return null;
}
}