index.ts 3.5 KB

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