index.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. import { getUserPosition } from '$lib/utils';
  3. export const getUserGroups = async (token: string) => {
  4. let error = null;
  5. const res = await fetch(`${WEBUI_API_BASE_URL}/users/groups`, {
  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 getUserDefaultPermissions = async (token: string) => {
  27. let error = null;
  28. const res = await fetch(`${WEBUI_API_BASE_URL}/users/default/permissions`, {
  29. method: 'GET',
  30. headers: {
  31. 'Content-Type': 'application/json',
  32. Authorization: `Bearer ${token}`
  33. }
  34. })
  35. .then(async (res) => {
  36. if (!res.ok) throw await res.json();
  37. return res.json();
  38. })
  39. .catch((err) => {
  40. console.log(err);
  41. error = err.detail;
  42. return null;
  43. });
  44. if (error) {
  45. throw error;
  46. }
  47. return res;
  48. };
  49. export const updateUserDefaultPermissions = async (token: string, permissions: object) => {
  50. let error = null;
  51. const res = await fetch(`${WEBUI_API_BASE_URL}/users/default/permissions`, {
  52. method: 'POST',
  53. headers: {
  54. 'Content-Type': 'application/json',
  55. Authorization: `Bearer ${token}`
  56. },
  57. body: JSON.stringify({
  58. ...permissions
  59. })
  60. })
  61. .then(async (res) => {
  62. if (!res.ok) throw await res.json();
  63. return res.json();
  64. })
  65. .catch((err) => {
  66. console.log(err);
  67. error = err.detail;
  68. return null;
  69. });
  70. if (error) {
  71. throw error;
  72. }
  73. return res;
  74. };
  75. export const updateUserRole = async (token: string, id: string, role: string) => {
  76. let error = null;
  77. const res = await fetch(`${WEBUI_API_BASE_URL}/users/update/role`, {
  78. method: 'POST',
  79. headers: {
  80. 'Content-Type': 'application/json',
  81. Authorization: `Bearer ${token}`
  82. },
  83. body: JSON.stringify({
  84. id: id,
  85. role: role
  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;
  101. };
  102. export const getUsers = async (token: string) => {
  103. let error = null;
  104. const res = await fetch(`${WEBUI_API_BASE_URL}/users/`, {
  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 ? res : [];
  124. };
  125. export const getUserSettings = async (token: string) => {
  126. let error = null;
  127. const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/settings`, {
  128. method: 'GET',
  129. headers: {
  130. 'Content-Type': 'application/json',
  131. Authorization: `Bearer ${token}`
  132. }
  133. })
  134. .then(async (res) => {
  135. if (!res.ok) throw await res.json();
  136. return res.json();
  137. })
  138. .catch((err) => {
  139. console.log(err);
  140. error = err.detail;
  141. return null;
  142. });
  143. if (error) {
  144. throw error;
  145. }
  146. return res;
  147. };
  148. export const updateUserSettings = async (token: string, settings: object) => {
  149. let error = null;
  150. const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/settings/update`, {
  151. method: 'POST',
  152. headers: {
  153. 'Content-Type': 'application/json',
  154. Authorization: `Bearer ${token}`
  155. },
  156. body: JSON.stringify({
  157. ...settings
  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 getUserById = async (token: string, userId: string) => {
  175. let error = null;
  176. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, {
  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 getUserInfo = async (token: string) => {
  198. let error = null;
  199. const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/info`, {
  200. method: 'GET',
  201. headers: {
  202. 'Content-Type': 'application/json',
  203. Authorization: `Bearer ${token}`
  204. }
  205. })
  206. .then(async (res) => {
  207. if (!res.ok) throw await res.json();
  208. return res.json();
  209. })
  210. .catch((err) => {
  211. console.log(err);
  212. error = err.detail;
  213. return null;
  214. });
  215. if (error) {
  216. throw error;
  217. }
  218. return res;
  219. };
  220. export const updateUserInfo = async (token: string, info: object) => {
  221. let error = null;
  222. const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/info/update`, {
  223. method: 'POST',
  224. headers: {
  225. 'Content-Type': 'application/json',
  226. Authorization: `Bearer ${token}`
  227. },
  228. body: JSON.stringify({
  229. ...info
  230. })
  231. })
  232. .then(async (res) => {
  233. if (!res.ok) throw await res.json();
  234. return res.json();
  235. })
  236. .catch((err) => {
  237. console.log(err);
  238. error = err.detail;
  239. return null;
  240. });
  241. if (error) {
  242. throw error;
  243. }
  244. return res;
  245. };
  246. export const getAndUpdateUserLocation = async (token: string) => {
  247. const location = await getUserPosition().catch((err) => {
  248. throw err;
  249. });
  250. if (location) {
  251. await updateUserInfo(token, { location: location });
  252. return location;
  253. } else {
  254. throw new Error('Failed to get user location');
  255. }
  256. };
  257. export const deleteUserById = async (token: string, userId: string) => {
  258. let error = null;
  259. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, {
  260. method: 'DELETE',
  261. headers: {
  262. 'Content-Type': 'application/json',
  263. Authorization: `Bearer ${token}`
  264. }
  265. })
  266. .then(async (res) => {
  267. if (!res.ok) throw await res.json();
  268. return res.json();
  269. })
  270. .catch((err) => {
  271. console.log(err);
  272. error = err.detail;
  273. return null;
  274. });
  275. if (error) {
  276. throw error;
  277. }
  278. return res;
  279. };
  280. type UserUpdateForm = {
  281. profile_image_url: string;
  282. email: string;
  283. name: string;
  284. password: string;
  285. };
  286. export const updateUserById = async (token: string, userId: string, user: UserUpdateForm) => {
  287. let error = null;
  288. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}/update`, {
  289. method: 'POST',
  290. headers: {
  291. 'Content-Type': 'application/json',
  292. Authorization: `Bearer ${token}`
  293. },
  294. body: JSON.stringify({
  295. profile_image_url: user.profile_image_url,
  296. email: user.email,
  297. name: user.name,
  298. password: user.password !== '' ? user.password : undefined
  299. })
  300. })
  301. .then(async (res) => {
  302. if (!res.ok) throw await res.json();
  303. return res.json();
  304. })
  305. .catch((err) => {
  306. console.log(err);
  307. error = err.detail;
  308. return null;
  309. });
  310. if (error) {
  311. throw error;
  312. }
  313. return res;
  314. };