index.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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.error(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.error(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.error(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.error(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 (
  103. token: string,
  104. query?: string,
  105. orderBy?: string,
  106. direction?: string,
  107. page = 1
  108. ) => {
  109. let error = null;
  110. let res = null;
  111. let searchParams = new URLSearchParams();
  112. searchParams.set('page', `${page}`);
  113. if (query) {
  114. searchParams.set('query', query);
  115. }
  116. if (orderBy) {
  117. searchParams.set('order_by', orderBy);
  118. }
  119. if (direction) {
  120. searchParams.set('direction', direction);
  121. }
  122. res = await fetch(`${WEBUI_API_BASE_URL}/users/?${searchParams.toString()}`, {
  123. method: 'GET',
  124. headers: {
  125. 'Content-Type': 'application/json',
  126. Authorization: `Bearer ${token}`
  127. }
  128. })
  129. .then(async (res) => {
  130. if (!res.ok) throw await res.json();
  131. return res.json();
  132. })
  133. .catch((err) => {
  134. console.error(err);
  135. error = err.detail;
  136. return null;
  137. });
  138. if (error) {
  139. throw error;
  140. }
  141. return res;
  142. };
  143. export const getAllUsers = async (token: string) => {
  144. let error = null;
  145. let res = null;
  146. res = await fetch(`${WEBUI_API_BASE_URL}/users/all`, {
  147. method: 'GET',
  148. headers: {
  149. 'Content-Type': 'application/json',
  150. Authorization: `Bearer ${token}`
  151. }
  152. })
  153. .then(async (res) => {
  154. if (!res.ok) throw await res.json();
  155. return res.json();
  156. })
  157. .catch((err) => {
  158. console.error(err);
  159. error = err.detail;
  160. return null;
  161. });
  162. if (error) {
  163. throw error;
  164. }
  165. return res;
  166. };
  167. export const getUserSettings = async (token: string) => {
  168. let error = null;
  169. const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/settings`, {
  170. method: 'GET',
  171. headers: {
  172. 'Content-Type': 'application/json',
  173. Authorization: `Bearer ${token}`
  174. }
  175. })
  176. .then(async (res) => {
  177. if (!res.ok) throw await res.json();
  178. return res.json();
  179. })
  180. .catch((err) => {
  181. console.error(err);
  182. error = err.detail;
  183. return null;
  184. });
  185. if (error) {
  186. throw error;
  187. }
  188. return res;
  189. };
  190. export const updateUserSettings = async (token: string, settings: object) => {
  191. let error = null;
  192. const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/settings/update`, {
  193. method: 'POST',
  194. headers: {
  195. 'Content-Type': 'application/json',
  196. Authorization: `Bearer ${token}`
  197. },
  198. body: JSON.stringify({
  199. ...settings
  200. })
  201. })
  202. .then(async (res) => {
  203. if (!res.ok) throw await res.json();
  204. return res.json();
  205. })
  206. .catch((err) => {
  207. console.error(err);
  208. error = err.detail;
  209. return null;
  210. });
  211. if (error) {
  212. throw error;
  213. }
  214. return res;
  215. };
  216. export const getUserById = async (token: string, userId: string) => {
  217. let error = null;
  218. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, {
  219. method: 'GET',
  220. headers: {
  221. 'Content-Type': 'application/json',
  222. Authorization: `Bearer ${token}`
  223. }
  224. })
  225. .then(async (res) => {
  226. if (!res.ok) throw await res.json();
  227. return res.json();
  228. })
  229. .catch((err) => {
  230. console.error(err);
  231. error = err.detail;
  232. return null;
  233. });
  234. if (error) {
  235. throw error;
  236. }
  237. return res;
  238. };
  239. export const getUserInfo = async (token: string) => {
  240. let error = null;
  241. const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/info`, {
  242. method: 'GET',
  243. headers: {
  244. 'Content-Type': 'application/json',
  245. Authorization: `Bearer ${token}`
  246. }
  247. })
  248. .then(async (res) => {
  249. if (!res.ok) throw await res.json();
  250. return res.json();
  251. })
  252. .catch((err) => {
  253. console.error(err);
  254. error = err.detail;
  255. return null;
  256. });
  257. if (error) {
  258. throw error;
  259. }
  260. return res;
  261. };
  262. export const updateUserInfo = async (token: string, info: object) => {
  263. let error = null;
  264. const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/info/update`, {
  265. method: 'POST',
  266. headers: {
  267. 'Content-Type': 'application/json',
  268. Authorization: `Bearer ${token}`
  269. },
  270. body: JSON.stringify({
  271. ...info
  272. })
  273. })
  274. .then(async (res) => {
  275. if (!res.ok) throw await res.json();
  276. return res.json();
  277. })
  278. .catch((err) => {
  279. console.error(err);
  280. error = err.detail;
  281. return null;
  282. });
  283. if (error) {
  284. throw error;
  285. }
  286. return res;
  287. };
  288. export const getAndUpdateUserLocation = async (token: string) => {
  289. const location = await getUserPosition().catch((err) => {
  290. console.error(err);
  291. return null;
  292. });
  293. if (location) {
  294. await updateUserInfo(token, { location: location });
  295. return location;
  296. } else {
  297. console.info('Failed to get user location');
  298. return null;
  299. }
  300. };
  301. export const getUserActiveStatusById = async (token: string, userId: string) => {
  302. let error = null;
  303. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}/active`, {
  304. method: 'GET',
  305. headers: {
  306. 'Content-Type': 'application/json',
  307. Authorization: `Bearer ${token}`
  308. }
  309. })
  310. .then(async (res) => {
  311. if (!res.ok) throw await res.json();
  312. return res.json();
  313. })
  314. .catch((err) => {
  315. console.error(err);
  316. error = err.detail;
  317. return null;
  318. });
  319. if (error) {
  320. throw error;
  321. }
  322. return res;
  323. };
  324. export const deleteUserById = async (token: string, userId: string) => {
  325. let error = null;
  326. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, {
  327. method: 'DELETE',
  328. headers: {
  329. 'Content-Type': 'application/json',
  330. Authorization: `Bearer ${token}`
  331. }
  332. })
  333. .then(async (res) => {
  334. if (!res.ok) throw await res.json();
  335. return res.json();
  336. })
  337. .catch((err) => {
  338. console.error(err);
  339. error = err.detail;
  340. return null;
  341. });
  342. if (error) {
  343. throw error;
  344. }
  345. return res;
  346. };
  347. type UserUpdateForm = {
  348. role: string;
  349. profile_image_url: string;
  350. email: string;
  351. name: string;
  352. password: string;
  353. };
  354. export const updateUserById = async (token: string, userId: string, user: UserUpdateForm) => {
  355. let error = null;
  356. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}/update`, {
  357. method: 'POST',
  358. headers: {
  359. 'Content-Type': 'application/json',
  360. Authorization: `Bearer ${token}`
  361. },
  362. body: JSON.stringify({
  363. profile_image_url: user.profile_image_url,
  364. role: user.role,
  365. email: user.email,
  366. name: user.name,
  367. password: user.password !== '' ? user.password : undefined
  368. })
  369. })
  370. .then(async (res) => {
  371. if (!res.ok) throw await res.json();
  372. return res.json();
  373. })
  374. .catch((err) => {
  375. console.error(err);
  376. error = err.detail;
  377. return null;
  378. });
  379. if (error) {
  380. throw error;
  381. }
  382. return res;
  383. };