File size: 3,826 Bytes
7a5aa35 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import { serve } from "bun";
import packages from "../database/packages.json";
import programs from "../database/programs.json";
serve({
port: 3000,
fetch(req) {
const url = new URL(req.url);
const pathname = url.pathname;
const q = url.searchParams.get("q")?.trim().toLowerCase();
const filter = url.searchParams.get("filter")?.trim().toLowerCase();
const corsHeaders = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
};
// CORS preflight handling
if (req.method === "OPTIONS") {
return new Response(null, {
status: 204,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
},
});
}
if (pathname === "/api/searchPackages") {
const results = packages.filter(
({ name, full_name, description, topics }) => {
if (
filter &&
!topics?.some((t: string) => t.toLowerCase() === filter)
)
return false;
if (!q) return true;
return [name, full_name, description, ...(topics || [])].some(
(field: string) => field?.toLowerCase().includes(q),
);
},
);
return new Response(JSON.stringify(results.slice(0, 25)), {
status: 200,
headers: corsHeaders,
});
}
if (pathname === "/api/searchProgams") {
const results = programs.filter(({ full_name, description, topics }) => {
if (filter && !topics?.some((t: string) => t.toLowerCase() === filter))
return false;
if (!q) return true;
return [full_name, description, ...(topics || [])].some(
(field: string) => field?.toLowerCase().includes(q),
);
});
return new Response(JSON.stringify(results.slice(0, 25)), {
status: 200,
headers: corsHeaders,
});
}
if (pathname === "/api/infiniteScrollPackages") {
const pageNumberParam = url.searchParams.get("pageNumber");
const pageNumber = parseInt(pageNumberParam || "0", 10);
if (isNaN(pageNumber) || pageNumber < 0) {
return new Response(JSON.stringify({ error: "Invalid page number" }), {
status: 400,
headers: corsHeaders,
});
}
const lowerLimit = pageNumber * 10;
const scrollResults = packages.slice(lowerLimit, lowerLimit + 10);
return new Response(JSON.stringify(scrollResults), {
status: 200,
headers: corsHeaders,
});
}
if (pathname === "/api/infiniteScrollPrograms") {
const section = url.searchParams.get("section");
const [ll, ul] = (url.searchParams.get("range") || "")
.split("..")
.map(Number);
if (!section || isNaN(ll) || isNaN(ul)) {
return new Response(JSON.stringify([]), {
status: 400,
headers: corsHeaders,
});
}
const getSortedResponse = (sortFn: Function) =>
new Response(JSON.stringify([...programs].sort(sortFn).slice(ll, ul)), {
status: 200,
headers: corsHeaders,
});
if (section === "mostUsed") {
return getSortedResponse(
(a: any, b: any) => b.stargazers_count - a.stargazers_count,
);
}
if (section === "latestRepos") {
return getSortedResponse(
(a: any, b: any) =>
new Date(b.created_at).getTime() - new Date(a.created_at).getTime(),
);
}
return new Response(JSON.stringify([]), {
status: 400,
headers: corsHeaders,
});
}
return new Response("Not Found", {
status: 404,
headers: { "Access-Control-Allow-Origin": "*" },
});
},
});
|