api.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import axios from "axios";
  2. import { apiUrl } from "@/env";
  3. import type { IUserProfile, IUserProfileUpdate, IUserProfileCreate, Video, VideoCreate} from "@/interfaces";
  4. function authHeaders(token: string) {
  5. return {
  6. headers: {
  7. Authorization: `Bearer ${token}`,
  8. },
  9. };
  10. }
  11. export const api = {
  12. async logInGetToken(username: string, password: string) {
  13. const params = new URLSearchParams();
  14. params.append("username", username);
  15. params.append("password", password);
  16. return axios.post(`${apiUrl}/api/v1/login/access-token`, params);
  17. },
  18. async getMe(token: string) {
  19. return axios.get<IUserProfile>(`${apiUrl}/api/v1/users/me`, authHeaders(token));
  20. },
  21. async updateMe(token: string, data: IUserProfileUpdate) {
  22. return axios.put<IUserProfile>(
  23. `${apiUrl}/api/v1/users/me`,
  24. data,
  25. authHeaders(token),
  26. );
  27. },
  28. async getUsers(token: string) {
  29. return axios.get<IUserProfile[]>(`${apiUrl}/api/v1/users/`, authHeaders(token));
  30. },
  31. async updateUser(token: string, userId: number, data: IUserProfileUpdate) {
  32. return axios.put(`${apiUrl}/api/v1/users/${userId}`, data, authHeaders(token));
  33. },
  34. async createUser(token: string, data: IUserProfileCreate) {
  35. return axios.post(`${apiUrl}/api/v1/users/create`, data, authHeaders(token));
  36. },
  37. async passwordRecovery(email: string) {
  38. return axios.post(`${apiUrl}/api/v1/password-recovery/${email}`);
  39. },
  40. async resetPassword(token: string, password: string) {
  41. return axios.post(`${apiUrl}/api/v1/reset-password/`, {
  42. token: token,
  43. new_password: password,
  44. });
  45. },
  46. async registerUser(data: IUserProfileCreate) {
  47. return axios.post(`${apiUrl}/api/v1/users/open`, data);
  48. },
  49. async testCeleryMsg(token: string, data:{msg: string}){
  50. return axios.post<{msg:string}>(`${apiUrl}/api/v1/utils/test-celery/msg`, data, authHeaders(token));
  51. },
  52. async testCeleryFile(token: string, file: File){
  53. const formData = new FormData();
  54. formData.append("file", file)
  55. return axios.post<{msg:string}>(`${apiUrl}/api/v1/utils/test-celery/file`, formData, authHeaders(token));
  56. },
  57. async uploadPlot(token: string, video_data:VideoCreate, file: File){
  58. const formData = new FormData();
  59. formData.append("title", video_data.title)
  60. formData.append("anchor_id", video_data.anchor_id.toString())
  61. formData.append("lang_id", video_data.lang_id.toString())
  62. formData.append("upload_file", file)
  63. return axios.post<{msg:string}>(`${apiUrl}/api/v1/videos/`, formData, authHeaders(token));
  64. },
  65. async getVideos(token: string) {
  66. return axios.get<Video[]>(`${apiUrl}/api/v1/videos/`, authHeaders(token));
  67. }
  68. };