@qlibs/utils 1.5.5 → 1.7.0

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.
@@ -0,0 +1,2 @@
1
+ import { AxiosInstance } from 'axios';
2
+ export declare function injectAxiosInstance(axiosInstance: AxiosInstance, contentType: 'application/json' | 'multipart/form-data' | string, Modal: any): AxiosInstance;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.injectAxiosInstance = injectAxiosInstance;
4
+ const auth_1 = require("./auth");
5
+ function injectAxiosInstance(axiosInstance, contentType, Modal) {
6
+ axiosInstance.interceptors.request.use(async (config) => {
7
+ config.headers['Content-Type'] = contentType || 'application/json';
8
+ config.headers['Authorization'] = 'Bearer ' + (0, auth_1.getToken)();
9
+ return config;
10
+ }, (error) => {
11
+ console.error('axiosInstance: Error interceptor request:::', error.response);
12
+ return Promise.reject(error);
13
+ });
14
+ axiosInstance.interceptors.response.use((response) => {
15
+ return response;
16
+ }, (error) => {
17
+ console.error(error);
18
+ if (error && error.response) {
19
+ handleNoAuth(error.response, Modal);
20
+ console.error('axiosInstance: Error interceptor response:::', error.response);
21
+ return Promise.reject(error.response);
22
+ }
23
+ else {
24
+ console.error(error);
25
+ }
26
+ });
27
+ return axiosInstance;
28
+ }
29
+ function handleNoAuth(errorResponse, Modal) {
30
+ var _a, _b;
31
+ if (errorResponse.status === 403 ||
32
+ errorResponse.status === 401 ||
33
+ ((_a = errorResponse.data) === null || _a === void 0 ? void 0 : _a.code) === 'err_unauthorized') {
34
+ clear();
35
+ let title, content;
36
+ if (errorResponse.status === 403) {
37
+ title = 'Forbidden';
38
+ content = `You don't have permission. Please contact administrator.`;
39
+ }
40
+ else if (errorResponse.status === 401) {
41
+ title = 'Unauthorized';
42
+ content = `Please login to continue`;
43
+ }
44
+ else if (((_b = errorResponse.data) === null || _b === void 0 ? void 0 : _b.code) === 'err_unauthorized') {
45
+ title = 'Unauthorized.';
46
+ content = `Please login to continue`;
47
+ }
48
+ if (!isModalVisible()) {
49
+ Modal.error({
50
+ className: 'noauth_401403',
51
+ title,
52
+ content,
53
+ onOk: () => {
54
+ window.location.href = '/login';
55
+ },
56
+ onCancel: () => {
57
+ window.location.href = '/login';
58
+ },
59
+ });
60
+ }
61
+ }
62
+ }
63
+ function clear() {
64
+ localStorage.clear();
65
+ }
66
+ const isModalVisible = () => {
67
+ return !!document.querySelector('.noauth_401403');
68
+ };
@@ -0,0 +1,4 @@
1
+ export declare const onFinishFailed: (antdErrorInfo: any, config: {
2
+ showErrorOnToast?: string[];
3
+ toastFunction?: any;
4
+ }) => void;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.onFinishFailed = void 0;
4
+ const onFinishFailed = (antdErrorInfo, config) => {
5
+ console.error('antdErrorInfo', antdErrorInfo);
6
+ const { errorFields } = antdErrorInfo;
7
+ if (config.showErrorOnToast) {
8
+ for (const errorField of errorFields) {
9
+ if (config.showErrorOnToast.includes(errorField.name.join('.')) && (config === null || config === void 0 ? void 0 : config.toastFunction)) {
10
+ config.toastFunction.error(errorField.errors.join(' '));
11
+ }
12
+ }
13
+ }
14
+ };
15
+ exports.onFinishFailed = onFinishFailed;
@@ -1,4 +1,5 @@
1
1
  export * from './auth';
2
+ export * from './axios';
2
3
  export * from './datetime';
3
4
  export * from './errorHandler';
4
5
  export * from './formRules';
@@ -10,3 +11,4 @@ export * from './order';
10
11
  export * from './pagination';
11
12
  export * from './price';
12
13
  export * from './text';
14
+ export * from './formHandler';
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./auth"), exports);
18
+ __exportStar(require("./axios"), exports);
18
19
  __exportStar(require("./datetime"), exports);
19
20
  __exportStar(require("./errorHandler"), exports);
20
21
  __exportStar(require("./formRules"), exports);
@@ -26,3 +27,4 @@ __exportStar(require("./order"), exports);
26
27
  __exportStar(require("./pagination"), exports);
27
28
  __exportStar(require("./price"), exports);
28
29
  __exportStar(require("./text"), exports);
30
+ __exportStar(require("./formHandler"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qlibs/utils",
3
- "version": "1.5.5",
3
+ "version": "1.7.0",
4
4
  "description": "",
5
5
  "author": "QBIT Developer",
6
6
  "license": "MIT",