mirror of
https://github.com/wassname/talk.git
synced 2026-07-07 19:26:29 +08:00
31 lines
646 B
JavaScript
31 lines
646 B
JavaScript
export const base = '/api/v1';
|
|
|
|
export const getInit = (method, body) => {
|
|
let init = {
|
|
method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json'
|
|
},
|
|
credentials: 'same-origin'
|
|
};
|
|
|
|
if (method.toLowerCase() !== 'get') {
|
|
init.body = JSON.stringify(body);
|
|
}
|
|
|
|
return init;
|
|
};
|
|
|
|
export const handleResp = res => {
|
|
if (res.status === 401) {
|
|
throw new Error('Not Authorized to make this request');
|
|
} else if (res.status > 399) {
|
|
throw new Error('Error! Status ', res.status);
|
|
} else if (res.status === 204) {
|
|
return res.text();
|
|
} else {
|
|
return res.json();
|
|
}
|
|
};
|