index.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const getSessionUser = async (token: string) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/`, {
  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 userSignIn = async (email: string, password: string) => {
  26. let error = null;
  27. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signin`, {
  28. method: 'POST',
  29. headers: {
  30. 'Content-Type': 'application/json'
  31. },
  32. body: JSON.stringify({
  33. email: email,
  34. password: password
  35. })
  36. })
  37. .then(async (res) => {
  38. if (!res.ok) throw await res.json();
  39. return res.json();
  40. })
  41. .catch((err) => {
  42. console.log(err);
  43. error = err.detail;
  44. return null;
  45. });
  46. if (error) {
  47. throw error;
  48. }
  49. return res;
  50. };
  51. export const userSignUp = async (
  52. name: string,
  53. email: string,
  54. password: string,
  55. profile_image_url: string
  56. ) => {
  57. let error = null;
  58. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup`, {
  59. method: 'POST',
  60. headers: {
  61. 'Content-Type': 'application/json'
  62. },
  63. body: JSON.stringify({
  64. name: name,
  65. email: email,
  66. password: password,
  67. profile_image_url: profile_image_url
  68. })
  69. })
  70. .then(async (res) => {
  71. if (!res.ok) throw await res.json();
  72. return res.json();
  73. })
  74. .catch((err) => {
  75. console.log(err);
  76. error = err.detail;
  77. return null;
  78. });
  79. if (error) {
  80. throw error;
  81. }
  82. return res;
  83. };
  84. export const updateUserProfile = async (token: string, name: string, profileImageUrl: string) => {
  85. let error = null;
  86. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/update/profile`, {
  87. method: 'POST',
  88. headers: {
  89. 'Content-Type': 'application/json',
  90. ...(token && { authorization: `Bearer ${token}` })
  91. },
  92. body: JSON.stringify({
  93. name: name,
  94. profile_image_url: profileImageUrl
  95. })
  96. })
  97. .then(async (res) => {
  98. if (!res.ok) throw await res.json();
  99. return res.json();
  100. })
  101. .catch((err) => {
  102. console.log(err);
  103. error = err.detail;
  104. return null;
  105. });
  106. if (error) {
  107. throw error;
  108. }
  109. return res;
  110. };
  111. export const updateUserPassword = async (token: string, password: string, newPassword: string) => {
  112. let error = null;
  113. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/update/password`, {
  114. method: 'POST',
  115. headers: {
  116. 'Content-Type': 'application/json',
  117. ...(token && { authorization: `Bearer ${token}` })
  118. },
  119. body: JSON.stringify({
  120. password: password,
  121. new_password: newPassword
  122. })
  123. })
  124. .then(async (res) => {
  125. if (!res.ok) throw await res.json();
  126. return res.json();
  127. })
  128. .catch((err) => {
  129. console.log(err);
  130. error = err.detail;
  131. return null;
  132. });
  133. if (error) {
  134. throw error;
  135. }
  136. return res;
  137. };
  138. export const getSignUpEnabledStatus = async (token: string) => {
  139. let error = null;
  140. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/enabled`, {
  141. method: 'GET',
  142. headers: {
  143. 'Content-Type': 'application/json',
  144. Authorization: `Bearer ${token}`
  145. }
  146. })
  147. .then(async (res) => {
  148. if (!res.ok) throw await res.json();
  149. return res.json();
  150. })
  151. .catch((err) => {
  152. console.log(err);
  153. error = err.detail;
  154. return null;
  155. });
  156. if (error) {
  157. throw error;
  158. }
  159. return res;
  160. };
  161. export const getDefaultUserRole = async (token: string) => {
  162. let error = null;
  163. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/user/role`, {
  164. method: 'GET',
  165. headers: {
  166. 'Content-Type': 'application/json',
  167. Authorization: `Bearer ${token}`
  168. }
  169. })
  170. .then(async (res) => {
  171. if (!res.ok) throw await res.json();
  172. return res.json();
  173. })
  174. .catch((err) => {
  175. console.log(err);
  176. error = err.detail;
  177. return null;
  178. });
  179. if (error) {
  180. throw error;
  181. }
  182. return res;
  183. };
  184. export const updateDefaultUserRole = async (token: string, role: string) => {
  185. let error = null;
  186. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/user/role`, {
  187. method: 'POST',
  188. headers: {
  189. 'Content-Type': 'application/json',
  190. Authorization: `Bearer ${token}`
  191. },
  192. body: JSON.stringify({
  193. role: role
  194. })
  195. })
  196. .then(async (res) => {
  197. if (!res.ok) throw await res.json();
  198. return res.json();
  199. })
  200. .catch((err) => {
  201. console.log(err);
  202. error = err.detail;
  203. return null;
  204. });
  205. if (error) {
  206. throw error;
  207. }
  208. return res;
  209. };
  210. export const toggleSignUpEnabledStatus = async (token: string) => {
  211. let error = null;
  212. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/enabled/toggle`, {
  213. method: 'GET',
  214. headers: {
  215. 'Content-Type': 'application/json',
  216. Authorization: `Bearer ${token}`
  217. }
  218. })
  219. .then(async (res) => {
  220. if (!res.ok) throw await res.json();
  221. return res.json();
  222. })
  223. .catch((err) => {
  224. console.log(err);
  225. error = err.detail;
  226. return null;
  227. });
  228. if (error) {
  229. throw error;
  230. }
  231. return res;
  232. };
  233. export const getJWTExpiresDuration = async (token: string) => {
  234. let error = null;
  235. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/token/expires`, {
  236. method: 'GET',
  237. headers: {
  238. 'Content-Type': 'application/json',
  239. Authorization: `Bearer ${token}`
  240. }
  241. })
  242. .then(async (res) => {
  243. if (!res.ok) throw await res.json();
  244. return res.json();
  245. })
  246. .catch((err) => {
  247. console.log(err);
  248. error = err.detail;
  249. return null;
  250. });
  251. if (error) {
  252. throw error;
  253. }
  254. return res;
  255. };
  256. export const updateJWTExpiresDuration = async (token: string, duration: string) => {
  257. let error = null;
  258. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/token/expires/update`, {
  259. method: 'POST',
  260. headers: {
  261. 'Content-Type': 'application/json',
  262. Authorization: `Bearer ${token}`
  263. },
  264. body: JSON.stringify({
  265. duration: duration
  266. })
  267. })
  268. .then(async (res) => {
  269. if (!res.ok) throw await res.json();
  270. return res.json();
  271. })
  272. .catch((err) => {
  273. console.log(err);
  274. error = err.detail;
  275. return null;
  276. });
  277. if (error) {
  278. throw error;
  279. }
  280. return res;
  281. };
  282. export const createAPIKey = async (token: string) => {
  283. let error = null;
  284. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, {
  285. method: 'POST',
  286. headers: {
  287. 'Content-Type': 'application/json',
  288. Authorization: `Bearer ${token}`
  289. }
  290. })
  291. .then(async (res) => {
  292. if (!res.ok) throw await res.json();
  293. return res.json();
  294. })
  295. .catch((err) => {
  296. console.log(err);
  297. error = err.detail;
  298. return null;
  299. });
  300. if (error) {
  301. throw error;
  302. }
  303. return res.api_key;
  304. };
  305. export const getAPIKey = async (token: string) => {
  306. let error = null;
  307. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, {
  308. method: 'GET',
  309. headers: {
  310. 'Content-Type': 'application/json',
  311. Authorization: `Bearer ${token}`
  312. }
  313. })
  314. .then(async (res) => {
  315. if (!res.ok) throw await res.json();
  316. return res.json();
  317. })
  318. .catch((err) => {
  319. console.log(err);
  320. error = err.detail;
  321. return null;
  322. });
  323. if (error) {
  324. throw error;
  325. }
  326. return res.api_key;
  327. };
  328. export const deleteAPIKey = async (token: string) => {
  329. let error = null;
  330. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, {
  331. method: 'DELETE',
  332. headers: {
  333. 'Content-Type': 'application/json',
  334. Authorization: `Bearer ${token}`
  335. }
  336. })
  337. .then(async (res) => {
  338. if (!res.ok) throw await res.json();
  339. return res.json();
  340. })
  341. .catch((err) => {
  342. console.log(err);
  343. error = err.detail;
  344. return null;
  345. });
  346. if (error) {
  347. throw error;
  348. }
  349. return res;
  350. };