1 |
- import React from'react';function checkIt(a){if(2===parseInt(a.status/100,10))return a;throw a}function catchIt(a){if(5===parseInt(a.status/100,10)||4===parseInt(a.status/100,10))throw a;else return a}class API{constructor(){this.baseURL=''}setUrl(a){this.baseURL=a}get(a,b){return a=this.baseURL+a,window.fetch(a,{method:'GET',headers:{token:b}}).then(checkIt).then(a=>a.json()).catch(catchIt)}post(a,b,c){let d=this.baseURL+a;return window.fetch(d,{method:'POST',headers:{"Content-Type":'application/json',token:c},body:JSON.stringify(b)}).then(checkIt).then(a=>a.json()).catch(catchIt)}}export default new API;
|