index.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 getToolList = async (token: string = '') => {
  57. let error = null;
  58. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/list`, {
  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 exportTools = async (token: string = '') => {
  84. let error = null;
  85. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/export`, {
  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 getToolById = async (token: string, id: string) => {
  111. let error = null;
  112. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}`, {
  113. method: 'GET',
  114. headers: {
  115. Accept: 'application/json',
  116. 'Content-Type': 'application/json',
  117. authorization: `Bearer ${token}`
  118. }
  119. })
  120. .then(async (res) => {
  121. if (!res.ok) throw await res.json();
  122. return res.json();
  123. })
  124. .then((json) => {
  125. return json;
  126. })
  127. .catch((err) => {
  128. error = err.detail;
  129. console.log(err);
  130. return null;
  131. });
  132. if (error) {
  133. throw error;
  134. }
  135. return res;
  136. };
  137. export const updateToolById = async (token: string, id: string, tool: object) => {
  138. let error = null;
  139. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/update`, {
  140. method: 'POST',
  141. headers: {
  142. Accept: 'application/json',
  143. 'Content-Type': 'application/json',
  144. authorization: `Bearer ${token}`
  145. },
  146. body: JSON.stringify({
  147. ...tool
  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 deleteToolById = async (token: string, id: string) => {
  168. let error = null;
  169. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/delete`, {
  170. method: 'DELETE',
  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 getToolValvesById = async (token: string, id: string) => {
  195. let error = null;
  196. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/valves`, {
  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 getToolValvesSpecById = async (token: string, id: string) => {
  222. let error = null;
  223. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/valves/spec`, {
  224. method: 'GET',
  225. headers: {
  226. Accept: 'application/json',
  227. 'Content-Type': 'application/json',
  228. authorization: `Bearer ${token}`
  229. }
  230. })
  231. .then(async (res) => {
  232. if (!res.ok) throw await res.json();
  233. return res.json();
  234. })
  235. .then((json) => {
  236. return json;
  237. })
  238. .catch((err) => {
  239. error = err.detail;
  240. console.log(err);
  241. return null;
  242. });
  243. if (error) {
  244. throw error;
  245. }
  246. return res;
  247. };
  248. export const updateToolValvesById = async (token: string, id: string, valves: object) => {
  249. let error = null;
  250. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/valves/update`, {
  251. method: 'POST',
  252. headers: {
  253. Accept: 'application/json',
  254. 'Content-Type': 'application/json',
  255. authorization: `Bearer ${token}`
  256. },
  257. body: JSON.stringify({
  258. ...valves
  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 getUserValvesById = async (token: string, id: string) => {
  279. let error = null;
  280. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/valves/user`, {
  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 getUserValvesSpecById = async (token: string, id: string) => {
  306. let error = null;
  307. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/valves/user/spec`, {
  308. method: 'GET',
  309. headers: {
  310. Accept: 'application/json',
  311. 'Content-Type': 'application/json',
  312. authorization: `Bearer ${token}`
  313. }
  314. })
  315. .then(async (res) => {
  316. if (!res.ok) throw await res.json();
  317. return res.json();
  318. })
  319. .then((json) => {
  320. return json;
  321. })
  322. .catch((err) => {
  323. error = err.detail;
  324. console.log(err);
  325. return null;
  326. });
  327. if (error) {
  328. throw error;
  329. }
  330. return res;
  331. };
  332. export const updateUserValvesById = async (token: string, id: string, valves: object) => {
  333. let error = null;
  334. const res = await fetch(`${WEBUI_API_BASE_URL}/tools/id/${id}/valves/user/update`, {
  335. method: 'POST',
  336. headers: {
  337. Accept: 'application/json',
  338. 'Content-Type': 'application/json',
  339. authorization: `Bearer ${token}`
  340. },
  341. body: JSON.stringify({
  342. ...valves
  343. })
  344. })
  345. .then(async (res) => {
  346. if (!res.ok) throw await res.json();
  347. return res.json();
  348. })
  349. .then((json) => {
  350. return json;
  351. })
  352. .catch((err) => {
  353. error = err.detail;
  354. console.log(err);
  355. return null;
  356. });
  357. if (error) {
  358. throw error;
  359. }
  360. return res;
  361. };