Update test api to TypeScript for better type handling

This commit is contained in:
Michał Gdula 2024-05-02 13:36:24 +01:00
parent 04f7d40e52
commit 4f0ecd33e4
7 changed files with 26 additions and 31 deletions

109
front/src/lib/test-api.ts Normal file
View file

@ -0,0 +1,109 @@
import Items from '%/lib/test-data.js';
let cache = {
announcement_banner: null,
popular_today: null,
};
async function fakeDelay(timeout: number = 1000) {
await new Promise(resolve => setTimeout(resolve, timeout));
}
export async function getAnnouncements() {
if (cache.announcement_banner) {
return cache.announcement_banner;
}
const data = {
image: "/BannerExampleImage.jpg",
};
cache.announcement_banner = data;
await fakeDelay(200)
return data;
}
export async function getPopularToday() {
if (cache.popular_today) {
return cache.popular_today;
}
const data = Items;
cache.popular_today = data;
await fakeDelay(200)
return data;
}
export async function getMenuItems() {
const data = [
{
name: "Main Menu",
items: Items,
},
{
name: "Breakfast",
items: [],
},
{
name: "Seasonal",
items: Items,
},
];
await fakeDelay(20)
return data;
}
export async function getItemsByUUID(items: string[]) {
let data = [];
Items.forEach((itemInDatabase) => {
items.forEach((itemInRequest) => {
if (itemInDatabase.uuid === itemInRequest) {
data.push(itemInDatabase);
}
});
});
await fakeDelay(200)
if (data.length < 0) {
throw new Error("Resource could not be found");
}
return data;
}
export async function getItemByUUID(uuid: string) {
let data = await getItemsByUUID([uuid]);
if (data.length != 1) {
throw new Error("Resource could not be found");
}
return data[0];
}
export async function postContactEmail(name: string, email: string, message: string) {
await fakeDelay(200)
if (!name) {
throw new Error("Namey missing");
}
if (!email) {
throw new Error("Emaily missing");
}
if (!message) {
throw new Error("Message missing");
} else if (message.length < 150) {
throw new Error("Message FUCKED");
}
return "Check your email to confirm the message!";
}