Initial commit
This commit is contained in:
40
src/api/axios.ts
Normal file
40
src/api/axios.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import axios, { AxiosError } from 'axios';
|
||||
import { navigate } from 'components/NavigationRef';
|
||||
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: 'https://api.cpcargo.uz/api/v1',
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
axiosInstance.interceptors.request.use(async config => {
|
||||
// Tokenni olish
|
||||
const token = await AsyncStorage.getItem('token');
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
// Language’ni olish
|
||||
const language = await AsyncStorage.getItem('language');
|
||||
if (language) {
|
||||
config.headers['Accept-Language'] = language;
|
||||
}
|
||||
|
||||
return config;
|
||||
});
|
||||
|
||||
axiosInstance.interceptors.response.use(
|
||||
response => response,
|
||||
async (error: AxiosError) => {
|
||||
if (error.response?.status === 401) {
|
||||
await AsyncStorage.removeItem('token');
|
||||
navigate('Login');
|
||||
}
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
|
||||
export default axiosInstance;
|
||||
Reference in New Issue
Block a user