edu-logic-lib 1.0.1
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/base-model-type-DvO53Lwi.d.mts +7 -0
- package/dist/base-model-type-DvO53Lwi.d.ts +7 -0
- package/dist/config.d.mts +15 -0
- package/dist/config.d.ts +15 -0
- package/dist/config.js +294 -0
- package/dist/config.mjs +257 -0
- package/dist/constants.d.mts +120 -0
- package/dist/constants.d.ts +120 -0
- package/dist/constants.js +191 -0
- package/dist/constants.mjs +153 -0
- package/dist/context-type-D5XefoL-.d.mts +8 -0
- package/dist/context-type-D5XefoL-.d.ts +8 -0
- package/dist/environment.d.mts +37 -0
- package/dist/environment.d.ts +37 -0
- package/dist/environment.js +910 -0
- package/dist/environment.mjs +870 -0
- package/dist/hooks.d.mts +216 -0
- package/dist/hooks.d.ts +216 -0
- package/dist/hooks.js +5094 -0
- package/dist/hooks.mjs +5013 -0
- package/dist/index-C_nK1Mii.d.mts +19 -0
- package/dist/index-C_nK1Mii.d.ts +19 -0
- package/dist/models.d.mts +35 -0
- package/dist/models.d.ts +35 -0
- package/dist/models.js +3288 -0
- package/dist/models.mjs +3249 -0
- package/dist/provider.d.mts +16 -0
- package/dist/provider.d.ts +16 -0
- package/dist/provider.js +3311 -0
- package/dist/provider.mjs +3272 -0
- package/dist/services.d.mts +160 -0
- package/dist/services.d.ts +160 -0
- package/dist/services.js +4113 -0
- package/dist/services.mjs +4069 -0
- package/dist/store.d.mts +505 -0
- package/dist/store.d.ts +505 -0
- package/dist/store.js +691 -0
- package/dist/store.mjs +603 -0
- package/dist/types.d.mts +12 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.js +18 -0
- package/dist/types.mjs +0 -0
- package/dist/use-get-selection-DFh6sc49.d.mts +26 -0
- package/dist/use-get-selection-DFh6sc49.d.ts +26 -0
- package/dist/utils.d.mts +52 -0
- package/dist/utils.d.ts +52 -0
- package/dist/utils.js +2402 -0
- package/dist/utils.mjs +2361 -0
- package/dist/view-type-BTzRpkT7.d.mts +106 -0
- package/dist/view-type-BTzRpkT7.d.ts +106 -0
- package/package.json +89 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EnvStore } from './environment.mjs';
|
|
2
|
+
import '@reduxjs/toolkit';
|
|
3
|
+
|
|
4
|
+
declare const axiosClient: {
|
|
5
|
+
init(config: EnvStore): {
|
|
6
|
+
get: (url: string, headers: any) => Promise<any>;
|
|
7
|
+
post: (url: string, body: any, headers: any) => Promise<any>;
|
|
8
|
+
post_excel: (url: string, body: any, headers: any) => Promise<any>;
|
|
9
|
+
put: (url: string, body: any, headers: any) => Promise<any>;
|
|
10
|
+
patch: (url: string, body: any) => Promise<any>;
|
|
11
|
+
delete: (url: string, body: any) => Promise<any>;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { axiosClient };
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EnvStore } from './environment.js';
|
|
2
|
+
import '@reduxjs/toolkit';
|
|
3
|
+
|
|
4
|
+
declare const axiosClient: {
|
|
5
|
+
init(config: EnvStore): {
|
|
6
|
+
get: (url: string, headers: any) => Promise<any>;
|
|
7
|
+
post: (url: string, body: any, headers: any) => Promise<any>;
|
|
8
|
+
post_excel: (url: string, body: any, headers: any) => Promise<any>;
|
|
9
|
+
put: (url: string, body: any, headers: any) => Promise<any>;
|
|
10
|
+
patch: (url: string, body: any) => Promise<any>;
|
|
11
|
+
delete: (url: string, body: any) => Promise<any>;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { axiosClient };
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/config.ts
|
|
31
|
+
var config_exports = {};
|
|
32
|
+
__export(config_exports, {
|
|
33
|
+
axiosClient: () => axiosClient
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(config_exports);
|
|
36
|
+
|
|
37
|
+
// src/config/axios-client.ts
|
|
38
|
+
var import_axios = __toESM(require("axios"));
|
|
39
|
+
var MAINT_KEY = "MAINTENANCE_ACTIVE";
|
|
40
|
+
var MAINT_AT = "MAINTENANCE_AT";
|
|
41
|
+
var MAINT_LAST_PATH = "MAINTENANCE_LAST_PATH";
|
|
42
|
+
var hasRedirectedToMaintenance = false;
|
|
43
|
+
function setMaintenanceFlags() {
|
|
44
|
+
if (typeof window === "undefined") return;
|
|
45
|
+
const { pathname, search } = window.location;
|
|
46
|
+
const lastPath = pathname + (search || "");
|
|
47
|
+
if (pathname !== "/maintenance" && !window.localStorage.getItem(MAINT_LAST_PATH)) {
|
|
48
|
+
window.localStorage.setItem(MAINT_LAST_PATH, lastPath);
|
|
49
|
+
}
|
|
50
|
+
window.localStorage.setItem(MAINT_KEY, "true");
|
|
51
|
+
window.localStorage.setItem(MAINT_AT, String(Date.now()));
|
|
52
|
+
}
|
|
53
|
+
async function clearMaintenanceAndExit(getToken, opts) {
|
|
54
|
+
if (typeof window === "undefined") return;
|
|
55
|
+
const forceLogin = opts?.forceLogin === true;
|
|
56
|
+
const clearTokenOnForce = opts?.clearTokenOnForce !== false;
|
|
57
|
+
window.localStorage.removeItem(MAINT_KEY);
|
|
58
|
+
window.localStorage.removeItem(MAINT_AT);
|
|
59
|
+
const lastPath = window.localStorage.getItem(MAINT_LAST_PATH);
|
|
60
|
+
window.localStorage.removeItem(MAINT_LAST_PATH);
|
|
61
|
+
try {
|
|
62
|
+
if (forceLogin) {
|
|
63
|
+
if (clearTokenOnForce) {
|
|
64
|
+
try {
|
|
65
|
+
await opts?.clearToken?.();
|
|
66
|
+
} catch {
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
window.location.replace("/login");
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const token = await getToken();
|
|
73
|
+
if (token) {
|
|
74
|
+
const target = lastPath && lastPath !== "/maintenance" ? lastPath : "/";
|
|
75
|
+
window.location.replace(target);
|
|
76
|
+
} else {
|
|
77
|
+
window.location.replace("/login");
|
|
78
|
+
}
|
|
79
|
+
} catch {
|
|
80
|
+
window.location.replace("/login");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
var axiosClient = {
|
|
84
|
+
init(config) {
|
|
85
|
+
const localStorage = config.localStorageUtils;
|
|
86
|
+
const sessionStorage = config.sessionStorageUtils;
|
|
87
|
+
const db = config.db;
|
|
88
|
+
let isRefreshing = false;
|
|
89
|
+
let failedQueue = [];
|
|
90
|
+
const processQueue = (error, token = null) => {
|
|
91
|
+
failedQueue?.forEach((prom) => {
|
|
92
|
+
if (error) {
|
|
93
|
+
prom.reject(error);
|
|
94
|
+
} else {
|
|
95
|
+
prom.resolve(token);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
failedQueue = [];
|
|
99
|
+
};
|
|
100
|
+
const instance = import_axios.default.create({
|
|
101
|
+
adapter: import_axios.default.defaults.adapter,
|
|
102
|
+
baseURL: config.baseUrl,
|
|
103
|
+
timeout: 5e4,
|
|
104
|
+
paramsSerializer: (params) => new URLSearchParams(params).toString()
|
|
105
|
+
});
|
|
106
|
+
if (typeof window !== "undefined") {
|
|
107
|
+
const isMaint = window.localStorage.getItem(MAINT_KEY) === "true";
|
|
108
|
+
const onMaintenancePage = window.location.pathname === "/maintenance";
|
|
109
|
+
if (isMaint && !onMaintenancePage) {
|
|
110
|
+
hasRedirectedToMaintenance = true;
|
|
111
|
+
window.location.replace("/maintenance");
|
|
112
|
+
}
|
|
113
|
+
if (isMaint && onMaintenancePage) {
|
|
114
|
+
const healthUrl = config.healthUrl || `${(config.baseUrl || "").replace(/\/+$/, "")}/health`;
|
|
115
|
+
(async () => {
|
|
116
|
+
try {
|
|
117
|
+
await import_axios.default.get(healthUrl, { timeout: 8e3 });
|
|
118
|
+
await clearMaintenanceAndExit(() => localStorage.getAccessToken(), {
|
|
119
|
+
forceLogin: true,
|
|
120
|
+
clearTokenOnForce: true,
|
|
121
|
+
clearToken: () => localStorage.clearToken()
|
|
122
|
+
});
|
|
123
|
+
} catch {
|
|
124
|
+
}
|
|
125
|
+
})();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
instance.interceptors.request.use(
|
|
129
|
+
async (configReq) => {
|
|
130
|
+
const token = await localStorage.getAccessToken();
|
|
131
|
+
if (token) {
|
|
132
|
+
configReq.headers["Authorization"] = "Bearer " + token;
|
|
133
|
+
}
|
|
134
|
+
return configReq;
|
|
135
|
+
},
|
|
136
|
+
(error) => Promise.reject(error)
|
|
137
|
+
);
|
|
138
|
+
instance.interceptors.response.use(
|
|
139
|
+
(response) => {
|
|
140
|
+
if (typeof window !== "undefined") {
|
|
141
|
+
const isMaint = window.localStorage.getItem(MAINT_KEY) === "true";
|
|
142
|
+
const onMaintenancePage = window.location.pathname === "/maintenance";
|
|
143
|
+
if (isMaint && onMaintenancePage) {
|
|
144
|
+
;
|
|
145
|
+
(async () => {
|
|
146
|
+
await clearMaintenanceAndExit(
|
|
147
|
+
() => localStorage.getAccessToken(),
|
|
148
|
+
{
|
|
149
|
+
forceLogin: true,
|
|
150
|
+
clearTokenOnForce: true,
|
|
151
|
+
clearToken: () => localStorage.clearToken()
|
|
152
|
+
}
|
|
153
|
+
);
|
|
154
|
+
})();
|
|
155
|
+
} else if (isMaint) {
|
|
156
|
+
window.localStorage.removeItem(MAINT_KEY);
|
|
157
|
+
window.localStorage.removeItem(MAINT_AT);
|
|
158
|
+
window.localStorage.removeItem(MAINT_LAST_PATH);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return handleResponse(response);
|
|
162
|
+
},
|
|
163
|
+
async (error) => {
|
|
164
|
+
const status = error?.response?.status;
|
|
165
|
+
if (status === 503) {
|
|
166
|
+
if (typeof window !== "undefined") {
|
|
167
|
+
setMaintenanceFlags();
|
|
168
|
+
if (!hasRedirectedToMaintenance && window.location.pathname !== "/maintenance") {
|
|
169
|
+
hasRedirectedToMaintenance = true;
|
|
170
|
+
window.location.replace("/maintenance");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return Promise.reject({
|
|
174
|
+
code: 503,
|
|
175
|
+
message: "SERVICE_UNAVAILABLE",
|
|
176
|
+
original: error?.response?.data
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
const handleError = async (err) => {
|
|
180
|
+
if (!err.response) {
|
|
181
|
+
return err;
|
|
182
|
+
}
|
|
183
|
+
const { data } = err.response;
|
|
184
|
+
if (data && data.code === 400 && ["invalid_grant"].includes(data.data?.error)) {
|
|
185
|
+
await clearAuthToken();
|
|
186
|
+
}
|
|
187
|
+
return data;
|
|
188
|
+
};
|
|
189
|
+
const originalRequest = error.config;
|
|
190
|
+
if ((error.response?.status === 403 || error.response?.status === 401 || error.response?.status === 404) && ["TOKEN_EXPIRED", "AUTHEN_FAIL", 401].includes(
|
|
191
|
+
error.response.data.code
|
|
192
|
+
)) {
|
|
193
|
+
if (isRefreshing) {
|
|
194
|
+
return new Promise(function(resolve, reject) {
|
|
195
|
+
failedQueue.push({ resolve, reject });
|
|
196
|
+
}).then((token) => {
|
|
197
|
+
originalRequest.headers["Authorization"] = "Bearer " + token;
|
|
198
|
+
return instance.request(originalRequest);
|
|
199
|
+
}).catch(async (err) => {
|
|
200
|
+
if ((err.response?.status === 400 || err.response?.status === 401) && ["invalid_grant"].includes(err.response.data.error)) {
|
|
201
|
+
await clearAuthToken();
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
const browserSession = await sessionStorage.getBrowserSession();
|
|
206
|
+
const refreshToken = await localStorage.getRefreshToken();
|
|
207
|
+
const accessTokenExp = await localStorage.getAccessToken();
|
|
208
|
+
isRefreshing = true;
|
|
209
|
+
if (!refreshToken && (!browserSession || browserSession == "unActive")) {
|
|
210
|
+
await clearAuthToken();
|
|
211
|
+
} else {
|
|
212
|
+
const payload = Object.fromEntries(
|
|
213
|
+
Object.entries({
|
|
214
|
+
refresh_token: refreshToken,
|
|
215
|
+
grant_type: "refresh_token",
|
|
216
|
+
client_id: config.config.clientId,
|
|
217
|
+
client_secret: config.config.clientSecret
|
|
218
|
+
}).filter(([_, value]) => !!value)
|
|
219
|
+
);
|
|
220
|
+
return new Promise(function(resolve) {
|
|
221
|
+
import_axios.default.post(
|
|
222
|
+
`${config.baseUrl}${"/authentication/oauth2/token" /* AUTH_TOKEN_PATH */}`,
|
|
223
|
+
payload,
|
|
224
|
+
{
|
|
225
|
+
headers: {
|
|
226
|
+
"Content-Type": "multipart/form-data",
|
|
227
|
+
Authorization: `Bearer ${accessTokenExp}`
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
).then(async (res) => {
|
|
231
|
+
const data = res.data;
|
|
232
|
+
await localStorage.setToken(data.access_token);
|
|
233
|
+
await localStorage.setRefreshToken(data.refresh_token);
|
|
234
|
+
import_axios.default.defaults.headers.common["Authorization"] = "Bearer " + data.access_token;
|
|
235
|
+
originalRequest.headers["Authorization"] = "Bearer " + data.access_token;
|
|
236
|
+
processQueue(null, data.access_token);
|
|
237
|
+
resolve(instance.request(originalRequest));
|
|
238
|
+
}).catch(async (err) => {
|
|
239
|
+
if (err && (err?.error_code === "AUTHEN_FAIL" || err?.error_code === "TOKEN_EXPIRED" || err?.error_code === "TOKEN_INCORRECT" || err?.code === "ERR_BAD_REQUEST")) {
|
|
240
|
+
await clearAuthToken();
|
|
241
|
+
}
|
|
242
|
+
if (err && err.response) {
|
|
243
|
+
const { error_code } = err.response?.data || {};
|
|
244
|
+
if (error_code === "AUTHEN_FAIL") {
|
|
245
|
+
await clearAuthToken();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
processQueue(err, null);
|
|
249
|
+
}).finally(() => {
|
|
250
|
+
isRefreshing = false;
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return Promise.reject(await handleError(error));
|
|
256
|
+
}
|
|
257
|
+
);
|
|
258
|
+
const handleResponse = (res) => {
|
|
259
|
+
if (res && res.data) {
|
|
260
|
+
return res.data;
|
|
261
|
+
}
|
|
262
|
+
return res;
|
|
263
|
+
};
|
|
264
|
+
const clearAuthToken = async () => {
|
|
265
|
+
await localStorage.clearToken();
|
|
266
|
+
if (typeof window !== "undefined") {
|
|
267
|
+
window.location.href = `/login`;
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
function formatUrl(url, db2) {
|
|
271
|
+
return url + (db2 ? "?db=" + db2 : "");
|
|
272
|
+
}
|
|
273
|
+
const responseBody = (response) => response;
|
|
274
|
+
const requests = {
|
|
275
|
+
get: (url, headers) => instance.get(formatUrl(url, db), headers).then(responseBody),
|
|
276
|
+
post: (url, body, headers) => instance.post(formatUrl(url, db), body, { headers }).then(responseBody),
|
|
277
|
+
post_excel: (url, body, headers) => instance.post(formatUrl(url, db), body, {
|
|
278
|
+
responseType: "arraybuffer",
|
|
279
|
+
headers: {
|
|
280
|
+
"Content-Type": typeof window !== "undefined" ? "application/json" : "application/javascript",
|
|
281
|
+
Accept: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
282
|
+
}
|
|
283
|
+
}).then(responseBody),
|
|
284
|
+
put: (url, body, headers) => instance.put(formatUrl(url, db), body, headers).then(responseBody),
|
|
285
|
+
patch: (url, body) => instance.patch(formatUrl(url, db), body).then(responseBody),
|
|
286
|
+
delete: (url, body) => instance.delete(formatUrl(url, db), body).then(responseBody)
|
|
287
|
+
};
|
|
288
|
+
return requests;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
292
|
+
0 && (module.exports = {
|
|
293
|
+
axiosClient
|
|
294
|
+
});
|
package/dist/config.mjs
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
// src/config/axios-client.ts
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
var MAINT_KEY = "MAINTENANCE_ACTIVE";
|
|
4
|
+
var MAINT_AT = "MAINTENANCE_AT";
|
|
5
|
+
var MAINT_LAST_PATH = "MAINTENANCE_LAST_PATH";
|
|
6
|
+
var hasRedirectedToMaintenance = false;
|
|
7
|
+
function setMaintenanceFlags() {
|
|
8
|
+
if (typeof window === "undefined") return;
|
|
9
|
+
const { pathname, search } = window.location;
|
|
10
|
+
const lastPath = pathname + (search || "");
|
|
11
|
+
if (pathname !== "/maintenance" && !window.localStorage.getItem(MAINT_LAST_PATH)) {
|
|
12
|
+
window.localStorage.setItem(MAINT_LAST_PATH, lastPath);
|
|
13
|
+
}
|
|
14
|
+
window.localStorage.setItem(MAINT_KEY, "true");
|
|
15
|
+
window.localStorage.setItem(MAINT_AT, String(Date.now()));
|
|
16
|
+
}
|
|
17
|
+
async function clearMaintenanceAndExit(getToken, opts) {
|
|
18
|
+
if (typeof window === "undefined") return;
|
|
19
|
+
const forceLogin = opts?.forceLogin === true;
|
|
20
|
+
const clearTokenOnForce = opts?.clearTokenOnForce !== false;
|
|
21
|
+
window.localStorage.removeItem(MAINT_KEY);
|
|
22
|
+
window.localStorage.removeItem(MAINT_AT);
|
|
23
|
+
const lastPath = window.localStorage.getItem(MAINT_LAST_PATH);
|
|
24
|
+
window.localStorage.removeItem(MAINT_LAST_PATH);
|
|
25
|
+
try {
|
|
26
|
+
if (forceLogin) {
|
|
27
|
+
if (clearTokenOnForce) {
|
|
28
|
+
try {
|
|
29
|
+
await opts?.clearToken?.();
|
|
30
|
+
} catch {
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
window.location.replace("/login");
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const token = await getToken();
|
|
37
|
+
if (token) {
|
|
38
|
+
const target = lastPath && lastPath !== "/maintenance" ? lastPath : "/";
|
|
39
|
+
window.location.replace(target);
|
|
40
|
+
} else {
|
|
41
|
+
window.location.replace("/login");
|
|
42
|
+
}
|
|
43
|
+
} catch {
|
|
44
|
+
window.location.replace("/login");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
var axiosClient = {
|
|
48
|
+
init(config) {
|
|
49
|
+
const localStorage = config.localStorageUtils;
|
|
50
|
+
const sessionStorage = config.sessionStorageUtils;
|
|
51
|
+
const db = config.db;
|
|
52
|
+
let isRefreshing = false;
|
|
53
|
+
let failedQueue = [];
|
|
54
|
+
const processQueue = (error, token = null) => {
|
|
55
|
+
failedQueue?.forEach((prom) => {
|
|
56
|
+
if (error) {
|
|
57
|
+
prom.reject(error);
|
|
58
|
+
} else {
|
|
59
|
+
prom.resolve(token);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
failedQueue = [];
|
|
63
|
+
};
|
|
64
|
+
const instance = axios.create({
|
|
65
|
+
adapter: axios.defaults.adapter,
|
|
66
|
+
baseURL: config.baseUrl,
|
|
67
|
+
timeout: 5e4,
|
|
68
|
+
paramsSerializer: (params) => new URLSearchParams(params).toString()
|
|
69
|
+
});
|
|
70
|
+
if (typeof window !== "undefined") {
|
|
71
|
+
const isMaint = window.localStorage.getItem(MAINT_KEY) === "true";
|
|
72
|
+
const onMaintenancePage = window.location.pathname === "/maintenance";
|
|
73
|
+
if (isMaint && !onMaintenancePage) {
|
|
74
|
+
hasRedirectedToMaintenance = true;
|
|
75
|
+
window.location.replace("/maintenance");
|
|
76
|
+
}
|
|
77
|
+
if (isMaint && onMaintenancePage) {
|
|
78
|
+
const healthUrl = config.healthUrl || `${(config.baseUrl || "").replace(/\/+$/, "")}/health`;
|
|
79
|
+
(async () => {
|
|
80
|
+
try {
|
|
81
|
+
await axios.get(healthUrl, { timeout: 8e3 });
|
|
82
|
+
await clearMaintenanceAndExit(() => localStorage.getAccessToken(), {
|
|
83
|
+
forceLogin: true,
|
|
84
|
+
clearTokenOnForce: true,
|
|
85
|
+
clearToken: () => localStorage.clearToken()
|
|
86
|
+
});
|
|
87
|
+
} catch {
|
|
88
|
+
}
|
|
89
|
+
})();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
instance.interceptors.request.use(
|
|
93
|
+
async (configReq) => {
|
|
94
|
+
const token = await localStorage.getAccessToken();
|
|
95
|
+
if (token) {
|
|
96
|
+
configReq.headers["Authorization"] = "Bearer " + token;
|
|
97
|
+
}
|
|
98
|
+
return configReq;
|
|
99
|
+
},
|
|
100
|
+
(error) => Promise.reject(error)
|
|
101
|
+
);
|
|
102
|
+
instance.interceptors.response.use(
|
|
103
|
+
(response) => {
|
|
104
|
+
if (typeof window !== "undefined") {
|
|
105
|
+
const isMaint = window.localStorage.getItem(MAINT_KEY) === "true";
|
|
106
|
+
const onMaintenancePage = window.location.pathname === "/maintenance";
|
|
107
|
+
if (isMaint && onMaintenancePage) {
|
|
108
|
+
;
|
|
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
|
+
}
|
|
254
|
+
};
|
|
255
|
+
export {
|
|
256
|
+
axiosClient
|
|
257
|
+
};
|