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

This commit is contained in:
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">
import Toast from "./Toast.svelte";
import { dismissToast, toasts } from "$lib/store";
import { dismissToast, toasts } from "$lib/stores";
</script>
{#if $toasts}

View File

@ -1,6 +1,12 @@
import { ToastType, type Toast } from "$lib/toast";
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 addToast = (toast: Toast) => {
@ -25,4 +31,13 @@ export const addToast = (toast: Toast) => {
export const dismissToast = (id: number) => {
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;
};
}