forked from konfuzio-ai/document-validation-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
80 lines (67 loc) · 1.67 KB
/
api.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
import axios from "axios";
let HTTP, FILE_REQUEST, authToken, appLocale;
const DEFAULT_URL = "https://app.konfuzio.com";
const FILE_URL = process.env.VUE_APP_DOCUMENT_IMAGES_URL;
axios.defaults.xsrfCookieName = "csrftoken";
axios.defaults.xsrfHeaderName = "X-CSRFToken";
HTTP = axios.create({
baseURL: process.env.VUE_APP_API_URL || `${DEFAULT_URL}/api/v3/`,
});
FILE_REQUEST = axios.create({
baseURL: FILE_URL || `${DEFAULT_URL}`,
responseType: "blob",
});
const setAuthToken = (token) => {
authToken = token;
};
const setApiUrl = (url) => {
HTTP.defaults.baseURL = url;
};
const setFileUrl = (url) => {
FILE_REQUEST.defaults.baseURL = url;
};
const setLocale = (locale) => {
appLocale = locale;
};
const getInterceptorConfig = (config) => {
if (authToken) {
config.headers["Authorization"] = `Token ${authToken}`;
config.headers["Accept-Language"] = `${appLocale}-${appLocale}`;
}
return config;
};
HTTP.interceptors.request.use(getInterceptorConfig, (error) => {
return Promise.reject(error);
});
FILE_REQUEST.interceptors.request.use(getInterceptorConfig, (error) => {
return Promise.reject(error);
});
const makeFileRequest = (fileUrl) => {
return new Promise((resolve, reject) => {
if (process.env.NODE_ENV === "test") {
reject("Running unit tests!");
return;
}
FILE_REQUEST.get(fileUrl)
.then((response) => {
return response.data;
})
.then((myBlob) => {
resolve(myBlob);
})
.catch((error) => {
reject(error);
});
});
};
export default {
HTTP,
setApiUrl,
setFileUrl,
makeFileRequest,
setAuthToken,
setLocale,
FILE_REQUEST,
DEFAULT_URL,
FILE_URL,
};