index.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const getAdminDetails = async (token: string) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/admin/details`, {
  5. method: 'GET',
  6. headers: {
  7. 'Content-Type': 'application/json',
  8. Authorization: `Bearer ${token}`
  9. }
  10. })
  11. .then(async (res) => {
  12. if (!res.ok) throw await res.json();
  13. return res.json();
  14. })
  15. .catch((err) => {
  16. console.error(err);
  17. error = err.detail;
  18. return null;
  19. });
  20. if (error) {
  21. throw error;
  22. }
  23. return res;
  24. };
  25. export const getAdminConfig = async (token: string) => {
  26. let error = null;
  27. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/admin/config`, {
  28. method: 'GET',
  29. headers: {
  30. 'Content-Type': 'application/json',
  31. Authorization: `Bearer ${token}`
  32. }
  33. })
  34. .then(async (res) => {
  35. if (!res.ok) throw await res.json();
  36. return res.json();
  37. })
  38. .catch((err) => {
  39. console.error(err);
  40. error = err.detail;
  41. return null;
  42. });
  43. if (error) {
  44. throw error;
  45. }
  46. return res;
  47. };
  48. export const updateAdminConfig = async (token: string, body: object) => {
  49. let error = null;
  50. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/admin/config`, {
  51. method: 'POST',
  52. headers: {
  53. 'Content-Type': 'application/json',
  54. Authorization: `Bearer ${token}`
  55. },
  56. body: JSON.stringify(body)
  57. })
  58. .then(async (res) => {
  59. if (!res.ok) throw await res.json();
  60. return res.json();
  61. })
  62. .catch((err) => {
  63. console.error(err);
  64. error = err.detail;
  65. return null;
  66. });
  67. if (error) {
  68. throw error;
  69. }
  70. return res;
  71. };
  72. export const getSessionUser = async (token: string) => {
  73. let error = null;
  74. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/`, {
  75. method: 'GET',
  76. headers: {
  77. 'Content-Type': 'application/json',
  78. Authorization: `Bearer ${token}`
  79. },
  80. credentials: 'include'
  81. })
  82. .then(async (res) => {
  83. if (!res.ok) throw await res.json();
  84. return res.json();
  85. })
  86. .catch((err) => {
  87. console.error(err);
  88. error = err.detail;
  89. return null;
  90. });
  91. if (error) {
  92. throw error;
  93. }
  94. return res;
  95. };
  96. export const ldapUserSignIn = async (user: string, password: string) => {
  97. let error = null;
  98. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/ldap`, {
  99. method: 'POST',
  100. headers: {
  101. 'Content-Type': 'application/json'
  102. },
  103. credentials: 'include',
  104. body: JSON.stringify({
  105. user: user,
  106. password: password
  107. })
  108. })
  109. .then(async (res) => {
  110. if (!res.ok) throw await res.json();
  111. return res.json();
  112. })
  113. .catch((err) => {
  114. console.error(err);
  115. error = err.detail;
  116. return null;
  117. });
  118. if (error) {
  119. throw error;
  120. }
  121. return res;
  122. };
  123. export const getLdapConfig = async (token: string = '') => {
  124. let error = null;
  125. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/admin/config/ldap`, {
  126. method: 'GET',
  127. headers: {
  128. 'Content-Type': 'application/json',
  129. ...(token && { authorization: `Bearer ${token}` })
  130. }
  131. })
  132. .then(async (res) => {
  133. if (!res.ok) throw await res.json();
  134. return res.json();
  135. })
  136. .catch((err) => {
  137. console.error(err);
  138. error = err.detail;
  139. return null;
  140. });
  141. if (error) {
  142. throw error;
  143. }
  144. return res;
  145. };
  146. export const updateLdapConfig = async (token: string = '', enable_ldap: boolean) => {
  147. let error = null;
  148. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/admin/config/ldap`, {
  149. method: 'POST',
  150. headers: {
  151. 'Content-Type': 'application/json',
  152. ...(token && { authorization: `Bearer ${token}` })
  153. },
  154. body: JSON.stringify({
  155. enable_ldap: enable_ldap
  156. })
  157. })
  158. .then(async (res) => {
  159. if (!res.ok) throw await res.json();
  160. return res.json();
  161. })
  162. .catch((err) => {
  163. console.error(err);
  164. error = err.detail;
  165. return null;
  166. });
  167. if (error) {
  168. throw error;
  169. }
  170. return res;
  171. };
  172. export const getLdapServer = async (token: string = '') => {
  173. let error = null;
  174. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/admin/config/ldap/server`, {
  175. method: 'GET',
  176. headers: {
  177. 'Content-Type': 'application/json',
  178. ...(token && { authorization: `Bearer ${token}` })
  179. }
  180. })
  181. .then(async (res) => {
  182. if (!res.ok) throw await res.json();
  183. return res.json();
  184. })
  185. .catch((err) => {
  186. console.error(err);
  187. error = err.detail;
  188. return null;
  189. });
  190. if (error) {
  191. throw error;
  192. }
  193. return res;
  194. };
  195. export const updateLdapServer = async (token: string = '', body: object) => {
  196. let error = null;
  197. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/admin/config/ldap/server`, {
  198. method: 'POST',
  199. headers: {
  200. 'Content-Type': 'application/json',
  201. ...(token && { authorization: `Bearer ${token}` })
  202. },
  203. body: JSON.stringify(body)
  204. })
  205. .then(async (res) => {
  206. if (!res.ok) throw await res.json();
  207. return res.json();
  208. })
  209. .catch((err) => {
  210. console.error(err);
  211. error = err.detail;
  212. return null;
  213. });
  214. if (error) {
  215. throw error;
  216. }
  217. return res;
  218. };
  219. export const userSignIn = async (email: string, password: string) => {
  220. let error = null;
  221. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signin`, {
  222. method: 'POST',
  223. headers: {
  224. 'Content-Type': 'application/json'
  225. },
  226. credentials: 'include',
  227. body: JSON.stringify({
  228. email: email,
  229. password: password
  230. })
  231. })
  232. .then(async (res) => {
  233. if (!res.ok) throw await res.json();
  234. return res.json();
  235. })
  236. .catch((err) => {
  237. console.error(err);
  238. error = err.detail;
  239. return null;
  240. });
  241. if (error) {
  242. throw error;
  243. }
  244. return res;
  245. };
  246. export const userSignUp = async (
  247. name: string,
  248. email: string,
  249. password: string,
  250. profile_image_url: string
  251. ) => {
  252. let error = null;
  253. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup`, {
  254. method: 'POST',
  255. headers: {
  256. 'Content-Type': 'application/json'
  257. },
  258. credentials: 'include',
  259. body: JSON.stringify({
  260. name: name,
  261. email: email,
  262. password: password,
  263. profile_image_url: profile_image_url
  264. })
  265. })
  266. .then(async (res) => {
  267. if (!res.ok) throw await res.json();
  268. return res.json();
  269. })
  270. .catch((err) => {
  271. console.error(err);
  272. error = err.detail;
  273. return null;
  274. });
  275. if (error) {
  276. throw error;
  277. }
  278. return res;
  279. };
  280. export const userSignOut = async () => {
  281. let error = null;
  282. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signout`, {
  283. method: 'GET',
  284. headers: {
  285. 'Content-Type': 'application/json'
  286. },
  287. credentials: 'include'
  288. })
  289. .then(async (res) => {
  290. if (!res.ok) throw await res.json();
  291. return res.json();
  292. })
  293. .catch((err) => {
  294. console.error(err);
  295. error = err.detail;
  296. return null;
  297. });
  298. if (error) {
  299. throw error;
  300. }
  301. sessionStorage.clear();
  302. return res;
  303. };
  304. export const addUser = async (
  305. token: string,
  306. name: string,
  307. email: string,
  308. password: string,
  309. role: string = 'pending',
  310. profile_image_url: null | string = null
  311. ) => {
  312. let error = null;
  313. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/add`, {
  314. method: 'POST',
  315. headers: {
  316. 'Content-Type': 'application/json',
  317. ...(token && { authorization: `Bearer ${token}` })
  318. },
  319. body: JSON.stringify({
  320. name: name,
  321. email: email,
  322. password: password,
  323. role: role,
  324. ...(profile_image_url && { profile_image_url: profile_image_url })
  325. })
  326. })
  327. .then(async (res) => {
  328. if (!res.ok) throw await res.json();
  329. return res.json();
  330. })
  331. .catch((err) => {
  332. console.error(err);
  333. error = err.detail;
  334. return null;
  335. });
  336. if (error) {
  337. throw error;
  338. }
  339. return res;
  340. };
  341. export const updateUserProfile = async (token: string, name: string, profileImageUrl: string) => {
  342. let error = null;
  343. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/update/profile`, {
  344. method: 'POST',
  345. headers: {
  346. 'Content-Type': 'application/json',
  347. ...(token && { authorization: `Bearer ${token}` })
  348. },
  349. body: JSON.stringify({
  350. name: name,
  351. profile_image_url: profileImageUrl
  352. })
  353. })
  354. .then(async (res) => {
  355. if (!res.ok) throw await res.json();
  356. return res.json();
  357. })
  358. .catch((err) => {
  359. console.error(err);
  360. error = err.detail;
  361. return null;
  362. });
  363. if (error) {
  364. throw error;
  365. }
  366. return res;
  367. };
  368. export const updateUserPassword = async (token: string, password: string, newPassword: string) => {
  369. let error = null;
  370. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/update/password`, {
  371. method: 'POST',
  372. headers: {
  373. 'Content-Type': 'application/json',
  374. ...(token && { authorization: `Bearer ${token}` })
  375. },
  376. body: JSON.stringify({
  377. password: password,
  378. new_password: newPassword
  379. })
  380. })
  381. .then(async (res) => {
  382. if (!res.ok) throw await res.json();
  383. return res.json();
  384. })
  385. .catch((err) => {
  386. console.error(err);
  387. error = err.detail;
  388. return null;
  389. });
  390. if (error) {
  391. throw error;
  392. }
  393. return res;
  394. };
  395. export const getSignUpEnabledStatus = async (token: string) => {
  396. let error = null;
  397. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/enabled`, {
  398. method: 'GET',
  399. headers: {
  400. 'Content-Type': 'application/json',
  401. Authorization: `Bearer ${token}`
  402. }
  403. })
  404. .then(async (res) => {
  405. if (!res.ok) throw await res.json();
  406. return res.json();
  407. })
  408. .catch((err) => {
  409. console.error(err);
  410. error = err.detail;
  411. return null;
  412. });
  413. if (error) {
  414. throw error;
  415. }
  416. return res;
  417. };
  418. export const getDefaultUserRole = async (token: string) => {
  419. let error = null;
  420. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/user/role`, {
  421. method: 'GET',
  422. headers: {
  423. 'Content-Type': 'application/json',
  424. Authorization: `Bearer ${token}`
  425. }
  426. })
  427. .then(async (res) => {
  428. if (!res.ok) throw await res.json();
  429. return res.json();
  430. })
  431. .catch((err) => {
  432. console.error(err);
  433. error = err.detail;
  434. return null;
  435. });
  436. if (error) {
  437. throw error;
  438. }
  439. return res;
  440. };
  441. export const updateDefaultUserRole = async (token: string, role: string) => {
  442. let error = null;
  443. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/user/role`, {
  444. method: 'POST',
  445. headers: {
  446. 'Content-Type': 'application/json',
  447. Authorization: `Bearer ${token}`
  448. },
  449. body: JSON.stringify({
  450. role: role
  451. })
  452. })
  453. .then(async (res) => {
  454. if (!res.ok) throw await res.json();
  455. return res.json();
  456. })
  457. .catch((err) => {
  458. console.error(err);
  459. error = err.detail;
  460. return null;
  461. });
  462. if (error) {
  463. throw error;
  464. }
  465. return res;
  466. };
  467. export const toggleSignUpEnabledStatus = async (token: string) => {
  468. let error = null;
  469. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/enabled/toggle`, {
  470. method: 'GET',
  471. headers: {
  472. 'Content-Type': 'application/json',
  473. Authorization: `Bearer ${token}`
  474. }
  475. })
  476. .then(async (res) => {
  477. if (!res.ok) throw await res.json();
  478. return res.json();
  479. })
  480. .catch((err) => {
  481. console.error(err);
  482. error = err.detail;
  483. return null;
  484. });
  485. if (error) {
  486. throw error;
  487. }
  488. return res;
  489. };
  490. export const getJWTExpiresDuration = async (token: string) => {
  491. let error = null;
  492. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/token/expires`, {
  493. method: 'GET',
  494. headers: {
  495. 'Content-Type': 'application/json',
  496. Authorization: `Bearer ${token}`
  497. }
  498. })
  499. .then(async (res) => {
  500. if (!res.ok) throw await res.json();
  501. return res.json();
  502. })
  503. .catch((err) => {
  504. console.error(err);
  505. error = err.detail;
  506. return null;
  507. });
  508. if (error) {
  509. throw error;
  510. }
  511. return res;
  512. };
  513. export const updateJWTExpiresDuration = async (token: string, duration: string) => {
  514. let error = null;
  515. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/token/expires/update`, {
  516. method: 'POST',
  517. headers: {
  518. 'Content-Type': 'application/json',
  519. Authorization: `Bearer ${token}`
  520. },
  521. body: JSON.stringify({
  522. duration: duration
  523. })
  524. })
  525. .then(async (res) => {
  526. if (!res.ok) throw await res.json();
  527. return res.json();
  528. })
  529. .catch((err) => {
  530. console.error(err);
  531. error = err.detail;
  532. return null;
  533. });
  534. if (error) {
  535. throw error;
  536. }
  537. return res;
  538. };
  539. export const createAPIKey = async (token: string) => {
  540. let error = null;
  541. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, {
  542. method: 'POST',
  543. headers: {
  544. 'Content-Type': 'application/json',
  545. Authorization: `Bearer ${token}`
  546. }
  547. })
  548. .then(async (res) => {
  549. if (!res.ok) throw await res.json();
  550. return res.json();
  551. })
  552. .catch((err) => {
  553. console.error(err);
  554. error = err.detail;
  555. return null;
  556. });
  557. if (error) {
  558. throw error;
  559. }
  560. return res.api_key;
  561. };
  562. export const getAPIKey = async (token: string) => {
  563. let error = null;
  564. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, {
  565. method: 'GET',
  566. headers: {
  567. 'Content-Type': 'application/json',
  568. Authorization: `Bearer ${token}`
  569. }
  570. })
  571. .then(async (res) => {
  572. if (!res.ok) throw await res.json();
  573. return res.json();
  574. })
  575. .catch((err) => {
  576. console.error(err);
  577. error = err.detail;
  578. return null;
  579. });
  580. if (error) {
  581. throw error;
  582. }
  583. return res.api_key;
  584. };
  585. export const deleteAPIKey = async (token: string) => {
  586. let error = null;
  587. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, {
  588. method: 'DELETE',
  589. headers: {
  590. 'Content-Type': 'application/json',
  591. Authorization: `Bearer ${token}`
  592. }
  593. })
  594. .then(async (res) => {
  595. if (!res.ok) throw await res.json();
  596. return res.json();
  597. })
  598. .catch((err) => {
  599. console.error(err);
  600. error = err.detail;
  601. return null;
  602. });
  603. if (error) {
  604. throw error;
  605. }
  606. return res;
  607. };