Files
cpost-mobile/src/api/axios.ts
Samandar Turgunboyev ef73715048 update
2025-08-27 15:37:37 +05:00

41 lines
1005 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import AsyncStorage from '@react-native-async-storage/async-storage';
import axios, { AxiosError } from 'axios';
import { navigate } from 'components/NavigationRef';
const axiosInstance = axios.create({
baseURL: 'http://141.105.64.233:7723/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}`;
}
// Languageni 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;