89 lines
2.8 KiB
TypeScript
89 lines
2.8 KiB
TypeScript
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
|
|
|
|
const corsHeaders = {
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type",
|
|
};
|
|
|
|
const MODRINTH_BASE = "https://api.modrinth.com/v2";
|
|
|
|
serve(async (req) => {
|
|
if (req.method === "OPTIONS") {
|
|
return new Response(null, { headers: corsHeaders });
|
|
}
|
|
|
|
try {
|
|
const apiKey = Deno.env.get("MODRINTH_API_KEY");
|
|
const url = new URL(req.url);
|
|
const action = url.searchParams.get("action");
|
|
|
|
const headers: Record<string, string> = {
|
|
"User-Agent": "FXCraft/1.0",
|
|
};
|
|
if (apiKey) {
|
|
headers["Authorization"] = apiKey;
|
|
}
|
|
|
|
let modrinthUrl = "";
|
|
|
|
switch (action) {
|
|
case "user_projects": {
|
|
const username = url.searchParams.get("username") || "fxfelixzero";
|
|
modrinthUrl = `${MODRINTH_BASE}/user/${username}/projects`;
|
|
break;
|
|
}
|
|
case "search": {
|
|
const query = url.searchParams.get("query") || "";
|
|
const facets = url.searchParams.get("facets") || "";
|
|
const offset = url.searchParams.get("offset") || "0";
|
|
const limit = url.searchParams.get("limit") || "20";
|
|
modrinthUrl = `${MODRINTH_BASE}/search?query=${encodeURIComponent(query)}&offset=${offset}&limit=${limit}`;
|
|
if (facets) {
|
|
modrinthUrl += `&facets=${encodeURIComponent(facets)}`;
|
|
}
|
|
break;
|
|
}
|
|
case "project": {
|
|
const id = url.searchParams.get("id");
|
|
if (!id) {
|
|
return new Response(JSON.stringify({ error: "Missing project id" }), {
|
|
status: 400,
|
|
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
modrinthUrl = `${MODRINTH_BASE}/project/${id}`;
|
|
break;
|
|
}
|
|
case "versions": {
|
|
const id = url.searchParams.get("id");
|
|
if (!id) {
|
|
return new Response(JSON.stringify({ error: "Missing project id" }), {
|
|
status: 400,
|
|
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
modrinthUrl = `${MODRINTH_BASE}/project/${id}/version`;
|
|
break;
|
|
}
|
|
default:
|
|
return new Response(JSON.stringify({ error: "Invalid action" }), {
|
|
status: 400,
|
|
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
|
|
const response = await fetch(modrinthUrl, { headers });
|
|
const data = await response.json();
|
|
|
|
return new Response(JSON.stringify(data), {
|
|
status: response.status,
|
|
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
|
});
|
|
} catch (error) {
|
|
return new Response(JSON.stringify({ error: error.message }), {
|
|
status: 500,
|
|
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
});
|