@qlibs/utils 1.5.4 → 1.6.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
+ };
@@ -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';
@@ -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);
@@ -89,12 +89,12 @@ function parseFormattedNumber(formattedString, locale = 'id-ID', currency) {
89
89
  ? currency
90
90
  ? uniqueSeparators[1]
91
91
  : uniqueSeparators[0]
92
- : ',';
92
+ : uniqueSeparators[0] || ',';
93
93
  const decimalSeparator = uniqueSeparators.length > 1
94
94
  ? currency
95
95
  ? uniqueSeparators[2]
96
96
  : uniqueSeparators[1]
97
- : '.';
97
+ : uniqueSeparators[1] || '.';
98
98
  const isNegative = formattedString.includes('-');
99
99
  let normalizedString = formattedString.replace(/[^\d.,]/g, '');
100
100
  const thousandSeparatorRegex = new RegExp(`[\\${thousandSeparator}\\s]`, 'g');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qlibs/utils",
3
- "version": "1.5.4",
3
+ "version": "1.6.0",
4
4
  "description": "",
5
5
  "author": "QBIT Developer",
6
6
  "license": "MIT",