From 1ef3c5a0eb4a37eb22fb31cabb7b541dc26bd356 Mon Sep 17 00:00:00 2001 From: Bram Suurd Date: Wed, 6 Nov 2024 17:24:34 +0100 Subject: [PATCH] Fetch categories and scripts from external sources, updating `GET` endpoint to aggregate data. Adjust type definitions for Script and Category --- frontend/src/app/api/categories/route.ts | 39 ++++++++--- frontend/src/lib/types.ts | 87 +++++++++++------------- 2 files changed, 67 insertions(+), 59 deletions(-) diff --git a/frontend/src/app/api/categories/route.ts b/frontend/src/app/api/categories/route.ts index 6b6368fb..66f10acc 100644 --- a/frontend/src/app/api/categories/route.ts +++ b/frontend/src/app/api/categories/route.ts @@ -1,23 +1,42 @@ -import { pb } from "@/lib/pocketbase"; -import { Category } from "@/lib/types"; +import { Category, Script } from "@/lib/types"; import { NextResponse } from "next/server"; export const dynamic = "force-static"; +const fetchCategories = async () => { + const response = await fetch("https://raw.githubusercontent.com/community-scripts/ProxmoxVE/refs/heads/main/json/metadata.json"); + const data = await response.json(); + return data.categories; +} + +const fetchAllMetaDataFiles = async () => { + const response = await fetch( + "https://api.github.com/repos/community-scripts/ProxmoxVE/contents/json", + ); + const files = await response.json(); + const scripts: Script[] = []; + for (const file of files) { + const response = await fetch(file.download_url); + console.log(file.name); + const script = await response.json(); + scripts.push(script); + } + return scripts; +} + export async function GET() { try { - const response = await pb.collection("categories").getFullList({ - expand: "items.alerts,items.alpine_script,items.default_login", - sort: "order", - }); - - return NextResponse.json(response); + const categories: Category[] = await fetchCategories(); + const scripts: Script[] = await fetchAllMetaDataFiles(); + for (const category of categories) { + category.scripts = scripts.filter((script) => script.categories.includes(category.id)); + } + return NextResponse.json(categories); } catch (error) { - console.error("Error fetching categories:", error); + console.error(error as Error); return NextResponse.json( { error: "Failed to fetch categories" }, { status: 500 }, ); } } - diff --git a/frontend/src/lib/types.ts b/frontend/src/lib/types.ts index b65d5a0a..3ce26cd1 100644 --- a/frontend/src/lib/types.ts +++ b/frontend/src/lib/types.ts @@ -1,55 +1,44 @@ -// these are all the interfaces that are used in the site. these all come from the pocketbase database - -export interface Script { - title: string; - description: string; - documentation: string; - website: string; - logo: string; - created: string; - updated: string; - id: string; - item_type: string; - interface: string; - installCommand: string; - port: number; - post_install: string; - default_cpu: string; - default_hdd: string; - default_ram: string; - isUpdateable: boolean; - isMostViewed: boolean; +export type Script = { + name: string; + slug: string; + categories: number[]; + date_created: string; + type: "vm" | "ct" | "misc"; + updateable: boolean; privileged: boolean; - alpineScript: alpine_script; - expand: { - alpine_script: alpine_script; - alerts: alerts[]; - default_login: default_login; + interface_port: number | null; + documentation: string | null; + website: string | null; + logo: string | null; + description: string; + install_methods: { + type: "default" | "alpine"; + script: string; + resources: { + cpu: number | null; + ram: number | null; + hdd: number | null; + os: string | null; + version: number | null; + }; + }[]; + default_credentials: { + username: string | null; + password: string | null; }; + notes: [{ + text: string; + type: string; + }] } -export interface Category { - catagoryName: string; - categoryId: string; - id: string; - created: string; - expand: { - items: Script[]; - }; +export type Category = { + name: string; + id: number; + sort_order: number; + scripts: Script[]; } -interface alpine_script { - installCommand: string; - default_cpu: string; - default_hdd: string; - default_ram: string; -} - -interface alerts { - content: string; -} - -interface default_login { - username: string; - password: string; -} +export type ScriptList = { + categories: Category[]; +} \ No newline at end of file