Update test api to TypeScript for better type handling

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

View file

@ -1,5 +1,5 @@
<script>
import { getAnnouncements } from '%/lib/test-api.js';
import { getAnnouncements } from '%/lib/test-api.ts';
let announcement = getAnnouncements();
</script>

View file

@ -1,16 +1,19 @@
import Items from '%/lib/test-data.js';
let cache = {};
let cache = {
announcement_banner: null,
popular_today: null,
};
async function fakeDelay(timeout = 1000) {
async function fakeDelay(timeout: number = 1000) {
await new Promise(resolve => setTimeout(resolve, timeout));
}
export async function getAnnouncements() {
if (cache.announcement_banner !== undefined) {
if (cache.announcement_banner) {
return cache.announcement_banner;
}
@ -24,7 +27,7 @@ export async function getAnnouncements() {
export async function getPopularToday() {
if (cache.popular_today !== undefined) {
if (cache.popular_today) {
return cache.popular_today;
}
@ -55,26 +58,7 @@ export async function getMenuItems() {
}
export async function getItemByUUID(uuid) {
let data;
Items.forEach((item) => {
if (item.uuid === uuid) {
data = item;
}
});
await fakeDelay(200)
if (!data) {
throw new Error("Resource could not be found");
}
return data;
}
export async function getItemsByUUID(items) {
export async function getItemsByUUID(items: string[]) {
let data = [];
Items.forEach((itemInDatabase) => {
@ -95,7 +79,18 @@ export async function getItemsByUUID(items) {
}
export async function postContactEmail(name, email, message) {
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) {

View file

@ -1,7 +1,7 @@
<script>
import { link } from 'svelte-spa-router';
import { getItemsByUUID } from "%/lib/test-api.js";
import { getItemsByUUID } from "%/lib/test-api.ts";
import LoadingBar from "%/components/LoadingBar.svelte";
import Cart from '%/lib/cart.ts';
import MenuList from "%/components/MenuList.svelte";

View file

@ -2,7 +2,7 @@
import { PaperPlaneRight, SealWarning, SealCheck } from "phosphor-svelte";
import DropDown from "%/components/DropDown.svelte";
import { postContactEmail } from "%/lib/test-api.js";
import { postContactEmail } from "%/lib/test-api.ts";
import { expandOnTyping } from "%/lib/utils.js";
const minMessageLength = 150;

View file

@ -4,7 +4,7 @@
import { ArrowUpRight } from "phosphor-svelte";
import L from 'leaflet';
import { getPopularToday } from "%/lib/test-api.js";
import { getPopularToday } from "%/lib/test-api.ts";
import LoadingBar from "%/components/LoadingBar.svelte";
import AnnouncementBanner from "%/components/AnnouncementBanner.svelte";
import MenuList from "%/components/MenuList.svelte";

View file

@ -2,7 +2,7 @@
import { SmileySad } from "phosphor-svelte";
import Cart from "%/lib/cart.ts";
import { getPopularToday, getItemByUUID } from "%/lib/test-api.js";
import { getPopularToday, getItemByUUID } from "%/lib/test-api.ts";
import MenuList from "%/components/MenuList.svelte";
import LoadingBar from "%/components/LoadingBar.svelte";
import LoadingImage from "/MenuItemLoading.svg";

View file

@ -4,7 +4,7 @@
import LoadingBar from "%/components/LoadingBar.svelte";
import MenuList from "%/components/MenuList.svelte";
import DropDown from "%/components/DropDown.svelte";
import { getMenuItems } from "%/lib/test-api.js";
import { getMenuItems } from "%/lib/test-api.ts";
let items = getMenuItems();