@campxdev/campx-web-utils 0.1.2 → 0.1.4
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/package.json +2 -1
- package/src/config/axios.ts +64 -7
- /package/{export.ts → exports.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@campxdev/campx-web-utils",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"main": "./exports.ts",
|
|
5
5
|
"private": false,
|
|
6
6
|
"dependencies": {
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"@testing-library/jest-dom": "^5.14.1",
|
|
9
9
|
"@testing-library/react": "^13.0.0",
|
|
10
10
|
"@testing-library/user-event": "^13.2.1",
|
|
11
|
+
"react-toastify": "^9.0.1",
|
|
11
12
|
"@types/jest": "^27.0.1",
|
|
12
13
|
"@types/node": "^16.7.13",
|
|
13
14
|
"@types/react": "^18.0.0",
|
package/src/config/axios.ts
CHANGED
|
@@ -1,22 +1,79 @@
|
|
|
1
|
-
import Axios from "axios";
|
|
1
|
+
import Axios, { InternalAxiosRequestConfig } from "axios";
|
|
2
2
|
import Cookies from "js-cookie";
|
|
3
|
+
import { toast } from "react-toastify";
|
|
4
|
+
// import { NetworkStore } from "../components/ErrorBoundary/GlobalNetworkLoadingIndicator";
|
|
3
5
|
|
|
4
6
|
const isDevelopment = process.env.NODE_ENV == "development";
|
|
5
7
|
|
|
6
8
|
const sessionKey = Cookies.get("campx_session_key");
|
|
7
9
|
const tenantCode = isDevelopment
|
|
8
|
-
?
|
|
10
|
+
? Cookies.get("campx_tenant")
|
|
9
11
|
: window.location.hostname.split(".")[0];
|
|
10
|
-
const institutionCode =
|
|
11
|
-
? window.location.pathname.split("/")[2]
|
|
12
|
-
: window.location.pathname.split("/")[1];
|
|
12
|
+
const institutionCode = window.location.pathname.split("/")[1];
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
const openPaymentsKey = Cookies.get("campx_open_payments_key");
|
|
15
|
+
|
|
16
|
+
export const formatParams = (params: any) => {
|
|
17
|
+
return Object.fromEntries(
|
|
18
|
+
Object.entries(params ?? {})?.map((i) => [
|
|
19
|
+
i[0],
|
|
20
|
+
i[1] === "__empty__" ? "" : i[1],
|
|
21
|
+
])
|
|
22
|
+
);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
let axios = Axios.create({
|
|
15
26
|
baseURL: process.env.REACT_APP_API_HOST,
|
|
16
27
|
withCredentials: true,
|
|
17
28
|
headers: {
|
|
18
29
|
"x-tenant-id": tenantCode,
|
|
19
30
|
"x-institution-code": institutionCode,
|
|
20
|
-
|
|
31
|
+
campx_session_key: sessionKey,
|
|
32
|
+
...(openPaymentsKey && {
|
|
33
|
+
campx_open_payments_key: openPaymentsKey,
|
|
34
|
+
}),
|
|
21
35
|
},
|
|
22
36
|
});
|
|
37
|
+
|
|
38
|
+
axios.interceptors.request.use(
|
|
39
|
+
function (config: InternalAxiosRequestConfig) {
|
|
40
|
+
const params = formatParams(config?.params);
|
|
41
|
+
// NetworkStore.update((s) => {
|
|
42
|
+
// s.loading = true;
|
|
43
|
+
// });
|
|
44
|
+
return { ...config, params };
|
|
45
|
+
},
|
|
46
|
+
function (error) {
|
|
47
|
+
// NetworkStore.update((s) => {
|
|
48
|
+
// s.loading = false;
|
|
49
|
+
// });
|
|
50
|
+
return Promise.reject(error);
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
axios.interceptors.response.use(
|
|
55
|
+
function (response) {
|
|
56
|
+
// NetworkStore.update((s) => {
|
|
57
|
+
// s.loading = false;
|
|
58
|
+
// });
|
|
59
|
+
return response;
|
|
60
|
+
},
|
|
61
|
+
function (err) {
|
|
62
|
+
// NetworkStore.update((s) => {
|
|
63
|
+
// s.loading = false;
|
|
64
|
+
// });
|
|
65
|
+
return Promise.reject(err);
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
export default axios;
|
|
70
|
+
|
|
71
|
+
export const axiosErrorToast = (error: any, fallback?: string) => {
|
|
72
|
+
const fallbackMessage = fallback ?? "Something went wrong.";
|
|
73
|
+
const errorMessage =
|
|
74
|
+
typeof error?.response?.data?.message !== "string"
|
|
75
|
+
? error?.response?.data?.message?.join("\n") ?? fallbackMessage
|
|
76
|
+
: error?.response?.data?.message;
|
|
77
|
+
|
|
78
|
+
return toast.error(errorMessage);
|
|
79
|
+
};
|
|
File without changes
|