1+ const { response } = require ( "express" )
2+
3+ const url = "http://localhost:5500/api"
4+
5+ function gerUsers ( ) {
6+ fetch ( url )
7+ . then ( response => response . json ( ) )
8+ . then ( data => renderApiResult . textContent = JSON . stringify ( data ) )
9+ . catch ( error => console . error ( error ) )
10+ }
11+
12+ function getUser ( user_id ) {
13+ fetch ( `${ url } /${ user_id } ` )
14+ . then ( response => response . json ( ) )
15+ . then ( data => {
16+ userName . textContent = data . name
17+ userCity . textContent = data . city
18+ userAvatar . src = data . avatar
19+ } )
20+ . catch ( error => console . log ( error ) )
21+ }
22+
23+ function addUser ( newUser ) {
24+ fetch ( url , {
25+ method : "POST" ,
26+ body : JSON . stringify ( newUser ) ,
27+ headers : {
28+ "Content-type" : "application/json; charset=UTF-8"
29+ }
30+ } )
31+ . then ( response => response . json ( ) )
32+ . then ( data => alertAPI . textContent = data )
33+ . catch ( error => console . error ( error ) )
34+ }
35+
36+ function updateUser ( updatedUser , id ) {
37+ fetch ( `${ url } /${ id } ` , {
38+ method : "PUT" ,
39+ body : JSON . stringify ( updateUser ) ,
40+ headers : {
41+ "Content-type" : "application/json;charset=UTF-8"
42+ }
43+ } )
44+ . then ( response => response . json ( ) )
45+ . then ( data => alertAPI . textContent = data )
46+ . catch ( error => console . error ( error ) )
47+ }
48+
49+ function deleteUser ( id ) {
50+ fetch ( `${ url } /${ id } ` , {
51+ method :"DELETE" ,
52+ headers : {
53+ "Content-type:" :"application/json;charset=UTF-8"
54+ }
55+ } )
56+ . then ( response => response . json ( ) )
57+ . then ( data => alertAPI . textContent = data )
58+ . catch ( error => console . error ( error ) )
59+ }
60+
61+ const updatedUser = {
62+ name :"Marcelo" ,
63+ avatar :"https://picsum.photos/200/300" ,
64+ city : "Recife"
65+ }
66+
67+ const newUser = {
68+ name : "Olivia" ,
69+ avatar : "http://lorempixel.com/400/200" ,
70+ city : "Rio do Sul"
71+ }
72+
73+ //addUser(newUser)
74+
75+ deleteUser ( )
76+ updateUser ( updateUser , 1 )
77+ getUsers ( )
78+ getUser ( 1 )
0 commit comments