@krainovsd/js-helpers 0.19.0-beta.1 → 0.19.0-beta.2

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/lib/cjs/index.cjs CHANGED
@@ -1757,13 +1757,14 @@ function throttle(func, interval) {
1757
1757
 
1758
1758
  const RESPONSE_DATA_SYMBOL = Symbol("response data");
1759
1759
  const REQUEST_ERROR = {
1760
- HTTP_ERROR: "http",
1761
- NETWORK_ERROR: "network",
1762
- TIMEOUT_ERROR: "timeout",
1763
- ABORT_ERROR: "abort",
1764
- VALIDATION_ERROR: "validation",
1765
- CACHE_ERROR: "cache",
1766
- UNKNOWN_ERROR: "unknown",
1760
+ Http: "http",
1761
+ Network: "network",
1762
+ Timeout: "timeout",
1763
+ Abort: "abort",
1764
+ Validation: "validation",
1765
+ Cache: "cache",
1766
+ Unknown: "unknown",
1767
+ Disabled: "disabled",
1767
1768
  };
1768
1769
  class OauthState {
1769
1770
  _fetching = false;
@@ -1789,6 +1790,7 @@ function createFetchClient(options) {
1789
1790
  let retries = options.retries;
1790
1791
  let timeout = options.timeout;
1791
1792
  let refetchAfterAuthGlobal = options.refetchAfterAuth;
1793
+ let enabled = options.enabled ?? true;
1792
1794
  function recreate(options) {
1793
1795
  if ("client" in options) {
1794
1796
  client = options.client;
@@ -1808,8 +1810,18 @@ function createFetchClient(options) {
1808
1810
  if ("refetchAfterAuth" in options) {
1809
1811
  refetchAfterAuthGlobal = options.refetchAfterAuth;
1810
1812
  }
1813
+ if ("enabled" in options) {
1814
+ enabled = options.enabled ?? true;
1815
+ }
1811
1816
  }
1812
1817
  async function handleRequest(request) {
1818
+ if (!enabled) {
1819
+ return {
1820
+ response: null,
1821
+ data: new ResponseError({ message: "requests disabled", status: 0 }),
1822
+ error: REQUEST_ERROR.Disabled,
1823
+ };
1824
+ }
1813
1825
  const timeoutController = new AbortController();
1814
1826
  const requestController = new AbortController();
1815
1827
  const requestTimeout = request.timeout ?? timeout;
@@ -1918,12 +1930,12 @@ function createFetchClient(options) {
1918
1930
  if (!response.ok) {
1919
1931
  if (response.status === 304) {
1920
1932
  const error = new ResponseError({
1921
- message: REQUEST_ERROR.CACHE_ERROR,
1933
+ message: REQUEST_ERROR.Cache,
1922
1934
  status: response.status,
1923
1935
  headers: Object.fromEntries(response.headers.entries()),
1924
1936
  });
1925
- request.onError?.(REQUEST_ERROR.CACHE_ERROR, error);
1926
- return { data: error, error: REQUEST_ERROR.CACHE_ERROR, response };
1937
+ request.onError?.(REQUEST_ERROR.Cache, error);
1938
+ return { data: error, error: REQUEST_ERROR.Cache, response };
1927
1939
  }
1928
1940
  /** if you need observe other than only 401 status, you can override some statuses in after handler middleware to 401 */
1929
1941
  if (response.status === 401 && refetchAfterAuth) {
@@ -1966,14 +1978,14 @@ function createFetchClient(options) {
1966
1978
  catch { }
1967
1979
  const error = new ResponseError({
1968
1980
  status: response.status,
1969
- message: REQUEST_ERROR.HTTP_ERROR,
1981
+ message: REQUEST_ERROR.Http,
1970
1982
  description: result,
1971
1983
  headers: Object.fromEntries(response.headers.entries()),
1972
1984
  });
1973
- request.onError?.(REQUEST_ERROR.HTTP_ERROR, error);
1985
+ request.onError?.(REQUEST_ERROR.Http, error);
1974
1986
  return {
1975
1987
  data: error,
1976
- error: REQUEST_ERROR.HTTP_ERROR,
1988
+ error: REQUEST_ERROR.Http,
1977
1989
  response,
1978
1990
  };
1979
1991
  }
@@ -2042,36 +2054,36 @@ function createFetchClient(options) {
2042
2054
  }
2043
2055
  if (err instanceof TypeError) {
2044
2056
  const error = new ResponseError({
2045
- message: REQUEST_ERROR.NETWORK_ERROR,
2057
+ message: REQUEST_ERROR.Network,
2046
2058
  status: 0,
2047
2059
  description: String(err.message),
2048
2060
  });
2049
- request.onError?.(REQUEST_ERROR.NETWORK_ERROR, error);
2050
- return { data: error, error: REQUEST_ERROR.NETWORK_ERROR, response: null };
2061
+ request.onError?.(REQUEST_ERROR.Network, error);
2062
+ return { data: error, error: REQUEST_ERROR.Network, response: null };
2051
2063
  }
2052
2064
  if (err instanceof Error && err.name === "AbortError") {
2053
2065
  if (timeoutController.signal.aborted) {
2054
2066
  const error = new ResponseError({
2055
- message: REQUEST_ERROR.TIMEOUT_ERROR,
2067
+ message: REQUEST_ERROR.Timeout,
2056
2068
  status: 0,
2057
2069
  });
2058
- request.onError?.(REQUEST_ERROR.TIMEOUT_ERROR, error);
2059
- return { data: error, error: REQUEST_ERROR.TIMEOUT_ERROR, response: null };
2070
+ request.onError?.(REQUEST_ERROR.Timeout, error);
2071
+ return { data: error, error: REQUEST_ERROR.Timeout, response: null };
2060
2072
  }
2061
2073
  const error = new ResponseError({
2062
- message: REQUEST_ERROR.ABORT_ERROR,
2074
+ message: REQUEST_ERROR.Abort,
2063
2075
  status: 0,
2064
2076
  });
2065
- request.onError?.(REQUEST_ERROR.ABORT_ERROR, error);
2066
- return { data: error, error: REQUEST_ERROR.ABORT_ERROR, response: null };
2077
+ request.onError?.(REQUEST_ERROR.Abort, error);
2078
+ return { data: error, error: REQUEST_ERROR.Abort, response: null };
2067
2079
  }
2068
2080
  const error = new ResponseError({
2069
- message: REQUEST_ERROR.UNKNOWN_ERROR,
2081
+ message: REQUEST_ERROR.Unknown,
2070
2082
  status: 0,
2071
2083
  description: String(err),
2072
2084
  });
2073
- request.onError?.(REQUEST_ERROR.UNKNOWN_ERROR, error);
2074
- return { data: error, error: REQUEST_ERROR.UNKNOWN_ERROR, response: null };
2085
+ request.onError?.(REQUEST_ERROR.Unknown, error);
2086
+ return { data: error, error: REQUEST_ERROR.Unknown, response: null };
2075
2087
  }
2076
2088
  finally {
2077
2089
  requestController.abort();
@@ -2261,6 +2273,9 @@ function createOauthProvider(opts = {}) {
2261
2273
  register,
2262
2274
  refetchAfterRefreshFlow,
2263
2275
  beforeHandler,
2276
+ get isServicePage() {
2277
+ return [errorPageUrl, clearPageUrl, logoutPageUrl].some((u) => document.location.pathname === u);
2278
+ },
2264
2279
  get processing() {
2265
2280
  return processing;
2266
2281
  },