react-http-client
Version:
A httpClient for react frontend app using which you can make api calls
30 lines (26 loc) • 609 B
JavaScript
const axios = require('axios');
const createHandler = (method) => {
return async function (url, body, headers = {}) {
const config = {
method,
url,
data: body,
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/json',
...headers,
},
};
try {
const response = await axios(config);
return response.data;
} catch (error) {
throw new Error(JSON.stringify(error.response));
}
};
};
const httpHandler = {
get: createHandler('get'),
post: createHandler('post'),
};
module.exports = httpHandler;