trithuc-mvc-react 3.4.2 → 3.4.3

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.
Files changed (2) hide show
  1. package/api/index.js +54 -8
  2. package/package.json +6 -5
package/api/index.js CHANGED
@@ -1,41 +1,87 @@
1
+ import qs from "qs"; // 👈 serialize query params
1
2
  import { storeGetlanguage, storeGetToken } from "@/utils/storage";
2
3
  import axios from "axios";
3
4
 
4
5
  let baseUrl = "";
5
6
  let apiUrl = "";
6
7
 
8
+ /**
9
+ * Cấu hình base URL cho toàn ứng dụng
10
+ */
7
11
  export function configure({ customBaseUrl, customApiUrl }) {
8
12
  if (customBaseUrl) {
9
13
  baseUrl = customBaseUrl;
10
- apiUrl = customApiUrl;
14
+ apiUrl = customApiUrl || customBaseUrl;
11
15
  } else {
12
- console.warn("baseUrl chưa được định cấu hình. Sử dụng giá trị mặc định.");
16
+ console.warn("⚠️ baseUrl chưa được định cấu hình. Sử dụng giá trị mặc định.");
13
17
  }
14
18
  }
15
19
 
20
+ /**
21
+ * Lấy base URL hiện tại
22
+ */
16
23
  export function getBaseUrl() {
17
24
  return baseUrl;
18
25
  }
19
26
 
27
+ /**
28
+ * Tạo instance axios chung
29
+ */
20
30
  const api = axios.create({
21
31
  baseURL: getBaseUrl() ?? "/",
32
+ responseType: "json",
22
33
  transitional: {
23
34
  silentJSONParsing: false
24
35
  },
25
- responseType: "json"
26
- });
27
36
 
28
- const token = storeGetToken();
29
- if (token) api.defaults.headers.common["Authorization"] = `Bearer ${token}`;
37
+ // 👇 Tùy chỉnh serialize query params để gửi mảng đúng định dạng
38
+ paramsSerializer: (params) =>
39
+ qs.stringify(params, {
40
+ arrayFormat: "repeat", // => ?a=1&a=2
41
+ skipNulls: true // bỏ các giá trị null
42
+ })
43
+ });
30
44
 
31
- // Interceptor tối ưu hóa
45
+ /**
46
+ * Interceptor REQUEST
47
+ */
32
48
  api.interceptors.request.use((config) => {
49
+ const token = storeGetToken();
33
50
  const language = storeGetlanguage();
34
- config.headers["Authorization"] = `Bearer ${token}`;
51
+
52
+ // 🔐 Thêm Authorization + ngôn ngữ
53
+ if (token) config.headers["Authorization"] = `Bearer ${token}`;
35
54
  if (language) config.headers["Accept-Language"] = language;
55
+
56
+ // ⚙️ Nếu query GET quá dài, tự động chuyển sang POST
57
+ if (config.method?.toUpperCase() === "GET" && config.params) {
58
+ const queryString = qs.stringify(config.params, { arrayFormat: "repeat" });
59
+
60
+ if (queryString.length > 2000) {
61
+ config.method = "POST";
62
+ config.data = config.params;
63
+ delete config.params;
64
+ console.warn("⚙️ Query quá dài → tự động chuyển sang POST:", config.url);
65
+ }
66
+ }
67
+
36
68
  return config;
37
69
  });
38
70
 
71
+ /**
72
+ * Interceptor RESPONSE (có thể mở rộng sau này, ví dụ refresh token)
73
+ */
74
+ api.interceptors.response.use(
75
+ (response) => response,
76
+ (error) => {
77
+ if (error.response?.status === 401) {
78
+ console.error("🚫 Token hết hạn hoặc không hợp lệ.");
79
+ // 👉 Có thể thêm xử lý refresh token ở đây sau này
80
+ }
81
+ return Promise.reject(error);
82
+ }
83
+ );
84
+
39
85
  export default api;
40
86
 
41
87
  export const getDatasFromTable = async ({ tableName, page, pageSize, data }) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trithuc-mvc-react",
3
- "version": "3.4.2",
3
+ "version": "3.4.3",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -22,6 +22,7 @@
22
22
  "lodash": "^4.17.21",
23
23
  "moment": "^2.30.1",
24
24
  "prop-types": "^15.8.1",
25
+ "qs": "^6.14.0",
25
26
  "yup": "^1.6.1"
26
27
  },
27
28
  "devDependencies": {
@@ -35,18 +36,18 @@
35
36
  "react-dom": "^18.3.1"
36
37
  },
37
38
  "peerDependencies": {
39
+ "@mui/icons-material": "^7.0.2",
38
40
  "@mui/lab": "^7.0.0-beta.11",
39
41
  "@mui/material": "^7.0.2",
40
42
  "@mui/system": "^5.17.1",
41
- "@mui/icons-material": "^7.0.2",
42
- "@mui/x-tree-view": "^7.28.1",
43
43
  "@mui/x-date-pickers": "^7.28.3",
44
+ "@mui/x-tree-view": "^7.28.1",
45
+ "dayjs": "^1.11.13",
44
46
  "material-ui-confirm": "^4.0.0",
45
47
  "react": ">=16",
46
48
  "react-dom": ">=16",
47
49
  "react-hook-form": "^7.55.0",
48
50
  "react-query": "^3.39.3",
49
- "react-toastify": "^11.0.5",
50
- "dayjs": "^1.11.13"
51
+ "react-toastify": "^11.0.5"
51
52
  }
52
53
  }