index.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. import { getUserPosition } from '$lib/utils';
  3. export const getUserPermissions = async (token: string) => {
  4. let error = null;
  5. const res = await fetch(`${WEBUI_API_BASE_URL}/users/permissions/user`, {
  6. method: 'GET',
  7. headers: {
  8. 'Content-Type': 'application/json',
  9. Authorization: `Bearer ${token}`
  10. }
  11. })
  12. .then(async (res) => {
  13. if (!res.ok) throw await res.json();
  14. return res.json();
  15. })
  16. .catch((err) => {
  17. console.log(err);
  18. error = err.detail;
  19. return null;
  20. });
  21. if (error) {
  22. throw error;
  23. }
  24. return res;
  25. };
  26. export const updateUserPermissions = async (token: string, permissions: object) => {
  27. let error = null;
  28. const res = await fetch(`${WEBUI_API_BASE_URL}/users/permissions/user`, {
  29. method: 'POST',
  30. headers: {
  31. 'Content-Type': 'application/json',
  32. Authorization: `Bearer ${token}`
  33. },
  34. body: JSON.stringify({
  35. ...permissions
  36. })
  37. })
  38. .then(async (res) => {
  39. if (!res.ok) throw await res.json();
  40. return res.json();
  41. })
  42. .catch((err) => {
  43. console.log(err);
  44. error = err.detail;
  45. return null;
  46. });
  47. if (error) {
  48. throw error;
  49. }
  50. return res;
  51. };
  52. export const updateUserRole = async (token: string, id: string, role: string) => {
  53. let error = null;
  54. const res = await fetch(`${WEBUI_API_BASE_URL}/users/update/role`, {
  55. method: 'POST',
  56. headers: {
  57. 'Content-Type': 'application/json',
  58. Authorization: `Bearer ${token}`
  59. },
  60. body: JSON.stringify({
  61. id: id,
  62. role: role
  63. })
  64. })
  65. .then(async (res) => {
  66. if (!res.ok) throw await res.json();
  67. return res.json();
  68. })
  69. .catch((err) => {
  70. console.log(err);
  71. error = err.detail;
  72. return null;
  73. });
  74. if (error) {
  75. throw error;
  76. }
  77. return res;
  78. };
  79. export const getUsers = async (token: string) => {
  80. let error = null;
  81. const res = await fetch(`${WEBUI_API_BASE_URL}/users/`, {
  82. method: 'GET',
  83. headers: {
  84. 'Content-Type': 'application/json',
  85. Authorization: `Bearer ${token}`
  86. }
  87. })
  88. .then(async (res) => {
  89. if (!res.ok) throw await res.json();
  90. return res.json();
  91. })
  92. .catch((err) => {
  93. console.log(err);
  94. error = err.detail;
  95. return null;
  96. });
  97. if (error) {
  98. throw error;
  99. }
  100. return res ? res : [];
  101. };
  102. export const getUserSettings = async (token: string) => {
  103. let error = null;
  104. const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/settings`, {
  105. method: 'GET',
  106. headers: {
  107. 'Content-Type': 'application/json',
  108. Authorization: `Bearer ${token}`
  109. }
  110. })
  111. .then(async (res) => {
  112. if (!res.ok) throw await res.json();
  113. return res.json();
  114. })
  115. .catch((err) => {
  116. console.log(err);
  117. error = err.detail;
  118. return null;
  119. });
  120. if (error) {
  121. throw error;
  122. }
  123. return res;
  124. };
  125. export const updateUserSettings = async (token: string, settings: object) => {
  126. let error = null;
  127. const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/settings/update`, {
  128. method: 'POST',
  129. headers: {
  130. 'Content-Type': 'application/json',
  131. Authorization: `Bearer ${token}`
  132. },
  133. body: JSON.stringify({
  134. ...settings
  135. })
  136. })
  137. .then(async (res) => {
  138. if (!res.ok) throw await res.json();
  139. return res.json();
  140. })
  141. .catch((err) => {
  142. console.log(err);
  143. error = err.detail;
  144. return null;
  145. });
  146. if (error) {
  147. throw error;
  148. }
  149. return res;
  150. };
  151. export const getUserById = async (token: string, userId: string) => {
  152. let error = null;
  153. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, {
  154. method: 'GET',
  155. headers: {
  156. 'Content-Type': 'application/json',
  157. Authorization: `Bearer ${token}`
  158. }
  159. })
  160. .then(async (res) => {
  161. if (!res.ok) throw await res.json();
  162. return res.json();
  163. })
  164. .catch((err) => {
  165. console.log(err);
  166. error = err.detail;
  167. return null;
  168. });
  169. if (error) {
  170. throw error;
  171. }
  172. return res;
  173. };
  174. export const getUserInfo = async (token: string) => {
  175. let error = null;
  176. const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/info`, {
  177. method: 'GET',
  178. headers: {
  179. 'Content-Type': 'application/json',
  180. Authorization: `Bearer ${token}`
  181. }
  182. })
  183. .then(async (res) => {
  184. if (!res.ok) throw await res.json();
  185. return res.json();
  186. })
  187. .catch((err) => {
  188. console.log(err);
  189. error = err.detail;
  190. return null;
  191. });
  192. if (error) {
  193. throw error;
  194. }
  195. return res;
  196. };
  197. export const updateUserInfo = async (token: string, info: object) => {
  198. let error = null;
  199. const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/info/update`, {
  200. method: 'POST',
  201. headers: {
  202. 'Content-Type': 'application/json',
  203. Authorization: `Bearer ${token}`
  204. },
  205. body: JSON.stringify({
  206. ...info
  207. })
  208. })
  209. .then(async (res) => {
  210. if (!res.ok) throw await res.json();
  211. return res.json();
  212. })
  213. .catch((err) => {
  214. console.log(err);
  215. error = err.detail;
  216. return null;
  217. });
  218. if (error) {
  219. throw error;
  220. }
  221. return res;
  222. };
  223. export const getAndUpdateUserLocation = async (token: string) => {
  224. const location = await getUserPosition().catch((err) => {
  225. throw err;
  226. });
  227. if (location) {
  228. await updateUserInfo(token, { location: location });
  229. return location;
  230. } else {
  231. throw new Error('Failed to get user location');
  232. }
  233. };
  234. export const deleteUserById = async (token: string, userId: string) => {
  235. let error = null;
  236. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, {
  237. method: 'DELETE',
  238. headers: {
  239. 'Content-Type': 'application/json',
  240. Authorization: `Bearer ${token}`
  241. }
  242. })
  243. .then(async (res) => {
  244. if (!res.ok) throw await res.json();
  245. return res.json();
  246. })
  247. .catch((err) => {
  248. console.log(err);
  249. error = err.detail;
  250. return null;
  251. });
  252. if (error) {
  253. throw error;
  254. }
  255. return res;
  256. };
  257. type UserUpdateForm = {
  258. profile_image_url: string;
  259. email: string;
  260. name: string;
  261. password: string;
  262. };
  263. export const updateUserById = async (token: string, userId: string, user: UserUpdateForm) => {
  264. let error = null;
  265. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}/update`, {
  266. method: 'POST',
  267. headers: {
  268. 'Content-Type': 'application/json',
  269. Authorization: `Bearer ${token}`
  270. },
  271. body: JSON.stringify({
  272. profile_image_url: user.profile_image_url,
  273. email: user.email,
  274. name: user.name,
  275. password: user.password !== '' ? user.password : undefined
  276. })
  277. })
  278. .then(async (res) => {
  279. if (!res.ok) throw await res.json();
  280. return res.json();
  281. })
  282. .catch((err) => {
  283. console.log(err);
  284. error = err.detail;
  285. return null;
  286. });
  287. if (error) {
  288. throw error;
  289. }
  290. return res;
  291. };