use-fetch-error 1.0.1 → 1.0.3
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/package.json +2 -2
- package/src/index.js +17 -17
- package/index.js +0 -35
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-fetch-error",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "A library for error handling with [error, result] tuples using a
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "A library for error handling with [error, result] tuples using a useFetchError() function.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"javascript",
|
|
7
7
|
"fetch",
|
package/src/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
const symbolResult = Symbol("result");
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Extends the `Function` prototype to include a method
|
|
5
|
+
* that handles errors and returns a tuple `[error, result]`.
|
|
6
6
|
*
|
|
7
|
-
* @param {...any[]} args -
|
|
8
|
-
* @returns {[any, any]}
|
|
9
|
-
*
|
|
7
|
+
* @param {...any[]} args - The arguments to be passed to the function.
|
|
8
|
+
* @returns {[any, any]} A tuple with `null` and the result if the function executes successfully,
|
|
9
|
+
* or the error and `undefined` if an exception occurs.
|
|
10
10
|
*/
|
|
11
11
|
Function.prototype[symbolResult] = function (...args) {
|
|
12
12
|
try {
|
|
@@ -18,11 +18,11 @@ Function.prototype[symbolResult] = function (...args) {
|
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
21
|
+
* Extends the `Promise` prototype to include a method
|
|
22
|
+
* that handles promise errors and returns a tuple `[error, result]`.
|
|
23
23
|
*
|
|
24
|
-
* @returns {Promise<[any, any]>}
|
|
25
|
-
*
|
|
24
|
+
* @returns {Promise<[any, any]>} A promise that resolves with a tuple containing `null` and the result
|
|
25
|
+
* if the promise resolves successfully, or the error and `undefined` if it is rejected.
|
|
26
26
|
*/
|
|
27
27
|
Promise.prototype[symbolResult] = async function () {
|
|
28
28
|
try {
|
|
@@ -34,25 +34,25 @@ Promise.prototype[symbolResult] = async function () {
|
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
37
|
+
* Makes an HTTP request using `fetch` and handles both network errors and JSON parsing errors.
|
|
38
38
|
*
|
|
39
|
-
* @param {string} url -
|
|
40
|
-
* @param {RequestInit} [options={}] -
|
|
41
|
-
* @returns {Promise<[any, any]>}
|
|
42
|
-
*
|
|
39
|
+
* @param {string} url - The URL to which the request will be made.
|
|
40
|
+
* @param {RequestInit} [options={}] - Optional options to configure the `fetch` request.
|
|
41
|
+
* @returns {Promise<[any, any]>} A promise that resolves with a tuple containing the error and `undefined` if an error occurs,
|
|
42
|
+
* or `null` and the parsed data if the request and JSON parsing are successful.
|
|
43
43
|
*/
|
|
44
44
|
async function useFetchError(url, options = {}) {
|
|
45
45
|
const [error, response] = await fetch(url, options)[symbolResult]();
|
|
46
46
|
|
|
47
47
|
if (error) {
|
|
48
|
-
return [error, undefined]; //
|
|
48
|
+
return [error, undefined]; // Returns the error if one occurred
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
const [parseError, data] = await response.json()[symbolResult]();
|
|
52
52
|
|
|
53
53
|
if (parseError) {
|
|
54
|
-
return [parseError, undefined]; //
|
|
54
|
+
return [parseError, undefined]; // Returns the error when parsing JSON
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
return [null, data]; //
|
|
57
|
+
return [null, data]; // Returns the result if everything went well
|
|
58
58
|
}
|
package/index.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
const symbolResult = Symbol("result");
|
|
2
|
-
|
|
3
|
-
Function.prototype[symbolResult] = function (...args) {
|
|
4
|
-
try {
|
|
5
|
-
const result = this.apply(this, args);
|
|
6
|
-
return [null, result];
|
|
7
|
-
} catch (error) {
|
|
8
|
-
return [error, undefined];
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
Promise.prototype[symbolResult] = async function () {
|
|
13
|
-
try {
|
|
14
|
-
const result = await this;
|
|
15
|
-
return [null, result];
|
|
16
|
-
} catch (error) {
|
|
17
|
-
return [error, undefined];
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
async function useFetch(url, options = {}) {
|
|
22
|
-
const [error, response] = await fetch(url, options)[symbolResult]();
|
|
23
|
-
|
|
24
|
-
if (error) {
|
|
25
|
-
return [error, undefined]; // Retorna el error si ocurrió
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const [parseError, data] = await response.json()[symbolResult]();
|
|
29
|
-
|
|
30
|
-
if (parseError) {
|
|
31
|
-
return [parseError, undefined]; // Retorna el error al parsear el JSON
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return [null, data]; // Retorna el resultado si todo fue bien
|
|
35
|
-
}
|