@putkoff/abstract-utilities 0.1.139 → 0.1.141

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.
@@ -5,7 +5,15 @@ export declare function ensureAbstractUrl(endpoint: string, slices?: string[]):
5
5
  */
6
6
  export declare function getResult<T>(obj: unknown): T;
7
7
  export declare function api(endpoint: string): string;
8
- export declare function checkResponse(res: Response): any;
8
+ /**
9
+ * Intercept 401/403 and force a clean redirect to login
10
+ * without ever showing an alert.
11
+ */
12
+ export declare function checkResponse(res: Response): Response;
13
+ /**
14
+ * parseResult no longer needs to worry about JSON vs HTML redirect errors;
15
+ * all 401/403 have already been handled above.
16
+ */
9
17
  export declare function parseResult(res: Response): Promise<unknown>;
10
18
  export declare function getMethod(method?: string | null, body?: unknown): string;
11
19
  export declare function getHeaders(headers?: Record<string, string>, method?: string | null, body?: unknown): Record<string, string>;
package/dist/cjs/index.js CHANGED
@@ -1053,34 +1053,36 @@ function getResult(obj) {
1053
1053
  }
1054
1054
  return current;
1055
1055
  }
1056
- // Checks if response is OK, handles 401/403 by redirecting to login
1056
+ /**
1057
+ * Intercept 401/403 and force a clean redirect to login
1058
+ * without ever showing an alert.
1059
+ */
1057
1060
  function checkResponse(res) {
1058
- if (!res.ok) {
1059
- if (res.status === 401 || res.status === 403) {
1060
- alert('Session expired. Please log in again.');
1061
- localStorage.removeItem('token');
1062
- window.location.href = '/secure-files';
1063
- }
1061
+ if (res.status === 401 || res.status === 403) {
1062
+ // 1) clear out the stale token
1063
+ localStorage.removeItem("token");
1064
+ // 2) replace history so "back" doesn’t re-trigger the protected route
1065
+ window.history.replaceState({}, "", "/secure-files/login");
1066
+ // 3) short-circuit all further fetch logic
1067
+ throw new Error("SessionExpired");
1064
1068
  }
1065
1069
  return res;
1066
1070
  }
1067
- // Parses JSON response and handles specific response shapes
1071
+ /**
1072
+ * parseResult no longer needs to worry about JSON vs HTML redirect errors;
1073
+ * all 401/403 have already been handled above.
1074
+ */
1068
1075
  function parseResult(res) {
1069
1076
  return __awaiter(this, void 0, void 0, function* () {
1077
+ // runs checkResponse first, will throw if session is expired
1070
1078
  res = checkResponse(res);
1071
- try {
1072
- const data = yield res.json();
1073
- if ((data === null || data === void 0 ? void 0 : data.success) === true && Array.isArray(data.result)) {
1074
- return data.result;
1075
- }
1076
- if ((data === null || data === void 0 ? void 0 : data.error) && (data === null || data === void 0 ? void 0 : data.message) && typeof data.message === 'string') {
1077
- alert(`Error: ${data.message}`);
1078
- }
1079
- return data;
1080
- }
1081
- catch (err) {
1082
- throw new Error('Server returned invalid JSON');
1079
+ if (!res.ok) {
1080
+ // for any other non-401 errors, you can still surface them
1081
+ const errorText = yield res.text();
1082
+ throw new Error(errorText || res.statusText);
1083
1083
  }
1084
+ // now safely parse JSON
1085
+ return res.json();
1084
1086
  });
1085
1087
  }
1086
1088
  // Determines HTTP method, defaults to GET or POST based on body