@capsitech/react-utilities 0.1.4 → 0.1.6
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/lib/Components/SuspenseRoute.d.ts +7 -7
- package/lib/Components/SuspenseRoute.js +29 -29
- package/lib/Components/index.d.ts +1 -1
- package/lib/Components/index.js +1 -1
- package/lib/Hooks/index.d.ts +45 -45
- package/lib/Hooks/index.js +98 -98
- package/lib/Hooks/useInfiniteScroll.d.ts +7 -7
- package/lib/Hooks/useInfiniteScroll.js +22 -22
- package/lib/Hooks/useNetworkState.d.ts +67 -67
- package/lib/Hooks/useNetworkState.js +41 -41
- package/lib/Hooks/useShortcuts.d.ts +4 -4
- package/lib/Hooks/useShortcuts.js +91 -91
- package/lib/Utilities/ApiUtility.axios.d.ts +60 -60
- package/lib/Utilities/ApiUtility.axios.js +305 -305
- package/lib/Utilities/BrowserInfo.d.ts +74 -74
- package/lib/Utilities/BrowserInfo.js +153 -153
- package/lib/Utilities/Countries.d.ts +14 -14
- package/lib/Utilities/Countries.js +290 -290
- package/lib/Utilities/CustomEventEmitter.d.ts +12 -12
- package/lib/Utilities/CustomEventEmitter.js +30 -30
- package/lib/Utilities/FastCompare.d.ts +1 -1
- package/lib/Utilities/FastCompare.js +128 -128
- package/lib/Utilities/HideablePromise.d.ts +5 -5
- package/lib/Utilities/HideablePromise.js +10 -10
- package/lib/Utilities/LoadScripts.d.ts +9 -9
- package/lib/Utilities/LoadScripts.js +51 -51
- package/lib/Utilities/MTDFraudPrevention.d.ts +28 -28
- package/lib/Utilities/MTDFraudPrevention.js +157 -157
- package/lib/Utilities/Nationalities.d.ts +5 -5
- package/lib/Utilities/Nationalities.js +245 -245
- package/lib/Utilities/RouteUtils.d.ts +129 -120
- package/lib/Utilities/RouteUtils.js +223 -206
- package/lib/Utilities/TimeZones.d.ts +10 -10
- package/lib/Utilities/TimeZones.js +1069 -1069
- package/lib/Utilities/Types.d.ts +19 -19
- package/lib/Utilities/Types.js +1 -1
- package/lib/Utilities/Utils.d.ts +174 -174
- package/lib/Utilities/Utils.js +331 -331
- package/lib/Utilities/dayjs.d.ts +18 -18
- package/lib/Utilities/dayjs.js +56 -56
- package/lib/Utilities/index.d.ts +14 -14
- package/lib/Utilities/index.js +14 -14
- package/lib/index.d.ts +3 -3
- package/lib/index.js +3 -3
- package/package.json +12 -25
|
@@ -1,305 +1,305 @@
|
|
|
1
|
-
import Axios from 'axios';
|
|
2
|
-
import { saveAs } from 'file-saver';
|
|
3
|
-
import { stringify } from 'qs';
|
|
4
|
-
export var HttpMethods;
|
|
5
|
-
(function (HttpMethods) {
|
|
6
|
-
HttpMethods["get"] = "get";
|
|
7
|
-
HttpMethods["post"] = "post";
|
|
8
|
-
})(HttpMethods || (HttpMethods = {}));
|
|
9
|
-
export var FileDownloadStatus;
|
|
10
|
-
(function (FileDownloadStatus) {
|
|
11
|
-
FileDownloadStatus["downloading"] = "downloading";
|
|
12
|
-
FileDownloadStatus["done"] = "done";
|
|
13
|
-
FileDownloadStatus["error"] = "error";
|
|
14
|
-
})(FileDownloadStatus || (FileDownloadStatus = {}));
|
|
15
|
-
class ApiUtilityBase {
|
|
16
|
-
accessToken;
|
|
17
|
-
handleError = (error, errors) => { };
|
|
18
|
-
// private getParams = (params?: any) => {
|
|
19
|
-
// if (params) {
|
|
20
|
-
// for (const key in params) {
|
|
21
|
-
// if (params[key] == null || params[key] === undefined || params[key] === '')
|
|
22
|
-
// delete params[key];
|
|
23
|
-
// }
|
|
24
|
-
// }
|
|
25
|
-
// return params;
|
|
26
|
-
// };
|
|
27
|
-
getResponse = async (endpoint, params, options) => {
|
|
28
|
-
return await Axios.get(endpoint, {
|
|
29
|
-
params,
|
|
30
|
-
paramsSerializer: {
|
|
31
|
-
serialize: (p, o) => {
|
|
32
|
-
return stringify(params, {
|
|
33
|
-
arrayFormat: 'indices',
|
|
34
|
-
allowDots: true,
|
|
35
|
-
skipNulls: true,
|
|
36
|
-
});
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
...this._axiosOptions(options || {}),
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
|
-
get = async (endpoint, params, throwErrorOn401, options) => {
|
|
43
|
-
const response = await this.getResponse(endpoint, params, options);
|
|
44
|
-
const data = this.handleResponse(response, throwErrorOn401);
|
|
45
|
-
return data;
|
|
46
|
-
};
|
|
47
|
-
getResult = async (endpoint, params, throwErrorOn401, options) => {
|
|
48
|
-
try {
|
|
49
|
-
const data = await this.get(endpoint, params, undefined, options);
|
|
50
|
-
if (data.status) {
|
|
51
|
-
return data.result;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
this.handleErrorResponse(data.message, data.errors);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
catch (error) {
|
|
58
|
-
if (error.isAxiosError)
|
|
59
|
-
this.handleAxiosError(error, throwErrorOn401);
|
|
60
|
-
else
|
|
61
|
-
this.handleResponse(error, throwErrorOn401);
|
|
62
|
-
}
|
|
63
|
-
return null;
|
|
64
|
-
};
|
|
65
|
-
post = async (endpoint, body, contentType, options) => {
|
|
66
|
-
try {
|
|
67
|
-
const response = await Axios.post(endpoint, body, this._axiosOptions({ contentType, ...(options || {}) }));
|
|
68
|
-
const data = this.handleResponse(response);
|
|
69
|
-
return data;
|
|
70
|
-
}
|
|
71
|
-
catch (ex) {
|
|
72
|
-
if (ex?.isAxiosError) {
|
|
73
|
-
return this.handleAxiosError(ex);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return {};
|
|
77
|
-
};
|
|
78
|
-
put = async (endpoint, body, contentType, options) => {
|
|
79
|
-
try {
|
|
80
|
-
const response = await Axios.put(endpoint, body, this._axiosOptions({ contentType, ...(options || {}) }));
|
|
81
|
-
const data = this.handleResponse(response);
|
|
82
|
-
return data;
|
|
83
|
-
}
|
|
84
|
-
catch (ex) {
|
|
85
|
-
if (ex?.isAxiosError) {
|
|
86
|
-
return this.handleAxiosError(ex);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return {};
|
|
90
|
-
};
|
|
91
|
-
appendFormDataValues = (form, values, preFix) => {
|
|
92
|
-
if (!preFix || preFix.length <= 0)
|
|
93
|
-
preFix = '';
|
|
94
|
-
//check if given object is a string/number/boolean then add directly to the list with prefix
|
|
95
|
-
if ((typeof values !== 'function' && typeof values !== 'object' && typeof values !== 'symbol') || values instanceof File) {
|
|
96
|
-
if (values && preFix) {
|
|
97
|
-
form.append(preFix, values);
|
|
98
|
-
}
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
for (const key in values) {
|
|
102
|
-
//prepare a field name
|
|
103
|
-
const fieldName = `${preFix}${preFix && !preFix.endsWith('.') ? '.' : ''}${key}`;
|
|
104
|
-
if (values[key] instanceof FileList) {
|
|
105
|
-
const fileList = values[key];
|
|
106
|
-
for (let fIndex = 0; fIndex < fileList.length; fIndex++) {
|
|
107
|
-
form.append(`${fieldName}[${fIndex}]`, fileList[fIndex]);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
if (Array.isArray(values[key])) {
|
|
112
|
-
values[key].forEach((el, idx) => {
|
|
113
|
-
this.appendFormDataValues(form, el, `${fieldName}[${idx}]`);
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
if (typeof values[key] === 'object') {
|
|
118
|
-
this.appendFormDataValues(form, values[key], fieldName);
|
|
119
|
-
}
|
|
120
|
-
else if (values[key]) {
|
|
121
|
-
form.append(fieldName, values[key]);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
postForm = async (endpoint, params, options) => {
|
|
128
|
-
let formData = new FormData();
|
|
129
|
-
if (params instanceof FormData) {
|
|
130
|
-
formData = params;
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
this.appendFormDataValues(formData, params);
|
|
134
|
-
}
|
|
135
|
-
try {
|
|
136
|
-
const response = await Axios.post(endpoint, formData, this._axiosOptions({ contentType: 'multipart/form-data', ...(options || {}) }));
|
|
137
|
-
const data = this.handleResponse(response);
|
|
138
|
-
return data;
|
|
139
|
-
}
|
|
140
|
-
catch (ex) {
|
|
141
|
-
if (ex?.isAxiosError) {
|
|
142
|
-
return this.handleAxiosError(ex);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return {};
|
|
146
|
-
};
|
|
147
|
-
putForm = async (endpoint, params, options) => {
|
|
148
|
-
let formData = new FormData();
|
|
149
|
-
if (params instanceof FormData) {
|
|
150
|
-
formData = params;
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
this.appendFormDataValues(formData, params);
|
|
154
|
-
}
|
|
155
|
-
try {
|
|
156
|
-
const response = await Axios.putForm(endpoint, formData, this._axiosOptions({ contentType: 'multipart/form-data', ...(options || {}) }));
|
|
157
|
-
const data = this.handleResponse(response);
|
|
158
|
-
return data;
|
|
159
|
-
}
|
|
160
|
-
catch (ex) {
|
|
161
|
-
if (ex?.isAxiosError) {
|
|
162
|
-
return this.handleAxiosError(ex);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return {};
|
|
166
|
-
};
|
|
167
|
-
delete = async (endpoint, contentType, options) => {
|
|
168
|
-
const response = await Axios.delete(endpoint, this._axiosOptions({ contentType, ...(options || {}) }));
|
|
169
|
-
const data = this.handleResponse(response);
|
|
170
|
-
return data;
|
|
171
|
-
};
|
|
172
|
-
getFileName = (contentDispositionValue) => {
|
|
173
|
-
var filename = undefined;
|
|
174
|
-
if (contentDispositionValue && contentDispositionValue.indexOf('attachment') !== -1) {
|
|
175
|
-
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
|
|
176
|
-
var matches = filenameRegex.exec(contentDispositionValue);
|
|
177
|
-
if (matches != null && matches[1]) {
|
|
178
|
-
filename = matches[1].replace(/['"]/g, '');
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
return filename;
|
|
182
|
-
};
|
|
183
|
-
downloadFile = async (endpoint, params, method, options) => {
|
|
184
|
-
let response;
|
|
185
|
-
const headers = {};
|
|
186
|
-
// return authorization header with jwt token
|
|
187
|
-
const token = this.accessToken; // || AuthService.getAuthToken();
|
|
188
|
-
if (token) {
|
|
189
|
-
headers['Authorization'] = `Bearer ${token}`;
|
|
190
|
-
}
|
|
191
|
-
const axiosOptions = { headers, ...(options || {}) };
|
|
192
|
-
if (method === HttpMethods.post) {
|
|
193
|
-
response = Axios.post(endpoint, params, {
|
|
194
|
-
...this._axiosOptions(axiosOptions),
|
|
195
|
-
responseType: 'blob',
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
else {
|
|
199
|
-
response = Axios.get(endpoint, {
|
|
200
|
-
params,
|
|
201
|
-
paramsSerializer: {
|
|
202
|
-
serialize: (p, o) => {
|
|
203
|
-
return stringify(params, {
|
|
204
|
-
arrayFormat: 'indices',
|
|
205
|
-
allowDots: true,
|
|
206
|
-
skipNulls: true,
|
|
207
|
-
});
|
|
208
|
-
},
|
|
209
|
-
},
|
|
210
|
-
...this._axiosOptions(axiosOptions),
|
|
211
|
-
responseType: 'blob',
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
try {
|
|
215
|
-
const result_1 = await response;
|
|
216
|
-
//extract file name from Content-Disposition header
|
|
217
|
-
const fileName = this.getFileName(result_1.headers.get('Content-Disposition'));
|
|
218
|
-
//invoke 'Save As' dialog
|
|
219
|
-
saveAs(result_1.data, fileName);
|
|
220
|
-
return { status: FileDownloadStatus.done };
|
|
221
|
-
}
|
|
222
|
-
catch (error) {
|
|
223
|
-
if (error.isAxiosError) {
|
|
224
|
-
this.handleResponse(error.response);
|
|
225
|
-
}
|
|
226
|
-
else
|
|
227
|
-
this.handleResponse(error);
|
|
228
|
-
if (error.response) {
|
|
229
|
-
return {
|
|
230
|
-
status: FileDownloadStatus.error,
|
|
231
|
-
error: error.response.status,
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
else {
|
|
235
|
-
return { status: FileDownloadStatus.error, error: error.message };
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
getAuthHeader = (contentType) => {
|
|
240
|
-
const headers = {
|
|
241
|
-
'Content-Type': contentType || 'application/json',
|
|
242
|
-
Accept: 'application/json',
|
|
243
|
-
};
|
|
244
|
-
// return authorization header with jwt token
|
|
245
|
-
const token = this.accessToken; //||AuthService.getAuthToken();
|
|
246
|
-
if (token) {
|
|
247
|
-
headers['Authorization'] = `Bearer ${token}`;
|
|
248
|
-
}
|
|
249
|
-
return headers;
|
|
250
|
-
};
|
|
251
|
-
handleResponse = (response, throwErrorOn401) => {
|
|
252
|
-
if (!response) {
|
|
253
|
-
if (this.handleError)
|
|
254
|
-
this.handleError('No response from the server, please try after some time.');
|
|
255
|
-
}
|
|
256
|
-
else if ([401, 403].indexOf(response.status) !== -1) {
|
|
257
|
-
if (throwErrorOn401) {
|
|
258
|
-
throw response;
|
|
259
|
-
}
|
|
260
|
-
else {
|
|
261
|
-
console.error('401 Unauthorized or 403 Forbidden response returned from api');
|
|
262
|
-
// // auto logout if 401 Unauthorized or 403 Forbidden response returned from api
|
|
263
|
-
// AuthService.logout();
|
|
264
|
-
// window.location.reload();
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
return response?.data;
|
|
268
|
-
};
|
|
269
|
-
handleAxiosError = (error, throwErrorOn401) => {
|
|
270
|
-
if (throwErrorOn401 && error.response?.status === 401)
|
|
271
|
-
throw error;
|
|
272
|
-
else if (error.response?.status === 400) {
|
|
273
|
-
const data = error.response.data;
|
|
274
|
-
if (data && data.errors) {
|
|
275
|
-
const arr = [];
|
|
276
|
-
for (const key in data.errors) {
|
|
277
|
-
if (data.errors[key] && data.errors[key].length > 0) {
|
|
278
|
-
arr.push(data.errors[key][0]);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return {
|
|
282
|
-
result: null,
|
|
283
|
-
status: false,
|
|
284
|
-
message: arr.length > 0 ? arr[0] : '',
|
|
285
|
-
errors: arr,
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return {};
|
|
290
|
-
};
|
|
291
|
-
handleErrorResponse = (message, errors) => {
|
|
292
|
-
if (this.handleError)
|
|
293
|
-
this.handleError(message, errors);
|
|
294
|
-
};
|
|
295
|
-
_axiosOptions = (options) => {
|
|
296
|
-
const { contentType, headers, ...rest } = options || {};
|
|
297
|
-
return {
|
|
298
|
-
headers: headers || this.getAuthHeader(contentType),
|
|
299
|
-
baseURL: this.getBaseUrl(),
|
|
300
|
-
...rest,
|
|
301
|
-
};
|
|
302
|
-
};
|
|
303
|
-
getBaseUrl = () => process.env.REACT_APP_API_URL; //EnvUtils.getEnv('REACT_APP_API_URL');
|
|
304
|
-
}
|
|
305
|
-
export const ApiUtility = new ApiUtilityBase();
|
|
1
|
+
import Axios from 'axios';
|
|
2
|
+
import { saveAs } from 'file-saver';
|
|
3
|
+
import { stringify } from 'qs';
|
|
4
|
+
export var HttpMethods;
|
|
5
|
+
(function (HttpMethods) {
|
|
6
|
+
HttpMethods["get"] = "get";
|
|
7
|
+
HttpMethods["post"] = "post";
|
|
8
|
+
})(HttpMethods || (HttpMethods = {}));
|
|
9
|
+
export var FileDownloadStatus;
|
|
10
|
+
(function (FileDownloadStatus) {
|
|
11
|
+
FileDownloadStatus["downloading"] = "downloading";
|
|
12
|
+
FileDownloadStatus["done"] = "done";
|
|
13
|
+
FileDownloadStatus["error"] = "error";
|
|
14
|
+
})(FileDownloadStatus || (FileDownloadStatus = {}));
|
|
15
|
+
class ApiUtilityBase {
|
|
16
|
+
accessToken;
|
|
17
|
+
handleError = (error, errors) => { };
|
|
18
|
+
// private getParams = (params?: any) => {
|
|
19
|
+
// if (params) {
|
|
20
|
+
// for (const key in params) {
|
|
21
|
+
// if (params[key] == null || params[key] === undefined || params[key] === '')
|
|
22
|
+
// delete params[key];
|
|
23
|
+
// }
|
|
24
|
+
// }
|
|
25
|
+
// return params;
|
|
26
|
+
// };
|
|
27
|
+
getResponse = async (endpoint, params, options) => {
|
|
28
|
+
return await Axios.get(endpoint, {
|
|
29
|
+
params,
|
|
30
|
+
paramsSerializer: {
|
|
31
|
+
serialize: (p, o) => {
|
|
32
|
+
return stringify(params, {
|
|
33
|
+
arrayFormat: 'indices',
|
|
34
|
+
allowDots: true,
|
|
35
|
+
skipNulls: true,
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
...this._axiosOptions(options || {}),
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
get = async (endpoint, params, throwErrorOn401, options) => {
|
|
43
|
+
const response = await this.getResponse(endpoint, params, options);
|
|
44
|
+
const data = this.handleResponse(response, throwErrorOn401);
|
|
45
|
+
return data;
|
|
46
|
+
};
|
|
47
|
+
getResult = async (endpoint, params, throwErrorOn401, options) => {
|
|
48
|
+
try {
|
|
49
|
+
const data = await this.get(endpoint, params, undefined, options);
|
|
50
|
+
if (data.status) {
|
|
51
|
+
return data.result;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
this.handleErrorResponse(data.message, data.errors);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (error.isAxiosError)
|
|
59
|
+
this.handleAxiosError(error, throwErrorOn401);
|
|
60
|
+
else
|
|
61
|
+
this.handleResponse(error, throwErrorOn401);
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
};
|
|
65
|
+
post = async (endpoint, body, contentType, options) => {
|
|
66
|
+
try {
|
|
67
|
+
const response = await Axios.post(endpoint, body, this._axiosOptions({ contentType, ...(options || {}) }));
|
|
68
|
+
const data = this.handleResponse(response);
|
|
69
|
+
return data;
|
|
70
|
+
}
|
|
71
|
+
catch (ex) {
|
|
72
|
+
if (ex?.isAxiosError) {
|
|
73
|
+
return this.handleAxiosError(ex);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return {};
|
|
77
|
+
};
|
|
78
|
+
put = async (endpoint, body, contentType, options) => {
|
|
79
|
+
try {
|
|
80
|
+
const response = await Axios.put(endpoint, body, this._axiosOptions({ contentType, ...(options || {}) }));
|
|
81
|
+
const data = this.handleResponse(response);
|
|
82
|
+
return data;
|
|
83
|
+
}
|
|
84
|
+
catch (ex) {
|
|
85
|
+
if (ex?.isAxiosError) {
|
|
86
|
+
return this.handleAxiosError(ex);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return {};
|
|
90
|
+
};
|
|
91
|
+
appendFormDataValues = (form, values, preFix) => {
|
|
92
|
+
if (!preFix || preFix.length <= 0)
|
|
93
|
+
preFix = '';
|
|
94
|
+
//check if given object is a string/number/boolean then add directly to the list with prefix
|
|
95
|
+
if ((typeof values !== 'function' && typeof values !== 'object' && typeof values !== 'symbol') || values instanceof File) {
|
|
96
|
+
if (values && preFix) {
|
|
97
|
+
form.append(preFix, values);
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
for (const key in values) {
|
|
102
|
+
//prepare a field name
|
|
103
|
+
const fieldName = `${preFix}${preFix && !preFix.endsWith('.') ? '.' : ''}${key}`;
|
|
104
|
+
if (values[key] instanceof FileList) {
|
|
105
|
+
const fileList = values[key];
|
|
106
|
+
for (let fIndex = 0; fIndex < fileList.length; fIndex++) {
|
|
107
|
+
form.append(`${fieldName}[${fIndex}]`, fileList[fIndex]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
if (Array.isArray(values[key])) {
|
|
112
|
+
values[key].forEach((el, idx) => {
|
|
113
|
+
this.appendFormDataValues(form, el, `${fieldName}[${idx}]`);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
if (typeof values[key] === 'object') {
|
|
118
|
+
this.appendFormDataValues(form, values[key], fieldName);
|
|
119
|
+
}
|
|
120
|
+
else if (values[key]) {
|
|
121
|
+
form.append(fieldName, values[key]);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
postForm = async (endpoint, params, options) => {
|
|
128
|
+
let formData = new FormData();
|
|
129
|
+
if (params instanceof FormData) {
|
|
130
|
+
formData = params;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
this.appendFormDataValues(formData, params);
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
const response = await Axios.post(endpoint, formData, this._axiosOptions({ contentType: 'multipart/form-data', ...(options || {}) }));
|
|
137
|
+
const data = this.handleResponse(response);
|
|
138
|
+
return data;
|
|
139
|
+
}
|
|
140
|
+
catch (ex) {
|
|
141
|
+
if (ex?.isAxiosError) {
|
|
142
|
+
return this.handleAxiosError(ex);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return {};
|
|
146
|
+
};
|
|
147
|
+
putForm = async (endpoint, params, options) => {
|
|
148
|
+
let formData = new FormData();
|
|
149
|
+
if (params instanceof FormData) {
|
|
150
|
+
formData = params;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
this.appendFormDataValues(formData, params);
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
const response = await Axios.putForm(endpoint, formData, this._axiosOptions({ contentType: 'multipart/form-data', ...(options || {}) }));
|
|
157
|
+
const data = this.handleResponse(response);
|
|
158
|
+
return data;
|
|
159
|
+
}
|
|
160
|
+
catch (ex) {
|
|
161
|
+
if (ex?.isAxiosError) {
|
|
162
|
+
return this.handleAxiosError(ex);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return {};
|
|
166
|
+
};
|
|
167
|
+
delete = async (endpoint, contentType, options) => {
|
|
168
|
+
const response = await Axios.delete(endpoint, this._axiosOptions({ contentType, ...(options || {}) }));
|
|
169
|
+
const data = this.handleResponse(response);
|
|
170
|
+
return data;
|
|
171
|
+
};
|
|
172
|
+
getFileName = (contentDispositionValue) => {
|
|
173
|
+
var filename = undefined;
|
|
174
|
+
if (contentDispositionValue && contentDispositionValue.indexOf('attachment') !== -1) {
|
|
175
|
+
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
|
|
176
|
+
var matches = filenameRegex.exec(contentDispositionValue);
|
|
177
|
+
if (matches != null && matches[1]) {
|
|
178
|
+
filename = matches[1].replace(/['"]/g, '');
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return filename;
|
|
182
|
+
};
|
|
183
|
+
downloadFile = async (endpoint, params, method, options) => {
|
|
184
|
+
let response;
|
|
185
|
+
const headers = {};
|
|
186
|
+
// return authorization header with jwt token
|
|
187
|
+
const token = this.accessToken; // || AuthService.getAuthToken();
|
|
188
|
+
if (token) {
|
|
189
|
+
headers['Authorization'] = `Bearer ${token}`;
|
|
190
|
+
}
|
|
191
|
+
const axiosOptions = { headers, ...(options || {}) };
|
|
192
|
+
if (method === HttpMethods.post) {
|
|
193
|
+
response = Axios.post(endpoint, params, {
|
|
194
|
+
...this._axiosOptions(axiosOptions),
|
|
195
|
+
responseType: 'blob',
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
response = Axios.get(endpoint, {
|
|
200
|
+
params,
|
|
201
|
+
paramsSerializer: {
|
|
202
|
+
serialize: (p, o) => {
|
|
203
|
+
return stringify(params, {
|
|
204
|
+
arrayFormat: 'indices',
|
|
205
|
+
allowDots: true,
|
|
206
|
+
skipNulls: true,
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
...this._axiosOptions(axiosOptions),
|
|
211
|
+
responseType: 'blob',
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
const result_1 = await response;
|
|
216
|
+
//extract file name from Content-Disposition header
|
|
217
|
+
const fileName = this.getFileName(result_1.headers.get('Content-Disposition'));
|
|
218
|
+
//invoke 'Save As' dialog
|
|
219
|
+
saveAs(result_1.data, fileName);
|
|
220
|
+
return { status: FileDownloadStatus.done };
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
if (error.isAxiosError) {
|
|
224
|
+
this.handleResponse(error.response);
|
|
225
|
+
}
|
|
226
|
+
else
|
|
227
|
+
this.handleResponse(error);
|
|
228
|
+
if (error.response) {
|
|
229
|
+
return {
|
|
230
|
+
status: FileDownloadStatus.error,
|
|
231
|
+
error: error.response.status,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
return { status: FileDownloadStatus.error, error: error.message };
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
getAuthHeader = (contentType) => {
|
|
240
|
+
const headers = {
|
|
241
|
+
'Content-Type': contentType || 'application/json',
|
|
242
|
+
Accept: 'application/json',
|
|
243
|
+
};
|
|
244
|
+
// return authorization header with jwt token
|
|
245
|
+
const token = this.accessToken; //||AuthService.getAuthToken();
|
|
246
|
+
if (token) {
|
|
247
|
+
headers['Authorization'] = `Bearer ${token}`;
|
|
248
|
+
}
|
|
249
|
+
return headers;
|
|
250
|
+
};
|
|
251
|
+
handleResponse = (response, throwErrorOn401) => {
|
|
252
|
+
if (!response) {
|
|
253
|
+
if (this.handleError)
|
|
254
|
+
this.handleError('No response from the server, please try after some time.');
|
|
255
|
+
}
|
|
256
|
+
else if ([401, 403].indexOf(response.status) !== -1) {
|
|
257
|
+
if (throwErrorOn401) {
|
|
258
|
+
throw response;
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
console.error('401 Unauthorized or 403 Forbidden response returned from api');
|
|
262
|
+
// // auto logout if 401 Unauthorized or 403 Forbidden response returned from api
|
|
263
|
+
// AuthService.logout();
|
|
264
|
+
// window.location.reload();
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return response?.data;
|
|
268
|
+
};
|
|
269
|
+
handleAxiosError = (error, throwErrorOn401) => {
|
|
270
|
+
if (throwErrorOn401 && error.response?.status === 401)
|
|
271
|
+
throw error;
|
|
272
|
+
else if (error.response?.status === 400) {
|
|
273
|
+
const data = error.response.data;
|
|
274
|
+
if (data && data.errors) {
|
|
275
|
+
const arr = [];
|
|
276
|
+
for (const key in data.errors) {
|
|
277
|
+
if (data.errors[key] && data.errors[key].length > 0) {
|
|
278
|
+
arr.push(data.errors[key][0]);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return {
|
|
282
|
+
result: null,
|
|
283
|
+
status: false,
|
|
284
|
+
message: arr.length > 0 ? arr[0] : '',
|
|
285
|
+
errors: arr,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return {};
|
|
290
|
+
};
|
|
291
|
+
handleErrorResponse = (message, errors) => {
|
|
292
|
+
if (this.handleError)
|
|
293
|
+
this.handleError(message, errors);
|
|
294
|
+
};
|
|
295
|
+
_axiosOptions = (options) => {
|
|
296
|
+
const { contentType, headers, ...rest } = options || {};
|
|
297
|
+
return {
|
|
298
|
+
headers: headers || this.getAuthHeader(contentType),
|
|
299
|
+
baseURL: this.getBaseUrl(),
|
|
300
|
+
...rest,
|
|
301
|
+
};
|
|
302
|
+
};
|
|
303
|
+
getBaseUrl = () => process.env.REACT_APP_API_URL; //EnvUtils.getEnv('REACT_APP_API_URL');
|
|
304
|
+
}
|
|
305
|
+
export const ApiUtility = new ApiUtilityBase();
|