-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes.js
102 lines (90 loc) · 2.61 KB
/
routes.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// const {
// fetchJSONApi,
// makeHeaders,
// getAccessToken,
// } = require('./utils');
// const config = require('./config/config.json');
import {
fetchJSONApi,
makeHeaders,
getAccessToken,
} from './utils.js'
import config from './config/config.js'
export async function getRoutes(tenantId) {
const accessToken = await getAccessToken();
const url = `${config.vaultApiUrl}/rule-chains`;
const params = {
method: 'GET',
headers: makeHeaders(accessToken, tenantId),
};
try {
const response = await fetchJSONApi(url, params);
return response.data.data;
} catch (error) {
throw new Error(error);
}
}
export async function updateRoute(tenantId, route) {
console.log('[vgs-plugin] updating route', route.data[0].id)
const accessToken = await getAccessToken();
const url = `${config.vaultApiUrl}/rule-chains/${route.data[0].id}`;
const params = {
method: 'PUT',
headers: makeHeaders(accessToken, tenantId),
data: {
data: route.data[0]
},
};
try {
const response = await fetchJSONApi(url, params);
console.log('[vgs-plugin] route updated', route.data[0].id)
return response.data.data;
} catch (error) {
throw new Error(error);
}
}
export const tokenizationCollectRouteTemplate = {
destination_override_endpoint: config.defaultRuleTokenizeHostOverrideEndpoint,
entries: [],
host_endpoint: config.defaultRuleTokenizeHostEndpoint,
port: 80,
protocol: 'http',
source_endpoint: '*',
transport: 'HTTP',
tags: {
name: 'collect.js',
source: 'tokenizationCollect',
},
};
export async function createRoute(tenantId, routeData) {
console.log('[vgs-plugin] creating route')
const accessToken = await getAccessToken();
const url = `${config.vaultApiUrl}/rule-chains`;
const params = {
method: 'POST',
headers: makeHeaders(accessToken, tenantId),
data: {
data: {
attributes: routeData,
type: 'rule-chains',
}
}
};
try {
const response = await fetchJSONApi(url, params);
return response.data.data;
} catch (error) {
throw new Error(error);
}
}
export async function applyCollectRoute(vaultId) {
const routesList = await getRoutes(vaultId);
let collectRoute = routesList.find(route => route?.attributes?.tags?.source === 'tokenizationCollect');
console.log('[vgs-plugin] collectRouteExists:', !!collectRoute);
if(!collectRoute) {
console.log('[vgs-plugin] there is no collect route, creating one')
collectRoute = await createRoute(vaultId, tokenizationCollectRouteTemplate);
console.log('[vgs-plugin] route creation finished!')
}
return collectRoute;
}