-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathhttp-common.js
More file actions
44 lines (35 loc) · 1.07 KB
/
http-common.js
File metadata and controls
44 lines (35 loc) · 1.07 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
import axios from "axios";
import { useAppStore } from "@/store/app";
const appStore = useAppStore();
appStore
const http = axios.create({
headers: {
"Content-type": "application/json"
}
});
http.interceptors.request.use(
config => {
config.baseURL = appStore.connection.host;
config.headers["apikey"] = appStore.connection.globalApiKey;
// find all uri variables and replace them with the value from the params object
// e.g. /instance/connect/:instance -> /instance/connect/instance1
const params = Object.entries(config.params || {});
if (params.length > 0) {
config.url = config.url.replace(/:(\w+)/g, (_, key) => {
const value = params.find(([k]) => k === key)?.[1];
if (value) {
delete config.params[key];
return value;
}
return _;
});
if (params.instance) {
const apikey = appStore.getInstanceApiKey(params.instance);
if (apikey) config.headers["apikey"] = apikey;
}
}
return config;
},
error => Promise.reject(error)
);
export default http;