@putkoff/abstract-utilities 0.1.164 → 0.1.166

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/cjs/index.js CHANGED
@@ -4,6 +4,85 @@ var react = require('react');
4
4
  var jsxRuntime = require('react/jsx-runtime');
5
5
 
6
6
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
7
+ /**
8
+ * Returns `window` if running in a browser, otherwise `undefined`.
9
+ */
10
+ function getSafeLocalStorage() {
11
+ if (typeof window === 'undefined')
12
+ return undefined;
13
+ try {
14
+ return window.localStorage;
15
+ }
16
+ catch (_a) {
17
+ return undefined; // e.g. Safari private-mode block
18
+ }
19
+ }
20
+ /**
21
+ * Call a Storage method by name, silencing any errors or missing storage.
22
+ *
23
+ * @param method One of the keys of the Storage interface: "getItem", "setItem", etc.
24
+ * @param args The arguments you’d normally pass to that method.
25
+ * @returns The method’s return value, or undefined if storage/method isn’t available.
26
+ */
27
+ function callStorage(method, ...args) {
28
+ const storage = getSafeLocalStorage();
29
+ if (!storage)
30
+ return undefined;
31
+ const fn = storage[method];
32
+ if (typeof fn !== 'function')
33
+ return undefined;
34
+ try {
35
+ // @ts-ignore – TS can’t infer that this is callable
36
+ return fn.apply(storage, args);
37
+ }
38
+ catch (_a) {
39
+ return undefined;
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Returns the global window object if it exists, otherwise undefined.
45
+ */
46
+ function getSafeWindow() {
47
+ return typeof window !== 'undefined' ? window : undefined;
48
+ }
49
+ /**
50
+ * Safely call a method on window by name.
51
+ *
52
+ * @param method The Window method to call (e.g. "alert", "open", etc.).
53
+ * @param args Arguments to pass to that method.
54
+ * @returns The method’s return value, or undefined if
55
+ * window/method isn’t available or throws.
56
+ */
57
+ function callWindowMethod(method, ...args) {
58
+ const w = getSafeWindow();
59
+ if (!w)
60
+ return undefined;
61
+ const fn = w[method];
62
+ if (typeof fn !== 'function')
63
+ return undefined;
64
+ try {
65
+ // cast to any so TS doesn’t complain about apply/invoke
66
+ return fn(...args);
67
+ }
68
+ catch (_a) {
69
+ return undefined;
70
+ }
71
+ }
72
+ /** implementation */
73
+ function getWindowProp(...keys) {
74
+ let obj = getSafeWindow();
75
+ for (const k of keys) {
76
+ if (obj == null || typeof obj !== 'object')
77
+ return undefined;
78
+ obj = obj[k];
79
+ }
80
+ return obj;
81
+ }
82
+ function getWindowHost() {
83
+ return getWindowProp('location', 'host');
84
+ }
85
+
7
86
  /**
8
87
  ***Changes**:
9
88
  *- Updated import path for `InputProps` to `../../types/interfaces`.
@@ -12,13 +91,13 @@ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentS
12
91
  * Copy from `/var/www/abstractendeavors/my-login-app/src/functions/auth_utils/token_utils.ts`.
13
92
  *
14
93
  */
15
- function isLoggedIn() {
16
- const tok = localStorage.getItem("token");
17
- return !!tok && !isTokenExpired(tok !== null && tok !== void 0 ? tok : "");
18
- }
19
94
  /** Read raw JWT from LocalStorage (or null if absent) */
20
95
  function getToken() {
21
- return localStorage.getItem("token");
96
+ return callStorage('getItem', 'token');
97
+ }
98
+ function isLoggedIn() {
99
+ const tok = getToken();
100
+ return !!tok && !isTokenExpired(tok !== null && tok !== void 0 ? tok : "");
22
101
  }
23
102
  /**
24
103
  * Add a Bearer Authorization header.
@@ -67,7 +146,7 @@ function currentUsername() {
67
146
  }
68
147
  function currentUsernames() {
69
148
  var _a;
70
- const tok = localStorage.getItem("token");
149
+ const tok = getToken();
71
150
  if (!tok)
72
151
  return null;
73
152
  try {
@@ -1038,11 +1117,12 @@ function ensureAbstractUrl(endpoint, slices = []) {
1038
1117
  slices = slices || ['https//abstractendeavors.com', 'api'];
1039
1118
  // 1) build a prefix string like "api/v1/"
1040
1119
  const prefix = slices.map((s) => `${s}/`).join("");
1120
+ const windowHost = getWindowHost();
1041
1121
  const normalized = [
1042
1122
  '/',
1043
1123
  ...slices,
1044
- window.location.host, // so "abstractendeavors.com" will be stripped
1045
- `${window.location.host}/api` // etc, if you need it
1124
+ windowHost, // so "abstractendeavors.com" will be stripped
1125
+ `${windowHost}/api` // etc, if you need it
1046
1126
  ];
1047
1127
  const stripped = stripPrefixes(endpoint, normalized);
1048
1128
  console.log('BUILD PREFIX:', prefix);
@@ -1112,7 +1192,7 @@ function getHeaders(headers = {}, method = null, body = null) {
1112
1192
  const result = Object.assign({}, headers);
1113
1193
  // inject auth if missing
1114
1194
  if (!result.Authorization) {
1115
- const token = localStorage.getItem('token');
1195
+ const token = getToken();
1116
1196
  Object.assign(result, getAuthorizationHeader(result, token));
1117
1197
  }
1118
1198
  method = getMethod(method, body);
@@ -1257,7 +1337,7 @@ function fetchSharePatch(file_1) {
1257
1337
  const cleanEndpoint = '/files/share';
1258
1338
  // build final URL
1259
1339
  const url = yield get_app_config_url(cleanEndpoint);
1260
- const token = localStorage.getItem('token');
1340
+ const token = callStorage('getItem', 'token');
1261
1341
  const body = JSON.stringify(file);
1262
1342
  const method = 'PATCH';
1263
1343
  const headers = {
@@ -1390,6 +1470,8 @@ exports.SUB_DIR = SUB_DIR;
1390
1470
  exports.Spinner = Spinner;
1391
1471
  exports.alertit = alertit;
1392
1472
  exports.api = api;
1473
+ exports.callStorage = callStorage;
1474
+ exports.callWindowMethod = callWindowMethod;
1393
1475
  exports.checkResponse = checkResponse;
1394
1476
  exports.currentUsername = currentUsername;
1395
1477
  exports.currentUsernames = currentUsernames;
@@ -1435,11 +1517,15 @@ exports.getLibUtilsDirectory = getLibUtilsDirectory;
1435
1517
  exports.getMethod = getMethod;
1436
1518
  exports.getPublicDir = getPublicDir;
1437
1519
  exports.getResult = getResult;
1520
+ exports.getSafeLocalStorage = getSafeLocalStorage;
1521
+ exports.getSafeWindow = getSafeWindow;
1438
1522
  exports.getSchemasDirPath = getSchemasDirPath;
1439
1523
  exports.getSchemasPath = getSchemasPath;
1440
1524
  exports.getSrcDir = getSrcDir;
1441
1525
  exports.getSubstring = getSubstring;
1442
1526
  exports.getToken = getToken;
1527
+ exports.getWindowHost = getWindowHost;
1528
+ exports.getWindowProp = getWindowProp;
1443
1529
  exports.get_app_config_url = get_app_config_url;
1444
1530
  exports.get_basename = get_basename;
1445
1531
  exports.get_dirname = get_dirname;