api.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import axios from "axios";
  2. import { apiUrl } from "@/env";
  3. import type { IUserProfile, IUserProfileUpdate, IUserProfileCreate, Video} 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, title:string, file: File){
  58. const formData = new FormData();
  59. formData.append("title", title)
  60. formData.append("upload_file", file)
  61. return axios.post<{msg:string}>(`${apiUrl}/api/v1/videos/`, formData, authHeaders(token));
  62. },
  63. async getVideos(token: string) {
  64. return axios.get<Video[]>(`${apiUrl}/api/v1/videos/`, authHeaders(token));
  65. }
  66. };