react-ecosistema-unp 1.5.2-pre.1 → 1.5.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.
|
@@ -12,8 +12,9 @@ interface UseFetchGenericoParams<T> {
|
|
|
12
12
|
id?: number | string | null;
|
|
13
13
|
transformarResultado?: (data: any) => T;
|
|
14
14
|
dataInResultsValue?: boolean;
|
|
15
|
+
debug?: boolean;
|
|
15
16
|
}
|
|
16
|
-
export declare const useFetchData: <T>({ endpoint, localJsonPath, label, dependencia, queryParamKey, all, filtrarResultados, selectorDatos, timeout, timeoutMs, id, transformarResultado, dataInResultsValue, }: UseFetchGenericoParams<T>) => {
|
|
17
|
+
export declare const useFetchData: <T>({ endpoint, localJsonPath, label, dependencia, queryParamKey, all, filtrarResultados, selectorDatos, timeout, timeoutMs, id, transformarResultado, dataInResultsValue, debug, }: UseFetchGenericoParams<T>) => {
|
|
17
18
|
data: T[];
|
|
18
19
|
loading: boolean;
|
|
19
20
|
error: string | null;
|
|
@@ -1,97 +1,98 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
import { y as
|
|
3
|
-
const
|
|
1
|
+
import { useState as b, useEffect as S } from "react";
|
|
2
|
+
import { y as d } from "../../index-BUe3EJDH.js";
|
|
3
|
+
const B = ({
|
|
4
4
|
endpoint: n,
|
|
5
|
-
localJsonPath:
|
|
6
|
-
label:
|
|
7
|
-
dependencia:
|
|
5
|
+
localJsonPath: $,
|
|
6
|
+
label: g,
|
|
7
|
+
dependencia: w = null,
|
|
8
8
|
queryParamKey: u,
|
|
9
|
-
all:
|
|
10
|
-
filtrarResultados:
|
|
11
|
-
selectorDatos:
|
|
12
|
-
timeout:
|
|
13
|
-
timeoutMs:
|
|
9
|
+
all: T = !1,
|
|
10
|
+
filtrarResultados: a,
|
|
11
|
+
selectorDatos: D,
|
|
12
|
+
timeout: I = !1,
|
|
13
|
+
timeoutMs: k = 25e3,
|
|
14
14
|
// 25 segundos por defecto.
|
|
15
|
-
id:
|
|
16
|
-
transformarResultado:
|
|
17
|
-
dataInResultsValue:
|
|
15
|
+
id: E = null,
|
|
16
|
+
transformarResultado: c,
|
|
17
|
+
dataInResultsValue: A = !0,
|
|
18
|
+
debug: i = !1
|
|
18
19
|
}) => {
|
|
19
|
-
const [
|
|
20
|
-
return
|
|
21
|
-
if (!
|
|
22
|
-
|
|
20
|
+
const [y, h] = b([]), [H, j] = b(!0), [C, p] = b(null), s = g || n.replace(/\/$/, "").split("/").pop() || "datos";
|
|
21
|
+
return S(() => {
|
|
22
|
+
if (!T && w === null && u) {
|
|
23
|
+
h([]), j(!1);
|
|
23
24
|
return;
|
|
24
25
|
}
|
|
25
|
-
const
|
|
26
|
-
|
|
26
|
+
const L = async () => {
|
|
27
|
+
j(!0), p(null);
|
|
27
28
|
let e = n;
|
|
28
|
-
if (
|
|
29
|
-
e = `${n.replace(/\/$/, "")}/${
|
|
30
|
-
else if (
|
|
31
|
-
const
|
|
32
|
-
e = `${n}${
|
|
29
|
+
if (E != null)
|
|
30
|
+
e = `${n.replace(/\/$/, "")}/${E}/`;
|
|
31
|
+
else if (w != null && u) {
|
|
32
|
+
const o = n.includes("?") ? "&" : "?";
|
|
33
|
+
e = `${n}${o}${u}=${w}`;
|
|
33
34
|
}
|
|
34
|
-
let
|
|
35
|
+
let r = e, l = [];
|
|
35
36
|
try {
|
|
36
|
-
if (
|
|
37
|
-
for (;
|
|
38
|
-
let
|
|
39
|
-
if (
|
|
40
|
-
const
|
|
41
|
-
|
|
37
|
+
if (A)
|
|
38
|
+
for (; r; ) {
|
|
39
|
+
let o;
|
|
40
|
+
if (I ? o = await x(r, k) : o = await fetch(r), o.ok) {
|
|
41
|
+
const t = await o.json(), f = D || ((F) => F.results || []), U = a ? c ? c(f(t).filter(a)) : f(t).filter(a) : c ? c(f(t)) : f(t);
|
|
42
|
+
l = l.concat(U), r = t.next, h(l);
|
|
42
43
|
} else {
|
|
43
|
-
console.warn(`Error HTTP (${
|
|
44
|
+
i && console.warn(`Error HTTP (${o.status}). Intentando cargar ${s} desde archivo local...`), await v();
|
|
44
45
|
return;
|
|
45
46
|
}
|
|
46
|
-
console.log(`Cargando ${
|
|
47
|
+
console.log(`Cargando ${s} desde API...`);
|
|
47
48
|
}
|
|
48
49
|
else {
|
|
49
|
-
const
|
|
50
|
-
if (
|
|
51
|
-
const
|
|
52
|
-
|
|
50
|
+
const o = await x(e, 5e3);
|
|
51
|
+
if (o.ok) {
|
|
52
|
+
const t = await o.json(), f = c ? c(t) : t;
|
|
53
|
+
h(f);
|
|
53
54
|
} else
|
|
54
|
-
console.warn(`Error HTTP (${
|
|
55
|
+
i && console.warn(`Error HTTP (${o.status}). Cargando ${s} desde archivo local...`), await v();
|
|
55
56
|
}
|
|
56
57
|
} catch {
|
|
57
|
-
console.warn(`Excepción al cargar desde API. Intentando cargar ${
|
|
58
|
+
i && console.warn(`Excepción al cargar desde API. Intentando cargar ${s} desde archivo local...`), await v();
|
|
58
59
|
} finally {
|
|
59
|
-
|
|
60
|
+
j(!1);
|
|
60
61
|
}
|
|
61
|
-
},
|
|
62
|
-
if (
|
|
62
|
+
}, v = async () => {
|
|
63
|
+
if ($)
|
|
63
64
|
try {
|
|
64
|
-
if (
|
|
65
|
-
const e = await fetch(
|
|
65
|
+
if (A) {
|
|
66
|
+
const e = await fetch($);
|
|
66
67
|
if (e.ok) {
|
|
67
|
-
const
|
|
68
|
-
|
|
68
|
+
const r = await e.json(), l = a ? c ? c(r.results.filter(a)) : r.results.filter(a) : c ? c(r.results) : r.results;
|
|
69
|
+
h(l);
|
|
69
70
|
} else
|
|
70
|
-
|
|
71
|
+
i && d.error(`Hubo un problema al obtener ${s} desde el archivo local.`), p(`Error al obtener ${s} desde el archivo local`);
|
|
71
72
|
} else {
|
|
72
|
-
const e = await fetch(
|
|
73
|
+
const e = await fetch($);
|
|
73
74
|
if (e.ok) {
|
|
74
|
-
const
|
|
75
|
-
|
|
75
|
+
const r = await e.json(), l = c ? c(r) : r;
|
|
76
|
+
h(l);
|
|
76
77
|
} else
|
|
77
|
-
|
|
78
|
+
i && d.error(`Error al cargar ${s} desde archivo local.`), p(`Error al obtener ${s} desde el archivo local.`);
|
|
78
79
|
}
|
|
79
80
|
} catch (e) {
|
|
80
|
-
|
|
81
|
+
i && (d.error(`Hubo un problema al obtener ${s}.`), console.error(`Error al obtener ${s}:`, e)), p(`Error al obtener ${s}`);
|
|
81
82
|
}
|
|
82
|
-
},
|
|
83
|
-
const
|
|
83
|
+
}, x = async (e, r) => {
|
|
84
|
+
const l = new AbortController(), o = setTimeout(() => l.abort(), r);
|
|
84
85
|
try {
|
|
85
|
-
return await fetch(e, { signal:
|
|
86
|
-
} catch (
|
|
87
|
-
throw
|
|
86
|
+
return await fetch(e, { signal: l.signal });
|
|
87
|
+
} catch (t) {
|
|
88
|
+
throw t instanceof Error ? t.name === "AbortError" ? new Error("La solicitud a la API ha tardado demasiado y fue cancelada.") : t : new Error("Un error desconocido ocurrió.");
|
|
88
89
|
} finally {
|
|
89
|
-
clearTimeout(
|
|
90
|
+
clearTimeout(o);
|
|
90
91
|
}
|
|
91
92
|
};
|
|
92
|
-
|
|
93
|
-
}, [n,
|
|
93
|
+
L();
|
|
94
|
+
}, [n, E, $, w, u, T, g]), { data: y, loading: H, error: C };
|
|
94
95
|
};
|
|
95
96
|
export {
|
|
96
|
-
|
|
97
|
+
B as useFetchData
|
|
97
98
|
};
|