index.ts 4.9 KB

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