index.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const createNewProject = async (token: string, name: string, description: string) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/projects/create`, {
  5. method: 'POST',
  6. headers: {
  7. Accept: 'application/json',
  8. 'Content-Type': 'application/json',
  9. authorization: `Bearer ${token}`
  10. },
  11. body: JSON.stringify({
  12. name: name,
  13. description: description
  14. })
  15. })
  16. .then(async (res) => {
  17. if (!res.ok) throw await res.json();
  18. return res.json();
  19. })
  20. .catch((err) => {
  21. error = err.detail;
  22. console.log(err);
  23. return null;
  24. });
  25. if (error) {
  26. throw error;
  27. }
  28. return res;
  29. };
  30. export const getProjects = async (token: string = '') => {
  31. let error = null;
  32. const res = await fetch(`${WEBUI_API_BASE_URL}/projects/`, {
  33. method: 'GET',
  34. headers: {
  35. Accept: 'application/json',
  36. 'Content-Type': 'application/json',
  37. authorization: `Bearer ${token}`
  38. }
  39. })
  40. .then(async (res) => {
  41. if (!res.ok) throw await res.json();
  42. return res.json();
  43. })
  44. .then((json) => {
  45. return json;
  46. })
  47. .catch((err) => {
  48. error = err.detail;
  49. console.log(err);
  50. return null;
  51. });
  52. if (error) {
  53. throw error;
  54. }
  55. return res;
  56. };
  57. export const getProjectById = async (token: string, id: string) => {
  58. let error = null;
  59. const res = await fetch(`${WEBUI_API_BASE_URL}/projects/${id}`, {
  60. method: 'GET',
  61. headers: {
  62. Accept: 'application/json',
  63. 'Content-Type': 'application/json',
  64. authorization: `Bearer ${token}`
  65. }
  66. })
  67. .then(async (res) => {
  68. if (!res.ok) throw await res.json();
  69. return res.json();
  70. })
  71. .then((json) => {
  72. return json;
  73. })
  74. .catch((err) => {
  75. error = err.detail;
  76. console.log(err);
  77. return null;
  78. });
  79. if (error) {
  80. throw error;
  81. }
  82. return res;
  83. };
  84. type ProjectForm = {
  85. name: string;
  86. };
  87. export const updateProjectById = async (token: string, id: string, form: ProjectForm) => {
  88. let error = null;
  89. const res = await fetch(`${WEBUI_API_BASE_URL}/projects/${id}/update`, {
  90. method: 'POST',
  91. headers: {
  92. Accept: 'application/json',
  93. 'Content-Type': 'application/json',
  94. authorization: `Bearer ${token}`
  95. },
  96. body: JSON.stringify({
  97. name: form.name
  98. })
  99. })
  100. .then(async (res) => {
  101. if (!res.ok) throw await res.json();
  102. return res.json();
  103. })
  104. .then((json) => {
  105. return json;
  106. })
  107. .catch((err) => {
  108. error = err.detail;
  109. console.log(err);
  110. return null;
  111. });
  112. if (error) {
  113. throw error;
  114. }
  115. return res;
  116. };
  117. export const deleteProjectById = async (token: string, id: string) => {
  118. let error = null;
  119. const res = await fetch(`${WEBUI_API_BASE_URL}/projects/${id}/delete`, {
  120. method: 'DELETE',
  121. headers: {
  122. Accept: 'application/json',
  123. 'Content-Type': 'application/json',
  124. authorization: `Bearer ${token}`
  125. }
  126. })
  127. .then(async (res) => {
  128. if (!res.ok) throw await res.json();
  129. return res.json();
  130. })
  131. .then((json) => {
  132. return json;
  133. })
  134. .catch((err) => {
  135. error = err.detail;
  136. console.log(err);
  137. return null;
  138. });
  139. if (error) {
  140. throw error;
  141. }
  142. return res;
  143. };