Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import serverConfig from "./ServerConfig";
  2. import APIHelpers from "./APIHelpers";
  3.  
  4. export default class API {
  5.  
  6.     constructor() {
  7.  
  8.     }
  9.  
  10.     static Authenticate() {
  11.         let token = 'Basic ';
  12.         return (
  13.             this.get()(token, '/session').then((body) => {
  14.                 return (body);
  15.             })
  16.         );
  17.     }
  18.  
  19.     static post(url, data, authorization = '', auth = false): Response {
  20.  
  21.         let request = {
  22.             method: 'POST',
  23.             headers: APIHelpers.defineHeaderWithContent(authorization, auth),
  24.             body: JSON.stringify(data)
  25.         };
  26.  
  27.         return (
  28.             fetch(serverConfig.host + url, request).then((response) => {
  29.                 return (response);
  30.             })
  31.         );
  32.     }
  33.  
  34.     static put(url, data, authorization = '', auth = false): Response {
  35.         let request = {
  36.             method: 'PUT',
  37.             headers: APIHelpers.defineHeaderWithContent(authorization, auth),
  38.             body: JSON.stringify(data)
  39.         };
  40.  
  41.         return (
  42.             fetch(serverConfig.host + url, request).then((response) => {
  43.                 return (response);
  44.             })
  45.         );
  46.     }
  47.  
  48.     static delete(url, authorization = '', auth = false): Response {
  49.  
  50.         let request = {
  51.             method: 'DELETE',
  52.             headers: APIHelpers.defineHeaderWithoutContent(authorization, auth),
  53.         };
  54.  
  55.         return (
  56.             fetch(serverConfig.host + url, request)
  57.         );
  58.     }
  59.  
  60.     static get(url, authorization = '', auth = false): Response {
  61.         let request = {
  62.             method: 'GET',
  63.             headers: APIHelpers.defineHeaderWithoutContent(authorization, auth),
  64.         };
  65.  
  66.         return (
  67.             fetch(serverConfig.host + url, request).then((response) => {
  68.                 return (response);
  69.             })
  70.         );
  71.     }
  72.  
  73.     static patch(url, data, authorization = '', auth = undefined): Response {
  74.         let request = {
  75.             method: 'PATCH',
  76.             headers: APIHelpers.defineHeaderWithContent(authorization, auth),
  77.             body: JSON.stringify(data)
  78.         };
  79.         return (
  80.             fetch(serverConfig.host + url, request).then((response) => {
  81.                 return (response);
  82.             })
  83.         );
  84.     }
  85.  
  86.     static asyncGet(url, callback, authorization = '', auth = false): void {
  87.         const request = async () => {
  88.             let request = {
  89.                 method: 'GET',
  90.                 headers: await APIHelpers.defineHeaderWithoutContent(authorization, auth),
  91.             };
  92.  
  93.             const response = await fetch(serverConfig.host + url, request);
  94.             if (await response.ok) {
  95.                 const json = await response.json();
  96.                 await callback(json);
  97.             }
  98.             else
  99.                 console.log(response);
  100.         };
  101.         request();
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement