Fetch categories and scripts from external sources, updating GET endpoint to aggregate data. Adjust type definitions for Script and Category

This commit is contained in:
Bram Suurd 2024-11-06 17:24:34 +01:00
parent 6c6ffc17ab
commit 1ef3c5a0eb
2 changed files with 67 additions and 59 deletions

View File

@ -1,23 +1,42 @@
import { pb } from "@/lib/pocketbase"; import { Category, Script } from "@/lib/types";
import { Category } from "@/lib/types";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
export const dynamic = "force-static"; 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() { export async function GET() {
try { try {
const response = await pb.collection("categories").getFullList<Category>({ const categories: Category[] = await fetchCategories();
expand: "items.alerts,items.alpine_script,items.default_login", const scripts: Script[] = await fetchAllMetaDataFiles();
sort: "order", for (const category of categories) {
}); category.scripts = scripts.filter((script) => script.categories.includes(category.id));
}
return NextResponse.json(response); return NextResponse.json(categories);
} catch (error) { } catch (error) {
console.error("Error fetching categories:", error); console.error(error as Error);
return NextResponse.json( return NextResponse.json(
{ error: "Failed to fetch categories" }, { error: "Failed to fetch categories" },
{ status: 500 }, { status: 500 },
); );
} }
} }

View File

@ -1,55 +1,44 @@
// these are all the interfaces that are used in the site. these all come from the pocketbase database export type Script = {
name: string;
export interface Script { slug: string;
title: string; categories: number[];
description: string; date_created: string;
documentation: string; type: "vm" | "ct" | "misc";
website: string; updateable: boolean;
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;
privileged: boolean; privileged: boolean;
alpineScript: alpine_script; interface_port: number | null;
expand: { documentation: string | null;
alpine_script: alpine_script; website: string | null;
alerts: alerts[]; logo: string | null;
default_login: default_login; 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 { export type Category = {
catagoryName: string; name: string;
categoryId: string; id: number;
id: string; sort_order: number;
created: string; scripts: Script[];
expand: {
items: Script[];
};
} }
interface alpine_script { export type ScriptList = {
installCommand: string; categories: Category[];
default_cpu: string; }
default_hdd: string;
default_ram: string;
}
interface alerts {
content: string;
}
interface default_login {
username: string;
password: string;
}