@gofreego/tsutils 0.1.12 → 0.1.14
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/index.d.mts +185 -57
- package/dist/index.d.ts +185 -57
- package/dist/index.js +162 -143
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +160 -141
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1187,7 +1187,7 @@ var HttpClient = class {
|
|
|
1187
1187
|
try {
|
|
1188
1188
|
error.data = await response.json();
|
|
1189
1189
|
} catch {
|
|
1190
|
-
error.data = await response.text();
|
|
1190
|
+
error.data = { code: response.status, message: await response.text() };
|
|
1191
1191
|
}
|
|
1192
1192
|
throw error;
|
|
1193
1193
|
}
|
|
@@ -1206,6 +1206,15 @@ var HttpClient = class {
|
|
|
1206
1206
|
throw error;
|
|
1207
1207
|
}
|
|
1208
1208
|
}
|
|
1209
|
+
setDefaultHeader(key, value) {
|
|
1210
|
+
this.defaultHeaders[key] = value;
|
|
1211
|
+
}
|
|
1212
|
+
removeDefaultHeader(key) {
|
|
1213
|
+
delete this.defaultHeaders[key];
|
|
1214
|
+
}
|
|
1215
|
+
getDefaultHeaders() {
|
|
1216
|
+
return { ...this.defaultHeaders };
|
|
1217
|
+
}
|
|
1209
1218
|
get(url, config) {
|
|
1210
1219
|
return this.request(url, { ...config, method: "GET" });
|
|
1211
1220
|
}
|
|
@@ -1222,6 +1231,16 @@ var HttpClient = class {
|
|
|
1222
1231
|
return this.request(url, { ...config, method: "DELETE" });
|
|
1223
1232
|
}
|
|
1224
1233
|
};
|
|
1234
|
+
function extractErrorMessage(error) {
|
|
1235
|
+
if (error instanceof Error) {
|
|
1236
|
+
const httpError = error;
|
|
1237
|
+
if (httpError.data) {
|
|
1238
|
+
return httpError.data.message;
|
|
1239
|
+
}
|
|
1240
|
+
return error.message;
|
|
1241
|
+
}
|
|
1242
|
+
return "An unexpected error occurred";
|
|
1243
|
+
}
|
|
1225
1244
|
|
|
1226
1245
|
// src/utils/cn.ts
|
|
1227
1246
|
function cn(...classes) {
|
|
@@ -1262,169 +1281,169 @@ function formatDate(date, options = {
|
|
|
1262
1281
|
const dateObj = typeof date === "string" || typeof date === "number" ? new Date(date) : date;
|
|
1263
1282
|
return new Intl.DateTimeFormat("en-US", options).format(dateObj);
|
|
1264
1283
|
}
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
try {
|
|
1274
|
-
const base64Url = token.split(".")[1];
|
|
1275
|
-
if (!base64Url) return null;
|
|
1276
|
-
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
|
|
1277
|
-
const jsonPayload = decodeURIComponent(
|
|
1278
|
-
window.atob(base64).split("").map(function(c) {
|
|
1279
|
-
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
|
|
1280
|
-
}).join("")
|
|
1281
|
-
);
|
|
1282
|
-
return JSON.parse(jsonPayload);
|
|
1283
|
-
} catch (e) {
|
|
1284
|
-
return null;
|
|
1284
|
+
|
|
1285
|
+
// src/auth/sessionManager.ts
|
|
1286
|
+
var SESSION_KEY = "session_details";
|
|
1287
|
+
var SessionManager = class _SessionManager {
|
|
1288
|
+
constructor(client, key = SESSION_KEY) {
|
|
1289
|
+
this.cache = null;
|
|
1290
|
+
this.client = client;
|
|
1291
|
+
this.key = key;
|
|
1285
1292
|
}
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
isLoading: true
|
|
1290
|
-
});
|
|
1291
|
-
var AuthProvider = ({
|
|
1292
|
-
cookieName = "authorization",
|
|
1293
|
-
redirectUrl,
|
|
1294
|
-
children,
|
|
1295
|
-
onAuthFail,
|
|
1296
|
-
onAuthSuccess,
|
|
1297
|
-
loadingElement = null
|
|
1298
|
-
}) => {
|
|
1299
|
-
const [authState, setAuthState] = react.useState({
|
|
1300
|
-
isAuthenticated: false,
|
|
1301
|
-
isLoading: true
|
|
1302
|
-
// starts loading until cookie check completes
|
|
1303
|
-
});
|
|
1304
|
-
const [authFailed, setAuthFailed] = react.useState(false);
|
|
1305
|
-
react.useEffect(() => {
|
|
1306
|
-
const verifyAuth = () => {
|
|
1307
|
-
const token = getCookie(cookieName);
|
|
1308
|
-
if (!token) {
|
|
1309
|
-
handleFail();
|
|
1310
|
-
return;
|
|
1311
|
-
}
|
|
1312
|
-
const decoded = decodeJWT(token);
|
|
1313
|
-
if (!decoded) {
|
|
1314
|
-
handleFail();
|
|
1315
|
-
return;
|
|
1316
|
-
}
|
|
1317
|
-
if (decoded.exp) {
|
|
1318
|
-
const expirationDate = new Date(decoded.exp * 1e3);
|
|
1319
|
-
if (expirationDate < /* @__PURE__ */ new Date()) {
|
|
1320
|
-
handleFail();
|
|
1321
|
-
return;
|
|
1322
|
-
}
|
|
1323
|
-
}
|
|
1324
|
-
setAuthState({
|
|
1325
|
-
isAuthenticated: true,
|
|
1326
|
-
isLoading: false
|
|
1327
|
-
});
|
|
1328
|
-
if (onAuthSuccess) {
|
|
1329
|
-
onAuthSuccess();
|
|
1330
|
-
}
|
|
1331
|
-
};
|
|
1332
|
-
verifyAuth();
|
|
1333
|
-
}, [cookieName]);
|
|
1334
|
-
const handleFail = () => {
|
|
1335
|
-
setAuthState({
|
|
1336
|
-
isAuthenticated: false,
|
|
1337
|
-
isLoading: false
|
|
1338
|
-
});
|
|
1339
|
-
setAuthFailed(true);
|
|
1340
|
-
if (onAuthFail) {
|
|
1341
|
-
onAuthFail();
|
|
1342
|
-
} else if (redirectUrl && typeof window !== "undefined") {
|
|
1343
|
-
window.location.href = redirectUrl;
|
|
1293
|
+
static getInstance(client) {
|
|
1294
|
+
if (!_SessionManager.instance) {
|
|
1295
|
+
_SessionManager.instance = new _SessionManager(client);
|
|
1344
1296
|
}
|
|
1345
|
-
|
|
1346
|
-
if (authState.isLoading || authFailed && !onAuthFail) {
|
|
1347
|
-
return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: loadingElement });
|
|
1297
|
+
return _SessionManager.instance;
|
|
1348
1298
|
}
|
|
1349
|
-
|
|
1350
|
-
|
|
1299
|
+
save(session) {
|
|
1300
|
+
this.cache = session;
|
|
1301
|
+
LocalStorage.setItem(this.key, session);
|
|
1302
|
+
this.setAuthToken(session.accessToken);
|
|
1303
|
+
}
|
|
1304
|
+
patch(updates) {
|
|
1305
|
+
const current = this.get();
|
|
1306
|
+
if (!current) return;
|
|
1307
|
+
const updated = { ...current, ...updates };
|
|
1308
|
+
this.cache = updated;
|
|
1309
|
+
LocalStorage.setItem(this.key, updated);
|
|
1310
|
+
if (updates.accessToken) {
|
|
1311
|
+
this.setAuthToken(updates.accessToken);
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
clear() {
|
|
1315
|
+
this.cache = null;
|
|
1316
|
+
LocalStorage.removeItem(this.key);
|
|
1317
|
+
this.clearAuthToken();
|
|
1318
|
+
}
|
|
1319
|
+
get() {
|
|
1320
|
+
if (this.cache !== null) return this.cache;
|
|
1321
|
+
this.cache = LocalStorage.getItem(this.key);
|
|
1322
|
+
return this.cache;
|
|
1323
|
+
}
|
|
1324
|
+
getAccessToken() {
|
|
1325
|
+
return this.get()?.accessToken || void 0;
|
|
1326
|
+
}
|
|
1327
|
+
getRefreshToken() {
|
|
1328
|
+
return this.get()?.refreshToken || void 0;
|
|
1329
|
+
}
|
|
1330
|
+
getSessionId() {
|
|
1331
|
+
return this.get()?.sessionId || void 0;
|
|
1332
|
+
}
|
|
1333
|
+
isAuthenticated() {
|
|
1334
|
+
const session = this.get();
|
|
1335
|
+
if (!session?.accessToken) return false;
|
|
1336
|
+
return Number(session.expiresAt) > Date.now() / 1e3;
|
|
1337
|
+
}
|
|
1338
|
+
initialize() {
|
|
1339
|
+
const session = this.get();
|
|
1340
|
+
if (session?.accessToken && Number(session.expiresAt) > Date.now() / 1e3) {
|
|
1341
|
+
this.setAuthToken(session.accessToken);
|
|
1342
|
+
} else {
|
|
1343
|
+
this.clear();
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
setAuthToken(token) {
|
|
1347
|
+
this.client.setDefaultHeader("Authorization", `Bearer ${token}`);
|
|
1348
|
+
}
|
|
1349
|
+
clearAuthToken() {
|
|
1350
|
+
this.client.removeDefaultHeader("Authorization");
|
|
1351
1351
|
}
|
|
1352
|
-
return /* @__PURE__ */ jsxRuntime.jsx(AuthContext.Provider, { value: authState, children });
|
|
1353
|
-
};
|
|
1354
|
-
var useAuth = () => {
|
|
1355
|
-
return react.useContext(AuthContext);
|
|
1356
1352
|
};
|
|
1357
1353
|
|
|
1358
|
-
// src/auth/
|
|
1359
|
-
var
|
|
1360
|
-
var
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
static
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
try {
|
|
1369
|
-
localStorage.setItem(PERMISSIONS_STORAGE_KEY, JSON.stringify(permissions));
|
|
1370
|
-
} catch (error) {
|
|
1371
|
-
console.error("Failed to save permissions to localStorage", error);
|
|
1354
|
+
// src/auth/AuthService.ts
|
|
1355
|
+
var BASE_URL = "/openauth/v1";
|
|
1356
|
+
var AuthService = class _AuthService {
|
|
1357
|
+
constructor(client) {
|
|
1358
|
+
this.client = client;
|
|
1359
|
+
this.sessionManager = SessionManager.getInstance(client);
|
|
1360
|
+
}
|
|
1361
|
+
static getInstance(client) {
|
|
1362
|
+
if (!_AuthService.instance) {
|
|
1363
|
+
_AuthService.instance = new _AuthService(client);
|
|
1372
1364
|
}
|
|
1365
|
+
return _AuthService.instance;
|
|
1373
1366
|
}
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
if (
|
|
1380
|
-
|
|
1367
|
+
async signIn(request) {
|
|
1368
|
+
const response = await this.client.post(
|
|
1369
|
+
`${BASE_URL}/auth/signin`,
|
|
1370
|
+
request
|
|
1371
|
+
);
|
|
1372
|
+
if (response.data.accessToken) {
|
|
1373
|
+
this.sessionManager.save(response.data);
|
|
1374
|
+
}
|
|
1375
|
+
return response.data;
|
|
1376
|
+
}
|
|
1377
|
+
async refreshToken(request) {
|
|
1378
|
+
const refreshToken = this.sessionManager.getRefreshToken();
|
|
1379
|
+
if (!refreshToken) {
|
|
1380
|
+
throw new Error("No refresh token available");
|
|
1381
|
+
}
|
|
1382
|
+
const response = await this.client.post(
|
|
1383
|
+
`${BASE_URL}/auth/refresh`,
|
|
1384
|
+
{ refreshToken, ...request }
|
|
1385
|
+
);
|
|
1386
|
+
if (response.data.accessToken) {
|
|
1387
|
+
this.sessionManager.patch({
|
|
1388
|
+
accessToken: response.data.accessToken,
|
|
1389
|
+
refreshToken: response.data.refreshToken
|
|
1390
|
+
});
|
|
1381
1391
|
}
|
|
1382
|
-
|
|
1392
|
+
return response.data;
|
|
1393
|
+
}
|
|
1394
|
+
async logout(request) {
|
|
1383
1395
|
try {
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
this.
|
|
1392
|
-
return permissionsArray;
|
|
1393
|
-
} catch (error) {
|
|
1394
|
-
console.error("Failed to parse permissions from localStorage", error);
|
|
1395
|
-
this.cachedPermissions = /* @__PURE__ */ new Set();
|
|
1396
|
-
return [];
|
|
1396
|
+
request.sessionId = this.sessionManager.getSessionId();
|
|
1397
|
+
const response = await this.client.post(
|
|
1398
|
+
`${BASE_URL}/auth/logout`,
|
|
1399
|
+
request || {}
|
|
1400
|
+
);
|
|
1401
|
+
return response.data;
|
|
1402
|
+
} finally {
|
|
1403
|
+
this.sessionManager.clear();
|
|
1397
1404
|
}
|
|
1398
1405
|
}
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
localStorage.removeItem(PERMISSIONS_STORAGE_KEY);
|
|
1406
|
+
async generateLoginToken(request) {
|
|
1407
|
+
const response = await this.client.put(
|
|
1408
|
+
`${BASE_URL}/auth/login-token`,
|
|
1409
|
+
request ?? {}
|
|
1410
|
+
);
|
|
1411
|
+
return response.data;
|
|
1406
1412
|
}
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
this.getPermissions();
|
|
1413
|
+
async signInWithLoginToken(request) {
|
|
1414
|
+
const response = await this.client.post(
|
|
1415
|
+
`${BASE_URL}/auth/signin-with-token`,
|
|
1416
|
+
request
|
|
1417
|
+
);
|
|
1418
|
+
if (response.data.accessToken) {
|
|
1419
|
+
this.sessionManager.save(response.data);
|
|
1415
1420
|
}
|
|
1416
|
-
return
|
|
1421
|
+
return response.data;
|
|
1422
|
+
}
|
|
1423
|
+
isAuthenticated() {
|
|
1424
|
+
return this.sessionManager.isAuthenticated();
|
|
1425
|
+
}
|
|
1426
|
+
getAccessToken() {
|
|
1427
|
+
return this.sessionManager.getAccessToken();
|
|
1428
|
+
}
|
|
1429
|
+
getSessionDetails() {
|
|
1430
|
+
return this.sessionManager.get();
|
|
1431
|
+
}
|
|
1432
|
+
getSessionId() {
|
|
1433
|
+
return this.sessionManager.getSessionId();
|
|
1434
|
+
}
|
|
1435
|
+
initializeAuth() {
|
|
1436
|
+
this.sessionManager.initialize();
|
|
1417
1437
|
}
|
|
1418
1438
|
};
|
|
1419
|
-
PermissionManager.cachedPermissions = null;
|
|
1420
1439
|
|
|
1421
|
-
exports.
|
|
1440
|
+
exports.AuthService = AuthService;
|
|
1422
1441
|
exports.ConfirmDialog = ConfirmDialog;
|
|
1423
1442
|
exports.HttpClient = HttpClient;
|
|
1424
1443
|
exports.LocalStorage = LocalStorage;
|
|
1425
1444
|
exports.NotificationProvider = NotificationProvider;
|
|
1426
|
-
exports.PermissionManager = PermissionManager;
|
|
1427
1445
|
exports.ReadmeViewer = ReadmeViewer_default;
|
|
1446
|
+
exports.SessionManager = SessionManager;
|
|
1428
1447
|
exports.SidebarLayout = SidebarLayout;
|
|
1429
1448
|
exports.ThemeProvider = ThemeProvider;
|
|
1430
1449
|
exports.ThemeToggle = ThemeToggle;
|
|
@@ -1433,6 +1452,7 @@ exports.cn = cn;
|
|
|
1433
1452
|
exports.darkTheme = darkTheme;
|
|
1434
1453
|
exports.debounce = debounce;
|
|
1435
1454
|
exports.elevation = elevation;
|
|
1455
|
+
exports.extractErrorMessage = extractErrorMessage;
|
|
1436
1456
|
exports.fontSize = fontSize;
|
|
1437
1457
|
exports.fontWeight = fontWeight;
|
|
1438
1458
|
exports.formatDate = formatDate;
|
|
@@ -1443,7 +1463,6 @@ exports.spacing = spacing;
|
|
|
1443
1463
|
exports.throttle = throttle;
|
|
1444
1464
|
exports.tokens = tokens_exports;
|
|
1445
1465
|
exports.transition = transition;
|
|
1446
|
-
exports.useAuth = useAuth;
|
|
1447
1466
|
exports.useNotification = useNotification;
|
|
1448
1467
|
exports.useTheme = useTheme;
|
|
1449
1468
|
exports.zIndex = zIndex;
|