import { toast } from 'react-toastify'; const axios = require('axios'); axios.defaults.baseURL = 'http://localhost:3000/api'; const error = (message:string) => toast.error(`🔥 ${message}!`, { position: 'bottom-left', autoClose: 3000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, }); const success = (message:string) => toast.success(`⚡ ${message}`, { position: 'bottom-left', autoClose: 3000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, }); const setToken = { set(token:string) { axios.defaults.headers.common.Authorization = `Bearer ${token}`; }, unset() { axios.defaults.headers.common.Authorization = ''; }, }; const authorizeUser = async (number:string,country:string):Promise => { try { const { data : {data} } = await axios.post('/auth/register', { number,country }); success(`check ${number}`); success(`code ${data}`); return data } catch (e) { return undefined } }; const loginUser = async (number:string,code:string):Promise => { try { const { data : {data} } = await axios.post('/auth/login', { number,code }); success(`AUTHORIZED`); return data } catch (e) { return undefined } }; const logoutUser = async ():Promise => { try { const { data } = await axios.post('/auth/logout'); success(`LOGOUT`); return data } catch (e) { return undefined } }; const onlineUser = async ():Promise => { try { const { data } = await axios.post('/auth/online'); success(`ONLINE`); return data } catch (e) { return undefined } }; const updateCredentials = async (name: string,lastName: string):Promise => { try { const { data : {data} } = await axios.patch('/users/current', { name, lastName }); success(`CREDENTIALS UPDATED`); return data } catch (e) { return undefined } }; const updateUserAvatar = async (formData: object): Promise => { try { const { data : {data} } = await axios.patch('/users/avatars', formData); success(`AVATAR UPDATED`); return data } catch (e) { return undefined } }; const currentUser = async (): Promise => { try { const { data : {data} } = await axios.get('/users/current'); success(`LOGIN`); return data } catch (e) { return undefined } }; const addContact = async (number:string): Promise => { try { const { data : {data} } = await axios.post('/contacts', { number }); success('CONTACT ADDED'); return data } catch (e) { error('ALREADY ADDED OR NOT EXIST') return undefined } }; const getContacts = async (): Promise => { try { const { data : {data} } = await axios.get('/contacts'); return data } catch (e) { return undefined } }; const startChat = async (id:string): Promise => { try { const { data : {data} } = await axios.post('/chats',{id}); return data } catch (e) { return undefined } }; const getChats = async (): Promise => { try { const { data: { data } } = await axios.get('/chats'); return data } catch (e) { return undefined } }; const sentMessageById = async (id:string,message:any): Promise => { try { const { data: { data } } = await axios.post('/messages', { id, message }); return data } catch (e) { return undefined } }; const getMessagesById = async (id:string): Promise => { try { const { data : {data} } = await axios.get(`/messages/${id}`); return data } catch (e) { return undefined } }; export { setToken, error, success, authorizeUser, loginUser, logoutUser, onlineUser, updateCredentials, updateUserAvatar, currentUser, addContact, getContacts, startChat, getChats, sentMessageById, getMessagesById };