index.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import { toast } from 'react-toastify';
  2. const axios = require('axios');
  3. axios.defaults.baseURL = 'http://localhost:3000/api';
  4. const error = (message:string) =>
  5. toast.error(`🔥 ${message}!`, {
  6. position: 'bottom-left',
  7. autoClose: 3000,
  8. hideProgressBar: false,
  9. closeOnClick: true,
  10. pauseOnHover: true,
  11. draggable: true,
  12. progress: undefined,
  13. });
  14. const success = (message:string) =>
  15. toast.success(`⚡ ${message}`, {
  16. position: 'bottom-left',
  17. autoClose: 3000,
  18. hideProgressBar: false,
  19. closeOnClick: true,
  20. pauseOnHover: true,
  21. draggable: true,
  22. progress: undefined,
  23. });
  24. const setToken = {
  25. set(token:string) {
  26. axios.defaults.headers.common.Authorization = `Bearer ${token}`;
  27. },
  28. unset() {
  29. axios.defaults.headers.common.Authorization = '';
  30. },
  31. };
  32. const forbidden = ({ message }: any) => {
  33. if (message.slice(-3) === '403') {
  34. localStorage.removeItem('persist:auth')
  35. window.location.reload(true);
  36. }
  37. }
  38. const authorizeUser = async (number:string,country:string):Promise<string | undefined> => {
  39. try {
  40. const { data : {data} } = await axios.post('/auth/register', { number,country });
  41. success(`code ${data}`);
  42. return data
  43. } catch (e) {
  44. return undefined
  45. }
  46. };
  47. const loginUser = async <T>(number:string,code:string):Promise<T | undefined> => {
  48. try {
  49. const { data : {data} } = await axios.patch('/auth/login', { number,code });
  50. return data
  51. } catch (e) {
  52. return undefined
  53. }
  54. };
  55. const logoutUser = async <T>():Promise<T | undefined> => {
  56. try {
  57. const { data } = await axios.patch('/auth/logout');
  58. return data
  59. } catch (e) {
  60. forbidden(e)
  61. return undefined
  62. }
  63. };
  64. const onlineUser = async <T>():Promise<T | undefined> => {
  65. try {
  66. const { data } = await axios.patch('/auth/online');
  67. return data
  68. } catch (e) {
  69. forbidden(e)
  70. return undefined
  71. }
  72. };
  73. const updateCredentials = async <T>(name: string,lastName: string):Promise<T | undefined> => {
  74. try {
  75. const { data : {data} } = await axios.patch('/users/current', { name, lastName });
  76. return data
  77. } catch (e) {
  78. forbidden(e)
  79. return undefined
  80. }
  81. };
  82. const updateUserAvatar = async <T>(formData: object): Promise<T | undefined> => {
  83. try {
  84. const { data : {data} } = await axios.patch('/users/avatars', formData);
  85. return data
  86. } catch (e) {
  87. forbidden(e)
  88. return undefined
  89. }
  90. };
  91. const currentUser = async <T>(): Promise<T | undefined> => {
  92. try {
  93. const { data : {data} } = await axios.get('/users/current');
  94. return data
  95. } catch (e) {
  96. forbidden(e)
  97. return undefined
  98. }
  99. };
  100. const addContact = async <T>(number:string): Promise<T | undefined> => {
  101. try {
  102. const { data : {data} } = await axios.post('/contacts', { number });
  103. return data
  104. } catch (e) {
  105. forbidden(e)
  106. return undefined
  107. }
  108. };
  109. const removeContact = async <T>(id:string): Promise<T | undefined> => {
  110. try {
  111. const { data : {data} } = await axios.delete(`/contacts/${id}`);
  112. return data
  113. } catch (e) {
  114. forbidden(e)
  115. return undefined
  116. }
  117. };
  118. const updateContact = async <T>(id:string,_id:string,name:string,lastName:string): Promise<T | undefined> => {
  119. try {
  120. const { data: { data } } = await axios.patch('/contacts', { id, _id, name, lastName });
  121. return data
  122. } catch (e) {
  123. forbidden(e)
  124. return undefined
  125. }
  126. };
  127. const getContacts = async <T>(): Promise<T | undefined> => {
  128. try {
  129. const { data : {data} } = await axios.get('/contacts');
  130. return data
  131. } catch (e) {
  132. forbidden(e)
  133. return undefined
  134. }
  135. };
  136. const getChatById = async <T>(id:string): Promise<T | undefined> => {
  137. try {
  138. const { data: { data } } = await axios.get(`/chats/${id}`);
  139. return data
  140. } catch (e) {
  141. forbidden(e)
  142. return undefined
  143. }
  144. };
  145. const startChat = async <T>(id:string): Promise<T | undefined> => {
  146. try {
  147. const { data: { data } } = await axios.post('/chats', { id });
  148. console.log(data)
  149. return data
  150. } catch (e) {
  151. forbidden(e)
  152. return undefined
  153. }
  154. };
  155. const removeChatForBoth = async <T>(id:string): Promise<T | undefined> => {
  156. try {
  157. const { data: { data } } = await axios.delete(`/chats/both/${id}`);
  158. return data
  159. } catch (e) {
  160. forbidden(e)
  161. return undefined
  162. }
  163. };
  164. const removeChatForMe = async <T>(id:string): Promise<T | undefined> => {
  165. try {
  166. const { data: { data } } = await axios.delete(`/chats/me/${id}`);
  167. return data
  168. } catch (e) {
  169. forbidden(e)
  170. return undefined
  171. }
  172. };
  173. const muteChat = async <T>(id:string): Promise<T | undefined> => {
  174. try {
  175. const { data: { data } } = await axios.patch('/chats/mute', {id});
  176. return data
  177. } catch (e) {
  178. forbidden(e)
  179. return undefined
  180. }
  181. };
  182. const sortChat = async <T>(id:string): Promise<T | undefined> => {
  183. try {
  184. const { data: { data } } = await axios.patch('/chats/sort', {id});
  185. return data
  186. } catch (e) {
  187. forbidden(e)
  188. return undefined
  189. }
  190. };
  191. const seenChat = async <T>(id:string): Promise<T | undefined> => {
  192. try {
  193. const { data: { data } } = await axios.patch('/chats/seen', { id });
  194. return data
  195. } catch (e) {
  196. forbidden(e)
  197. return undefined
  198. }
  199. };
  200. const typingChat = async <T>(id:string,typing:boolean): Promise<T | undefined> => {
  201. try {
  202. const { data: { data } } = await axios.patch('/chats/typing', { id,typing});
  203. return data
  204. } catch (e) {
  205. forbidden(e)
  206. return undefined
  207. }
  208. };
  209. const getChats = async <T>(): Promise<T | undefined> => {
  210. try {
  211. const { data: { data } } = await axios.get('/chats');
  212. console.log(data)
  213. return data
  214. } catch (e) {
  215. forbidden(e)
  216. return undefined
  217. }
  218. };
  219. const sentMessageById = async <T>(id:string,message:any): Promise<T | undefined> => {
  220. try {
  221. const { data: { data } } = await axios.post('/messages', { id, message });
  222. return data
  223. } catch (e) {
  224. forbidden(e)
  225. return undefined
  226. }
  227. };
  228. const sentImgMessageById = async <T>(id:string,formData: object): Promise<T | undefined> => {
  229. try {
  230. const { data: { data } } = await axios.post(`/messages/image/${id}`, formData);
  231. return data
  232. } catch (e) {
  233. forbidden(e)
  234. return undefined
  235. }
  236. };
  237. const sentAudioMessageById = async <T>(id:string,formData: object): Promise<T | undefined> => {
  238. try {
  239. const { data: { data } } = await axios.post(`/messages/audio/${id}`, formData);
  240. return data
  241. } catch (e) {
  242. forbidden(e)
  243. return undefined
  244. }
  245. };
  246. const sentVideoMessageById = async <T>(id:string,formData: object): Promise<T | undefined> => {
  247. try {
  248. const { data: { data } } = await axios.post(`/messages/video/${id}`, formData);
  249. return data
  250. } catch (e) {
  251. forbidden(e)
  252. return undefined
  253. }
  254. };
  255. const sentFileMessageById = async <T>(id:string,formData: object): Promise<T | undefined> => {
  256. try {
  257. const { data: { data } } = await axios.post(`/messages/file/${id}`, formData);
  258. return data
  259. } catch (e) {
  260. forbidden(e)
  261. return undefined
  262. }
  263. };
  264. const getMessagesById = async <T>(id:string): Promise<T | undefined> => {
  265. try {
  266. const { data : {data} } = await axios.get(`/messages/${id}`);
  267. return data
  268. } catch (e) {
  269. forbidden(e)
  270. return undefined
  271. }
  272. };
  273. export {
  274. setToken,
  275. error,
  276. success,
  277. authorizeUser,
  278. loginUser,
  279. logoutUser,
  280. onlineUser,
  281. updateCredentials,
  282. updateUserAvatar,
  283. currentUser,
  284. addContact,
  285. removeContact,
  286. updateContact,
  287. getContacts,
  288. startChat,
  289. removeChatForBoth,
  290. removeChatForMe,
  291. getChatById,
  292. muteChat,
  293. sortChat,
  294. seenChat,
  295. typingChat,
  296. getChats,
  297. sentMessageById,
  298. sentImgMessageById,
  299. sentAudioMessageById,
  300. sentVideoMessageById,
  301. sentFileMessageById,
  302. getMessagesById
  303. };