index.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 getUserById = async (token: string, userId: string) => {
  102. let error = null;
  103. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, {
  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 deleteUserById = async (token: string, userId: string) => {
  125. let error = null;
  126. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, {
  127. method: 'DELETE',
  128. headers: {
  129. 'Content-Type': 'application/json',
  130. Authorization: `Bearer ${token}`
  131. }
  132. })
  133. .then(async (res) => {
  134. if (!res.ok) throw await res.json();
  135. return res.json();
  136. })
  137. .catch((err) => {
  138. console.log(err);
  139. error = err.detail;
  140. return null;
  141. });
  142. if (error) {
  143. throw error;
  144. }
  145. return res;
  146. };
  147. type UserUpdateForm = {
  148. profile_image_url: string;
  149. email: string;
  150. name: string;
  151. password: string;
  152. };
  153. export const updateUserById = async (token: string, userId: string, user: UserUpdateForm) => {
  154. let error = null;
  155. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}/update`, {
  156. method: 'POST',
  157. headers: {
  158. 'Content-Type': 'application/json',
  159. Authorization: `Bearer ${token}`
  160. },
  161. body: JSON.stringify({
  162. profile_image_url: user.profile_image_url,
  163. email: user.email,
  164. name: user.name,
  165. password: user.password !== '' ? user.password : undefined
  166. })
  167. })
  168. .then(async (res) => {
  169. if (!res.ok) throw await res.json();
  170. return res.json();
  171. })
  172. .catch((err) => {
  173. console.log(err);
  174. error = err.detail;
  175. return null;
  176. });
  177. if (error) {
  178. throw error;
  179. }
  180. return res;
  181. };