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 } 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<Category>({
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 },
);
}
}

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 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[];
}