Potion/web/src/services/UserService.ts
2025-12-28 22:20:39 -07:00

108 lines
3.5 KiB
TypeScript

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<User | ApiError> {
const response = await axios.get<GetUserResponse>(`${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<User | ApiError> {
const response = await axios.get<GetAuthenticateUserResponse>(`${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<Recipe[] | ApiError> {
const response = await axios.get<GetAuthenticateUserRecipesResponse>(`${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<Recipe[] | ApiError> {
const response = await axios.get<GetAuthenticateUserFavoritesResponse>(`${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<Engagement[] | ApiError> {
const response = await axios.get<GetAuthenticateUserEngagementResponse>(`${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<Recipe[] | ApiError> {
const response = await axios.get<GetAuthenticateUserMadeRecipesResponse>(`${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<Recipe[] | ApiError> {
const response = await axios.get<GetAuthenticateUserViewedRecipesResponse>(`${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;
}