Advertisement
WIlliams72

Untitled

Jun 16th, 2022
1,320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //customAPI.js
  2.  
  3. class EasyHTTP {
  4.   get(url){
  5.     return new Promise((resolve, reject)=> {
  6.       fetch(url, {
  7.         method: 'GET',
  8.       })
  9.     .then(res => res.json())
  10.     .then(data => resolve(data))
  11.     .catch(err => reject(err))
  12.     })
  13.   }
  14.   //post request
  15.   post(url, data) {
  16.    return new Promise((resolve, reject)=> {
  17.      fetch(url, {
  18.        method: 'POST',
  19.        headers:{
  20.          'content-type': 'application/json'
  21.        },
  22.        body: JSON.stringify(data)
  23.      })
  24.    })
  25.   }
  26.  
  27. }
  28.  
  29. //app.js
  30.  
  31. const http = new EasyHTTP;
  32. const url = '';
  33.  
  34. /*get users*/
  35.  
  36. http.get(url)
  37. .then(data => console.log(data))
  38. .catch(err => console.log('Something went wrong', err))
  39.  
  40.  
  41. const data = {
  42.   name: 'Jimmy Black',
  43.   username: 'Jeemy',
  44.   email: 'Jimmy@mail.com'
  45. }
  46.  
  47.  
  48. //send data
  49. http.post('https://jsonplaceholder.typicode.com/users', data)
  50. .then(data => console.log(data))
  51. .catch(err => console.log('Something went wrong', err))
  52. console.log('Easyhttp')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement