#5 Started on creating page to handle API requests to https://git.luke-else.co.uk

This commit is contained in:
Luke Else 2025-02-06 10:55:26 +00:00
parent 8ab101727e
commit 712d7857db
7 changed files with 78 additions and 7 deletions

26
src/lib/api/git.ts Normal file
View File

@ -0,0 +1,26 @@
import type { GitRepo } from "../types";
const API_BASE_URL = "https://git.luke-else.co.uk/api/v1";
// const ACCESS_TOKEN = import.meta.env.VITE_GITEA_TOKEN;
export async function fetchRepos(): Promise<GitRepo[]> {
try {
console.log("Fetching repos...");
const response = await fetch(`${API_BASE_URL}/repos/search`, {
headers: {
// "Authorization": `token ${ACCESS_TOKEN}`,
"Content-Type": "application/json"
}
});
if (!response.ok) {
throw new Error(`Error: ${response.statusText}`);
}
const data: { data: GitRepo[] } = await response.json();
return data.data; // Extract the list of repositories
} catch (error) {
console.error("Failed to fetch repos:", error);
return [];
}
}

View File

@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import Toast from "./Toast.svelte"; import Toast from "./Toast.svelte";
import { dismissToast, toasts } from "$lib/store"; import { dismissToast, toasts } from "$lib/stores";
</script> </script>
{#if $toasts} {#if $toasts}

View File

@ -1,6 +1,12 @@
import { ToastType, type Toast } from "$lib/toast"; import { ToastType, type Toast } from "$lib/toast";
import { writable, type Writable } from "svelte/store"; import { writable, type Writable } from "svelte/store";
import type { GitRepo } from "./types";
import { fetchRepos } from "./api/git";
////////////////////////////////////////
// Toast Stores
////////////////////////////////////////
export const toasts: Writable<Toast[]> = writable([]); export const toasts: Writable<Toast[]> = writable([]);
export const addToast = (toast: Toast) => { export const addToast = (toast: Toast) => {
@ -26,3 +32,12 @@ export const addToast = (toast: Toast) => {
export const dismissToast = (id: number) => { export const dismissToast = (id: number) => {
toasts.update((all) => all.filter((t) => t.id !== id)); toasts.update((all) => all.filter((t) => t.id !== id));
}; };
////////////////////////////////////////
// Git Repo Stores
////////////////////////////////////////
export const repos = writable<GitRepo[]>([]);
export async function loadRepos() {
repos.set(await fetchRepos());
}

13
src/lib/types.ts Normal file
View File

@ -0,0 +1,13 @@
export interface GitRepo {
id: number;
name: string;
full_name: string;
description: string;
html_url: string;
private: boolean;
fork: boolean;
owner: {
login: string;
avatar_url: string;
};
}

View File

@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import { getJson } from "$lib/data"; import { getJson } from "$lib/data";
import { Toast, ToastType } from "$lib/toast"; import { Toast, ToastType } from "$lib/toast";
import { addToast } from "$lib/store"; import { addToast } from "$lib/stores";
import Skills from './skills.svelte'; import Skills from './skills.svelte';

View File

@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import Card from '$lib/components/Cards/Card.svelte'; import Card from '$lib/components/Cards/Card.svelte';
import { Toast, ToastType } from "$lib/toast"; import { Toast, ToastType } from "$lib/toast";
import { addToast } from "$lib/store"; import { addToast } from "$lib/stores";
import { page } from '$app/stores'; import { page } from '$app/stores';
const sent = $page.url.searchParams.get('sent'); const sent = $page.url.searchParams.get('sent');

View File

@ -1,3 +1,20 @@
<h1>Repos</h1> <script lang="ts">
<p>Stay tuned! This is still in development.</p> import { onMount } from "svelte";
<p>Come back later to find out what projects I'm currently working on!</p> import { repos, loadRepos } from "$lib/stores";
onMount(loadRepos);
</script>
<h1>My Projects</h1>
{#if $repos.length > 0}
<ul>
{#each $repos as repo}
<li>
<a href="{repo.html_url}" target="_blank">{repo.name}</a> - {repo.description}
</li>
{/each}
</ul>
{:else}
<p>Loading repositories...</p>
{/if}