ebanutsya
This commit is contained in:
		@@ -1,12 +1,15 @@
 | 
			
		||||
import axios, {AxiosHeaders, AxiosRequestConfig, AxiosRequestHeaders, InternalAxiosRequestConfig} from 'axios';
 | 
			
		||||
import * as SecureStore from 'expo-secure-store';
 | 
			
		||||
import {useDispatch} from "react-redux";
 | 
			
		||||
import {logout} from "../features/auth/authSlice";
 | 
			
		||||
import {store} from "../redux/store";
 | 
			
		||||
 | 
			
		||||
const apiClient = axios.create({
 | 
			
		||||
    baseURL: 'http://192.168.1.101:5000',
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
apiClient.interceptors.request.use(async (config) => {
 | 
			
		||||
    const accessToken = await SecureStore.getItemAsync('access_token');
 | 
			
		||||
    const accessToken = await SecureStore.getItemAsync('accessToken');
 | 
			
		||||
    if (!config.headers) {
 | 
			
		||||
        config.headers = new AxiosHeaders();
 | 
			
		||||
    }
 | 
			
		||||
@@ -14,12 +17,22 @@ apiClient.interceptors.request.use(async (config) => {
 | 
			
		||||
        config.headers.set('Authorization', `Bearer ${accessToken}`, true);
 | 
			
		||||
    }
 | 
			
		||||
    config.validateStatus = (status) => {
 | 
			
		||||
        return true
 | 
			
		||||
        if (status == 401) {
 | 
			
		||||
            SecureStore.deleteItemAsync('accessToken');
 | 
			
		||||
            store.dispatch(logout());
 | 
			
		||||
        }
 | 
			
		||||
        return true;
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    return config;
 | 
			
		||||
}, function (error) {
 | 
			
		||||
    return Promise.reject(error);
 | 
			
		||||
    if (error.response && error.response.status === 401) {
 | 
			
		||||
        console.log("очко")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
    // return Promise.reject(error);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
export default apiClient;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										13
									
								
								src/api/barcodeApi.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/api/barcodeApi.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
import {SupplierProduct} from "../types/supplierProduct";
 | 
			
		||||
import apiClient from "./apiClient";
 | 
			
		||||
 | 
			
		||||
const router = '/barcode';
 | 
			
		||||
 | 
			
		||||
const barcodeApi = {
 | 
			
		||||
    searchProducts: async (barcode: string): Promise<SupplierProduct[]> => {
 | 
			
		||||
        let response = await apiClient.get(`${router}/searchProducts?barcode=${barcode}`);
 | 
			
		||||
        return response.data;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default barcodeApi;
 | 
			
		||||
							
								
								
									
										16
									
								
								src/api/ordersApi.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/api/ordersApi.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
import apiClient from "./apiClient";
 | 
			
		||||
import {Order} from "../types/order";
 | 
			
		||||
 | 
			
		||||
const router = '/orders';
 | 
			
		||||
 | 
			
		||||
const ordersApi = {
 | 
			
		||||
    getOrders: async (lastId: number) => {
 | 
			
		||||
 | 
			
		||||
    },
 | 
			
		||||
    getOrdersBySupplierProduct: async (supplierProductId: number): Promise<Order[]> => {
 | 
			
		||||
        let response = await apiClient.get(`${router}/getBySupplierProductId?supplierProductId=${supplierProductId}`);
 | 
			
		||||
        return response.data;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default ordersApi;
 | 
			
		||||
							
								
								
									
										12
									
								
								src/api/userApi.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/api/userApi.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
import apiClient from "./apiClient";
 | 
			
		||||
 | 
			
		||||
const userApi = {
 | 
			
		||||
    login: async (login: string, password: string): Promise<{ accessToken: string, ok: boolean }> => {
 | 
			
		||||
        let response = await apiClient.post('/auth/login', {login, password});
 | 
			
		||||
        return response.data;
 | 
			
		||||
    },
 | 
			
		||||
    test: async () => {
 | 
			
		||||
        await apiClient.post('/auth/protected');
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
export default userApi;
 | 
			
		||||
		Reference in New Issue
	
	Block a user