use-fetch-error 1.0.0 → 1.0.1
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/README.md +20 -0
- package/package.json +9 -1
- package/src/index.js +52 -97
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Use-fetch-error
|
|
2
|
+
|
|
3
|
+
## A library for error handling with [error, result] tuples using a `useFetchError()` function.
|
|
4
|
+
|
|
5
|
+
### Example usage:
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
(async () => {
|
|
9
|
+
const validURL = "https://jsonplaceholder.typicode.com/posts/1";
|
|
10
|
+
const invalidURL = "https://url-invalid.com";
|
|
11
|
+
|
|
12
|
+
const [error, data] = await useFetchError(invalidURL);
|
|
13
|
+
|
|
14
|
+
if (error) {
|
|
15
|
+
console.error("An error occurred:", error);
|
|
16
|
+
} else {
|
|
17
|
+
console.log("Data received:", data);
|
|
18
|
+
}
|
|
19
|
+
})();
|
|
20
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-fetch-error",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "A library for error handling with [error, result] tuples using a useFetch() function.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"javascript",
|
|
7
|
+
"fetch",
|
|
8
|
+
"handle error"
|
|
9
|
+
],
|
|
10
|
+
"author": "llatzer",
|
|
11
|
+
"license": "MIT",
|
|
4
12
|
"main": "src/index.js",
|
|
5
13
|
"browser": "src/index.js"
|
|
6
14
|
}
|
package/src/index.js
CHANGED
|
@@ -1,103 +1,58 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.useFetchError = useFetchError;
|
|
40
|
-
var symbol = Symbol("result");
|
|
41
|
-
Function.prototype[symbol] = function () {
|
|
42
|
-
var args = [];
|
|
43
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
44
|
-
args[_i] = arguments[_i];
|
|
45
|
-
}
|
|
46
|
-
try {
|
|
47
|
-
var result = this.apply(this, args);
|
|
48
|
-
return [null, result];
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
return [error, undefined];
|
|
52
|
-
}
|
|
1
|
+
const symbolResult = Symbol("result");
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Extiende el prototipo de `Function` para incluir un método
|
|
5
|
+
* que maneja errores y devuelve una tupla `[error, result]`.
|
|
6
|
+
*
|
|
7
|
+
* @param {...any[]} args - Los argumentos que se pasarán a la función.
|
|
8
|
+
* @returns {[any, any]} Una tupla con `null` y el resultado si la función se ejecuta correctamente,
|
|
9
|
+
* o el error y `undefined` si ocurre una excepción.
|
|
10
|
+
*/
|
|
11
|
+
Function.prototype[symbolResult] = function (...args) {
|
|
12
|
+
try {
|
|
13
|
+
const result = this.apply(this, args);
|
|
14
|
+
return [null, result];
|
|
15
|
+
} catch (error) {
|
|
16
|
+
return [error, undefined];
|
|
17
|
+
}
|
|
53
18
|
};
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
});
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Extiende el prototipo de `Promise` para incluir un método
|
|
22
|
+
* que maneja errores de promesas y devuelve una tupla `[error, result]`.
|
|
23
|
+
*
|
|
24
|
+
* @returns {Promise<[any, any]>} Una promesa que se resuelve con una tupla que contiene `null` y el resultado
|
|
25
|
+
* si la promesa se resuelve correctamente, o el error y `undefined` si es rechazada.
|
|
26
|
+
*/
|
|
27
|
+
Promise.prototype[symbolResult] = async function () {
|
|
28
|
+
try {
|
|
29
|
+
const result = await this;
|
|
30
|
+
return [null, result];
|
|
31
|
+
} catch (error) {
|
|
32
|
+
return [error, undefined];
|
|
33
|
+
}
|
|
72
34
|
};
|
|
35
|
+
|
|
73
36
|
/**
|
|
74
|
-
*
|
|
37
|
+
* Realiza una solicitud HTTP utilizando `fetch` y maneja tanto los errores de red como los de parseo JSON.
|
|
75
38
|
*
|
|
76
|
-
* @
|
|
77
|
-
* @param {
|
|
78
|
-
* @
|
|
79
|
-
*
|
|
39
|
+
* @param {string} url - La URL a la que se hará la solicitud.
|
|
40
|
+
* @param {RequestInit} [options={}] - Opciones opcionales para configurar la solicitud `fetch`.
|
|
41
|
+
* @returns {Promise<[any, any]>} Una promesa que se resuelve con una tupla que contiene el error y `undefined` si ocurre un error,
|
|
42
|
+
* o `null` y los datos parseados si la solicitud y el parseo del JSON son exitosos.
|
|
80
43
|
*/
|
|
81
|
-
function useFetchError(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
_b = _c.sent(), parseError = _b[0], data = _b[1];
|
|
96
|
-
if (parseError) {
|
|
97
|
-
return [2 /*return*/, [parseError, undefined]]; // Retorna el error al parsear el JSON
|
|
98
|
-
}
|
|
99
|
-
return [2 /*return*/, [null, data]]; // Retorna el resultado si todo fue bien
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
});
|
|
44
|
+
async function useFetchError(url, options = {}) {
|
|
45
|
+
const [error, response] = await fetch(url, options)[symbolResult]();
|
|
46
|
+
|
|
47
|
+
if (error) {
|
|
48
|
+
return [error, undefined]; // Retorna el error si ocurrió
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const [parseError, data] = await response.json()[symbolResult]();
|
|
52
|
+
|
|
53
|
+
if (parseError) {
|
|
54
|
+
return [parseError, undefined]; // Retorna el error al parsear el JSON
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return [null, data]; // Retorna el resultado si todo fue bien
|
|
103
58
|
}
|