But this includes templ builds also... Needed for compilation. Search is the last broken piece, I believe.
63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
import axios from "axios";
|
|
import type { CreateRecipeRequest, CreateRecipeResponse, GetRecipeOfTheWeekResponse, GetRecipeResponse, SearchRecipesResponse } from "../types/api/recipe";
|
|
import type { Recipe } from "../types/recipe";
|
|
import type { ApiError } from "../types/api/error";
|
|
import type { SearchFilters } from "../types/search";
|
|
|
|
|
|
export async function GetRecipeOfTheWeek(): Promise<Recipe | ApiError> {
|
|
const response = await axios.get<GetRecipeOfTheWeekResponse>("http://localhost:3000/v2/api/recipe/of-the-week");
|
|
|
|
if (response.status !== 200 || response.data.recipe === undefined) {
|
|
const err: ApiError = {
|
|
status: response.data.status,
|
|
message: response.data.message
|
|
};
|
|
return err;
|
|
}
|
|
|
|
return response.data.recipe;
|
|
}
|
|
|
|
export async function GetRecipe(id: number): Promise<Recipe | ApiError> {
|
|
const response = await axios.get<GetRecipeResponse>(`http://localhost:3000/v2/api/recipe/${id}`);
|
|
|
|
if (response.status !== 200 || response.data.recipe === undefined) {
|
|
const err: ApiError = {
|
|
status: response.data.status,
|
|
message: response.data.message
|
|
};
|
|
return err;
|
|
}
|
|
|
|
return response.data.recipe;
|
|
}
|
|
|
|
export async function SearchRecipes(filters: SearchFilters): Promise<Recipe[] | ApiError> {
|
|
const response = await axios.post<SearchRecipesResponse>("http://localhost:3000/v2/api/recipe/search", filters);
|
|
|
|
if (response.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 CreateRecipe(data: CreateRecipeRequest): Promise<Recipe | ApiError> {
|
|
const response = await axios.post<CreateRecipeResponse>("http://localhost:3000/v2/api/recipe", data);
|
|
|
|
if (response.status !== 200 || response.data.recipe === undefined) {
|
|
const err: ApiError = {
|
|
status: response.data.status,
|
|
message: response.data.message
|
|
};
|
|
return err;
|
|
}
|
|
|
|
return response.data.recipe;
|
|
}
|