index.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const createNewFunction = async (token: string, func: object) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/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. ...func
  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 getFunctions = async (token: string = '') => {
  30. let error = null;
  31. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/`, {
  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 exportFunctions = async (token: string = '') => {
  57. let error = null;
  58. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/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 getFunctionById = async (token: string, id: string) => {
  84. let error = null;
  85. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/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 updateFunctionById = async (token: string, id: string, func: object) => {
  111. let error = null;
  112. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/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. ...func
  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 deleteFunctionById = async (token: string, id: string) => {
  141. let error = null;
  142. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/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 toggleFunctionById = async (token: string, id: string) => {
  168. let error = null;
  169. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/id/${id}/toggle`, {
  170. method: 'POST',
  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 toggleGlobalById = async (token: string, id: string) => {
  195. let error = null;
  196. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/id/${id}/toggle/global`, {
  197. method: 'POST',
  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 getFunctionValvesById = async (token: string, id: string) => {
  222. let error = null;
  223. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/id/${id}/valves`, {
  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 getFunctionValvesSpecById = async (token: string, id: string) => {
  249. let error = null;
  250. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/id/${id}/valves/spec`, {
  251. method: 'GET',
  252. headers: {
  253. Accept: 'application/json',
  254. 'Content-Type': 'application/json',
  255. authorization: `Bearer ${token}`
  256. }
  257. })
  258. .then(async (res) => {
  259. if (!res.ok) throw await res.json();
  260. return res.json();
  261. })
  262. .then((json) => {
  263. return json;
  264. })
  265. .catch((err) => {
  266. error = err.detail;
  267. console.log(err);
  268. return null;
  269. });
  270. if (error) {
  271. throw error;
  272. }
  273. return res;
  274. };
  275. export const updateFunctionValvesById = async (token: string, id: string, valves: object) => {
  276. let error = null;
  277. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/id/${id}/valves/update`, {
  278. method: 'POST',
  279. headers: {
  280. Accept: 'application/json',
  281. 'Content-Type': 'application/json',
  282. authorization: `Bearer ${token}`
  283. },
  284. body: JSON.stringify({
  285. ...valves
  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 getUserValvesById = async (token: string, id: string) => {
  306. let error = null;
  307. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/id/${id}/valves/user`, {
  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 getUserValvesSpecById = async (token: string, id: string) => {
  333. let error = null;
  334. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/id/${id}/valves/user/spec`, {
  335. method: 'GET',
  336. headers: {
  337. Accept: 'application/json',
  338. 'Content-Type': 'application/json',
  339. authorization: `Bearer ${token}`
  340. }
  341. })
  342. .then(async (res) => {
  343. if (!res.ok) throw await res.json();
  344. return res.json();
  345. })
  346. .then((json) => {
  347. return json;
  348. })
  349. .catch((err) => {
  350. error = err.detail;
  351. console.log(err);
  352. return null;
  353. });
  354. if (error) {
  355. throw error;
  356. }
  357. return res;
  358. };
  359. export const updateUserValvesById = async (token: string, id: string, valves: object) => {
  360. let error = null;
  361. const res = await fetch(`${WEBUI_API_BASE_URL}/functions/id/${id}/valves/user/update`, {
  362. method: 'POST',
  363. headers: {
  364. Accept: 'application/json',
  365. 'Content-Type': 'application/json',
  366. authorization: `Bearer ${token}`
  367. },
  368. body: JSON.stringify({
  369. ...valves
  370. })
  371. })
  372. .then(async (res) => {
  373. if (!res.ok) throw await res.json();
  374. return res.json();
  375. })
  376. .then((json) => {
  377. return json;
  378. })
  379. .catch((err) => {
  380. error = err.detail;
  381. console.log(err);
  382. return null;
  383. });
  384. if (error) {
  385. throw error;
  386. }
  387. return res;
  388. };