index.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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.log(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.log(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.log(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.log(err);
  88. error = err.detail;
  89. return null;
  90. });
  91. if (error) {
  92. throw error;
  93. }
  94. return res;
  95. };
  96. export const userSignIn = async (email: string, password: string) => {
  97. let error = null;
  98. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signin`, {
  99. method: 'POST',
  100. headers: {
  101. 'Content-Type': 'application/json'
  102. },
  103. body: JSON.stringify({
  104. email: email,
  105. password: password
  106. })
  107. })
  108. .then(async (res) => {
  109. if (!res.ok) throw await res.json();
  110. return res.json();
  111. })
  112. .catch((err) => {
  113. console.log(err);
  114. error = err.detail;
  115. return null;
  116. });
  117. if (error) {
  118. throw error;
  119. }
  120. return res;
  121. };
  122. export const userSignUp = async (
  123. name: string,
  124. email: string,
  125. password: string,
  126. profile_image_url: string
  127. ) => {
  128. let error = null;
  129. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup`, {
  130. method: 'POST',
  131. headers: {
  132. 'Content-Type': 'application/json'
  133. },
  134. body: JSON.stringify({
  135. name: name,
  136. email: email,
  137. password: password,
  138. profile_image_url: profile_image_url
  139. })
  140. })
  141. .then(async (res) => {
  142. if (!res.ok) throw await res.json();
  143. return res.json();
  144. })
  145. .catch((err) => {
  146. console.log(err);
  147. error = err.detail;
  148. return null;
  149. });
  150. if (error) {
  151. throw error;
  152. }
  153. return res;
  154. };
  155. export const addUser = async (
  156. token: string,
  157. name: string,
  158. email: string,
  159. password: string,
  160. role: string = 'pending'
  161. ) => {
  162. let error = null;
  163. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/add`, {
  164. method: 'POST',
  165. headers: {
  166. 'Content-Type': 'application/json',
  167. ...(token && { authorization: `Bearer ${token}` })
  168. },
  169. body: JSON.stringify({
  170. name: name,
  171. email: email,
  172. password: password,
  173. role: role
  174. })
  175. })
  176. .then(async (res) => {
  177. if (!res.ok) throw await res.json();
  178. return res.json();
  179. })
  180. .catch((err) => {
  181. console.log(err);
  182. error = err.detail;
  183. return null;
  184. });
  185. if (error) {
  186. throw error;
  187. }
  188. return res;
  189. };
  190. export const updateUserProfile = async (token: string, name: string, profileImageUrl: string) => {
  191. let error = null;
  192. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/update/profile`, {
  193. method: 'POST',
  194. headers: {
  195. 'Content-Type': 'application/json',
  196. ...(token && { authorization: `Bearer ${token}` })
  197. },
  198. body: JSON.stringify({
  199. name: name,
  200. profile_image_url: profileImageUrl
  201. })
  202. })
  203. .then(async (res) => {
  204. if (!res.ok) throw await res.json();
  205. return res.json();
  206. })
  207. .catch((err) => {
  208. console.log(err);
  209. error = err.detail;
  210. return null;
  211. });
  212. if (error) {
  213. throw error;
  214. }
  215. return res;
  216. };
  217. export const updateUserPassword = async (token: string, password: string, newPassword: string) => {
  218. let error = null;
  219. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/update/password`, {
  220. method: 'POST',
  221. headers: {
  222. 'Content-Type': 'application/json',
  223. ...(token && { authorization: `Bearer ${token}` })
  224. },
  225. body: JSON.stringify({
  226. password: password,
  227. new_password: newPassword
  228. })
  229. })
  230. .then(async (res) => {
  231. if (!res.ok) throw await res.json();
  232. return res.json();
  233. })
  234. .catch((err) => {
  235. console.log(err);
  236. error = err.detail;
  237. return null;
  238. });
  239. if (error) {
  240. throw error;
  241. }
  242. return res;
  243. };
  244. export const getSignUpEnabledStatus = async (token: string) => {
  245. let error = null;
  246. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/enabled`, {
  247. method: 'GET',
  248. headers: {
  249. 'Content-Type': 'application/json',
  250. Authorization: `Bearer ${token}`
  251. }
  252. })
  253. .then(async (res) => {
  254. if (!res.ok) throw await res.json();
  255. return res.json();
  256. })
  257. .catch((err) => {
  258. console.log(err);
  259. error = err.detail;
  260. return null;
  261. });
  262. if (error) {
  263. throw error;
  264. }
  265. return res;
  266. };
  267. export const getDefaultUserRole = async (token: string) => {
  268. let error = null;
  269. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/user/role`, {
  270. method: 'GET',
  271. headers: {
  272. 'Content-Type': 'application/json',
  273. Authorization: `Bearer ${token}`
  274. }
  275. })
  276. .then(async (res) => {
  277. if (!res.ok) throw await res.json();
  278. return res.json();
  279. })
  280. .catch((err) => {
  281. console.log(err);
  282. error = err.detail;
  283. return null;
  284. });
  285. if (error) {
  286. throw error;
  287. }
  288. return res;
  289. };
  290. export const updateDefaultUserRole = async (token: string, role: string) => {
  291. let error = null;
  292. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/user/role`, {
  293. method: 'POST',
  294. headers: {
  295. 'Content-Type': 'application/json',
  296. Authorization: `Bearer ${token}`
  297. },
  298. body: JSON.stringify({
  299. role: role
  300. })
  301. })
  302. .then(async (res) => {
  303. if (!res.ok) throw await res.json();
  304. return res.json();
  305. })
  306. .catch((err) => {
  307. console.log(err);
  308. error = err.detail;
  309. return null;
  310. });
  311. if (error) {
  312. throw error;
  313. }
  314. return res;
  315. };
  316. export const toggleSignUpEnabledStatus = async (token: string) => {
  317. let error = null;
  318. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/enabled/toggle`, {
  319. method: 'GET',
  320. headers: {
  321. 'Content-Type': 'application/json',
  322. Authorization: `Bearer ${token}`
  323. }
  324. })
  325. .then(async (res) => {
  326. if (!res.ok) throw await res.json();
  327. return res.json();
  328. })
  329. .catch((err) => {
  330. console.log(err);
  331. error = err.detail;
  332. return null;
  333. });
  334. if (error) {
  335. throw error;
  336. }
  337. return res;
  338. };
  339. export const getJWTExpiresDuration = async (token: string) => {
  340. let error = null;
  341. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/token/expires`, {
  342. method: 'GET',
  343. headers: {
  344. 'Content-Type': 'application/json',
  345. Authorization: `Bearer ${token}`
  346. }
  347. })
  348. .then(async (res) => {
  349. if (!res.ok) throw await res.json();
  350. return res.json();
  351. })
  352. .catch((err) => {
  353. console.log(err);
  354. error = err.detail;
  355. return null;
  356. });
  357. if (error) {
  358. throw error;
  359. }
  360. return res;
  361. };
  362. export const updateJWTExpiresDuration = async (token: string, duration: string) => {
  363. let error = null;
  364. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/token/expires/update`, {
  365. method: 'POST',
  366. headers: {
  367. 'Content-Type': 'application/json',
  368. Authorization: `Bearer ${token}`
  369. },
  370. body: JSON.stringify({
  371. duration: duration
  372. })
  373. })
  374. .then(async (res) => {
  375. if (!res.ok) throw await res.json();
  376. return res.json();
  377. })
  378. .catch((err) => {
  379. console.log(err);
  380. error = err.detail;
  381. return null;
  382. });
  383. if (error) {
  384. throw error;
  385. }
  386. return res;
  387. };
  388. export const createAPIKey = async (token: string) => {
  389. let error = null;
  390. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, {
  391. method: 'POST',
  392. headers: {
  393. 'Content-Type': 'application/json',
  394. Authorization: `Bearer ${token}`
  395. }
  396. })
  397. .then(async (res) => {
  398. if (!res.ok) throw await res.json();
  399. return res.json();
  400. })
  401. .catch((err) => {
  402. console.log(err);
  403. error = err.detail;
  404. return null;
  405. });
  406. if (error) {
  407. throw error;
  408. }
  409. return res.api_key;
  410. };
  411. export const getAPIKey = async (token: string) => {
  412. let error = null;
  413. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, {
  414. method: 'GET',
  415. headers: {
  416. 'Content-Type': 'application/json',
  417. Authorization: `Bearer ${token}`
  418. }
  419. })
  420. .then(async (res) => {
  421. if (!res.ok) throw await res.json();
  422. return res.json();
  423. })
  424. .catch((err) => {
  425. console.log(err);
  426. error = err.detail;
  427. return null;
  428. });
  429. if (error) {
  430. throw error;
  431. }
  432. return res.api_key;
  433. };
  434. export const deleteAPIKey = async (token: string) => {
  435. let error = null;
  436. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, {
  437. method: 'DELETE',
  438. headers: {
  439. 'Content-Type': 'application/json',
  440. Authorization: `Bearer ${token}`
  441. }
  442. })
  443. .then(async (res) => {
  444. if (!res.ok) throw await res.json();
  445. return res.json();
  446. })
  447. .catch((err) => {
  448. console.log(err);
  449. error = err.detail;
  450. return null;
  451. });
  452. if (error) {
  453. throw error;
  454. }
  455. return res;
  456. };