Requests can be made by passing the relevant config to axios.
Add API_URL to the environment variable in the .env file.
API_URL = MY_API_URL
These are the basic configuration options for making requests.
const instance = axios.create({
baseURL: Config.API_URL,
timeout: AXIOS_TIMEOUT,
withCredentials: false,
responseType: 'json',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8',
},
data: {},
})
Important: If axios is used with multiple domains, the AUTH_TOKEN will be sent to all of them.
setToken(token, TokenType.Bearer)
Add refreshToken api to services/api/api
export const AUTH_API = {
// ADD ENDPOINT REFRESH TOKEN HERE
refreshToken: 'api/refreshToken',
}
getRequest(API_URL, config)
postRequest(API_URL, body, config)
putRequest(API_URL, body, config)
deleteRequest(API_URL, config)
You can intercept requests or responses before they are handled by then or catch.
instance.interceptors.request.use(
config => {
// Do something before request is sent
return config
},
error => Promise.reject(error),
)
instance.interceptors.response.use(
response => {
// Do something with response data
return response
},
error => Promise.reject(error),
)
We downgrade axios to version 0.27.2 to resolve this issue: react/react-native#34868