40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import axios from 'axios';
|
|
|
|
const api = axios.create({
|
|
baseURL: process.env.NEXT_PUBLIC_API_URL,
|
|
});
|
|
|
|
// Интерцептор для добавления токена к запросам
|
|
api.interceptors.request.use((config) => {
|
|
const token = localStorage.getItem('token');
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
return config;
|
|
});
|
|
|
|
export const auth = async (telegramId: string, username: string) => {
|
|
const response = await api.post('/auth', { telegramId, username });
|
|
localStorage.setItem('token', response.data.token);
|
|
return response.data;
|
|
};
|
|
|
|
export const getProfile = async () => {
|
|
const response = await api.get('/profile');
|
|
return response.data;
|
|
};
|
|
|
|
export const getShopItems = async () => {
|
|
const response = await api.get('/shop');
|
|
return response.data;
|
|
};
|
|
|
|
export const purchaseItem = async (itemId: string) => {
|
|
const response = await api.post('/shop/purchase', { itemId });
|
|
return response.data;
|
|
};
|
|
|
|
export const transferBalance = async (recipientUsername: string, amount: number) => {
|
|
const response = await api.post('/transfer', { recipientUsername, amount });
|
|
return response.data;
|
|
};
|