@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/esm/index.js CHANGED
@@ -1,6 +1,85 @@
1
1
  export { useCallback, useEffect, useRef, useState } from 'react';
2
2
  import { jsx, jsxs } from 'react/jsx-runtime';
3
3
 
4
+ /**
5
+ * Returns `window` if running in a browser, otherwise `undefined`.
6
+ */
7
+ function getSafeLocalStorage() {
8
+ if (typeof window === 'undefined')
9
+ return undefined;
10
+ try {
11
+ return window.localStorage;
12
+ }
13
+ catch (_a) {
14
+ return undefined; // e.g. Safari private-mode block
15
+ }
16
+ }
17
+ /**
18
+ * Call a Storage method by name, silencing any errors or missing storage.
19
+ *
20
+ * @param method One of the keys of the Storage interface: "getItem", "setItem", etc.
21
+ * @param args The arguments you’d normally pass to that method.
22
+ * @returns The method’s return value, or undefined if storage/method isn’t available.
23
+ */
24
+ function callStorage(method, ...args) {
25
+ const storage = getSafeLocalStorage();
26
+ if (!storage)
27
+ return undefined;
28
+ const fn = storage[method];
29
+ if (typeof fn !== 'function')
30
+ return undefined;
31
+ try {
32
+ // @ts-ignore – TS can’t infer that this is callable
33
+ return fn.apply(storage, args);
34
+ }
35
+ catch (_a) {
36
+ return undefined;
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Returns the global window object if it exists, otherwise undefined.
42
+ */
43
+ function getSafeWindow() {
44
+ return typeof window !== 'undefined' ? window : undefined;
45
+ }
46
+ /**
47
+ * Safely call a method on window by name.
48
+ *
49
+ * @param method The Window method to call (e.g. "alert", "open", etc.).
50
+ * @param args Arguments to pass to that method.
51
+ * @returns The method’s return value, or undefined if
52
+ * window/method isn’t available or throws.
53
+ */
54
+ function callWindowMethod(method, ...args) {
55
+ const w = getSafeWindow();
56
+ if (!w)
57
+ return undefined;
58
+ const fn = w[method];
59
+ if (typeof fn !== 'function')
60
+ return undefined;
61
+ try {
62
+ // cast to any so TS doesn’t complain about apply/invoke
63
+ return fn(...args);
64
+ }
65
+ catch (_a) {
66
+ return undefined;
67
+ }
68
+ }
69
+ /** implementation */
70
+ function getWindowProp(...keys) {
71
+ let obj = getSafeWindow();
72
+ for (const k of keys) {
73
+ if (obj == null || typeof obj !== 'object')
74
+ return undefined;
75
+ obj = obj[k];
76
+ }
77
+ return obj;
78
+ }
79
+ function getWindowHost() {
80
+ return getWindowProp('location', 'host');
81
+ }
82
+
4
83
  /**
5
84
  ***Changes**:
6
85
  *- Updated import path for `InputProps` to `../../types/interfaces`.
@@ -9,13 +88,13 @@ import { jsx, jsxs } from 'react/jsx-runtime';
9
88
  * Copy from `/var/www/abstractendeavors/my-login-app/src/functions/auth_utils/token_utils.ts`.
10
89
  *
11
90
  */
12
- function isLoggedIn() {
13
- const tok = localStorage.getItem("token");
14
- return !!tok && !isTokenExpired(tok !== null && tok !== void 0 ? tok : "");
15
- }
16
91
  /** Read raw JWT from LocalStorage (or null if absent) */
17
92
  function getToken() {
18
- return localStorage.getItem("token");
93
+ return callStorage('getItem', 'token');
94
+ }
95
+ function isLoggedIn() {
96
+ const tok = getToken();
97
+ return !!tok && !isTokenExpired(tok !== null && tok !== void 0 ? tok : "");
19
98
  }
20
99
  /**
21
100
  * Add a Bearer Authorization header.
@@ -64,7 +143,7 @@ function currentUsername() {
64
143
  }
65
144
  function currentUsernames() {
66
145
  var _a;
67
- const tok = localStorage.getItem("token");
146
+ const tok = getToken();
68
147
  if (!tok)
69
148
  return null;
70
149
  try {
@@ -1035,11 +1114,12 @@ function ensureAbstractUrl(endpoint, slices = []) {
1035
1114
  slices = slices || ['https//abstractendeavors.com', 'api'];
1036
1115
  // 1) build a prefix string like "api/v1/"
1037
1116
  const prefix = slices.map((s) => `${s}/`).join("");
1117
+ const windowHost = getWindowHost();
1038
1118
  const normalized = [
1039
1119
  '/',
1040
1120
  ...slices,
1041
- window.location.host, // so "abstractendeavors.com" will be stripped
1042
- `${window.location.host}/api` // etc, if you need it
1121
+ windowHost, // so "abstractendeavors.com" will be stripped
1122
+ `${windowHost}/api` // etc, if you need it
1043
1123
  ];
1044
1124
  const stripped = stripPrefixes(endpoint, normalized);
1045
1125
  console.log('BUILD PREFIX:', prefix);
@@ -1109,7 +1189,7 @@ function getHeaders(headers = {}, method = null, body = null) {
1109
1189
  const result = Object.assign({}, headers);
1110
1190
  // inject auth if missing
1111
1191
  if (!result.Authorization) {
1112
- const token = localStorage.getItem('token');
1192
+ const token = getToken();
1113
1193
  Object.assign(result, getAuthorizationHeader(result, token));
1114
1194
  }
1115
1195
  method = getMethod(method, body);
@@ -1254,7 +1334,7 @@ function fetchSharePatch(file_1) {
1254
1334
  const cleanEndpoint = '/files/share';
1255
1335
  // build final URL
1256
1336
  const url = yield get_app_config_url(cleanEndpoint);
1257
- const token = localStorage.getItem('token');
1337
+ const token = callStorage('getItem', 'token');
1258
1338
  const body = JSON.stringify(file);
1259
1339
  const method = 'PATCH';
1260
1340
  const headers = {
@@ -1358,5 +1438,5 @@ function readJsonFile(relativeOrAbsolutePath) {
1358
1438
  });
1359
1439
  }
1360
1440
 
1361
- export { API_PREFIX, BASE_URL, Button, Checkbox, DEV_PREFIX, DOMAIN_NAME, Input, PROD_PREFIX, PROTOCOL, SUB_DIR, Spinner, alertit, api, checkResponse, currentUsername, currentUsernames, decodeJwt, eatAll, eatEnd, eatInner, eatOuter, ensureAbstractUrl, ensure_list, fetchIndexHtml, fetchIndexHtmlContainer, fetchIt, fetchSharePatch, geAuthsUtilsDirectory, geBackupsUtilsDirectory, geConstantsUtilsDirectory, geEnvUtilsDirectory, geFetchUtilsDirectory, geFileUtilsDirectory, gePathUtilsDirectory, geStaticDirectory, geStringUtilsDirectory, geTypeUtilsDirectory, getAbsDir, getAbsPath, getAuthorizationHeader, getBaseDir, getBody, getComponentsUtilsDirectory, getConfig, getDbConfigsPath, getDistDir, getEnvDir, getEnvPath, getFetchVars, getFunctionsDir, getFunctionsUtilsDirectory, getHeaders, getHooksUtilsDirectory, getHtmlDirectory, getLibUtilsDirectory, getMethod, getPublicDir, getResult, getSchemasDirPath, getSchemasPath, getSrcDir, getSubstring, getToken, get_app_config_url, get_basename, get_dirname, get_extname, get_filename, get_splitext, get_window, get_window_location, get_window_parts, get_window_pathname, isLoggedIn, isTokenExpired, loadConfig, make_path, make_sanitized_path, normalizeUrl, parseResult, readFileContents, readJsonFile, requestPatch, requireToken, sanitizeFilename, secureFetchIt, stripPrefixes, truncateString };
1441
+ export { API_PREFIX, BASE_URL, Button, Checkbox, DEV_PREFIX, DOMAIN_NAME, Input, PROD_PREFIX, PROTOCOL, SUB_DIR, Spinner, alertit, api, callStorage, callWindowMethod, checkResponse, currentUsername, currentUsernames, decodeJwt, eatAll, eatEnd, eatInner, eatOuter, ensureAbstractUrl, ensure_list, fetchIndexHtml, fetchIndexHtmlContainer, fetchIt, fetchSharePatch, geAuthsUtilsDirectory, geBackupsUtilsDirectory, geConstantsUtilsDirectory, geEnvUtilsDirectory, geFetchUtilsDirectory, geFileUtilsDirectory, gePathUtilsDirectory, geStaticDirectory, geStringUtilsDirectory, geTypeUtilsDirectory, getAbsDir, getAbsPath, getAuthorizationHeader, getBaseDir, getBody, getComponentsUtilsDirectory, getConfig, getDbConfigsPath, getDistDir, getEnvDir, getEnvPath, getFetchVars, getFunctionsDir, getFunctionsUtilsDirectory, getHeaders, getHooksUtilsDirectory, getHtmlDirectory, getLibUtilsDirectory, getMethod, getPublicDir, getResult, getSafeLocalStorage, getSafeWindow, getSchemasDirPath, getSchemasPath, getSrcDir, getSubstring, getToken, getWindowHost, getWindowProp, get_app_config_url, get_basename, get_dirname, get_extname, get_filename, get_splitext, get_window, get_window_location, get_window_parts, get_window_pathname, isLoggedIn, isTokenExpired, loadConfig, make_path, make_sanitized_path, normalizeUrl, parseResult, readFileContents, readJsonFile, requestPatch, requireToken, sanitizeFilename, secureFetchIt, stripPrefixes, truncateString };
1362
1442
  //# sourceMappingURL=index.js.map