@gandalan/weblibs 1.0.12 → 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 +30 -39
- package/package.json +1 -1
package/api/RESTClient.js
CHANGED
|
@@ -1,49 +1,40 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
|
-
import { jwtTokenInvalid, jwtTokenRenew } from
|
|
2
|
+
import { jwtTokenInvalid, jwtTokenRenew } from "./authUtils";
|
|
3
3
|
|
|
4
4
|
export class RESTClient {
|
|
5
5
|
lastError = "";
|
|
6
6
|
settings = {};
|
|
7
|
+
axiosInstance = null;
|
|
7
8
|
|
|
8
|
-
constructor(settings)
|
|
9
|
-
{
|
|
9
|
+
constructor(settings) {
|
|
10
10
|
this.settings = settings;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
11
|
+
|
|
12
|
+
this.axiosInstance = axios.create({
|
|
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;
|
|
22
|
+
});
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
async checkTokenBeforeRequest(config) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
{
|
|
29
|
-
let parts = authHeader.toString().split(" ");
|
|
30
|
-
let jwt = parts[1];
|
|
31
|
-
if (this.settings.jwtToken === jwt && jwtTokenInvalid(this.settings)) // ignore custom/different JWT tokens
|
|
32
|
-
await jwtTokenRenew(this.settings);
|
|
26
|
+
if (this.settings.jwtToken && jwtTokenInvalid(this.settings)) { // ignore custom/different JWT tokens
|
|
27
|
+
await jwtTokenRenew(this.settings);
|
|
33
28
|
}
|
|
34
29
|
}
|
|
35
30
|
|
|
36
|
-
getUrlOptions(
|
|
37
|
-
|
|
38
|
-
/*if (this.isJWT && !noJWT) {
|
|
39
|
-
options.headers = { Authorization: `Bearer ${this.token}` }
|
|
40
|
-
}*/
|
|
41
|
-
return options;
|
|
31
|
+
getUrlOptions() {
|
|
32
|
+
return { withCredentials: false };
|
|
42
33
|
}
|
|
43
34
|
|
|
44
|
-
async get(uri
|
|
35
|
+
async get(uri) {
|
|
45
36
|
try {
|
|
46
|
-
const response = await
|
|
37
|
+
const response = await this.axiosInstance.get(uri, this.getUrlOptions());
|
|
47
38
|
this.lastError = "";
|
|
48
39
|
return response.data;
|
|
49
40
|
} catch (error) {
|
|
@@ -53,7 +44,7 @@ export class RESTClient {
|
|
|
53
44
|
|
|
54
45
|
async getFile(uri) {
|
|
55
46
|
try {
|
|
56
|
-
const response = await
|
|
47
|
+
const response = await this.axiosInstance.get(uri, { responseType: "blob" });
|
|
57
48
|
let fileName = "1000.pdf";
|
|
58
49
|
if (response.headers["content-disposition"]) {
|
|
59
50
|
fileName = response.headers["content-disposition"].split(";")[1];
|
|
@@ -66,10 +57,10 @@ export class RESTClient {
|
|
|
66
57
|
}
|
|
67
58
|
}
|
|
68
59
|
|
|
69
|
-
async getRaw(uri
|
|
60
|
+
async getRaw(uri) {
|
|
70
61
|
let response = {};
|
|
71
62
|
try {
|
|
72
|
-
response = await
|
|
63
|
+
response = await this.axiosInstance.get(uri, this.getUrlOptions())
|
|
73
64
|
this.lastError = "";
|
|
74
65
|
} catch (error) {
|
|
75
66
|
this.handleError(error);
|
|
@@ -77,9 +68,9 @@ export class RESTClient {
|
|
|
77
68
|
return response;
|
|
78
69
|
}
|
|
79
70
|
|
|
80
|
-
async post(uri, formData
|
|
71
|
+
async post(uri, formData) {
|
|
81
72
|
try {
|
|
82
|
-
const response = await
|
|
73
|
+
const response = await this.axiosInstance.post(uri, formData, this.getUrlOptions());
|
|
83
74
|
this.lastError = "";
|
|
84
75
|
return response;
|
|
85
76
|
} catch (error) {
|
|
@@ -87,9 +78,9 @@ export class RESTClient {
|
|
|
87
78
|
}
|
|
88
79
|
}
|
|
89
80
|
|
|
90
|
-
async put(uri, formData
|
|
81
|
+
async put(uri, formData) {
|
|
91
82
|
try {
|
|
92
|
-
const response = await
|
|
83
|
+
const response = await this.axiosInstance.put(uri, formData, this.getUrlOptions());
|
|
93
84
|
this.lastError = "";
|
|
94
85
|
return response;
|
|
95
86
|
} catch (error) {
|
|
@@ -97,9 +88,9 @@ export class RESTClient {
|
|
|
97
88
|
}
|
|
98
89
|
}
|
|
99
90
|
|
|
100
|
-
async delete(uri
|
|
91
|
+
async delete(uri) {
|
|
101
92
|
try {
|
|
102
|
-
const response = await
|
|
93
|
+
const response = await this.axiosInstance.delete(uri, this.getUrlOptions());
|
|
103
94
|
this.lastError = "";
|
|
104
95
|
return response;
|
|
105
96
|
} catch (error) {
|