index.ts 3.2 KB

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