import { AxiosError } from "axios"; import { DELETE_USER_AVATAR, GET_CURRENT_USER_STATS, PATCH_USER_AVATAR, PATCH_USER_INFO } from "../constants/api"; import { IHttpResponse } from "../types/common"; import { client } from "./config"; import { UserInfo } from "../../src/domains/User"; async function getUserStatsService(): Promise { const newState: IHttpResponse = { data: null, error: null }; try { const res = await client({ method: 'GET', url: GET_CURRENT_USER_STATS, withCredentials: true}) newState.data = res.data newState.status = res.status return newState } catch(error) { let err = error as AxiosError newState.error = err newState.status = err.status throw(newState) } } async function patchUserAvatarService(form: FormData): Promise { const newState: IHttpResponse = { data: null, error: null}; try { const res = await client({ method: "PATCH", url: PATCH_USER_AVATAR, data: form, withCredentials: true}) newState.data = res.data; newState.status = res.status; return newState; } catch(error) { let err = error as AxiosError; newState.error = err newState.status = err.status throw(newState); } } async function patchUserInfoService(data: UserInfo): Promise { const newState: IHttpResponse = { data: null, error: null}; try { const res = await client({ method: 'PATCH', url: PATCH_USER_INFO, data: data, withCredentials: true}) newState.data = res.data; newState.status = res.status; return newState; } catch(error) { let err = error as AxiosError; newState.error = err; newState.status = err.status; throw(newState); } } async function deleteUserAvatarService(): Promise { const newState: IHttpResponse = { data: null, error: null}; try { const res = await client({ method: 'DELETE', url: DELETE_USER_AVATAR, withCredentials: true}) newState.data = res.data; newState.status = res.status return newState } catch (error) { let err = error as AxiosError; newState.error = err; newState.status = err.status; throw(newState); } } export { getUserStatsService, patchUserAvatarService, deleteUserAvatarService, patchUserInfoService }