-
Notifications
You must be signed in to change notification settings - Fork 406
/
axios.js
114 lines (103 loc) · 3.35 KB
/
axios.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
103
104
105
106
107
108
109
110
111
112
113
114
/*******************************************************************************
* Copyright (c) 2021 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*******************************************************************************/
"use strict";
import Vue from "vue";
import axios from "axios";
// Full config: https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
let config = {
// baseURL: process.env.baseURL || process.env.apiUrl || ""
// timeout: 60 * 1000, // Timeout
// withCredentials: true, // Check cross-site Access-Control
responseType: "text",
};
// HACK waiting we get a solution for : https://github.com/yariksav/vuetify-dialog/issues/110#issuecomment-1145981361
// and unfortenately there is not standard way to do that ... : https://stackoverflow.com/questions/40263803/native-javascript-or-es6-way-to-encode-and-decode-html-entities
const escapeHTML = (str) =>
str.replace(
/[&<>'"]/g,
(tag) =>
({
"&": "&",
"<": "<",
">": ">",
"'": "'",
'"': """,
}[tag])
);
const _axios = axios.create(config);
_axios.interceptors.request.use(
function (config) {
// Do something before request is sent
return config;
},
function (error) {
// Do something with request error
return Promise.reject(error);
}
);
// Add a response interceptor
_axios.interceptors.response.use(
function (response) {
// show error message if device return a failure code
if (response.data && response.data.failure) {
let msg = `Device response : ${response.data.status}`;
if (response.data.errormessage) msg += ` - ${response.data.errormessage}`;
Vue.prototype.$dialog.notify.warning(escapeHTML(msg), {
position: "bottom-right",
timeout: 5000,
});
}
return response;
},
function (error) {
let message;
if (error.response) {
console.log(
`${error.message}[${error.response.status}]:${error.response.data}`
);
message = error.response.data ? error.response.data : error.message;
} else if (error.request) {
console.log(`${error.message}:${error.request.data}`);
message = error.request.data ? error.request.data : error.message;
} else {
console.log(error.message);
message = error.message;
}
Vue.prototype.$dialog.notify.error(escapeHTML(message), {
position: "bottom-right",
timeout: 5000,
});
return Promise.reject(error);
}
);
Plugin.install = function (Vue) {
Vue.axios = _axios;
window.axios = _axios;
Object.defineProperties(Vue.prototype, {
axios: {
get() {
return _axios;
},
},
$axios: {
get() {
return _axios;
},
},
});
};
Vue.use(Plugin);
export default Plugin;