SHARE
TWEET

Untitled

a guest Jan 18th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Backend {
  2.     //do not modify the constructor
  3.     constructor(){
  4.         this.baseUrl = '';
  5.     }
  6.  
  7.     setBaseUrl(theURL) {
  8.         return this.baseUrl = theURL
  9.     }
  10.     getBaseUrl() {
  11.         return this.baseUrl
  12.     }
  13.     get(theGetQuery) {
  14.         fetch(this.baseUrl + theGetQuery)
  15.         .then(res => res.json())
  16.     }
  17.  
  18.     post(thePostQuery, thePostArg) {
  19.  
  20.     fetch(this.baseUrl + thePostQuery, {
  21.     method: "POST",
  22.     headers: {
  23.         "Content-Type": "application/json"
  24.     },
  25.     body: JSON.stringify(thePostArg)
  26.     })
  27.     }
  28. }
  29.  
  30.  
  31. //sample usage
  32. const API = new Backend();
  33. API.setBaseUrl("https://api.learnjavascript.online/demo");
  34. console.log(API.getBaseUrl());
  35.  
  36. API.get("/notifications/new.json")
  37. .then(data => {
  38.     console.log(data.count);
  39. });
  40.  
  41. API.post("/food.json", {
  42.     food: "Pasta"
  43. })
  44. .then(data => {
  45.     console.log(data);
  46. })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top