@fctc/edu-logic-lib 1.1.8 → 1.1.10
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/chunk-227GQM56.mjs +295 -0
- package/dist/chunk-2QWNSNZX.mjs +62 -0
- package/dist/chunk-3B6NWGPJ.js +303 -0
- package/dist/chunk-6BLY7NZ6.mjs +120 -0
- package/dist/chunk-7JD5GMIZ.js +2 -0
- package/dist/chunk-7L75ULY6.js +32 -0
- package/dist/chunk-ELARQVCE.mjs +2344 -0
- package/dist/chunk-FVGPSTJ7.js +122 -0
- package/dist/chunk-GGOFXFSX.js +2362 -0
- package/dist/chunk-IX7MI3OE.mjs +1059 -0
- package/dist/chunk-IXDDYGKE.js +61 -0
- package/dist/chunk-MLJQPO4Q.mjs +57 -0
- package/dist/chunk-OBR6UTC5.mjs +1 -0
- package/dist/chunk-ODQQQ7WA.js +94 -0
- package/dist/chunk-PG7J6HGQ.js +66 -0
- package/dist/chunk-QLUONJPQ.mjs +519 -0
- package/dist/chunk-QYYFZUNV.mjs +90 -0
- package/dist/chunk-RGJT5VWB.mjs +585 -0
- package/dist/chunk-RZBHZYXG.js +582 -0
- package/dist/chunk-S7B3VKMJ.mjs +85 -0
- package/dist/chunk-S7YF2I23.js +95 -0
- package/dist/chunk-SJSYRHBU.js +635 -0
- package/dist/chunk-YIQHTB3S.js +1068 -0
- package/dist/chunk-ZARZLSLP.mjs +29 -0
- package/dist/config.js +6 -257
- package/dist/config.mjs +2 -256
- package/dist/constants.js +53 -152
- package/dist/constants.mjs +2 -141
- package/dist/environment.d.mts +18 -1
- package/dist/environment.d.ts +18 -1
- package/dist/environment.js +16 -840
- package/dist/environment.mjs +4 -841
- package/dist/hooks.d.mts +207 -2
- package/dist/hooks.d.ts +207 -2
- package/dist/hooks.js +180 -3040
- package/dist/hooks.mjs +9 -3048
- package/dist/index-C_nK1Mii.d.mts +19 -0
- package/dist/index-C_nK1Mii.d.ts +19 -0
- package/dist/index.d.mts +8 -6
- package/dist/index.d.ts +8 -6
- package/dist/index.js +626 -4711
- package/dist/index.mjs +12 -4601
- package/dist/models.d.mts +4 -21
- package/dist/models.d.ts +4 -21
- package/dist/models.js +18 -3217
- package/dist/models.mjs +9 -3217
- package/dist/provider.js +17 -3224
- package/dist/provider.mjs +8 -3224
- package/dist/services.d.mts +2 -13
- package/dist/services.d.ts +2 -13
- package/dist/services.js +31 -4032
- package/dist/services.mjs +7 -4032
- package/dist/store.js +248 -576
- package/dist/store.mjs +1 -519
- package/dist/types.js +2 -0
- package/dist/types.mjs +1 -1
- package/dist/use-get-selection-DFh6sc49.d.mts +26 -0
- package/dist/use-get-selection-DFh6sc49.d.ts +26 -0
- package/dist/utils.js +61 -2355
- package/dist/utils.mjs +1 -2344
- package/package.json +1 -1
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
|
|
3
|
+
// src/config/axios-client.ts
|
|
4
|
+
|
|
5
|
+
// src/utils/storage/local-storage.ts
|
|
6
|
+
var localStorageUtils = () => {
|
|
7
|
+
const setToken = async (access_token) => {
|
|
8
|
+
localStorage.setItem("accessToken", access_token);
|
|
9
|
+
};
|
|
10
|
+
const setRefreshToken = async (refresh_token) => {
|
|
11
|
+
localStorage.setItem("refreshToken", refresh_token);
|
|
12
|
+
};
|
|
13
|
+
const getAccessToken = async () => {
|
|
14
|
+
return localStorage.getItem("accessToken");
|
|
15
|
+
};
|
|
16
|
+
const getRefreshToken = async () => {
|
|
17
|
+
return localStorage.getItem("refreshToken");
|
|
18
|
+
};
|
|
19
|
+
const clearToken = async () => {
|
|
20
|
+
localStorage.removeItem("accessToken");
|
|
21
|
+
localStorage.removeItem("refreshToken");
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
setToken,
|
|
25
|
+
setRefreshToken,
|
|
26
|
+
getAccessToken,
|
|
27
|
+
getRefreshToken,
|
|
28
|
+
clearToken
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// src/utils/storage/session-storage.ts
|
|
33
|
+
var sessionStorageUtils = () => {
|
|
34
|
+
const getBrowserSession = async () => {
|
|
35
|
+
return sessionStorage.getItem("browserSession");
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
getBrowserSession
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// src/config/axios-client.ts
|
|
43
|
+
var MAINT_KEY = "MAINTENANCE_ACTIVE";
|
|
44
|
+
var MAINT_AT = "MAINTENANCE_AT";
|
|
45
|
+
var MAINT_LAST_PATH = "MAINTENANCE_LAST_PATH";
|
|
46
|
+
var hasRedirectedToMaintenance = false;
|
|
47
|
+
function setMaintenanceFlags() {
|
|
48
|
+
if (typeof window === "undefined") return;
|
|
49
|
+
const { pathname, search } = window.location;
|
|
50
|
+
const lastPath = pathname + (search || "");
|
|
51
|
+
if (pathname !== "/maintenance" && !window.localStorage.getItem(MAINT_LAST_PATH)) {
|
|
52
|
+
window.localStorage.setItem(MAINT_LAST_PATH, lastPath);
|
|
53
|
+
}
|
|
54
|
+
window.localStorage.setItem(MAINT_KEY, "true");
|
|
55
|
+
window.localStorage.setItem(MAINT_AT, String(Date.now()));
|
|
56
|
+
}
|
|
57
|
+
async function clearMaintenanceAndExit(getToken, opts) {
|
|
58
|
+
if (typeof window === "undefined") return;
|
|
59
|
+
const forceLogin = opts?.forceLogin === true;
|
|
60
|
+
const clearTokenOnForce = opts?.clearTokenOnForce !== false;
|
|
61
|
+
window.localStorage.removeItem(MAINT_KEY);
|
|
62
|
+
window.localStorage.removeItem(MAINT_AT);
|
|
63
|
+
const lastPath = window.localStorage.getItem(MAINT_LAST_PATH);
|
|
64
|
+
window.localStorage.removeItem(MAINT_LAST_PATH);
|
|
65
|
+
try {
|
|
66
|
+
if (forceLogin) {
|
|
67
|
+
if (clearTokenOnForce) {
|
|
68
|
+
try {
|
|
69
|
+
await opts?.clearToken?.();
|
|
70
|
+
} catch {
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
window.location.replace("/login");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const token = await getToken();
|
|
77
|
+
if (token) {
|
|
78
|
+
const target = lastPath && lastPath !== "/maintenance" ? lastPath : "/";
|
|
79
|
+
window.location.replace(target);
|
|
80
|
+
} else {
|
|
81
|
+
window.location.replace("/login");
|
|
82
|
+
}
|
|
83
|
+
} catch {
|
|
84
|
+
window.location.replace("/login");
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
var axiosClient = {
|
|
88
|
+
init(config) {
|
|
89
|
+
const localStorage2 = config.localStorageUtils ?? localStorageUtils();
|
|
90
|
+
const sessionStorage2 = config.sessionStorageUtils ?? sessionStorageUtils();
|
|
91
|
+
const db = config.db;
|
|
92
|
+
let isRefreshing = false;
|
|
93
|
+
let failedQueue = [];
|
|
94
|
+
const processQueue = (error, token = null) => {
|
|
95
|
+
failedQueue?.forEach((prom) => {
|
|
96
|
+
if (error) {
|
|
97
|
+
prom.reject(error);
|
|
98
|
+
} else {
|
|
99
|
+
prom.resolve(token);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
failedQueue = [];
|
|
103
|
+
};
|
|
104
|
+
const instance = axios.create({
|
|
105
|
+
adapter: axios.defaults.adapter,
|
|
106
|
+
baseURL: config.baseUrl,
|
|
107
|
+
timeout: 5e4,
|
|
108
|
+
paramsSerializer: (params) => new URLSearchParams(params).toString()
|
|
109
|
+
});
|
|
110
|
+
if (typeof window !== "undefined") {
|
|
111
|
+
const isMaint = window.localStorage.getItem(MAINT_KEY) === "true";
|
|
112
|
+
const onMaintenancePage = window.location.pathname === "/maintenance";
|
|
113
|
+
if (isMaint && !onMaintenancePage) {
|
|
114
|
+
hasRedirectedToMaintenance = true;
|
|
115
|
+
window.location.replace("/maintenance");
|
|
116
|
+
}
|
|
117
|
+
if (isMaint && onMaintenancePage) {
|
|
118
|
+
const healthUrl = config.healthUrl || `${(config.baseUrl || "").replace(/\/+$/, "")}/health`;
|
|
119
|
+
(async () => {
|
|
120
|
+
try {
|
|
121
|
+
await axios.get(healthUrl, { timeout: 8e3 });
|
|
122
|
+
await clearMaintenanceAndExit(() => localStorage2.getAccessToken(), {
|
|
123
|
+
forceLogin: true,
|
|
124
|
+
clearTokenOnForce: true,
|
|
125
|
+
clearToken: () => localStorage2.clearToken()
|
|
126
|
+
});
|
|
127
|
+
} catch {
|
|
128
|
+
}
|
|
129
|
+
})();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
instance.interceptors.request.use(
|
|
133
|
+
async (configReq) => {
|
|
134
|
+
const token = await localStorage2.getAccessToken();
|
|
135
|
+
if (token) {
|
|
136
|
+
configReq.headers["Authorization"] = "Bearer " + token;
|
|
137
|
+
}
|
|
138
|
+
return configReq;
|
|
139
|
+
},
|
|
140
|
+
(error) => Promise.reject(error)
|
|
141
|
+
);
|
|
142
|
+
instance.interceptors.response.use(
|
|
143
|
+
(response) => {
|
|
144
|
+
if (typeof window !== "undefined") {
|
|
145
|
+
const isMaint = window.localStorage.getItem(MAINT_KEY) === "true";
|
|
146
|
+
const onMaintenancePage = window.location.pathname === "/maintenance";
|
|
147
|
+
if (isMaint && onMaintenancePage) {
|
|
148
|
+
(async () => {
|
|
149
|
+
await clearMaintenanceAndExit(
|
|
150
|
+
() => localStorage2.getAccessToken(),
|
|
151
|
+
{
|
|
152
|
+
forceLogin: true,
|
|
153
|
+
clearTokenOnForce: true,
|
|
154
|
+
clearToken: () => localStorage2.clearToken()
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
})();
|
|
158
|
+
} else if (isMaint) {
|
|
159
|
+
window.localStorage.removeItem(MAINT_KEY);
|
|
160
|
+
window.localStorage.removeItem(MAINT_AT);
|
|
161
|
+
window.localStorage.removeItem(MAINT_LAST_PATH);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return handleResponse(response);
|
|
165
|
+
},
|
|
166
|
+
async (error) => {
|
|
167
|
+
const status = error?.response?.status;
|
|
168
|
+
if (status === 503) {
|
|
169
|
+
if (typeof window !== "undefined") {
|
|
170
|
+
setMaintenanceFlags();
|
|
171
|
+
if (!hasRedirectedToMaintenance && window.location.pathname !== "/maintenance") {
|
|
172
|
+
hasRedirectedToMaintenance = true;
|
|
173
|
+
window.location.replace("/maintenance");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return Promise.reject({
|
|
177
|
+
code: 503,
|
|
178
|
+
message: "SERVICE_UNAVAILABLE",
|
|
179
|
+
original: error?.response?.data
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
const handleError = async (err) => {
|
|
183
|
+
if (!err.response) {
|
|
184
|
+
return err;
|
|
185
|
+
}
|
|
186
|
+
const { data } = err.response;
|
|
187
|
+
if (data && data.code === 400 && ["invalid_grant"].includes(data.data?.error)) {
|
|
188
|
+
await clearAuthToken();
|
|
189
|
+
}
|
|
190
|
+
return data;
|
|
191
|
+
};
|
|
192
|
+
const originalRequest = error.config;
|
|
193
|
+
if ((error.response?.status === 403 || error.response?.status === 401 || error.response?.status === 404) && ["TOKEN_EXPIRED", "AUTHEN_FAIL", 401].includes(
|
|
194
|
+
error.response.data.code
|
|
195
|
+
)) {
|
|
196
|
+
if (isRefreshing) {
|
|
197
|
+
return new Promise(function(resolve, reject) {
|
|
198
|
+
failedQueue.push({ resolve, reject });
|
|
199
|
+
}).then((token) => {
|
|
200
|
+
originalRequest.headers["Authorization"] = "Bearer " + token;
|
|
201
|
+
return instance.request(originalRequest);
|
|
202
|
+
}).catch(async (err) => {
|
|
203
|
+
if ((err.response?.status === 400 || err.response?.status === 401) && ["invalid_grant"].includes(err.response.data.error)) {
|
|
204
|
+
await clearAuthToken();
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
const browserSession = await sessionStorage2.getBrowserSession();
|
|
209
|
+
const refreshToken = await localStorage2.getRefreshToken();
|
|
210
|
+
const accessTokenExp = await localStorage2.getAccessToken();
|
|
211
|
+
isRefreshing = true;
|
|
212
|
+
if (!refreshToken && (!browserSession || browserSession == "unActive")) {
|
|
213
|
+
await clearAuthToken();
|
|
214
|
+
} else {
|
|
215
|
+
const payload = Object.fromEntries(
|
|
216
|
+
Object.entries({
|
|
217
|
+
refresh_token: refreshToken,
|
|
218
|
+
grant_type: "refresh_token",
|
|
219
|
+
client_id: config.config.clientId,
|
|
220
|
+
client_secret: config.config.clientSecret
|
|
221
|
+
}).filter(([_, value]) => !!value)
|
|
222
|
+
);
|
|
223
|
+
return new Promise(function(resolve) {
|
|
224
|
+
axios.post(
|
|
225
|
+
`${config.baseUrl}${"/authentication/oauth2/token" /* AUTH_TOKEN_PATH */}`,
|
|
226
|
+
payload,
|
|
227
|
+
{
|
|
228
|
+
headers: {
|
|
229
|
+
"Content-Type": "multipart/form-data",
|
|
230
|
+
Authorization: `Bearer ${accessTokenExp}`
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
).then(async (res) => {
|
|
234
|
+
const data = res.data;
|
|
235
|
+
await localStorage2.setToken(data.access_token);
|
|
236
|
+
await localStorage2.setRefreshToken(data.refresh_token);
|
|
237
|
+
axios.defaults.headers.common["Authorization"] = "Bearer " + data.access_token;
|
|
238
|
+
originalRequest.headers["Authorization"] = "Bearer " + data.access_token;
|
|
239
|
+
processQueue(null, data.access_token);
|
|
240
|
+
resolve(instance.request(originalRequest));
|
|
241
|
+
}).catch(async (err) => {
|
|
242
|
+
if (err && (err?.error_code === "AUTHEN_FAIL" || err?.error_code === "TOKEN_EXPIRED" || err?.error_code === "TOKEN_INCORRECT" || err?.code === "ERR_BAD_REQUEST")) {
|
|
243
|
+
await clearAuthToken();
|
|
244
|
+
}
|
|
245
|
+
if (err && err.response) {
|
|
246
|
+
const { error_code } = err.response?.data || {};
|
|
247
|
+
if (error_code === "AUTHEN_FAIL") {
|
|
248
|
+
await clearAuthToken();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
processQueue(err, null);
|
|
252
|
+
}).finally(() => {
|
|
253
|
+
isRefreshing = false;
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return Promise.reject(await handleError(error));
|
|
259
|
+
}
|
|
260
|
+
);
|
|
261
|
+
const handleResponse = (res) => {
|
|
262
|
+
if (res && res.data) {
|
|
263
|
+
return res.data;
|
|
264
|
+
}
|
|
265
|
+
return res;
|
|
266
|
+
};
|
|
267
|
+
const clearAuthToken = async () => {
|
|
268
|
+
await localStorage2.clearToken();
|
|
269
|
+
if (typeof window !== "undefined") {
|
|
270
|
+
window.location.href = `/login`;
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
function formatUrl(url, db2) {
|
|
274
|
+
return url + (db2 ? "?db=" + db2 : "");
|
|
275
|
+
}
|
|
276
|
+
const responseBody = (response) => response;
|
|
277
|
+
const requests = {
|
|
278
|
+
get: (url, headers) => instance.get(formatUrl(url, db), headers).then(responseBody),
|
|
279
|
+
post: (url, body, headers) => instance.post(formatUrl(url, db), body, { headers }).then(responseBody),
|
|
280
|
+
post_excel: (url, body, headers) => instance.post(formatUrl(url, db), body, {
|
|
281
|
+
responseType: "arraybuffer",
|
|
282
|
+
headers: {
|
|
283
|
+
"Content-Type": typeof window !== "undefined" ? "application/json" : "application/javascript",
|
|
284
|
+
Accept: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
285
|
+
}
|
|
286
|
+
}).then(responseBody),
|
|
287
|
+
put: (url, body, headers) => instance.put(formatUrl(url, db), body, headers).then(responseBody),
|
|
288
|
+
patch: (url, body) => instance.patch(formatUrl(url, db), body).then(responseBody),
|
|
289
|
+
delete: (url, body) => instance.delete(formatUrl(url, db), body).then(responseBody)
|
|
290
|
+
};
|
|
291
|
+
return requests;
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
export { axiosClient, localStorageUtils, sessionStorageUtils };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { view_service_default } from './chunk-IX7MI3OE.mjs';
|
|
2
|
+
import { envStore } from './chunk-QLUONJPQ.mjs';
|
|
3
|
+
import { useState, useEffect } from 'react';
|
|
4
|
+
import { QueryClient, QueryClientProvider, useQueryClient } from '@tanstack/react-query';
|
|
5
|
+
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
6
|
+
import { Provider } from 'react-redux';
|
|
7
|
+
|
|
8
|
+
var ReactQueryProvider = ({ children }) => {
|
|
9
|
+
const [queryClient] = useState(
|
|
10
|
+
() => new QueryClient({
|
|
11
|
+
defaultOptions: {
|
|
12
|
+
queries: {
|
|
13
|
+
refetchOnWindowFocus: false,
|
|
14
|
+
refetchOnMount: false,
|
|
15
|
+
refetchOnReconnect: false,
|
|
16
|
+
retry: false
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
);
|
|
21
|
+
return /* @__PURE__ */ jsx(QueryClientProvider, { client: queryClient, children });
|
|
22
|
+
};
|
|
23
|
+
var ReduxProvider = ({ children }) => {
|
|
24
|
+
return /* @__PURE__ */ jsx(Provider, { store: envStore, children });
|
|
25
|
+
};
|
|
26
|
+
var MainProvider = ({ children }) => {
|
|
27
|
+
return /* @__PURE__ */ jsx(ReduxProvider, { children: /* @__PURE__ */ jsx(ReactQueryProvider, { children }) });
|
|
28
|
+
};
|
|
29
|
+
var VersionGate = ({ children }) => {
|
|
30
|
+
const queryClient = useQueryClient();
|
|
31
|
+
const [ready, setReady] = useState(false);
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const clearVersion = () => {
|
|
34
|
+
queryClient.clear();
|
|
35
|
+
localStorage.removeItem("__api_version__");
|
|
36
|
+
};
|
|
37
|
+
const validateVersion = async () => {
|
|
38
|
+
const serverVersion = await view_service_default.getVersion();
|
|
39
|
+
const cached = localStorage.getItem("__api_version__");
|
|
40
|
+
if (cached !== serverVersion?.api_version) {
|
|
41
|
+
clearVersion();
|
|
42
|
+
localStorage.setItem("__api_version__", serverVersion?.api_version);
|
|
43
|
+
} else {
|
|
44
|
+
console.log("Api version:", serverVersion?.api_version);
|
|
45
|
+
}
|
|
46
|
+
setReady(true);
|
|
47
|
+
};
|
|
48
|
+
validateVersion();
|
|
49
|
+
if (typeof window !== "undefined") {
|
|
50
|
+
const onKey = (e) => {
|
|
51
|
+
const key = e.key.toLowerCase();
|
|
52
|
+
const isHardRefresh = (key === "f5" || key === "r") && e.ctrlKey && (key !== "r" || e.shiftKey) || key === "r" && e.metaKey && e.shiftKey || key === "r" && e.metaKey && e.altKey;
|
|
53
|
+
if (isHardRefresh) clearVersion();
|
|
54
|
+
};
|
|
55
|
+
window.addEventListener("keydown", onKey);
|
|
56
|
+
return () => window.removeEventListener("keydown", onKey);
|
|
57
|
+
}
|
|
58
|
+
}, [queryClient]);
|
|
59
|
+
return ready ? /* @__PURE__ */ jsx(Fragment, { children }) : null;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export { MainProvider, ReactQueryProvider, VersionGate };
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var axios = require('axios');
|
|
4
|
+
|
|
5
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
+
|
|
7
|
+
var axios__default = /*#__PURE__*/_interopDefault(axios);
|
|
8
|
+
|
|
9
|
+
// src/config/axios-client.ts
|
|
10
|
+
|
|
11
|
+
// src/utils/storage/local-storage.ts
|
|
12
|
+
var localStorageUtils = () => {
|
|
13
|
+
const setToken = async (access_token) => {
|
|
14
|
+
localStorage.setItem("accessToken", access_token);
|
|
15
|
+
};
|
|
16
|
+
const setRefreshToken = async (refresh_token) => {
|
|
17
|
+
localStorage.setItem("refreshToken", refresh_token);
|
|
18
|
+
};
|
|
19
|
+
const getAccessToken = async () => {
|
|
20
|
+
return localStorage.getItem("accessToken");
|
|
21
|
+
};
|
|
22
|
+
const getRefreshToken = async () => {
|
|
23
|
+
return localStorage.getItem("refreshToken");
|
|
24
|
+
};
|
|
25
|
+
const clearToken = async () => {
|
|
26
|
+
localStorage.removeItem("accessToken");
|
|
27
|
+
localStorage.removeItem("refreshToken");
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
setToken,
|
|
31
|
+
setRefreshToken,
|
|
32
|
+
getAccessToken,
|
|
33
|
+
getRefreshToken,
|
|
34
|
+
clearToken
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// src/utils/storage/session-storage.ts
|
|
39
|
+
var sessionStorageUtils = () => {
|
|
40
|
+
const getBrowserSession = async () => {
|
|
41
|
+
return sessionStorage.getItem("browserSession");
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
getBrowserSession
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/config/axios-client.ts
|
|
49
|
+
var MAINT_KEY = "MAINTENANCE_ACTIVE";
|
|
50
|
+
var MAINT_AT = "MAINTENANCE_AT";
|
|
51
|
+
var MAINT_LAST_PATH = "MAINTENANCE_LAST_PATH";
|
|
52
|
+
var hasRedirectedToMaintenance = false;
|
|
53
|
+
function setMaintenanceFlags() {
|
|
54
|
+
if (typeof window === "undefined") return;
|
|
55
|
+
const { pathname, search } = window.location;
|
|
56
|
+
const lastPath = pathname + (search || "");
|
|
57
|
+
if (pathname !== "/maintenance" && !window.localStorage.getItem(MAINT_LAST_PATH)) {
|
|
58
|
+
window.localStorage.setItem(MAINT_LAST_PATH, lastPath);
|
|
59
|
+
}
|
|
60
|
+
window.localStorage.setItem(MAINT_KEY, "true");
|
|
61
|
+
window.localStorage.setItem(MAINT_AT, String(Date.now()));
|
|
62
|
+
}
|
|
63
|
+
async function clearMaintenanceAndExit(getToken, opts) {
|
|
64
|
+
if (typeof window === "undefined") return;
|
|
65
|
+
const forceLogin = opts?.forceLogin === true;
|
|
66
|
+
const clearTokenOnForce = opts?.clearTokenOnForce !== false;
|
|
67
|
+
window.localStorage.removeItem(MAINT_KEY);
|
|
68
|
+
window.localStorage.removeItem(MAINT_AT);
|
|
69
|
+
const lastPath = window.localStorage.getItem(MAINT_LAST_PATH);
|
|
70
|
+
window.localStorage.removeItem(MAINT_LAST_PATH);
|
|
71
|
+
try {
|
|
72
|
+
if (forceLogin) {
|
|
73
|
+
if (clearTokenOnForce) {
|
|
74
|
+
try {
|
|
75
|
+
await opts?.clearToken?.();
|
|
76
|
+
} catch {
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
window.location.replace("/login");
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const token = await getToken();
|
|
83
|
+
if (token) {
|
|
84
|
+
const target = lastPath && lastPath !== "/maintenance" ? lastPath : "/";
|
|
85
|
+
window.location.replace(target);
|
|
86
|
+
} else {
|
|
87
|
+
window.location.replace("/login");
|
|
88
|
+
}
|
|
89
|
+
} catch {
|
|
90
|
+
window.location.replace("/login");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
var axiosClient = {
|
|
94
|
+
init(config) {
|
|
95
|
+
const localStorage2 = config.localStorageUtils ?? localStorageUtils();
|
|
96
|
+
const sessionStorage2 = config.sessionStorageUtils ?? sessionStorageUtils();
|
|
97
|
+
const db = config.db;
|
|
98
|
+
let isRefreshing = false;
|
|
99
|
+
let failedQueue = [];
|
|
100
|
+
const processQueue = (error, token = null) => {
|
|
101
|
+
failedQueue?.forEach((prom) => {
|
|
102
|
+
if (error) {
|
|
103
|
+
prom.reject(error);
|
|
104
|
+
} else {
|
|
105
|
+
prom.resolve(token);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
failedQueue = [];
|
|
109
|
+
};
|
|
110
|
+
const instance = axios__default.default.create({
|
|
111
|
+
adapter: axios__default.default.defaults.adapter,
|
|
112
|
+
baseURL: config.baseUrl,
|
|
113
|
+
timeout: 5e4,
|
|
114
|
+
paramsSerializer: (params) => new URLSearchParams(params).toString()
|
|
115
|
+
});
|
|
116
|
+
if (typeof window !== "undefined") {
|
|
117
|
+
const isMaint = window.localStorage.getItem(MAINT_KEY) === "true";
|
|
118
|
+
const onMaintenancePage = window.location.pathname === "/maintenance";
|
|
119
|
+
if (isMaint && !onMaintenancePage) {
|
|
120
|
+
hasRedirectedToMaintenance = true;
|
|
121
|
+
window.location.replace("/maintenance");
|
|
122
|
+
}
|
|
123
|
+
if (isMaint && onMaintenancePage) {
|
|
124
|
+
const healthUrl = config.healthUrl || `${(config.baseUrl || "").replace(/\/+$/, "")}/health`;
|
|
125
|
+
(async () => {
|
|
126
|
+
try {
|
|
127
|
+
await axios__default.default.get(healthUrl, { timeout: 8e3 });
|
|
128
|
+
await clearMaintenanceAndExit(() => localStorage2.getAccessToken(), {
|
|
129
|
+
forceLogin: true,
|
|
130
|
+
clearTokenOnForce: true,
|
|
131
|
+
clearToken: () => localStorage2.clearToken()
|
|
132
|
+
});
|
|
133
|
+
} catch {
|
|
134
|
+
}
|
|
135
|
+
})();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
instance.interceptors.request.use(
|
|
139
|
+
async (configReq) => {
|
|
140
|
+
const token = await localStorage2.getAccessToken();
|
|
141
|
+
if (token) {
|
|
142
|
+
configReq.headers["Authorization"] = "Bearer " + token;
|
|
143
|
+
}
|
|
144
|
+
return configReq;
|
|
145
|
+
},
|
|
146
|
+
(error) => Promise.reject(error)
|
|
147
|
+
);
|
|
148
|
+
instance.interceptors.response.use(
|
|
149
|
+
(response) => {
|
|
150
|
+
if (typeof window !== "undefined") {
|
|
151
|
+
const isMaint = window.localStorage.getItem(MAINT_KEY) === "true";
|
|
152
|
+
const onMaintenancePage = window.location.pathname === "/maintenance";
|
|
153
|
+
if (isMaint && onMaintenancePage) {
|
|
154
|
+
(async () => {
|
|
155
|
+
await clearMaintenanceAndExit(
|
|
156
|
+
() => localStorage2.getAccessToken(),
|
|
157
|
+
{
|
|
158
|
+
forceLogin: true,
|
|
159
|
+
clearTokenOnForce: true,
|
|
160
|
+
clearToken: () => localStorage2.clearToken()
|
|
161
|
+
}
|
|
162
|
+
);
|
|
163
|
+
})();
|
|
164
|
+
} else if (isMaint) {
|
|
165
|
+
window.localStorage.removeItem(MAINT_KEY);
|
|
166
|
+
window.localStorage.removeItem(MAINT_AT);
|
|
167
|
+
window.localStorage.removeItem(MAINT_LAST_PATH);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return handleResponse(response);
|
|
171
|
+
},
|
|
172
|
+
async (error) => {
|
|
173
|
+
const status = error?.response?.status;
|
|
174
|
+
if (status === 503) {
|
|
175
|
+
if (typeof window !== "undefined") {
|
|
176
|
+
setMaintenanceFlags();
|
|
177
|
+
if (!hasRedirectedToMaintenance && window.location.pathname !== "/maintenance") {
|
|
178
|
+
hasRedirectedToMaintenance = true;
|
|
179
|
+
window.location.replace("/maintenance");
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return Promise.reject({
|
|
183
|
+
code: 503,
|
|
184
|
+
message: "SERVICE_UNAVAILABLE",
|
|
185
|
+
original: error?.response?.data
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
const handleError = async (err) => {
|
|
189
|
+
if (!err.response) {
|
|
190
|
+
return err;
|
|
191
|
+
}
|
|
192
|
+
const { data } = err.response;
|
|
193
|
+
if (data && data.code === 400 && ["invalid_grant"].includes(data.data?.error)) {
|
|
194
|
+
await clearAuthToken();
|
|
195
|
+
}
|
|
196
|
+
return data;
|
|
197
|
+
};
|
|
198
|
+
const originalRequest = error.config;
|
|
199
|
+
if ((error.response?.status === 403 || error.response?.status === 401 || error.response?.status === 404) && ["TOKEN_EXPIRED", "AUTHEN_FAIL", 401].includes(
|
|
200
|
+
error.response.data.code
|
|
201
|
+
)) {
|
|
202
|
+
if (isRefreshing) {
|
|
203
|
+
return new Promise(function(resolve, reject) {
|
|
204
|
+
failedQueue.push({ resolve, reject });
|
|
205
|
+
}).then((token) => {
|
|
206
|
+
originalRequest.headers["Authorization"] = "Bearer " + token;
|
|
207
|
+
return instance.request(originalRequest);
|
|
208
|
+
}).catch(async (err) => {
|
|
209
|
+
if ((err.response?.status === 400 || err.response?.status === 401) && ["invalid_grant"].includes(err.response.data.error)) {
|
|
210
|
+
await clearAuthToken();
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
const browserSession = await sessionStorage2.getBrowserSession();
|
|
215
|
+
const refreshToken = await localStorage2.getRefreshToken();
|
|
216
|
+
const accessTokenExp = await localStorage2.getAccessToken();
|
|
217
|
+
isRefreshing = true;
|
|
218
|
+
if (!refreshToken && (!browserSession || browserSession == "unActive")) {
|
|
219
|
+
await clearAuthToken();
|
|
220
|
+
} else {
|
|
221
|
+
const payload = Object.fromEntries(
|
|
222
|
+
Object.entries({
|
|
223
|
+
refresh_token: refreshToken,
|
|
224
|
+
grant_type: "refresh_token",
|
|
225
|
+
client_id: config.config.clientId,
|
|
226
|
+
client_secret: config.config.clientSecret
|
|
227
|
+
}).filter(([_, value]) => !!value)
|
|
228
|
+
);
|
|
229
|
+
return new Promise(function(resolve) {
|
|
230
|
+
axios__default.default.post(
|
|
231
|
+
`${config.baseUrl}${"/authentication/oauth2/token" /* AUTH_TOKEN_PATH */}`,
|
|
232
|
+
payload,
|
|
233
|
+
{
|
|
234
|
+
headers: {
|
|
235
|
+
"Content-Type": "multipart/form-data",
|
|
236
|
+
Authorization: `Bearer ${accessTokenExp}`
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
).then(async (res) => {
|
|
240
|
+
const data = res.data;
|
|
241
|
+
await localStorage2.setToken(data.access_token);
|
|
242
|
+
await localStorage2.setRefreshToken(data.refresh_token);
|
|
243
|
+
axios__default.default.defaults.headers.common["Authorization"] = "Bearer " + data.access_token;
|
|
244
|
+
originalRequest.headers["Authorization"] = "Bearer " + data.access_token;
|
|
245
|
+
processQueue(null, data.access_token);
|
|
246
|
+
resolve(instance.request(originalRequest));
|
|
247
|
+
}).catch(async (err) => {
|
|
248
|
+
if (err && (err?.error_code === "AUTHEN_FAIL" || err?.error_code === "TOKEN_EXPIRED" || err?.error_code === "TOKEN_INCORRECT" || err?.code === "ERR_BAD_REQUEST")) {
|
|
249
|
+
await clearAuthToken();
|
|
250
|
+
}
|
|
251
|
+
if (err && err.response) {
|
|
252
|
+
const { error_code } = err.response?.data || {};
|
|
253
|
+
if (error_code === "AUTHEN_FAIL") {
|
|
254
|
+
await clearAuthToken();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
processQueue(err, null);
|
|
258
|
+
}).finally(() => {
|
|
259
|
+
isRefreshing = false;
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return Promise.reject(await handleError(error));
|
|
265
|
+
}
|
|
266
|
+
);
|
|
267
|
+
const handleResponse = (res) => {
|
|
268
|
+
if (res && res.data) {
|
|
269
|
+
return res.data;
|
|
270
|
+
}
|
|
271
|
+
return res;
|
|
272
|
+
};
|
|
273
|
+
const clearAuthToken = async () => {
|
|
274
|
+
await localStorage2.clearToken();
|
|
275
|
+
if (typeof window !== "undefined") {
|
|
276
|
+
window.location.href = `/login`;
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
function formatUrl(url, db2) {
|
|
280
|
+
return url + (db2 ? "?db=" + db2 : "");
|
|
281
|
+
}
|
|
282
|
+
const responseBody = (response) => response;
|
|
283
|
+
const requests = {
|
|
284
|
+
get: (url, headers) => instance.get(formatUrl(url, db), headers).then(responseBody),
|
|
285
|
+
post: (url, body, headers) => instance.post(formatUrl(url, db), body, { headers }).then(responseBody),
|
|
286
|
+
post_excel: (url, body, headers) => instance.post(formatUrl(url, db), body, {
|
|
287
|
+
responseType: "arraybuffer",
|
|
288
|
+
headers: {
|
|
289
|
+
"Content-Type": typeof window !== "undefined" ? "application/json" : "application/javascript",
|
|
290
|
+
Accept: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
291
|
+
}
|
|
292
|
+
}).then(responseBody),
|
|
293
|
+
put: (url, body, headers) => instance.put(formatUrl(url, db), body, headers).then(responseBody),
|
|
294
|
+
patch: (url, body) => instance.patch(formatUrl(url, db), body).then(responseBody),
|
|
295
|
+
delete: (url, body) => instance.delete(formatUrl(url, db), body).then(responseBody)
|
|
296
|
+
};
|
|
297
|
+
return requests;
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
exports.axiosClient = axiosClient;
|
|
302
|
+
exports.localStorageUtils = localStorageUtils;
|
|
303
|
+
exports.sessionStorageUtils = sessionStorageUtils;
|