Skip to content
Snippets Groups Projects
Verified Commit 69722ac0 authored by Defendi Alberto's avatar Defendi Alberto
Browse files

Set up axios middleware.

Will camelize/decamelize each request. Default axios will be replaced
with this.
parent 3803a38e
No related branches found
No related tags found
2 merge requests!69Possibility to insert a reservation and new docs.,!62Set up axios middleware.
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { camelizeKeys, decamelizeKeys } from 'humps';
const api = axios.create();
// Axios middleware to convert all api responses to camelCase
api.interceptors.response.use((response: AxiosResponse) => {
if (
response.data &&
response.headers['content-type'] === 'application/json'
) {
response.data = camelizeKeys(response.data);
}
return response;
});
// Axios middleware to convert all api requests to snake_case
api.interceptors.request.use((config: AxiosRequestConfig) => {
const newConfig = { ...config };
newConfig.url = `api/${config.url}`;
if (newConfig.headers['Content-Type'] === 'multipart/form-data')
return newConfig;
if (config.params) {
newConfig.params = decamelizeKeys(config.params);
}
if (config.data) {
newConfig.data = decamelizeKeys(config.data);
}
return newConfig;
});
export default api;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment