Files
wehub-resource-sync e30e75b5d4
Changesets / Create Version PR (push) Has been cancelled
Deploy Shadcn Registry / Deploy Production (push) Has been cancelled
Template Metrics / LOC + Bundle Size (push) Has been cancelled
Code Quality / Oxlint + Oxfmt (push) Has been cancelled
Code Quality / Template Sync (push) Has been cancelled
Code Quality / Build Changed Packages (push) Has been cancelled
Code Quality / Test Changed Packages (push) Has been cancelled
Deploy Expo Example / Deploy Production (push) Has been cancelled
Deploy Ink Example / Deploy Production (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.12) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.12) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:40:13 +08:00

60 lines
1.5 KiB
TypeScript

import "server-only";
const NPM_BASE = "https://api.npmjs.org";
export const NPM_REVALIDATE = {
WARM: 3600,
COOL: 21_600,
} as const;
export type NpmDailyDownloads = { day: string; downloads: number };
async function npmGetJson(path: string, revalidate: number): Promise<unknown> {
try {
const res = await fetch(
`${NPM_BASE}${path}`,
revalidate === 0 ? { cache: "no-store" } : { next: { revalidate } },
);
if (!res.ok) return null;
return await res.json();
} catch {
return null;
}
}
async function npmFetch(
path: string,
revalidate: number,
): Promise<NpmDailyDownloads[]> {
const data = (await npmGetJson(path, revalidate)) as {
downloads?: NpmDailyDownloads[];
} | null;
return data?.downloads ?? [];
}
export function getDownloadsRange(
pkg: string,
startDate: string,
endDate: string,
revalidate: number = NPM_REVALIDATE.WARM,
): Promise<NpmDailyDownloads[]> {
return npmFetch(
`/downloads/range/${startDate}:${endDate}/${pkg}`,
revalidate,
);
}
// The flagship package; its last-week downloads stand in for the headline figure.
export const FLAGSHIP_PACKAGE = "@assistant-ui/react";
export async function getWeeklyDownloads(
pkg: string = FLAGSHIP_PACKAGE,
revalidate: number = NPM_REVALIDATE.COOL,
): Promise<number | null> {
const data = (await npmGetJson(
`/downloads/point/last-week/${pkg}`,
revalidate,
)) as { downloads?: number } | null;
return typeof data?.downloads === "number" ? data.downloads : null;
}