Skip to content

Instantly share code, notes, and snippets.

@ottino
Created August 9, 2020 23:16
Show Gist options
  • Save ottino/d7367e24e7ee0a5e7c04dff934e65b21 to your computer and use it in GitHub Desktop.
Save ottino/d7367e24e7ee0a5e7c04dff934e65b21 to your computer and use it in GitHub Desktop.
js: funciones CRUD
const urlCRUD = 'https://reqres.in/api/users';
const getUsuario = async ( id )=> {
const resp = await fetch(`${ urlCRUD }/${ id }`) ;
const { data } = await resp.json();
return data;
};
const crearUsuario = async ( usuario ) => {
const resp = await fetch( urlCRUD , {
method: 'POST',
body: JSON.stringify( usuario ),
headers: {
'Content-Type':'application/json'
}
});
return await resp.json();
}
const actualizarUsuario = async ( id , usuario ) => {
const resp = await fetch( `${ urlCRUD }/${ id }` , {
method: 'PUT',
body: JSON.stringify( usuario ),
headers: {
'Content-Type':'application/json'
}
});
return await resp.json();
}
const eliminarUsuario = async ( id ) => {
const resp = await fetch(`${ urlCRUD }/${ id }` , {
method:'DELETE'
});
return ( resp.ok ) ? `id ${ id } eliminado` : 'No se pudo eliminar el usuario';
};
export {
getUsuario,
crearUsuario,
actualizarUsuario,
eliminarUsuario
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment