react-ecosistema-unp 1.6.0-pre.9 → 1.6.4

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.
@@ -1,47 +1,57 @@
1
+ import { useNavigate as m } from "react-router-dom";
1
2
  window.originalFetch = window.fetch.bind(window);
2
- let c = !1, h = null;
3
- const s = [];
3
+ let l = !1, h = null;
4
+ const i = [];
4
5
  async function k() {
5
- const e = `${`${window.location.protocol}//${window.location.host}/`}api-auth/auth/refresh_token/`, r = await window.originalFetch(e, {
6
+ const e = `${`${window.location.protocol}//${window.location.host}/`}api-auth/auth/refresh_token/`, t = await window.originalFetch(e, {
6
7
  method: "POST",
7
8
  credentials: "include"
8
9
  });
9
- if (!r.ok)
10
- throw new Error("Error al refrescar token, status " + r.status);
11
- const n = await r.json();
12
- if (!n.token)
13
- throw new Error("No se recibió token en la respuesta de refresh");
14
- const t = n.token;
15
- return sessionStorage.setItem("user_token", `${t}`), `${t}`;
10
+ if (!t.ok)
11
+ throw new Error("Error al refrescar token, status " + t.status);
12
+ return "Token refreshed";
16
13
  }
17
14
  window.fetch = async function(o, e) {
18
- const r = typeof o == "string" ? o : o.toString();
19
- if (console.log("URL petición:", r), r.includes("/api-auth/"))
15
+ const t = typeof o == "string" ? o : o.toString();
16
+ if (console.log("URL petición:", t), t.includes("/api-auth/"))
20
17
  return window.originalFetch(o, e);
21
18
  e = e || {};
22
- const n = sessionStorage.getItem("user_token");
19
+ const u = sessionStorage.getItem("user_token");
23
20
  e.headers = {
24
21
  ...e.headers,
25
- ...n ? { Authorization: n } : {}
22
+ ...u ? { Authorization: u } : {}
26
23
  };
27
24
  try {
28
- const t = await window.originalFetch(o, e);
29
- return console.log("Response petición:", t), t.status !== 401 && !(await t.text()).includes("Signature has expired") ? t : new Promise((l, w) => {
30
- s.push({ input: o, init: e, resolve: l, reject: w }), c || (c = !0, h = k(), h.then((a) => {
31
- s.forEach(({ input: i, init: u, resolve: d, reject: f }) => {
32
- const g = {
33
- ...u,
34
- headers: { ...u.headers, Authorization: a }
25
+ const a = m(), d = `${window.location.protocol}//${window.location.host}/`, r = await window.originalFetch(o, e);
26
+ if (r.status !== 401) {
27
+ const n = await r.clone().text();
28
+ if (!n.includes("Signature has expired"))
29
+ return r;
30
+ if (n.includes("Invalid or expired token"))
31
+ return sessionStorage.removeItem("user_token"), setTimeout(() => {
32
+ a("/"), window.location.href = d;
33
+ }, 1e3), r;
34
+ }
35
+ return new Promise((w, n) => {
36
+ i.push({ input: o, init: e, resolve: w, reject: n }), l || (l = !0, h = k(), h.then(() => {
37
+ i.forEach(({ input: c, init: s, resolve: f, reject: g }) => {
38
+ const p = {
39
+ ...s,
40
+ headers: { ...s.headers, Authorization: sessionStorage.getItem("user_token") }
35
41
  };
36
- window.originalFetch(i, g).then(d).catch(f);
42
+ window.originalFetch(c, p).then(f).catch(g);
37
43
  });
38
- }).catch((a) => {
39
- s.forEach(({ reject: i }) => i(a));
44
+ }).catch((c) => {
45
+ i.forEach(({ reject: s }) => s(c));
40
46
  }).finally(() => {
41
- s.length = 0, c = !1, h = null;
47
+ i.length = 0, l = !1, h = null;
42
48
  }));
43
49
  });
44
- } catch (t) {
45
- return Promise.reject(t);
50
+ } catch (a) {
51
+ return Promise.reject(a);
46
52
  }
47
53
  };
54
+ const I = {};
55
+ export {
56
+ I as default
57
+ };
package/dist/utils.js CHANGED
@@ -1,17 +1,19 @@
1
1
  import { AuthProvider as t } from "./auth/contexts/AuthContex.js";
2
- import { ProtectedRoute as m } from "./utils/components/ProtectedRoute.js";
3
- import { UserRoute as a } from "./utils/components/UserRoute.js";
4
- import { UnpHistorico as f } from "./utils/unp-historico/UnpHistorico.js";
5
- import { IsInvalidStyles as x, useFormValidation as c } from "./utils/validations/useFormValidation.js";
2
+ import { ProtectedRoute as p } from "./utils/components/ProtectedRoute.js";
3
+ import { UserRoute as f } from "./utils/components/UserRoute.js";
4
+ import { UnpHistorico as s } from "./utils/unp-historico/UnpHistorico.js";
5
+ import { IsInvalidStyles as c, useFormValidation as i } from "./utils/validations/useFormValidation.js";
6
6
  import { CaptchaEcosistema as d } from "./utils/captcha/CaptchaEcosistema.js";
7
7
  import { useFetchData as l } from "./utils/fetch-data/useFetchData.js";
8
+ import { default as I } from "./utils/components/fetchInterceptor.js";
8
9
  export {
9
10
  t as AuthProvider,
10
11
  d as CaptchaEcosistema,
11
- x as IsInvalidStyles,
12
- m as ProtectedRoute,
13
- f as UnpHistorico,
14
- a as UserRoute,
12
+ c as IsInvalidStyles,
13
+ p as ProtectedRoute,
14
+ s as UnpHistorico,
15
+ f as UserRoute,
16
+ I as fetchInterceptor,
15
17
  l as useFetchData,
16
- c as useFormValidation
18
+ i as useFormValidation
17
19
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "react-ecosistema-unp",
3
3
  "author": "Ecosistema de Información - Unidad Nacional de Protección",
4
4
  "private": false,
5
- "version": "1.6.0-pre.9",
5
+ "version": "1.6.4",
6
6
  "type": "module",
7
7
  "exports": {
8
8
  ".": {