@gandalan/weblibs 1.1.5 → 1.1.9
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 +59 -31
- package/package.json +1 -1
package/api/RESTClient.js
CHANGED
|
@@ -8,7 +8,7 @@ export class RESTClient
|
|
|
8
8
|
axiosInstance = null;
|
|
9
9
|
|
|
10
10
|
constructor(settings)
|
|
11
|
-
|
|
11
|
+
{
|
|
12
12
|
this.settings = settings;
|
|
13
13
|
|
|
14
14
|
this.axiosInstance = axios.create({
|
|
@@ -39,32 +39,34 @@ export class RESTClient
|
|
|
39
39
|
}*/
|
|
40
40
|
|
|
41
41
|
getUrlOptions()
|
|
42
|
-
|
|
42
|
+
{
|
|
43
43
|
return { withCredentials: false };
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
async get(uri)
|
|
47
|
-
|
|
47
|
+
{
|
|
48
48
|
try
|
|
49
|
-
|
|
49
|
+
{
|
|
50
|
+
this.axiosInstance = this.getNewAxiosInstance();
|
|
50
51
|
const response = await this.axiosInstance.get(uri, this.getUrlOptions());
|
|
51
52
|
this.lastError = "";
|
|
52
53
|
return response.data;
|
|
53
54
|
}
|
|
54
55
|
catch (error)
|
|
55
|
-
|
|
56
|
+
{
|
|
56
57
|
this.handleError(error);
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
async getFile(uri)
|
|
61
|
-
|
|
62
|
+
{
|
|
62
63
|
try
|
|
63
|
-
|
|
64
|
+
{
|
|
65
|
+
this.axiosInstance = this.getNewAxiosInstance();
|
|
64
66
|
const response = await this.axiosInstance.get(uri, { responseType: "blob" });
|
|
65
67
|
let fileName = "1000.pdf";
|
|
66
68
|
if (response.headers["content-disposition"])
|
|
67
|
-
|
|
69
|
+
{
|
|
68
70
|
fileName = response.headers["content-disposition"].split(";")[1];
|
|
69
71
|
fileName = fileName.replace("filename=", "").trim();
|
|
70
72
|
}
|
|
@@ -72,79 +74,105 @@ export class RESTClient
|
|
|
72
74
|
return { data: response.data, filename: fileName, contentType: "application/pdf" };
|
|
73
75
|
}
|
|
74
76
|
catch (error)
|
|
75
|
-
|
|
77
|
+
{
|
|
76
78
|
this.handleError(error);
|
|
77
79
|
}
|
|
78
80
|
}
|
|
79
81
|
|
|
80
82
|
async getRaw(uri)
|
|
81
|
-
|
|
83
|
+
{
|
|
82
84
|
let response = {};
|
|
83
85
|
try
|
|
84
|
-
|
|
86
|
+
{
|
|
87
|
+
this.axiosInstance = this.getNewAxiosInstance();
|
|
85
88
|
response = await this.axiosInstance.get(uri, this.getUrlOptions())
|
|
86
89
|
this.lastError = "";
|
|
87
90
|
}
|
|
88
91
|
catch (error)
|
|
89
|
-
|
|
92
|
+
{
|
|
90
93
|
this.handleError(error);
|
|
91
94
|
}
|
|
92
95
|
return response;
|
|
93
96
|
}
|
|
94
97
|
|
|
95
98
|
async post(uri, formData)
|
|
96
|
-
|
|
99
|
+
{
|
|
97
100
|
try
|
|
98
|
-
|
|
101
|
+
{
|
|
102
|
+
this.axiosInstance = this.getNewAxiosInstance();
|
|
99
103
|
const response = await this.axiosInstance.post(uri, formData, this.getUrlOptions());
|
|
100
104
|
this.lastError = "";
|
|
101
105
|
return response;
|
|
102
106
|
}
|
|
103
107
|
catch (error)
|
|
104
|
-
|
|
108
|
+
{
|
|
105
109
|
this.handleError(error);
|
|
106
110
|
}
|
|
107
111
|
}
|
|
108
112
|
|
|
109
113
|
async put(uri, formData)
|
|
110
|
-
|
|
114
|
+
{
|
|
111
115
|
try
|
|
112
|
-
|
|
116
|
+
{
|
|
117
|
+
this.axiosInstance = this.getNewAxiosInstance();
|
|
113
118
|
const response = await this.axiosInstance.put(uri, formData, this.getUrlOptions());
|
|
114
119
|
this.lastError = "";
|
|
115
120
|
return response;
|
|
116
121
|
}
|
|
117
122
|
catch (error)
|
|
118
|
-
|
|
123
|
+
{
|
|
119
124
|
this.handleError(error);
|
|
120
125
|
}
|
|
121
126
|
}
|
|
122
127
|
|
|
123
128
|
async delete(uri)
|
|
124
|
-
|
|
129
|
+
{
|
|
125
130
|
try
|
|
126
|
-
|
|
131
|
+
{
|
|
132
|
+
this.axiosInstance = this.getNewAxiosInstance();
|
|
127
133
|
const response = await this.axiosInstance.delete(uri, this.getUrlOptions());
|
|
128
134
|
this.lastError = "";
|
|
129
135
|
return response;
|
|
130
136
|
}
|
|
131
137
|
catch (error)
|
|
132
|
-
|
|
138
|
+
{
|
|
133
139
|
this.handleError(error);
|
|
134
140
|
}
|
|
135
141
|
}
|
|
136
142
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
{
|
|
143
|
+
getNewAxiosInstance()
|
|
144
|
+
{
|
|
145
|
+
return axios.create({
|
|
146
|
+
baseURL: this.settings.apiBaseurl,
|
|
147
|
+
headers: {
|
|
148
|
+
"Authorization": `Bearer ${currentToken}`,
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
}
|
|
140
152
|
|
|
141
153
|
handleError(error)
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
154
|
+
{
|
|
155
|
+
if (error.response)
|
|
156
|
+
{
|
|
157
|
+
// The request was made and the server responded with a status code
|
|
158
|
+
// that falls out of the range of 2xx
|
|
159
|
+
console.log(error.response.data);
|
|
160
|
+
console.log(error.response.status);
|
|
161
|
+
console.log(error.response.headers);
|
|
162
|
+
}
|
|
163
|
+
else if (error.request)
|
|
164
|
+
{
|
|
165
|
+
// The request was made but no response was received
|
|
166
|
+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
|
167
|
+
// http.ClientRequest in node.js
|
|
168
|
+
console.log(error.request);
|
|
169
|
+
}
|
|
170
|
+
else
|
|
171
|
+
{
|
|
172
|
+
// Something happened in setting up the request that triggered an Error
|
|
173
|
+
console.log("Error", error.message);
|
|
174
|
+
}
|
|
175
|
+
console.log(error.config);
|
|
176
|
+
this.lastError = error;
|
|
149
177
|
}
|
|
150
178
|
}
|