From ae403a6ba0293994a64f55af25b1dec4697afe34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Fri, 21 Jun 2024 00:36:52 +0200 Subject: [PATCH 1/4] feat: agent version in footer --- src/lib/components/footer/main.svelte | 17 +++++++++++- src/lib/functions/api/fetchversion.js | 37 +++++++++++++++++++++++++++ src/routes/+layout.js | 9 +++++++ src/routes/+layout.svelte | 5 +++- src/routes/search/+page.js | 1 - src/routes/search/+page.svelte | 2 -- 6 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 src/lib/functions/api/fetchversion.js create mode 100644 src/routes/+layout.js diff --git a/src/lib/components/footer/main.svelte b/src/lib/components/footer/main.svelte index 4e39d690..dd548b00 100644 --- a/src/lib/components/footer/main.svelte +++ b/src/lib/components/footer/main.svelte @@ -1,10 +1,25 @@ + + diff --git a/src/lib/functions/api/fetchversion.js b/src/lib/functions/api/fetchversion.js new file mode 100644 index 00000000..a809b8ea --- /dev/null +++ b/src/lib/functions/api/fetchversion.js @@ -0,0 +1,37 @@ +import { error } from '@sveltejs/kit'; +import { createApiUrl } from '$lib/functions/api/createurl.js'; + +/** + * @param {typeof fetch} [fetcher] + * @returns {Promise} + */ +export async function fetchVersion(fetcher = fetch) { + /** @type {URL} */ + let apiUrl; + try { + apiUrl = createApiUrl('versionz'); + } catch (/** @type {any} */ err) { + // Internal Server Error + throw error(500, `Failed to create API URL: ${err.message}`); + } + + /** @type {Response} */ + let response; + try { + response = await fetcher(apiUrl, { + method: 'GET', // POST doesn't cache on CDN + headers: { + Accept: 'text/plain', + 'Accept-Encoding': 'gzip, deflate, br' + } + }); + } catch (/** @type {any} */ err) { + // Bad Gateway + throw error(502, `Failed to fetch version: ${err.message}`); + } + + /** @type {string} */ + const version = await response.text(); + + return version; +} diff --git a/src/routes/+layout.js b/src/routes/+layout.js new file mode 100644 index 00000000..ac251094 --- /dev/null +++ b/src/routes/+layout.js @@ -0,0 +1,9 @@ +import { fetchVersion } from '$lib/functions/api/fetchversion'; + +/** @type {import('./$types').LayoutLoad} */ +export async function load({ fetch }) { + const apiVersion = await fetchVersion(fetch); + return { + apiVersion: apiVersion + }; +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index ae311a17..45747691 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -3,6 +3,9 @@ import Preconnect from '$lib/components/preconnect/main.svelte'; import ThemeToggle from '$lib/components/themetoggle/main.svelte'; import Footer from '$lib/components/footer/main.svelte'; + + let { data } = $props(); + const apiVersion = $derived(data.apiVersion); @@ -15,4 +18,4 @@ -