import Cookie from 'js-cookie';
const queryString = (data)=>{
return Object.entries(data).map(item=>`${item[0]}=${item[1]}`).join('&');
}
const request = (url,method,data)=>{
let params = queryString(data);
url = method === 'GET' ? `${url}${params ? '?' + params : ''}`:url;
let initOptions = {
method,
headers:{
'content-type':'application/x-www-form-urlencoded; charset=utf-8',
'authorization':Cookie.get('token')
},
}
let options = method === 'GET' ? initOptions : {
...initOptions,
body:params
}
return fetch(url,options).then(res=>{
if(res.ok){
return res.json();
}else{
return Promise.reject(res)
}
});
}
export default{
get(url,data={}){
return request(url,'GET',data);
},
post(url,data){
return request(url,'POST',data);
}
}