@gandalan/weblibs 1.0.13 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api/RESTClient.js +22 -34
- package/package.json +1 -1
package/api/RESTClient.js
CHANGED
|
@@ -9,44 +9,32 @@ export class RESTClient {
|
|
|
9
9
|
constructor(settings) {
|
|
10
10
|
this.settings = settings;
|
|
11
11
|
|
|
12
|
-
if (settings.jwtToken) {
|
|
13
|
-
axios.defaults.headers.common["Authorization"] = `Bearer ${ this.token}`;
|
|
14
|
-
axios.interceptors.request.use(async (config) => {
|
|
15
|
-
await this.checkTokenBeforeRequest(config);
|
|
16
|
-
return config;
|
|
17
|
-
});
|
|
18
|
-
} else {
|
|
19
|
-
// might contain the classic token for fallback
|
|
20
|
-
axios.defaults.headers.common["X-Gdl-AuthToken"] = settings.jwtRefreshToken;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
12
|
this.axiosInstance = axios.create({
|
|
24
13
|
baseURL: settings.apiBaseurl,
|
|
14
|
+
headers: {
|
|
15
|
+
"Authorization" : `Bearer ${ settings.jwtToken }`
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
this.axiosInstance.interceptors.request.use(async (config) => {
|
|
20
|
+
await this.checkTokenBeforeRequest(config);
|
|
21
|
+
return config;
|
|
25
22
|
});
|
|
26
23
|
}
|
|
27
24
|
|
|
28
25
|
async checkTokenBeforeRequest(config) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
let parts = authHeader.toString().split(" ");
|
|
32
|
-
let jwt = parts[1];
|
|
33
|
-
if (this.settings.jwtToken === jwt && jwtTokenInvalid(this.settings)) { // ignore custom/different JWT tokens
|
|
34
|
-
await jwtTokenRenew(this.settings);
|
|
35
|
-
}
|
|
26
|
+
if (this.settings.jwtToken && jwtTokenInvalid(this.settings)) { // ignore custom/different JWT tokens
|
|
27
|
+
await jwtTokenRenew(this.settings);
|
|
36
28
|
}
|
|
37
29
|
}
|
|
38
30
|
|
|
39
|
-
getUrlOptions(
|
|
40
|
-
|
|
41
|
-
/*if (this.isJWT && !noJWT) {
|
|
42
|
-
options.headers = { Authorization: `Bearer ${this.token}` }
|
|
43
|
-
}*/
|
|
44
|
-
return options;
|
|
31
|
+
getUrlOptions() {
|
|
32
|
+
return { withCredentials: false };
|
|
45
33
|
}
|
|
46
34
|
|
|
47
|
-
async get(uri
|
|
35
|
+
async get(uri) {
|
|
48
36
|
try {
|
|
49
|
-
const response = await this.axiosInstance.get(uri, this.getUrlOptions(
|
|
37
|
+
const response = await this.axiosInstance.get(uri, this.getUrlOptions());
|
|
50
38
|
this.lastError = "";
|
|
51
39
|
return response.data;
|
|
52
40
|
} catch (error) {
|
|
@@ -69,10 +57,10 @@ export class RESTClient {
|
|
|
69
57
|
}
|
|
70
58
|
}
|
|
71
59
|
|
|
72
|
-
async getRaw(uri
|
|
60
|
+
async getRaw(uri) {
|
|
73
61
|
let response = {};
|
|
74
62
|
try {
|
|
75
|
-
response = await this.axiosInstance.get(uri, this.getUrlOptions(
|
|
63
|
+
response = await this.axiosInstance.get(uri, this.getUrlOptions())
|
|
76
64
|
this.lastError = "";
|
|
77
65
|
} catch (error) {
|
|
78
66
|
this.handleError(error);
|
|
@@ -80,9 +68,9 @@ export class RESTClient {
|
|
|
80
68
|
return response;
|
|
81
69
|
}
|
|
82
70
|
|
|
83
|
-
async post(uri, formData
|
|
71
|
+
async post(uri, formData) {
|
|
84
72
|
try {
|
|
85
|
-
const response = await this.axiosInstance.post(uri, formData, this.getUrlOptions(
|
|
73
|
+
const response = await this.axiosInstance.post(uri, formData, this.getUrlOptions());
|
|
86
74
|
this.lastError = "";
|
|
87
75
|
return response;
|
|
88
76
|
} catch (error) {
|
|
@@ -90,9 +78,9 @@ export class RESTClient {
|
|
|
90
78
|
}
|
|
91
79
|
}
|
|
92
80
|
|
|
93
|
-
async put(uri, formData
|
|
81
|
+
async put(uri, formData) {
|
|
94
82
|
try {
|
|
95
|
-
const response = await this.axiosInstance.put(uri, formData, this.getUrlOptions(
|
|
83
|
+
const response = await this.axiosInstance.put(uri, formData, this.getUrlOptions());
|
|
96
84
|
this.lastError = "";
|
|
97
85
|
return response;
|
|
98
86
|
} catch (error) {
|
|
@@ -100,9 +88,9 @@ export class RESTClient {
|
|
|
100
88
|
}
|
|
101
89
|
}
|
|
102
90
|
|
|
103
|
-
async delete(uri
|
|
91
|
+
async delete(uri) {
|
|
104
92
|
try {
|
|
105
|
-
const response = await this.axiosInstance.delete(uri, this.getUrlOptions(
|
|
93
|
+
const response = await this.axiosInstance.delete(uri, this.getUrlOptions());
|
|
106
94
|
this.lastError = "";
|
|
107
95
|
return response;
|
|
108
96
|
} catch (error) {
|