index.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. };
  75. type UserUpdateForm = {
  76. profile_image_url: string;
  77. email: string;
  78. name: string;
  79. password: string;
  80. };
  81. export const updateUserById = async (token: string, userId: string, user: UserUpdateForm) => {
  82. let error = null;
  83. const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}/update`, {
  84. method: 'POST',
  85. headers: {
  86. 'Content-Type': 'application/json',
  87. Authorization: `Bearer ${token}`
  88. },
  89. body: JSON.stringify({
  90. profile_image_url: user.profile_image_url,
  91. email: user.email,
  92. name: user.name,
  93. password: user.password !== '' ? user.password : undefined
  94. })
  95. })
  96. .then(async (res) => {
  97. if (!res.ok) throw await res.json();
  98. return res.json();
  99. })
  100. .catch((err) => {
  101. console.log(err);
  102. error = err.detail;
  103. return null;
  104. });
  105. if (error) {
  106. throw error;
  107. }
  108. return res;
  109. };