@fctc/edu-logic-lib 1.0.2 → 1.0.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.
- package/dist/config.js +259 -4
- package/dist/config.mjs +255 -6
- package/dist/constants.js +154 -30
- package/dist/constants.mjs +140 -29
- package/dist/environment.js +841 -7
- package/dist/environment.mjs +840 -14
- package/dist/hooks.d.mts +2 -0
- package/dist/hooks.d.ts +2 -0
- package/dist/hooks.js +4372 -283
- package/dist/hooks.mjs +4275 -237
- package/dist/index.d.mts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +5323 -0
- package/dist/index.mjs +5165 -0
- package/dist/models.js +3200 -18
- package/dist/models.mjs +3192 -18
- package/dist/provider.js +3179 -33
- package/dist/provider.mjs +3171 -33
- package/dist/services.js +4028 -9
- package/dist/services.mjs +4031 -25
- package/dist/store.js +582 -128
- package/dist/store.mjs +502 -111
- package/dist/types.js +2 -1
- package/dist/types.mjs +1 -0
- package/dist/utils.js +2346 -20
- package/dist/utils.mjs +2343 -33
- package/package.json +1 -1
- package/dist/chunk-6BLY7NZ6.mjs +0 -124
- package/dist/chunk-6QXB3XX7.mjs +0 -258
- package/dist/chunk-ELARQVCE.mjs +0 -2364
- package/dist/chunk-FVGPSTJ7.js +0 -124
- package/dist/chunk-GGOFXFSX.js +0 -2364
- package/dist/chunk-IXDDYGKE.js +0 -61
- package/dist/chunk-MJLXGYQ4.mjs +0 -102
- package/dist/chunk-MLJQPO4Q.mjs +0 -61
- package/dist/chunk-QLUONJPQ.mjs +0 -604
- package/dist/chunk-RZBHZYXG.js +0 -604
- package/dist/chunk-S7B3VKMJ.mjs +0 -95
- package/dist/chunk-S7YF2I23.js +0 -95
- package/dist/chunk-U4CC2BBB.js +0 -1074
- package/dist/chunk-UY6GNZNB.js +0 -258
- package/dist/chunk-W4W2L2NA.js +0 -102
- package/dist/chunk-WYXAE5LI.mjs +0 -1074
package/dist/config.js
CHANGED
|
@@ -1,7 +1,262 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
require('./chunk-IXDDYGKE.js');
|
|
3
|
+
var axios = require('axios');
|
|
5
4
|
|
|
5
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
var axios__default = /*#__PURE__*/_interopDefault(axios);
|
|
8
|
+
|
|
9
|
+
// src/config/axios-client.ts
|
|
10
|
+
var MAINT_KEY = "MAINTENANCE_ACTIVE";
|
|
11
|
+
var MAINT_AT = "MAINTENANCE_AT";
|
|
12
|
+
var MAINT_LAST_PATH = "MAINTENANCE_LAST_PATH";
|
|
13
|
+
var hasRedirectedToMaintenance = false;
|
|
14
|
+
function setMaintenanceFlags() {
|
|
15
|
+
if (typeof window === "undefined") return;
|
|
16
|
+
const { pathname, search } = window.location;
|
|
17
|
+
const lastPath = pathname + (search || "");
|
|
18
|
+
if (pathname !== "/maintenance" && !window.localStorage.getItem(MAINT_LAST_PATH)) {
|
|
19
|
+
window.localStorage.setItem(MAINT_LAST_PATH, lastPath);
|
|
20
|
+
}
|
|
21
|
+
window.localStorage.setItem(MAINT_KEY, "true");
|
|
22
|
+
window.localStorage.setItem(MAINT_AT, String(Date.now()));
|
|
23
|
+
}
|
|
24
|
+
async function clearMaintenanceAndExit(getToken, opts) {
|
|
25
|
+
if (typeof window === "undefined") return;
|
|
26
|
+
const forceLogin = opts?.forceLogin === true;
|
|
27
|
+
const clearTokenOnForce = opts?.clearTokenOnForce !== false;
|
|
28
|
+
window.localStorage.removeItem(MAINT_KEY);
|
|
29
|
+
window.localStorage.removeItem(MAINT_AT);
|
|
30
|
+
const lastPath = window.localStorage.getItem(MAINT_LAST_PATH);
|
|
31
|
+
window.localStorage.removeItem(MAINT_LAST_PATH);
|
|
32
|
+
try {
|
|
33
|
+
if (forceLogin) {
|
|
34
|
+
if (clearTokenOnForce) {
|
|
35
|
+
try {
|
|
36
|
+
await opts?.clearToken?.();
|
|
37
|
+
} catch {
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
window.location.replace("/login");
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const token = await getToken();
|
|
44
|
+
if (token) {
|
|
45
|
+
const target = lastPath && lastPath !== "/maintenance" ? lastPath : "/";
|
|
46
|
+
window.location.replace(target);
|
|
47
|
+
} else {
|
|
48
|
+
window.location.replace("/login");
|
|
49
|
+
}
|
|
50
|
+
} catch {
|
|
51
|
+
window.location.replace("/login");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
var axiosClient = {
|
|
55
|
+
init(config) {
|
|
56
|
+
const localStorage = config.localStorageUtils;
|
|
57
|
+
const sessionStorage = config.sessionStorageUtils;
|
|
58
|
+
const db = config.db;
|
|
59
|
+
let isRefreshing = false;
|
|
60
|
+
let failedQueue = [];
|
|
61
|
+
const processQueue = (error, token = null) => {
|
|
62
|
+
failedQueue?.forEach((prom) => {
|
|
63
|
+
if (error) {
|
|
64
|
+
prom.reject(error);
|
|
65
|
+
} else {
|
|
66
|
+
prom.resolve(token);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
failedQueue = [];
|
|
70
|
+
};
|
|
71
|
+
const instance = axios__default.default.create({
|
|
72
|
+
adapter: axios__default.default.defaults.adapter,
|
|
73
|
+
baseURL: config.baseUrl,
|
|
74
|
+
timeout: 5e4,
|
|
75
|
+
paramsSerializer: (params) => new URLSearchParams(params).toString()
|
|
76
|
+
});
|
|
77
|
+
if (typeof window !== "undefined") {
|
|
78
|
+
const isMaint = window.localStorage.getItem(MAINT_KEY) === "true";
|
|
79
|
+
const onMaintenancePage = window.location.pathname === "/maintenance";
|
|
80
|
+
if (isMaint && !onMaintenancePage) {
|
|
81
|
+
hasRedirectedToMaintenance = true;
|
|
82
|
+
window.location.replace("/maintenance");
|
|
83
|
+
}
|
|
84
|
+
if (isMaint && onMaintenancePage) {
|
|
85
|
+
const healthUrl = config.healthUrl || `${(config.baseUrl || "").replace(/\/+$/, "")}/health`;
|
|
86
|
+
(async () => {
|
|
87
|
+
try {
|
|
88
|
+
await axios__default.default.get(healthUrl, { timeout: 8e3 });
|
|
89
|
+
await clearMaintenanceAndExit(() => localStorage.getAccessToken(), {
|
|
90
|
+
forceLogin: true,
|
|
91
|
+
clearTokenOnForce: true,
|
|
92
|
+
clearToken: () => localStorage.clearToken()
|
|
93
|
+
});
|
|
94
|
+
} catch {
|
|
95
|
+
}
|
|
96
|
+
})();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
instance.interceptors.request.use(
|
|
100
|
+
async (configReq) => {
|
|
101
|
+
const token = await localStorage.getAccessToken();
|
|
102
|
+
if (token) {
|
|
103
|
+
configReq.headers["Authorization"] = "Bearer " + token;
|
|
104
|
+
}
|
|
105
|
+
return configReq;
|
|
106
|
+
},
|
|
107
|
+
(error) => Promise.reject(error)
|
|
108
|
+
);
|
|
109
|
+
instance.interceptors.response.use(
|
|
110
|
+
(response) => {
|
|
111
|
+
if (typeof window !== "undefined") {
|
|
112
|
+
const isMaint = window.localStorage.getItem(MAINT_KEY) === "true";
|
|
113
|
+
const onMaintenancePage = window.location.pathname === "/maintenance";
|
|
114
|
+
if (isMaint && onMaintenancePage) {
|
|
115
|
+
(async () => {
|
|
116
|
+
await clearMaintenanceAndExit(
|
|
117
|
+
() => localStorage.getAccessToken(),
|
|
118
|
+
{
|
|
119
|
+
forceLogin: true,
|
|
120
|
+
clearTokenOnForce: true,
|
|
121
|
+
clearToken: () => localStorage.clearToken()
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
})();
|
|
125
|
+
} else if (isMaint) {
|
|
126
|
+
window.localStorage.removeItem(MAINT_KEY);
|
|
127
|
+
window.localStorage.removeItem(MAINT_AT);
|
|
128
|
+
window.localStorage.removeItem(MAINT_LAST_PATH);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return handleResponse(response);
|
|
132
|
+
},
|
|
133
|
+
async (error) => {
|
|
134
|
+
const status = error?.response?.status;
|
|
135
|
+
if (status === 503) {
|
|
136
|
+
if (typeof window !== "undefined") {
|
|
137
|
+
setMaintenanceFlags();
|
|
138
|
+
if (!hasRedirectedToMaintenance && window.location.pathname !== "/maintenance") {
|
|
139
|
+
hasRedirectedToMaintenance = true;
|
|
140
|
+
window.location.replace("/maintenance");
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return Promise.reject({
|
|
144
|
+
code: 503,
|
|
145
|
+
message: "SERVICE_UNAVAILABLE",
|
|
146
|
+
original: error?.response?.data
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
const handleError = async (err) => {
|
|
150
|
+
if (!err.response) {
|
|
151
|
+
return err;
|
|
152
|
+
}
|
|
153
|
+
const { data } = err.response;
|
|
154
|
+
if (data && data.code === 400 && ["invalid_grant"].includes(data.data?.error)) {
|
|
155
|
+
await clearAuthToken();
|
|
156
|
+
}
|
|
157
|
+
return data;
|
|
158
|
+
};
|
|
159
|
+
const originalRequest = error.config;
|
|
160
|
+
if ((error.response?.status === 403 || error.response?.status === 401 || error.response?.status === 404) && ["TOKEN_EXPIRED", "AUTHEN_FAIL", 401].includes(
|
|
161
|
+
error.response.data.code
|
|
162
|
+
)) {
|
|
163
|
+
if (isRefreshing) {
|
|
164
|
+
return new Promise(function(resolve, reject) {
|
|
165
|
+
failedQueue.push({ resolve, reject });
|
|
166
|
+
}).then((token) => {
|
|
167
|
+
originalRequest.headers["Authorization"] = "Bearer " + token;
|
|
168
|
+
return instance.request(originalRequest);
|
|
169
|
+
}).catch(async (err) => {
|
|
170
|
+
if ((err.response?.status === 400 || err.response?.status === 401) && ["invalid_grant"].includes(err.response.data.error)) {
|
|
171
|
+
await clearAuthToken();
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
const browserSession = await sessionStorage.getBrowserSession();
|
|
176
|
+
const refreshToken = await localStorage.getRefreshToken();
|
|
177
|
+
const accessTokenExp = await localStorage.getAccessToken();
|
|
178
|
+
isRefreshing = true;
|
|
179
|
+
if (!refreshToken && (!browserSession || browserSession == "unActive")) {
|
|
180
|
+
await clearAuthToken();
|
|
181
|
+
} else {
|
|
182
|
+
const payload = Object.fromEntries(
|
|
183
|
+
Object.entries({
|
|
184
|
+
refresh_token: refreshToken,
|
|
185
|
+
grant_type: "refresh_token",
|
|
186
|
+
client_id: config.config.clientId,
|
|
187
|
+
client_secret: config.config.clientSecret
|
|
188
|
+
}).filter(([_, value]) => !!value)
|
|
189
|
+
);
|
|
190
|
+
return new Promise(function(resolve) {
|
|
191
|
+
axios__default.default.post(
|
|
192
|
+
`${config.baseUrl}${"/authentication/oauth2/token" /* AUTH_TOKEN_PATH */}`,
|
|
193
|
+
payload,
|
|
194
|
+
{
|
|
195
|
+
headers: {
|
|
196
|
+
"Content-Type": "multipart/form-data",
|
|
197
|
+
Authorization: `Bearer ${accessTokenExp}`
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
).then(async (res) => {
|
|
201
|
+
const data = res.data;
|
|
202
|
+
await localStorage.setToken(data.access_token);
|
|
203
|
+
await localStorage.setRefreshToken(data.refresh_token);
|
|
204
|
+
axios__default.default.defaults.headers.common["Authorization"] = "Bearer " + data.access_token;
|
|
205
|
+
originalRequest.headers["Authorization"] = "Bearer " + data.access_token;
|
|
206
|
+
processQueue(null, data.access_token);
|
|
207
|
+
resolve(instance.request(originalRequest));
|
|
208
|
+
}).catch(async (err) => {
|
|
209
|
+
if (err && (err?.error_code === "AUTHEN_FAIL" || err?.error_code === "TOKEN_EXPIRED" || err?.error_code === "TOKEN_INCORRECT" || err?.code === "ERR_BAD_REQUEST")) {
|
|
210
|
+
await clearAuthToken();
|
|
211
|
+
}
|
|
212
|
+
if (err && err.response) {
|
|
213
|
+
const { error_code } = err.response?.data || {};
|
|
214
|
+
if (error_code === "AUTHEN_FAIL") {
|
|
215
|
+
await clearAuthToken();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
processQueue(err, null);
|
|
219
|
+
}).finally(() => {
|
|
220
|
+
isRefreshing = false;
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return Promise.reject(await handleError(error));
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
const handleResponse = (res) => {
|
|
229
|
+
if (res && res.data) {
|
|
230
|
+
return res.data;
|
|
231
|
+
}
|
|
232
|
+
return res;
|
|
233
|
+
};
|
|
234
|
+
const clearAuthToken = async () => {
|
|
235
|
+
await localStorage.clearToken();
|
|
236
|
+
if (typeof window !== "undefined") {
|
|
237
|
+
window.location.href = `/login`;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
function formatUrl(url, db2) {
|
|
241
|
+
return url + (db2 ? "?db=" + db2 : "");
|
|
242
|
+
}
|
|
243
|
+
const responseBody = (response) => response;
|
|
244
|
+
const requests = {
|
|
245
|
+
get: (url, headers) => instance.get(formatUrl(url, db), headers).then(responseBody),
|
|
246
|
+
post: (url, body, headers) => instance.post(formatUrl(url, db), body, { headers }).then(responseBody),
|
|
247
|
+
post_excel: (url, body, headers) => instance.post(formatUrl(url, db), body, {
|
|
248
|
+
responseType: "arraybuffer",
|
|
249
|
+
headers: {
|
|
250
|
+
"Content-Type": typeof window !== "undefined" ? "application/json" : "application/javascript",
|
|
251
|
+
Accept: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
252
|
+
}
|
|
253
|
+
}).then(responseBody),
|
|
254
|
+
put: (url, body, headers) => instance.put(formatUrl(url, db), body, headers).then(responseBody),
|
|
255
|
+
patch: (url, body) => instance.patch(formatUrl(url, db), body).then(responseBody),
|
|
256
|
+
delete: (url, body) => instance.delete(formatUrl(url, db), body).then(responseBody)
|
|
257
|
+
};
|
|
258
|
+
return requests;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
exports.axiosClient = axiosClient;
|
package/dist/config.mjs
CHANGED
|
@@ -1,7 +1,256 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
|
|
3
|
+
// src/config/axios-client.ts
|
|
4
|
+
var MAINT_KEY = "MAINTENANCE_ACTIVE";
|
|
5
|
+
var MAINT_AT = "MAINTENANCE_AT";
|
|
6
|
+
var MAINT_LAST_PATH = "MAINTENANCE_LAST_PATH";
|
|
7
|
+
var hasRedirectedToMaintenance = false;
|
|
8
|
+
function setMaintenanceFlags() {
|
|
9
|
+
if (typeof window === "undefined") return;
|
|
10
|
+
const { pathname, search } = window.location;
|
|
11
|
+
const lastPath = pathname + (search || "");
|
|
12
|
+
if (pathname !== "/maintenance" && !window.localStorage.getItem(MAINT_LAST_PATH)) {
|
|
13
|
+
window.localStorage.setItem(MAINT_LAST_PATH, lastPath);
|
|
14
|
+
}
|
|
15
|
+
window.localStorage.setItem(MAINT_KEY, "true");
|
|
16
|
+
window.localStorage.setItem(MAINT_AT, String(Date.now()));
|
|
17
|
+
}
|
|
18
|
+
async function clearMaintenanceAndExit(getToken, opts) {
|
|
19
|
+
if (typeof window === "undefined") return;
|
|
20
|
+
const forceLogin = opts?.forceLogin === true;
|
|
21
|
+
const clearTokenOnForce = opts?.clearTokenOnForce !== false;
|
|
22
|
+
window.localStorage.removeItem(MAINT_KEY);
|
|
23
|
+
window.localStorage.removeItem(MAINT_AT);
|
|
24
|
+
const lastPath = window.localStorage.getItem(MAINT_LAST_PATH);
|
|
25
|
+
window.localStorage.removeItem(MAINT_LAST_PATH);
|
|
26
|
+
try {
|
|
27
|
+
if (forceLogin) {
|
|
28
|
+
if (clearTokenOnForce) {
|
|
29
|
+
try {
|
|
30
|
+
await opts?.clearToken?.();
|
|
31
|
+
} catch {
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
window.location.replace("/login");
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const token = await getToken();
|
|
38
|
+
if (token) {
|
|
39
|
+
const target = lastPath && lastPath !== "/maintenance" ? lastPath : "/";
|
|
40
|
+
window.location.replace(target);
|
|
41
|
+
} else {
|
|
42
|
+
window.location.replace("/login");
|
|
43
|
+
}
|
|
44
|
+
} catch {
|
|
45
|
+
window.location.replace("/login");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
var axiosClient = {
|
|
49
|
+
init(config) {
|
|
50
|
+
const localStorage = config.localStorageUtils;
|
|
51
|
+
const sessionStorage = config.sessionStorageUtils;
|
|
52
|
+
const db = config.db;
|
|
53
|
+
let isRefreshing = false;
|
|
54
|
+
let failedQueue = [];
|
|
55
|
+
const processQueue = (error, token = null) => {
|
|
56
|
+
failedQueue?.forEach((prom) => {
|
|
57
|
+
if (error) {
|
|
58
|
+
prom.reject(error);
|
|
59
|
+
} else {
|
|
60
|
+
prom.resolve(token);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
failedQueue = [];
|
|
64
|
+
};
|
|
65
|
+
const instance = axios.create({
|
|
66
|
+
adapter: axios.defaults.adapter,
|
|
67
|
+
baseURL: config.baseUrl,
|
|
68
|
+
timeout: 5e4,
|
|
69
|
+
paramsSerializer: (params) => new URLSearchParams(params).toString()
|
|
70
|
+
});
|
|
71
|
+
if (typeof window !== "undefined") {
|
|
72
|
+
const isMaint = window.localStorage.getItem(MAINT_KEY) === "true";
|
|
73
|
+
const onMaintenancePage = window.location.pathname === "/maintenance";
|
|
74
|
+
if (isMaint && !onMaintenancePage) {
|
|
75
|
+
hasRedirectedToMaintenance = true;
|
|
76
|
+
window.location.replace("/maintenance");
|
|
77
|
+
}
|
|
78
|
+
if (isMaint && onMaintenancePage) {
|
|
79
|
+
const healthUrl = config.healthUrl || `${(config.baseUrl || "").replace(/\/+$/, "")}/health`;
|
|
80
|
+
(async () => {
|
|
81
|
+
try {
|
|
82
|
+
await axios.get(healthUrl, { timeout: 8e3 });
|
|
83
|
+
await clearMaintenanceAndExit(() => localStorage.getAccessToken(), {
|
|
84
|
+
forceLogin: true,
|
|
85
|
+
clearTokenOnForce: true,
|
|
86
|
+
clearToken: () => localStorage.clearToken()
|
|
87
|
+
});
|
|
88
|
+
} catch {
|
|
89
|
+
}
|
|
90
|
+
})();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
instance.interceptors.request.use(
|
|
94
|
+
async (configReq) => {
|
|
95
|
+
const token = await localStorage.getAccessToken();
|
|
96
|
+
if (token) {
|
|
97
|
+
configReq.headers["Authorization"] = "Bearer " + token;
|
|
98
|
+
}
|
|
99
|
+
return configReq;
|
|
100
|
+
},
|
|
101
|
+
(error) => Promise.reject(error)
|
|
102
|
+
);
|
|
103
|
+
instance.interceptors.response.use(
|
|
104
|
+
(response) => {
|
|
105
|
+
if (typeof window !== "undefined") {
|
|
106
|
+
const isMaint = window.localStorage.getItem(MAINT_KEY) === "true";
|
|
107
|
+
const onMaintenancePage = window.location.pathname === "/maintenance";
|
|
108
|
+
if (isMaint && onMaintenancePage) {
|
|
109
|
+
(async () => {
|
|
110
|
+
await clearMaintenanceAndExit(
|
|
111
|
+
() => localStorage.getAccessToken(),
|
|
112
|
+
{
|
|
113
|
+
forceLogin: true,
|
|
114
|
+
clearTokenOnForce: true,
|
|
115
|
+
clearToken: () => localStorage.clearToken()
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
})();
|
|
119
|
+
} else if (isMaint) {
|
|
120
|
+
window.localStorage.removeItem(MAINT_KEY);
|
|
121
|
+
window.localStorage.removeItem(MAINT_AT);
|
|
122
|
+
window.localStorage.removeItem(MAINT_LAST_PATH);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return handleResponse(response);
|
|
126
|
+
},
|
|
127
|
+
async (error) => {
|
|
128
|
+
const status = error?.response?.status;
|
|
129
|
+
if (status === 503) {
|
|
130
|
+
if (typeof window !== "undefined") {
|
|
131
|
+
setMaintenanceFlags();
|
|
132
|
+
if (!hasRedirectedToMaintenance && window.location.pathname !== "/maintenance") {
|
|
133
|
+
hasRedirectedToMaintenance = true;
|
|
134
|
+
window.location.replace("/maintenance");
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return Promise.reject({
|
|
138
|
+
code: 503,
|
|
139
|
+
message: "SERVICE_UNAVAILABLE",
|
|
140
|
+
original: error?.response?.data
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
const handleError = async (err) => {
|
|
144
|
+
if (!err.response) {
|
|
145
|
+
return err;
|
|
146
|
+
}
|
|
147
|
+
const { data } = err.response;
|
|
148
|
+
if (data && data.code === 400 && ["invalid_grant"].includes(data.data?.error)) {
|
|
149
|
+
await clearAuthToken();
|
|
150
|
+
}
|
|
151
|
+
return data;
|
|
152
|
+
};
|
|
153
|
+
const originalRequest = error.config;
|
|
154
|
+
if ((error.response?.status === 403 || error.response?.status === 401 || error.response?.status === 404) && ["TOKEN_EXPIRED", "AUTHEN_FAIL", 401].includes(
|
|
155
|
+
error.response.data.code
|
|
156
|
+
)) {
|
|
157
|
+
if (isRefreshing) {
|
|
158
|
+
return new Promise(function(resolve, reject) {
|
|
159
|
+
failedQueue.push({ resolve, reject });
|
|
160
|
+
}).then((token) => {
|
|
161
|
+
originalRequest.headers["Authorization"] = "Bearer " + token;
|
|
162
|
+
return instance.request(originalRequest);
|
|
163
|
+
}).catch(async (err) => {
|
|
164
|
+
if ((err.response?.status === 400 || err.response?.status === 401) && ["invalid_grant"].includes(err.response.data.error)) {
|
|
165
|
+
await clearAuthToken();
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const browserSession = await sessionStorage.getBrowserSession();
|
|
170
|
+
const refreshToken = await localStorage.getRefreshToken();
|
|
171
|
+
const accessTokenExp = await localStorage.getAccessToken();
|
|
172
|
+
isRefreshing = true;
|
|
173
|
+
if (!refreshToken && (!browserSession || browserSession == "unActive")) {
|
|
174
|
+
await clearAuthToken();
|
|
175
|
+
} else {
|
|
176
|
+
const payload = Object.fromEntries(
|
|
177
|
+
Object.entries({
|
|
178
|
+
refresh_token: refreshToken,
|
|
179
|
+
grant_type: "refresh_token",
|
|
180
|
+
client_id: config.config.clientId,
|
|
181
|
+
client_secret: config.config.clientSecret
|
|
182
|
+
}).filter(([_, value]) => !!value)
|
|
183
|
+
);
|
|
184
|
+
return new Promise(function(resolve) {
|
|
185
|
+
axios.post(
|
|
186
|
+
`${config.baseUrl}${"/authentication/oauth2/token" /* AUTH_TOKEN_PATH */}`,
|
|
187
|
+
payload,
|
|
188
|
+
{
|
|
189
|
+
headers: {
|
|
190
|
+
"Content-Type": "multipart/form-data",
|
|
191
|
+
Authorization: `Bearer ${accessTokenExp}`
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
).then(async (res) => {
|
|
195
|
+
const data = res.data;
|
|
196
|
+
await localStorage.setToken(data.access_token);
|
|
197
|
+
await localStorage.setRefreshToken(data.refresh_token);
|
|
198
|
+
axios.defaults.headers.common["Authorization"] = "Bearer " + data.access_token;
|
|
199
|
+
originalRequest.headers["Authorization"] = "Bearer " + data.access_token;
|
|
200
|
+
processQueue(null, data.access_token);
|
|
201
|
+
resolve(instance.request(originalRequest));
|
|
202
|
+
}).catch(async (err) => {
|
|
203
|
+
if (err && (err?.error_code === "AUTHEN_FAIL" || err?.error_code === "TOKEN_EXPIRED" || err?.error_code === "TOKEN_INCORRECT" || err?.code === "ERR_BAD_REQUEST")) {
|
|
204
|
+
await clearAuthToken();
|
|
205
|
+
}
|
|
206
|
+
if (err && err.response) {
|
|
207
|
+
const { error_code } = err.response?.data || {};
|
|
208
|
+
if (error_code === "AUTHEN_FAIL") {
|
|
209
|
+
await clearAuthToken();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
processQueue(err, null);
|
|
213
|
+
}).finally(() => {
|
|
214
|
+
isRefreshing = false;
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return Promise.reject(await handleError(error));
|
|
220
|
+
}
|
|
221
|
+
);
|
|
222
|
+
const handleResponse = (res) => {
|
|
223
|
+
if (res && res.data) {
|
|
224
|
+
return res.data;
|
|
225
|
+
}
|
|
226
|
+
return res;
|
|
227
|
+
};
|
|
228
|
+
const clearAuthToken = async () => {
|
|
229
|
+
await localStorage.clearToken();
|
|
230
|
+
if (typeof window !== "undefined") {
|
|
231
|
+
window.location.href = `/login`;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
function formatUrl(url, db2) {
|
|
235
|
+
return url + (db2 ? "?db=" + db2 : "");
|
|
236
|
+
}
|
|
237
|
+
const responseBody = (response) => response;
|
|
238
|
+
const requests = {
|
|
239
|
+
get: (url, headers) => instance.get(formatUrl(url, db), headers).then(responseBody),
|
|
240
|
+
post: (url, body, headers) => instance.post(formatUrl(url, db), body, { headers }).then(responseBody),
|
|
241
|
+
post_excel: (url, body, headers) => instance.post(formatUrl(url, db), body, {
|
|
242
|
+
responseType: "arraybuffer",
|
|
243
|
+
headers: {
|
|
244
|
+
"Content-Type": typeof window !== "undefined" ? "application/json" : "application/javascript",
|
|
245
|
+
Accept: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
246
|
+
}
|
|
247
|
+
}).then(responseBody),
|
|
248
|
+
put: (url, body, headers) => instance.put(formatUrl(url, db), body, headers).then(responseBody),
|
|
249
|
+
patch: (url, body) => instance.patch(formatUrl(url, db), body).then(responseBody),
|
|
250
|
+
delete: (url, body) => instance.delete(formatUrl(url, db), body).then(responseBody)
|
|
251
|
+
};
|
|
252
|
+
return requests;
|
|
253
|
+
}
|
|
7
254
|
};
|
|
255
|
+
|
|
256
|
+
export { axiosClient };
|