api / src /index.ts
RohanVashisht's picture
Update src/index.ts
9050040 verified
raw
history blame
7.02 kB
import { serve } from "bun";
import { type Repo } from "./types.ts";
const packagesUrls = [
"https://raw.githubusercontent.com/Zigistry/database/refs/heads/main/database/packages.json",
];
const programsUrl =
"https://raw.githubusercontent.com/Zigistry/database/refs/heads/main/database/programs.json";
let packages: Repo[] = [];
let programs: Repo[] = [];
async function loadData() {
const packagesFile = await Promise.all(packagesUrls.map((url) => fetch(url)));
packages = (await Promise.all(packagesFile.map((file) => file.json())))
.flat() as Repo[];
const programsFile = await fetch(programsUrl);
programs = (await programsFile.json()) as Repo[];
console.log("Data loaded");
}
await loadData();
// Refresh the data every 30 minutes
setInterval(loadData, 60 * 60 * 500);
// --- CORS Headers ---
const corsHeaders: Record<string, string> = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
};
// --- Sorting Helpers ---
const sortByDate = (a: Repo, b: Repo) =>
new Date(b.created_at ?? "").getTime() - new Date(a.created_at ?? "").getTime();
const sortByUsage = (a: Repo, b: Repo) =>
(b.stargazers_count ?? 0) - (a.stargazers_count ?? 0);
// --- Pre-sorted Data ---
function getSorted() {
const packagesArray = packages as Repo[];
const programsArray = programs as Repo[];
return {
packages: {
latest: [...packagesArray].sort(sortByDate),
mostUsed: [...packagesArray].sort(sortByUsage),
},
programs: {
latest: [...programsArray].sort(sortByDate),
mostUsed: [...programsArray].sort(sortByUsage),
},
};
}
// --- Filtering ---
function filterItems(
items: Repo[],
q: string | null,
filter: string | null
): Repo[] {
return items.filter(({ name, full_name, description, topics, readme_content }) => {
if (filter && !topics?.some((t) => t.toLowerCase() === filter)) return false;
if (!q) return true;
const lowerQ = q.toLowerCase();
return [name, full_name, description, ...(topics ?? []), readme_content].some((field) =>
field?.toLowerCase().includes(lowerQ)
);
});
}
// --- Pagination ---
function getPaginated(items: Repo[], page = 0, size = 10): Repo[] {
const start = page * size;
return items.slice(start, start + size);
}
// --- Find by Owner/Repo ---
function findItem(items: Repo[], owner: string, repo: string): Repo | undefined {
return items.find(
({ full_name }) => full_name?.toLowerCase() === `${owner}/${repo}`
);
}
// --- Parse Range ---
function parseRange(str: string | null, max: number): [number, number] {
const match = str?.match(/^(\d+)\.\.(\d+)$/);
const [start, end] = match
? [parseInt(match[1] ?? "0", 10), parseInt(match[2] ?? "10", 10)]
: [0, 10];
return [Math.max(0, start), Math.min(max, end)];
}
// --- Server ---
serve({
port: 7860,
async fetch(req) {
const url = new URL(req.url);
const { pathname, searchParams } = url;
const q = searchParams.get("q")?.trim().toLowerCase() ?? null;
const filter = searchParams.get("filter")?.trim().toLowerCase() ?? null;
const sorted = getSorted();
// Handle CORS preflight
if (req.method === "OPTIONS") {
return new Response(null, {
status: 204,
headers: {
...corsHeaders,
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
},
});
}
// Search endpoints
if (pathname === "/api/searchPackages") {
const result = filterItems(packages, q, filter).slice(0, 25);
return Response.json(result, { headers: corsHeaders });
}
if (pathname === "/api/searchPrograms") {
const result = filterItems(programs, q, filter).slice(0, 25);
return Response.json(result, { headers: corsHeaders });
}
// Infinite scroll endpoints
if (pathname === "/api/infiniteScrollPackages") {
const page = parseInt(searchParams.get("pageNumber") || "0", 10);
if (isNaN(page) || page < 0)
return Response.json(
{ error: "Invalid page number" },
{ status: 400, headers: corsHeaders }
);
return Response.json(getPaginated(packages, page), {
headers: corsHeaders,
});
}
if (pathname === "/api/infiniteScrollPrograms") {
const page = parseInt(searchParams.get("pageNumber") || "0", 10);
if (isNaN(page) || page < 0)
return Response.json(
{ error: "Invalid page number" },
{ status: 400, headers: corsHeaders }
);
return Response.json(getPaginated(programs, page), {
headers: corsHeaders,
});
}
// Get single program/package by owner/repo
const programMatch = pathname.match(/^\/api\/programs\/([^/]+)\/([^/]+)$/);
if (programMatch) {
const owner = programMatch[1]?.toLowerCase() ?? "";
const repo = programMatch[2]?.toLowerCase() ?? "";
const found = findItem(programs, owner, repo);
return Response.json(found || { error: "Program not found" }, {
status: found ? 200 : 404,
headers: corsHeaders,
});
}
const packageMatch = pathname.match(/^\/api\/packages\/([^/]+)\/([^/]+)$/);
if (packageMatch) {
const owner = packageMatch[1]?.toLowerCase() ?? "";
const repo = packageMatch[2]?.toLowerCase() ?? "";
const found = findItem(packages, owner, repo);
return Response.json(found || { error: "Package not found" }, {
status: found ? 200 : 404,
headers: corsHeaders,
});
}
// Index details endpoints
if (pathname === "/api/indexDetailsPackages") {
const section = searchParams.get("section");
if (section !== "latestRepos" && section !== "mostUsed") {
return Response.json(
{ error: "Invalid section" },
{ status: 400, headers: corsHeaders }
);
}
const sortKey = section === "latestRepos" ? "latest" : "mostUsed";
const data = sorted.packages[sortKey as keyof typeof sorted.packages] ?? packages;
const [start, end] = parseRange(searchParams.get("range"), data.length);
return Response.json(data.slice(start, end), { headers: corsHeaders });
}
if (pathname === "/api/indexDetailsPrograms") {
const section = searchParams.get("section");
if (section !== "latestRepos" && section !== "mostUsed") {
return Response.json(
{ error: "Invalid section" },
{ status: 400, headers: corsHeaders }
);
}
const sortKey = section === "latestRepos" ? "latest" : "mostUsed";
const data = sorted.programs[sortKey as keyof typeof sorted.programs] ?? programs;
const [start, end] = parseRange(searchParams.get("range"), data.length);
return Response.json(data.slice(start, end), { headers: corsHeaders });
}
// Not found
return new Response("Not Found", {
status: 404,
headers: corsHeaders,
});
},
});
console.log("Server running on http://localhost:7860");