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.
Files changed (3) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +17 -17
  3. 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.1",
4
- "description": "A library for error handling with [error, result] tuples using a useFetch() function.",
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
- * Extiende el prototipo de `Function` para incluir un método
5
- * que maneja errores y devuelve una tupla `[error, result]`.
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 - 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.
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
- * Extiende el prototipo de `Promise` para incluir un método
22
- * que maneja errores de promesas y devuelve una tupla `[error, result]`.
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]>} 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.
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
- * Realiza una solicitud HTTP utilizando `fetch` y maneja tanto los errores de red como los de parseo JSON.
37
+ * Makes an HTTP request using `fetch` and handles both network errors and JSON parsing errors.
38
38
  *
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.
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]; // Retorna el error si ocurrió
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]; // Retorna el error al parsear el JSON
54
+ return [parseError, undefined]; // Returns the error when parsing JSON
55
55
  }
56
56
 
57
- return [null, data]; // Retorna el resultado si todo fue bien
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
- }