diff --git a/ui/src/api/index.ts b/ui/src/api/index.ts new file mode 100644 index 0000000..0a2a3a8 --- /dev/null +++ b/ui/src/api/index.ts @@ -0,0 +1,29 @@ +import { query } from "@solidjs/router"; + +const API_BASE_URL = process.env.API_BASE_URL ?? "http://localhost:5013"; + +const buildUrl = (path: string) => + `${API_BASE_URL.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`; + +async function fetchApi(path: string, init?: RequestInit): Promise { + const response = await fetch(buildUrl(path), { + ...init, + headers: { + "content-type": "application/json", + ...(init?.headers ?? {}), + }, + }); + + if (!response.ok) { + const text = await response.text(); + throw new Error(`API ${response.status}: ${text || response.statusText}`); + } + + return (await response.json()) as T; +} + +export const queryApiVersion = query(async () => { + "use server"; + const data = await fetchApi<{ version: string }>("/"); + return data.version; +}, "api-version"); diff --git a/ui/src/routes/about.tsx b/ui/src/routes/about.tsx index ae50cf7..abbefe5 100644 --- a/ui/src/routes/about.tsx +++ b/ui/src/routes/about.tsx @@ -1,18 +1,20 @@ import { Title } from "@solidjs/meta"; +import { createAsync } from "@solidjs/router"; +import { Show } from "solid-js"; import Counter from "~/components/Counter"; +import { queryApiVersion } from "~/api"; export default function About() { + const apiVersion = createAsync(() => queryApiVersion()); + return (
- About Page -

About Page

- + Tietoja

- Visit{" "} - - start.solidjs.com - {" "} - to learn more on SolidStart + API version:{" "} + + {apiVersion()} +

);