feat: add API client and services for medicines, pharmacies, auth

This commit is contained in:
Antoni Nuñez Romeu
2026-07-06 11:00:44 +02:00
parent 259bdb1479
commit a5064f2172
5 changed files with 132 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
import axios from 'axios';
import * as SecureStore from 'expo-secure-store';
import { config } from '../constants/config';
export const api = axios.create({
baseURL: config.API_BASE_URL,
timeout: 10000,
headers: {
'Content-Type': 'application/json',
},
});
// Request interceptor - add auth token
api.interceptors.request.use(
async (axiosConfig) => {
const token = await SecureStore.getItemAsync('auth_token');
if (token) {
axiosConfig.headers.Authorization = `Bearer ${token}`;
}
return axiosConfig;
},
(error) => Promise.reject(error)
);
// Response interceptor - handle errors
api.interceptors.response.use(
(response) => response,
async (error) => {
if (error.response?.status === 401) {
await SecureStore.deleteItemAsync('auth_token');
}
return Promise.reject(error);
}
);
export default api;
+43
View File
@@ -0,0 +1,43 @@
import api from './api';
import * as SecureStore from 'expo-secure-store';
import { AuthResponse, User } from '../types';
export async function login(username: string, password: string): Promise<AuthResponse> {
const response = await api.post('/auth/login', { username, password });
const { user, token } = response.data;
await SecureStore.setItemAsync('auth_token', token);
await SecureStore.setItemAsync('user', JSON.stringify(user));
return response.data;
}
export async function logout(): Promise<void> {
await api.post('/auth/logout');
await SecureStore.deleteItemAsync('auth_token');
await SecureStore.deleteItemAsync('user');
}
export async function checkAuth(): Promise<User | null> {
try {
const response = await api.get('/auth/check');
return response.data.user;
} catch {
return null;
}
}
export async function getStoredUser(): Promise<User | null> {
const userStr = await SecureStore.getItemAsync('user');
return userStr ? JSON.parse(userStr) : null;
}
export async function register(username: string, password: string): Promise<AuthResponse> {
const response = await api.post('/auth/register', { username, password });
const { user, token } = response.data;
await SecureStore.setItemAsync('auth_token', token);
await SecureStore.setItemAsync('user', JSON.stringify(user));
return response.data;
}
+17
View File
@@ -0,0 +1,17 @@
import api from './api';
import { Medicine, PharmacyMedicine } from '../types';
export async function searchMedicines(query: string): Promise<Medicine[]> {
const response = await api.get(`/medicines/search?q=${encodeURIComponent(query)}`);
return response.data;
}
export async function getMedicine(nregistro: string): Promise<Medicine> {
const response = await api.get(`/medicines/${nregistro}`);
return response.data;
}
export async function getMedicinePharmacies(nregistro: string): Promise<PharmacyMedicine[]> {
const response = await api.get(`/medicines/${nregistro}/pharmacies`);
return response.data;
}
+17
View File
@@ -0,0 +1,17 @@
import api from './api';
import { Pharmacy } from '../types';
export async function getPharmacies(): Promise<Pharmacy[]> {
const response = await api.get('/pharmacies');
return response.data;
}
export async function getPharmacy(id: number): Promise<Pharmacy> {
const response = await api.get(`/pharmacies/${id}`);
return response.data;
}
export async function getPharmacyMedicines(id: number): Promise<any[]> {
const response = await api.get(`/pharmacies/${id}/medicines`);
return response.data;
}