import axios from "axios"; import type { ApiError } from "../types/api/error"; import type { User } from "../types/user"; import type { GetAuthenticateUserEngagementResponse, GetAuthenticateUserFavoritesResponse, GetAuthenticateUserMadeRecipesResponse, GetAuthenticateUserRecipesResponse, GetAuthenticateUserResponse, GetAuthenticateUserViewedRecipesResponse, GetUserResponse } from "../types/api/user"; import type { Recipe } from "../types/recipe"; import type { Engagement } from "../types/engagement"; import { GetBackendUrl } from "./util"; const BACKEND_URL = GetBackendUrl(); export async function GetUser(id: number): Promise { const response = await axios.get(`${BACKEND_URL}/v2/api/user/${id}`); if (response.data.status !== 200 || response.data.user === undefined) { const err: ApiError = { status: response.data.status, message: response.data.message }; return err; } return response.data.user; } export async function GetAuthenticatedUser(): Promise { const response = await axios.get(`${BACKEND_URL}/v2/api/user`); if (response.data.status !== 200 || response.data.user === undefined) { const err: ApiError = { status: response.data.status, message: response.data.message }; return err; } return response.data.user; } export async function GetAuthenticatedUserRecipes(): Promise { const response = await axios.get(`${BACKEND_URL}/v2/api/user/recipes`); if (response.data.status !== 200 || response.data.recipes === undefined) { const err: ApiError = { status: response.data.status, message: response.data.message }; return err; } return response.data.recipes; } export async function GetAuthenticatedUserFavorites(): Promise { const response = await axios.get(`${BACKEND_URL}/v2/api/user/favorites`); if (response.data.status !== 200 || response.data.favorites === undefined) { const err: ApiError = { status: response.data.status, message: response.data.message }; return err; } return response.data.favorites; } export async function GetAuthenticatedUserEngagement(): Promise { const response = await axios.get(`${BACKEND_URL}/v2/api/user/engagement`); if (response.data.status !== 200 || response.data.engagement === undefined) { const err: ApiError = { status: response.data.status, message: response.data.message }; return err; } return response.data.engagement; } export async function GetAuthenticatedUserMadeRecipes(): Promise { const response = await axios.get(`${BACKEND_URL}/v2/api/user/recipes/made`); if (response.data.status !== 200 || response.data.recipes === undefined) { const err: ApiError = { status: response.data.status, message: response.data.message }; return err; } return response.data.recipes; } export async function GetAuthenticateUserViewedRecipes(): Promise { const response = await axios.get(`${BACKEND_URL}/v2/api/user/recipes/viewed`); if (response.data.status !== 200 || response.data.recipes === undefined) { const err: ApiError = { status: response.data.status, message: response.data.message }; return err; } return response.data.recipes; }