Advertisement
megalaren

Api.js

Jun 3rd, 2021
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. class Api {
  3.     constructor(apiUrl) {
  4.         this.apiUrl =  apiUrl;
  5.     }
  6.   getPurchases () {
  7.     return fetch(`/purchases`, {
  8.       headers: {
  9.         'Content-Type': 'application/json'
  10.       }
  11.     })
  12.       .then( e => {
  13.           if(e.ok) {
  14.               return e.json()
  15.           }
  16.           return Promise.reject(e.statusText)
  17.       })
  18.   }
  19.   addPurchases (id) {
  20.     return fetch(`/purchases`, {
  21.       method: 'POST',
  22.       headers: {
  23.         'Content-Type': 'application/json'
  24.       },
  25.       body: JSON.stringify({
  26.         id: id
  27.       })
  28.     })
  29.       .then( e => {
  30.           if(e.ok) {
  31.               return e.json()
  32.           }
  33.           return Promise.reject(e.statusText)
  34.       })
  35.   }
  36.   removePurchases (id){
  37.     return fetch(`/purchases/${id}`, {
  38.       method: 'DELETE',
  39.       headers: {
  40.         'Content-Type': 'application/json'
  41.       }
  42.     })
  43.       .then( e => {
  44.           if(e.ok) {
  45.               return e.json()
  46.           }
  47.           return Promise.reject(e.statusText)
  48.       })
  49.   }
  50.   addSubscriptions(id) {
  51.     return fetch(`/subscriptions`, {
  52.       method: 'POST',
  53.       headers: {
  54.         'Content-Type': 'application/json'
  55.       },
  56.       body: JSON.stringify({
  57.         id: id
  58.       })
  59.     })
  60.       .then( e => {
  61.           if(e.ok) {
  62.               return e.json()
  63.           }
  64.           return Promise.reject(e.statusText)
  65.       })
  66.   }
  67.   removeSubscriptions (id) {
  68.     return fetch(`/subscriptions/${id}`, {
  69.       method: 'DELETE',
  70.       headers: {
  71.         'Content-Type': 'application/json'
  72.       }
  73.     })
  74.       .then( e => {
  75.           if(e.ok) {
  76.               return e.json()
  77.           }
  78.           return Promise.reject(e.statusText)
  79.       })
  80.   }
  81.   addFavorites (id)  {
  82.     return fetch(`/favorites`, {
  83.       method: 'POST',
  84.       headers: {
  85.         'Content-Type': 'application/json'
  86.       },
  87.       body: JSON.stringify({
  88.         id: id
  89.       })
  90.     })
  91.         .then( e => {
  92.             if(e.ok) {
  93.                 return e.json()
  94.             }
  95.             return Promise.reject(e.statusText)
  96.         })
  97.   }
  98.   removeFavorites (id) {
  99.     return fetch(`/favorites/${id}`, {
  100.       method: 'DELETE',
  101.       headers: {
  102.         'Content-Type': 'application/json'
  103.       }
  104.     })
  105.         .then( e => {
  106.             if(e.ok) {
  107.                 return e.json()
  108.             }
  109.             return Promise.reject(e.statusText)
  110.         })
  111.   }
  112.     getIngredients  (text)  {
  113.         return fetch(`/ingredients?query=${text}`, {
  114.             headers: {
  115.                 'Content-Type': 'application/json'
  116.             }
  117.         })
  118.             .then( e => {
  119.                 if(e.ok) {
  120.                     return e.json()
  121.                 }
  122.                 return Promise.reject(e.statusText)
  123.             })
  124.     }
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement