API.js 897 B

1
  1. import React from'react';import Module from'../Module';function checkIt(a){if(2===parseInt(a.status/100,10))return a;throw a}function catchIt(a){return 5===parseInt(a.status/500,10)?a:a}class APIService{constructor(a){this.baseURL=a}get(a){return a=this.baseURL+a,window.fetch(a,{method:'GET'}).then(checkIt).then(a=>a.json()).catch(catchIt)}post(a,b){let c=this.baseURL+a;return window.fetch(c,{method:'POST',headers:{"Content-Type":'application/json',Token:''},body:JSON.stringify(b)}).then(checkIt).then(a=>a.json()).catch(catchIt)}}class API extends Module{constructor(a){super(a),this.baseURL.onUpdate(a=>this.setAPI(a)),this.setAPI(this.baseURL.value)}setAPI(a){let b=new APIService(a);this.api.set(b)}render(){return super.render()}}API.Inputs={baseURL:{type:Module.Types.Text,required:!0,comment:'API URL',defaultValue:'/'}},API.Outputs={api:{}},API.Category='Services';export default API;