index.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const createNewTool = async (token: string, tool: object) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/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. ...tool
  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.detail;
  21. console.log(err);
  22. return null;
  23. });
  24. if (error) {
  25. throw error;
  26. }
  27. return res;
  28. };
  29. export const getTools = async (token: string = '') => {
  30. let error = null;
  31. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/`, {
  32. method: 'GET',
  33. headers: {
  34. Accept: 'application/json',
  35. 'Content-Type': 'application/json',
  36. 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.detail;
  48. console.log(err);
  49. return null;
  50. });
  51. if (error) {
  52. throw error;
  53. }
  54. return res;
  55. };
  56. export const exportTools = async (token: string = '') => {
  57. let error = null;
  58. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/export`, {
  59. method: 'GET',
  60. headers: {
  61. Accept: 'application/json',
  62. 'Content-Type': 'application/json',
  63. 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.detail;
  75. console.log(err);
  76. return null;
  77. });
  78. if (error) {
  79. throw error;
  80. }
  81. return res;
  82. };
  83. export const getToolById = async (token: string, id: string) => {
  84. let error = null;
  85. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}`, {
  86. method: 'GET',
  87. headers: {
  88. Accept: 'application/json',
  89. 'Content-Type': 'application/json',
  90. 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.detail;
  102. console.log(err);
  103. return null;
  104. });
  105. if (error) {
  106. throw error;
  107. }
  108. return res;
  109. };
  110. export const updateToolById = async (token: string, id: string, tool: object) => {
  111. let error = null;
  112. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/update`, {
  113. method: 'POST',
  114. headers: {
  115. Accept: 'application/json',
  116. 'Content-Type': 'application/json',
  117. authorization: `Bearer ${token}`
  118. },
  119. body: JSON.stringify({
  120. ...tool
  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.detail;
  132. console.log(err);
  133. return null;
  134. });
  135. if (error) {
  136. throw error;
  137. }
  138. return res;
  139. };
  140. export const deleteToolById = async (token: string, id: string) => {
  141. let error = null;
  142. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/delete`, {
  143. method: 'DELETE',
  144. headers: {
  145. Accept: 'application/json',
  146. 'Content-Type': 'application/json',
  147. 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.detail;
  159. console.log(err);
  160. return null;
  161. });
  162. if (error) {
  163. throw error;
  164. }
  165. return res;
  166. };
  167. export const getToolValvesById = async (token: string, id: string) => {
  168. let error = null;
  169. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/valves`, {
  170. method: 'GET',
  171. headers: {
  172. Accept: 'application/json',
  173. 'Content-Type': 'application/json',
  174. authorization: `Bearer ${token}`
  175. }
  176. })
  177. .then(async (res) => {
  178. if (!res.ok) throw await res.json();
  179. return res.json();
  180. })
  181. .then((json) => {
  182. return json;
  183. })
  184. .catch((err) => {
  185. error = err.detail;
  186. console.log(err);
  187. return null;
  188. });
  189. if (error) {
  190. throw error;
  191. }
  192. return res;
  193. };
  194. export const getToolValvesSpecById = async (token: string, id: string) => {
  195. let error = null;
  196. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/valves/spec`, {
  197. method: 'GET',
  198. headers: {
  199. Accept: 'application/json',
  200. 'Content-Type': 'application/json',
  201. authorization: `Bearer ${token}`
  202. }
  203. })
  204. .then(async (res) => {
  205. if (!res.ok) throw await res.json();
  206. return res.json();
  207. })
  208. .then((json) => {
  209. return json;
  210. })
  211. .catch((err) => {
  212. error = err.detail;
  213. console.log(err);
  214. return null;
  215. });
  216. if (error) {
  217. throw error;
  218. }
  219. return res;
  220. };
  221. export const updateToolValvesById = async (token: string, id: string, valves: object) => {
  222. let error = null;
  223. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/valves/update`, {
  224. method: 'POST',
  225. headers: {
  226. Accept: 'application/json',
  227. 'Content-Type': 'application/json',
  228. authorization: `Bearer ${token}`
  229. },
  230. body: JSON.stringify({
  231. ...valves
  232. })
  233. })
  234. .then(async (res) => {
  235. if (!res.ok) throw await res.json();
  236. return res.json();
  237. })
  238. .then((json) => {
  239. return json;
  240. })
  241. .catch((err) => {
  242. error = err.detail;
  243. console.log(err);
  244. return null;
  245. });
  246. if (error) {
  247. throw error;
  248. }
  249. return res;
  250. };
  251. export const getUserValvesById = async (token: string, id: string) => {
  252. let error = null;
  253. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/valves/user`, {
  254. method: 'GET',
  255. headers: {
  256. Accept: 'application/json',
  257. 'Content-Type': 'application/json',
  258. authorization: `Bearer ${token}`
  259. }
  260. })
  261. .then(async (res) => {
  262. if (!res.ok) throw await res.json();
  263. return res.json();
  264. })
  265. .then((json) => {
  266. return json;
  267. })
  268. .catch((err) => {
  269. error = err.detail;
  270. console.log(err);
  271. return null;
  272. });
  273. if (error) {
  274. throw error;
  275. }
  276. return res;
  277. };
  278. export const getUserValvesSpecById = async (token: string, id: string) => {
  279. let error = null;
  280. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/valves/user/spec`, {
  281. method: 'GET',
  282. headers: {
  283. Accept: 'application/json',
  284. 'Content-Type': 'application/json',
  285. authorization: `Bearer ${token}`
  286. }
  287. })
  288. .then(async (res) => {
  289. if (!res.ok) throw await res.json();
  290. return res.json();
  291. })
  292. .then((json) => {
  293. return json;
  294. })
  295. .catch((err) => {
  296. error = err.detail;
  297. console.log(err);
  298. return null;
  299. });
  300. if (error) {
  301. throw error;
  302. }
  303. return res;
  304. };
  305. export const updateUserValvesById = async (token: string, id: string, valves: object) => {
  306. let error = null;
  307. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/valves/user/update`, {
  308. method: 'POST',
  309. headers: {
  310. Accept: 'application/json',
  311. 'Content-Type': 'application/json',
  312. authorization: `Bearer ${token}`
  313. },
  314. body: JSON.stringify({
  315. ...valves
  316. })
  317. })
  318. .then(async (res) => {
  319. if (!res.ok) throw await res.json();
  320. return res.json();
  321. })
  322. .then((json) => {
  323. return json;
  324. })
  325. .catch((err) => {
  326. error = err.detail;
  327. console.log(err);
  328. return null;
  329. });
  330. if (error) {
  331. throw error;
  332. }
  333. return res;
  334. };