Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: created endpoint to get the latest version
Elijah Potter10c2f8854206
diff --git a/packages/web/src/lib/GitHubClient.ts b/packages/web/src/lib/GitHubClient.tsnew file mode 100644index 000000000..03bdcce84--- /dev/null+++ b/packages/web/src/lib/GitHubClient.ts@@ -0,0 +1,45 @@+export class GithubClient {+ /// Map of string -> [content, expiration time]+ private static versionCache: Map<string, [string, number]> = new Map();++ public static async getLatestReleaseFromCache(+ repoOwner: string,+ repoName: string+ ): Promise<string | null> {+ const key = `${repoOwner}/${repoName}`;++ const val = this.versionCache.get(key);++ if (val == null) {+ const updatedValue = await this.getLatestRelease(repoOwner, repoName);+ this.versionCache.set(key, [updatedValue, Date.now() + 3600 * 3000]);+ return updatedValue;+ }++ const [value, expiry] = val;++ if (expiry - Date.now() < 0) {+ this.versionCache.delete(key);+ const updatedValue = await this.getLatestRelease(repoOwner, repoName);+ this.versionCache.set(key, [updatedValue, Date.now() + 3600 * 3000]);+ return updatedValue;+ }++ return value;+ }++ public static async getLatestRelease(repoOwner: string, repoName: string): Promise<string> {+ const resp = await fetch(+ `https://api.github.com/repos/${encodeURIComponent(repoOwner)}/${encodeURIComponent(repoName)}/releases/latest`,+ {+ headers: {+ ['ContentType']: 'application/json'+ }+ }+ );++ const body = await resp.json();++ return body.name;+ }+}diff --git a/packages/web/src/routes/+layout.svelte b/packages/web/src/routes/+layout.svelteindex f87b35e58..fdeeb5e07 100644--- a/packages/web/src/routes/+layout.svelte+++ b/packages/web/src/routes/+layout.svelte@@ -1,5 +1,12 @@<script lang="ts">import '../app.css';+ import GutterCenter from '$lib/GutterCenter.svelte';+ import { DarkMode } from 'flowbite-svelte';</script>-<slot />+<div class="h-16" />+<div class="absolute top-8 right-8"><DarkMode /></div>++<GutterCenter>+ <slot />+</GutterCenter>diff --git a/packages/web/src/routes/latestversion/+server.ts b/packages/web/src/routes/latestversion/+server.tsnew file mode 100644index 000000000..79bb24c8e--- /dev/null+++ b/packages/web/src/routes/latestversion/+server.ts@@ -0,0 +1,17 @@+import { GithubClient } from '$lib/GitHubClient';++export async function GET() {+ const latestVersion = await GithubClient.getLatestReleaseFromCache('elijah-potter', 'harper');++ if (latestVersion == null) {+ throw new Error('Unable to get latest version.');+ }++ console.log(`Received request for latest version. Responding with ${latestVersion}`);++ return new Response(latestVersion, {+ headers: {+ ['Cache-Control']: 'no-cache'+ }+ });+}