Last active
December 21, 2023 21:32
-
-
Save alexlatam/472278a6dae36a76f4359f4bcea14500 to your computer and use it in GitHub Desktop.
Consultas a una API de manera concatenada usando fetchData Promises - JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* npm i node-fetch // Instalamos el paquete | |
* Necesario agregar en el archivo package.json la directiva "type": "module" | |
*/ | |
import fetch from "node-fetch"; | |
const API = 'https://api.escuelajs.co/api/v1'; | |
/*--------------------GET---------------------------*/ | |
function fetchData(urlApi){ | |
return fetch(urlApi); | |
}; | |
fetchData (`${API}/products`) | |
.then(response => response.json()) | |
.then(products => { | |
console.log(products); | |
}) | |
.catch(error => console.log(error)) | |
fetchData(`${API}/products`) | |
.then(response => response.json()) | |
.then(products=> { | |
console.log(products); | |
return fetchData (`${API}/products/${products[0].id}`) | |
}) | |
.then(response => response.json()) | |
.then(product => { | |
console.log(product.title) | |
return fetchData (`${API}/categories/${product.category.id}`) | |
}) | |
.then(response => response.json()) | |
.then(category => { | |
console.log(category.name); | |
}) | |
.catch(err => console.log(err)) | |
.finally (()=> console.log('Finally')) | |
/*--------------------POST---------------------------*/ | |
const API = 'https://api.escuelajs.co/api/v1'; | |
function postData(urlApi, data) { | |
//ya no se solicita informarción si no se guardará información | |
const response = fetch(urlApi, { | |
method: 'POST', //tiene que ir en mayúscula | |
mode: 'cors', //cors es el permiso que va a tener, por defecto va estar siempre en cors | |
credentials: 'same-origin', //es opcional | |
headers:{ | |
'Content-Type': 'application/json' //necesario indicar que lo que se está enviando es de tipo json | |
}, | |
body: JSON.stringify(data) //el método JSON.stringify() convierte un objeto o valor de JavaScript en una cadena de texto JSON | |
}); | |
return response; | |
} | |
//En https://fakeapi.platzi.com/doc/products se consigue la estructura de como debe ser el objeto que se quiere crear con POST | |
const data = { | |
"title": "Nunca pares de aprender", | |
"price": 2, | |
"description": "A description", | |
"categoryId": 1, | |
"images": ["https://placeimg.com/640/480/any"] | |
} | |
//podemos usar el postData como una promesa y con .then obtener la respuesta como un objeto json y mostrarlo después en la consola | |
postData(`${API}/products`, data) | |
.then(response => response.json()) | |
.then(data => console.log(data)); | |
/*--------------------PUT---------------------------*/ | |
//Con PUT para actualizar un objeto | |
function putData(urlApi, dataUpdate) { | |
const response = fetch(urlApi, { | |
method: 'PUT', | |
mode: 'cors', | |
credentials: 'same-origin', | |
headers:{ | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify(dataUpdate) | |
}); | |
return response; | |
} | |
const dataUpdate = { | |
"title": "Se puede cambiar tambien otras caracteristicas", | |
"price": 10 // no es necesario colocar todas las características del objeto, solo las que se cambiarán | |
} | |
putData(`${API}/products/271`, dataUpdate) //se debe colocar el id del objeto que se quiere modificar | |
.then(response => response.json()) | |
.then(dataUpdate => console.log(dataUpdate)); | |
/*--------------------DELETE---------------------------*/ | |
//Eliminar un objeto indicando el id con DELETE | |
function deleteData(urlApi) { //no es necesario pasar la data | |
const response = fetch(urlApi, { | |
method: 'DELETE', | |
mode: 'cors', | |
credentials: 'same-origin', | |
headers:{ | |
'Content-Type': 'application/json' | |
} //no es necesario especificar el body | |
}); | |
return response; | |
} | |
const idNumber = 271; //se debe colocar el id del objeto qu se quiere modificar | |
deleteData(`${API}/products/${idNumber}`) //no es necesario pasar data | |
.then(() => { | |
console.log(`Borrado ${idNumber}`); //es opcional imprimir en consola | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment