index.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const updateUserRole = async (token: string, id: string, role: string) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/users/update/role`, {
  5. method: 'POST',
  6. headers: {
  7. 'Content-Type': 'application/json',
  8. Authorization: `Bearer ${token}`
  9. },
  10. body: JSON.stringify({
  11. id: id,
  12. role: role
  13. })
  14. })
  15. .then(async (res) => {
  16. if (!res.ok) throw await res.json();
  17. return res.json();
  18. })
  19. .catch((err) => {
  20. console.log(err);
  21. error = err.detail;
  22. return null;
  23. });
  24. if (error) {
  25. throw error;
  26. }
  27. return res;
  28. };
  29. export const getUsers = async (token: string) => {
  30. let error = null;
  31. const res = await fetch(`${WEBUI_API_BASE_URL}/users/`, {
  32. method: 'GET',
  33. headers: {
  34. 'Content-Type': 'application/json',
  35. Authorization: `Bearer ${token}`
  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 ? res : [];
  51. };
  52. export const deleteUserById = async (token: string, userId: string) => {
  53. let error = null;
  54. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, {
  55. method: 'DELETE',
  56. headers: {
  57. 'Content-Type': 'application/json',
  58. Authorization: `Bearer ${token}`
  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. };