leduong http service
import { API_ENDPOINT } from '../constants';
import { loadState } from './localStorage';
const TOKEN = loadState('token') || '';
const httpConfig = {
method: `GET`,
headers: {
Authorization: TOKEN,
'Content-Type': 'application/json'
}
};
class ApiService {
fetch(url, httpRequest) {
const pattern = /^((http|https):\/\/)/;
if (!pattern.test(url)) {
url = `${API_ENDPOINT}/${url}`;
}
return fetch(url, httpRequest)
.then(resp => resp.json())
.catch(function(error) {
console.log(error);
});
}
get(path) {
return this.fetch(path, {
...httpConfig,
method: 'GET'
});
}
post(path, params) {
return this.fetch(path, {
...httpConfig,
body: JSON.stringify(params),
method: 'POST'
});
}
put(path, params = {}) {
return this.fetch(path, {
...httpConfig,
body: JSON.stringify(params),
method: 'PUT'
});
}
patch(path, params = {}) {
return this.fetch(path, {
...httpConfig,
body: JSON.stringify(params),
method: 'PATCH'
});
}
delete(path) {
return this.fetch(path, {
...httpConfig,
method: 'DELETE'
});
}
}
export default new ApiService();