Created
March 13, 2026 16:51
-
-
Save santiagosilas/dd71e4869aa5c0213616e93d77f8f451 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const url = "http://localhost:3000/usuarios"; | |
| const apiListarUsuarios = async () => { | |
| try { | |
| const response = await fetch(url); | |
| if (!response.ok) | |
| // response.status | |
| throw new Error(`Erro ao obter dados da api: ${response.status}`); | |
| const jsonData = await response.json(); | |
| console.log("GET", response.status); // 200 no Json-Server | |
| return jsonData; | |
| } catch (error) { | |
| console.log("Falha ao obter usuários", error); | |
| throw error; | |
| } | |
| }; | |
| const apiObterUsuario = async (id) => { | |
| try { | |
| const response = await fetch(`${url}/${id}`); | |
| if (!response.ok) | |
| // response.status | |
| throw new Error(`Erro ao obter dados da api: ${response.status}`); | |
| const jsonData = await response.json(); | |
| console.log("GET", response.status); // 200 no Json-Server | |
| return jsonData; | |
| } catch (error) { | |
| console.log("Falha ao obter usuários", error); | |
| throw error; | |
| } | |
| }; | |
| const apiAddUsuario = async (usuario) => { | |
| try { | |
| const response = await fetch(`${url}`, { | |
| method: "POST", | |
| headers: {"Content-Type": "application/json"}, | |
| body: JSON.stringify(usuario), | |
| }); | |
| if (!response.ok) | |
| throw new Error(`Erro ao adicionar usuário: ${response.status}`); | |
| const jsonData = response.json(); | |
| console.log("POST", response.status); // 201 no Json-Server | |
| return jsonData; | |
| } catch (error) { | |
| console.log("Falha ao adicionar usuário", error); | |
| throw error; | |
| } | |
| }; | |
| const apiAtualizarUsuario = async (id, usuario) => { | |
| try { | |
| const response = await fetch(`${url}/${id}`, { | |
| method: "PUT", | |
| headers: {"Content-Type": "application/json"}, | |
| body: JSON.stringify(usuario), | |
| }); | |
| if (!response.ok) | |
| throw new Error(`Erro ao atualizar usuário: ${response.status}`); | |
| const jsonData = response.json(); | |
| console.log("PUT", response.status); // 200 no Json-Server | |
| return jsonData; | |
| } catch (error) { | |
| console.log("Falha ao atualizar usuário", error); | |
| throw error; | |
| } | |
| }; | |
| const apiRemover = async (id) => { | |
| try { | |
| const response = await fetch(`${url}/${id}`, { | |
| method: "DELETE", | |
| }); | |
| if (!response.ok) | |
| throw new Error(`Erro ao remover usuário: ${response.status}`); | |
| const jsonData = response.json(); | |
| console.log("DELETE", response.status); // 200 no Json-Server | |
| return jsonData; | |
| } catch (error) { | |
| console.log("Falha ao remover usuário", error); | |
| throw error; | |
| } | |
| }; | |
| (async () => { | |
| // CREATE | |
| const resAdd = await apiAddUsuario({ | |
| nome: "Roberto", | |
| email: "roberto@gmail.com", | |
| }); | |
| // UPDATE | |
| const resUpdate = await apiAtualizarUsuario("1", { | |
| nome: "Hugo", | |
| email: "hugo@gmail.com", | |
| }); | |
| // DELETE | |
| const resAdd2 = await apiAddUsuario({nome: "Yu", email: "Yu@gmail.com"}); | |
| const resDel = await apiRemover(resAdd2.id); | |
| // READ | |
| const data = await apiListarUsuarios(); | |
| console.log(data); | |
| // Response Status | |
| console.log("CREATE"); | |
| console.log(resAdd); | |
| console.log("PUT"); | |
| console.log(resUpdate); | |
| console.log("DELETE"); | |
| console.log(resDel); | |
| })(); | |
| /* | |
| EXEMPLOS DE RETORNOS NO JSON-SERVER | |
| CREATE | |
| { id: '84be', nome: 'Roberto', email: 'roberto@gmail.com' } | |
| PUT | |
| { nome: 'Hugo', email: 'hugo@gmail.com', id: '1' } | |
| DELETE | |
| { id: '9f3a', nome: 'Roberto', email: 'roberto@gmail.com' } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment