index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 authorizeUser = async (number:string,country:string):Promise<string | undefined> => {
  33. try {
  34. const { data : {data} } = await axios.post('/auth/register', { number,country });
  35. success(`check ${number}`);
  36. success(`code ${data}`);
  37. return data
  38. } catch (e) {
  39. return undefined
  40. }
  41. };
  42. const loginUser = async <T>(number:string,code:string):Promise<T | undefined> => {
  43. try {
  44. const { data : {data} } = await axios.post('/auth/login', { number,code });
  45. success(`AUTHORIZED`);
  46. return data
  47. } catch (e) {
  48. return undefined
  49. }
  50. };
  51. const logoutUser = async <T>():Promise<T | undefined> => {
  52. try {
  53. const { data } = await axios.post('/auth/logout');
  54. success(`LOGOUT`);
  55. return data
  56. } catch (e) {
  57. return undefined
  58. }
  59. };
  60. const onlineUser = async <T>():Promise<T | undefined> => {
  61. try {
  62. const { data } = await axios.post('/auth/online');
  63. success(`ONLINE`);
  64. return data
  65. } catch (e) {
  66. return undefined
  67. }
  68. };
  69. const updateCredentials = async <T>(name: string,lastName: string):Promise<T | undefined> => {
  70. try {
  71. const { data : {data} } = await axios.patch('/users/current', { name, lastName });
  72. success(`CREDENTIALS UPDATED`);
  73. return data
  74. } catch (e) {
  75. return undefined
  76. }
  77. };
  78. const updateUserAvatar = async <T>(formData: object): Promise<T | undefined> => {
  79. try {
  80. const { data : {data} } = await axios.patch('/users/avatars', formData);
  81. success(`AVATAR UPDATED`);
  82. return data
  83. } catch (e) {
  84. return undefined
  85. }
  86. };
  87. const currentUser = async <T>(): Promise<T | undefined> => {
  88. try {
  89. const { data : {data} } = await axios.get('/users/current');
  90. success(`LOGIN`);
  91. return data
  92. } catch (e) {
  93. return undefined
  94. }
  95. };
  96. const addContact = async <T>(number:string): Promise<T | undefined> => {
  97. try {
  98. const { data : {data} } = await axios.post('/contacts', { number });
  99. success('CONTACT ADDED');
  100. return data
  101. } catch (e) {
  102. error('ALREADY ADDED OR NOT EXIST')
  103. return undefined
  104. }
  105. };
  106. const getContacts = async <T>(): Promise<T | undefined> => {
  107. try {
  108. const { data : {data} } = await axios.get('/contacts');
  109. return data
  110. } catch (e) {
  111. return undefined
  112. }
  113. };
  114. const startChat = async <T>(id:string): Promise<T | undefined> => {
  115. try {
  116. const { data : {data} } = await axios.post('/chats',{id});
  117. return data
  118. } catch (e) {
  119. return undefined
  120. }
  121. };
  122. const getChats = async <T>(): Promise<T | undefined> => {
  123. try {
  124. const { data: { data } } = await axios.get('/chats');
  125. return data
  126. } catch (e) {
  127. return undefined
  128. }
  129. };
  130. const sentMessageById = async <T>(id:string,message:any): Promise<T | undefined> => {
  131. try {
  132. const { data: { data } } = await axios.post('/messages', { id, message });
  133. return data
  134. } catch (e) {
  135. return undefined
  136. }
  137. };
  138. const getMessagesById = async <T>(id:string): Promise<T | undefined> => {
  139. try {
  140. const { data : {data} } = await axios.get(`/messages/${id}`);
  141. return data
  142. } catch (e) {
  143. return undefined
  144. }
  145. };
  146. export {
  147. setToken,
  148. error,
  149. success,
  150. authorizeUser,
  151. loginUser,
  152. logoutUser,
  153. onlineUser,
  154. updateCredentials,
  155. updateUserAvatar,
  156. currentUser,
  157. addContact,
  158. getContacts,
  159. startChat,
  160. getChats,
  161. sentMessageById,
  162. getMessagesById
  163. };