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.

Revisions

  1. ottino created this gist Aug 9, 2020.
    59 changes: 59 additions & 0 deletions crud-provider.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@

    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

    }