mirror of
https://github.com/wassname/talk.git
synced 2026-07-02 07:56:39 +08:00
37 lines
863 B
JavaScript
37 lines
863 B
JavaScript
export const base = '/api/v1';
|
|
|
|
const buildOptions = (inputOptions = {}) => {
|
|
|
|
const defaultOptions = {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json'
|
|
},
|
|
credentials: 'same-origin'
|
|
};
|
|
const options = Object.assign({}, defaultOptions, inputOptions);
|
|
|
|
if (options.method.toLowerCase() !== 'get') {
|
|
options.body = JSON.stringify(options.body);
|
|
}
|
|
|
|
return options;
|
|
};
|
|
|
|
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();
|
|
}
|
|
};
|
|
|
|
export default (url, options) => {
|
|
return fetch(`${base}${url}`, buildOptions(options)).then(handleResp);
|
|
};
|