-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (110 loc) · 3.38 KB
/
index.js
File metadata and controls
116 lines (110 loc) · 3.38 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import axios from 'axios';
import { persistor, store } from '../store/store';
import { clearAllState } from '../store/actions';
const isDev = process.env.NODE_ENV === 'development';
const apiUrl = isDev ? process.env.BACKEND_DEV_HOST : process.env.BACKEND_PROD_HOST;
const clearState = async () => {
store.dispatch(clearAllState());
await persistor.purge();
localStorage.clear();
};
const instance = axios.create({
baseURL: apiUrl,
withCredentials: true,
});
instance.interceptors.response.use(
response => response,
async err => {
if (err?.response?.status === 401) {
await clearState();
}
throw err;
}
);
export const api = Object.freeze({
jwt: {
// Login by JSON Web Token
login: async () => {
const response = await instance.get('/auth/jwt/login');
// If user has never logged in before, redirect to consent screen
if (response.status === 210) {
window.location = response.data;
return;
}
return response;
},
logout: async () => {
await instance.get('/auth/jwt/logout');
await clearState();
},
loginStatus: async () => {
const response = await instance.get('/auth/jwt/login-status');
return JSON.parse(response.data); // response boolean
},
},
acg: {
// Login by Authorization Code Grant
login: () => {
// Avoid here XHR requests because we encounter problems with CORS for ACG Authorization
window.location.href = `${apiUrl}/auth/passport/login`;
},
logout: async () => {
await instance.get('/auth/passport/logout');
await clearState();
},
callbackExecute: async code => {
const response = await instance.get(`/auth/passport/callback?code=${code}`);
return response;
},
loginStatus: async () => {
const response = await instance.get('/auth/passport/login-status');
return JSON.parse(response.data); // response boolean
},
},
workflows: {
triggerWorkflow: async (workflowId, templateType, body) => {
try {
const response = await instance.put(`/workflows/${workflowId}/trigger?type=${templateType}`, body);
return response;
} catch (error) {
console.log(error);
}
},
getWorkflowDefinitions: async () => {
const response = await instance.get(`/workflows/definitions`);
return response;
},
getWorkflowTriggerRequirements: async workflowId => {
try {
const response = await instance.get(`/workflows/${workflowId}/requirements`);
return response;
} catch (error) {
return error.response;
}
},
pauseWorkflow: async workflow => {
try {
const response = await instance.post(`/workflows/${workflow.id}/pause`);
return response;
} catch (error) {
return error.response;
}
},
resumeWorkflow: async workflow => {
try {
const response = await instance.post(`/workflows/${workflow.id}/resume`);
return response;
} catch (error) {
return error.response;
}
},
getWorkflowInstances: async workflowId => {
const response = await instance.get(`/workflows/${workflowId}/instances`);
return response;
},
cancelWorkflowInstance: async (workflowId, instanceId) => {
const response = await instance.post(`/workflows/${workflowId}/instances/${instanceId}/cancel`);
return response;
}
},
});