index.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. type ChannelForm = {
  3. name: string;
  4. data?: object;
  5. meta?: object;
  6. access_control?: object;
  7. }
  8. export const createNewChannel = async (token: string = '', channel: ChannelForm) => {
  9. let error = null;
  10. const res = await fetch(`${WEBUI_API_BASE_URL}/channels/create`, {
  11. method: 'POST',
  12. headers: {
  13. Accept: 'application/json',
  14. 'Content-Type': 'application/json',
  15. authorization: `Bearer ${token}`
  16. },
  17. body: JSON.stringify({ ...channel })
  18. })
  19. .then(async (res) => {
  20. if (!res.ok) throw await res.json();
  21. return res.json();
  22. })
  23. .then((json) => {
  24. return json;
  25. })
  26. .catch((err) => {
  27. error = err.detail;
  28. console.log(err);
  29. return null;
  30. });
  31. if (error) {
  32. throw error;
  33. }
  34. return res;
  35. };
  36. export const getChannels = async (token: string = '') => {
  37. let error = null;
  38. const res = await fetch(`${WEBUI_API_BASE_URL}/channels/`, {
  39. method: 'GET',
  40. headers: {
  41. Accept: 'application/json',
  42. 'Content-Type': 'application/json',
  43. authorization: `Bearer ${token}`
  44. }
  45. })
  46. .then(async (res) => {
  47. if (!res.ok) throw await res.json();
  48. return res.json();
  49. })
  50. .then((json) => {
  51. return json;
  52. })
  53. .catch((err) => {
  54. error = err.detail;
  55. console.log(err);
  56. return null;
  57. });
  58. if (error) {
  59. throw error;
  60. }
  61. return res;
  62. };
  63. export const getChannelById = async (token: string = '', channel_id: string) => {
  64. let error = null;
  65. const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_id}`, {
  66. method: 'GET',
  67. headers: {
  68. Accept: 'application/json',
  69. 'Content-Type': 'application/json',
  70. authorization: `Bearer ${token}`
  71. }
  72. })
  73. .then(async (res) => {
  74. if (!res.ok) throw await res.json();
  75. return res.json();
  76. })
  77. .then((json) => {
  78. return json;
  79. })
  80. .catch((err) => {
  81. error = err.detail;
  82. console.log(err);
  83. return null;
  84. });
  85. if (error) {
  86. throw error;
  87. }
  88. return res;
  89. }
  90. export const updateChannelById = async (token: string = '', channel_id: string, channel: ChannelForm) => {
  91. let error = null;
  92. const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_id}/update`, {
  93. method: 'POST',
  94. headers: {
  95. Accept: 'application/json',
  96. 'Content-Type': 'application/json',
  97. authorization: `Bearer ${token}`
  98. },
  99. body: JSON.stringify({ ...channel })
  100. })
  101. .then(async (res) => {
  102. if (!res.ok) throw await res.json();
  103. return res.json();
  104. })
  105. .then((json) => {
  106. return json;
  107. })
  108. .catch((err) => {
  109. error = err.detail;
  110. console.log(err);
  111. return null;
  112. });
  113. if (error) {
  114. throw error;
  115. }
  116. return res;
  117. }
  118. export const deleteChannelById = async (token: string = '', channel_id: string) => {
  119. let error = null;
  120. const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_id}/delete`, {
  121. method: 'DELETE',
  122. headers: {
  123. Accept: 'application/json',
  124. 'Content-Type': 'application/json',
  125. authorization: `Bearer ${token}`
  126. }
  127. })
  128. .then(async (res) => {
  129. if (!res.ok) throw await res.json();
  130. return res.json();
  131. })
  132. .then((json) => {
  133. return json;
  134. })
  135. .catch((err) => {
  136. error = err.detail;
  137. console.log(err);
  138. return null;
  139. });
  140. if (error) {
  141. throw error;
  142. }
  143. return res;
  144. }
  145. export const getChannelMessages = async (token: string = '', channel_id: string, skip: number = 0, limit: number = 50) => {
  146. let error = null;
  147. const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_id}/messages?skip=${skip}&limit=${limit}`, {
  148. method: 'GET',
  149. headers: {
  150. Accept: 'application/json',
  151. 'Content-Type': 'application/json',
  152. authorization: `Bearer ${token}`
  153. }
  154. })
  155. .then(async (res) => {
  156. if (!res.ok) throw await res.json();
  157. return res.json();
  158. })
  159. .then((json) => {
  160. return json;
  161. })
  162. .catch((err) => {
  163. error = err.detail;
  164. console.log(err);
  165. return null;
  166. });
  167. if (error) {
  168. throw error;
  169. }
  170. return res;
  171. }
  172. type MessageForm = {
  173. content: string;
  174. data?: object;
  175. meta?: object;
  176. }
  177. export const sendMessage = async (token: string = '', channel_id: string, message: MessageForm) => {
  178. let error = null;
  179. const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_id}/messages/post`, {
  180. method: 'POST',
  181. headers: {
  182. Accept: 'application/json',
  183. 'Content-Type': 'application/json',
  184. authorization: `Bearer ${token}`
  185. },
  186. body: JSON.stringify({ ...message })
  187. })
  188. .then(async (res) => {
  189. if (!res.ok) throw await res.json();
  190. return res.json();
  191. })
  192. .then((json) => {
  193. return json;
  194. })
  195. .catch((err) => {
  196. error = err.detail;
  197. console.log(err);
  198. return null;
  199. });
  200. if (error) {
  201. throw error;
  202. }
  203. return res;
  204. }
  205. export const updateMessage = async (token: string = '', channel_id: string, message_id: string, message: MessageForm) => {
  206. let error = null;
  207. const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_id}/messages/${message_id}/update`, {
  208. method: 'POST',
  209. headers: {
  210. Accept: 'application/json',
  211. 'Content-Type': 'application/json',
  212. authorization: `Bearer ${token}`
  213. },
  214. body: JSON.stringify({ ...message })
  215. })
  216. .then(async (res) => {
  217. if (!res.ok) throw await res.json();
  218. return res.json();
  219. })
  220. .then((json) => {
  221. return json;
  222. })
  223. .catch((err) => {
  224. error = err.detail;
  225. console.log(err);
  226. return null;
  227. });
  228. if (error) {
  229. throw error;
  230. }
  231. return res;
  232. }
  233. export const deleteMessage = async (token: string = '', channel_id: string, message_id: string) => {
  234. let error = null;
  235. const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_id}/messages/${message_id}/delete`, {
  236. method: 'DELETE',
  237. headers: {
  238. Accept: 'application/json',
  239. 'Content-Type': 'application/json',
  240. authorization: `Bearer ${token}`
  241. }
  242. })
  243. .then(async (res) => {
  244. if (!res.ok) throw await res.json();
  245. return res.json();
  246. })
  247. .then((json) => {
  248. return json;
  249. })
  250. .catch((err) => {
  251. error = err.detail;
  252. console.log(err);
  253. return null;
  254. });
  255. if (error) {
  256. throw error;
  257. }
  258. return res;
  259. }