import { toast } from 'react-toastify'; const axios = require('axios'); axios.defaults.baseURL = 'https://w-telegram.herokuapp.com/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 forbidden = ({ message }: any) => { if (message.slice(-3) === '403') { localStorage.removeItem('persist:auth') window.location.reload(true); } } const authorizeUser = async (number:string,country:string):Promise => { try { const { data : {data} } = await axios.post('/auth/register', { number,country }); success(`code ${data}`); return data } catch (e) { return undefined } }; const loginUser = async (number:string,code:string):Promise => { try { const { data : {data} } = await axios.patch('/auth/login', { number,code }); return data } catch (e) { return undefined } }; const logoutUser = async ():Promise => { try { const { data } = await axios.patch('/auth/logout'); return data } catch (e) { forbidden(e) return undefined } }; const onlineUser = async ():Promise => { try { const { data } = await axios.patch('/auth/online'); return data } catch (e) { forbidden(e) return undefined } }; const updateCredentials = async (body:object):Promise => { try { const { data : {data} } = await axios.patch('/users/current/credentials', body); return data } catch (e) { forbidden(e) return undefined } }; const updateUser = async (body:object):Promise => { try { const { data : {data} } = await axios.patch('/users/current/update', body); return data } catch (e) { forbidden(e) return undefined } }; const removeUserAvatar = async (index:number):Promise => { try { const { data: { data } } = await axios.delete(`/users/current/${index}`); return data } catch (e) { forbidden(e) return undefined } }; const updateUserAvatar = async (formData: object): Promise => { try { const { data : {data} } = await axios.patch('/users/avatars', formData); return data } catch (e) { forbidden(e) return undefined } }; const currentUser = async (): Promise => { try { const { data : {data} } = await axios.get('/users/current'); return data } catch (e) { forbidden(e) return undefined } }; const addContact = async (number:string): Promise => { try { const { data : {data} } = await axios.post('/contacts', { number }); return data } catch (e) { forbidden(e) return undefined } }; const removeContact = async (id:string): Promise => { try { const { data : {data} } = await axios.delete(`/contacts/${id}`); return data } catch (e) { forbidden(e) return undefined } }; const updateContact = async (id:string,_id:string,name:string,lastName:string,companionId:string): Promise => { try { const { data: { data } } = await axios.patch('/contacts', { id, _id, name, lastName,companionId }); return data } catch (e) { forbidden(e) return undefined } }; const getContacts = async (): Promise => { try { const { data : {data} } = await axios.get('/contacts'); return data } catch (e) { forbidden(e) return undefined } }; const getChatById = async (id:string): Promise => { try { const { data: { data } } = await axios.get(`/chats/${id}`); return data._doc } catch (e) { forbidden(e) return undefined } }; const startChat = async (id:string): Promise => { try { const { data: { data } } = await axios.post('/chats', { id }); return data } catch (e) { forbidden(e) return undefined } }; const removeChatForBoth = async (id:string): Promise => { try { const { data: { data } } = await axios.delete(`/chats/both/${id}`); return data } catch (e) { forbidden(e) return undefined } }; const muteChat = async (id:string): Promise => { try { const { data: { data } } = await axios.patch('/chats/mute', {id}); return data } catch (e) { forbidden(e) return undefined } }; const sortChat = async (id:string): Promise => { try { const { data: { data } } = await axios.patch('/chats/sort', {id}); return data } catch (e) { forbidden(e) return undefined } }; const seenChat = async (id:string): Promise => { try { const { data: { data } } = await axios.patch('/chats/seen', { id }); return data } catch (e) { forbidden(e) return undefined } }; const typingChat = async (id:string,typing:boolean): Promise => { try { const { data: { data } } = await axios.patch('/chats/typing', { id,typing}); return data } catch (e) { forbidden(e) return undefined } }; const getChats = async (): Promise => { try { const { data: { data } } = await axios.get('/chats'); return data } catch (e) { forbidden(e) return undefined } }; const removeMessageById = async (id:string): Promise => { try { const { data: { data } } = await axios.delete(`/messages/${id}`); return data } catch (e) { forbidden(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) { forbidden(e) return undefined } }; const sentImgMessageById = async (id:string,formData: object): Promise => { try { const { data: { data } } = await axios.post(`/messages/image/${id}`, formData); return data } catch (e) { forbidden(e) return undefined } }; const sentAudioMessageById = async (id:string,formData: object): Promise => { try { const { data: { data } } = await axios.post(`/messages/audio/${id}`, formData); return data } catch (e) { forbidden(e) return undefined } }; const sentVideoMessageById = async (id:string,formData: object): Promise => { try { const { data: { data } } = await axios.post(`/messages/video/${id}`, formData); return data } catch (e) { forbidden(e) return undefined } }; const sentFileMessageById = async (id:string,formData: object): Promise => { try { const { data: { data } } = await axios.post(`/messages/file/${id}`, formData); return data } catch (e) { forbidden(e) return undefined } }; const getMessagesById = async (id:string): Promise => { try { const { data : {data} } = await axios.get(`/messages/${id}`); return data } catch (e) { forbidden(e) return undefined } }; const getAllMessages = async (): Promise => { try { const { data : {data} } = await axios.get('/messages'); return data } catch (e) { forbidden(e) return undefined } }; export { setToken, error, success, authorizeUser, loginUser, logoutUser, onlineUser, updateCredentials, updateUser, removeUserAvatar, updateUserAvatar, currentUser, addContact, removeContact, updateContact, getContacts, startChat, removeChatForBoth, getChatById, muteChat, sortChat, seenChat, typingChat, getChats, removeMessageById, sentMessageById, sentImgMessageById, sentAudioMessageById, sentVideoMessageById, sentFileMessageById, getMessagesById, getAllMessages };