mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-09-16 19:45:33 -04:00
103 lines
2.8 KiB
TypeScript
103 lines
2.8 KiB
TypeScript
import { prisma } from "../../lib/helpers/prisma";
|
|
import { getAllNotes } from "../../lib/helpers/parse-notes";
|
|
import { logServerError } from "../../lib/helpers/sentry";
|
|
import { NOTES_DIR } from "../../lib/config/constants";
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import type { PageStats, DetailedPageStats, SiteStats } from "../../types";
|
|
|
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|
try {
|
|
if (req.method !== "GET") {
|
|
// 405 Method Not Allowed
|
|
return res.status(405).end();
|
|
}
|
|
|
|
const { slug } = req.query;
|
|
let data;
|
|
|
|
if (slug) {
|
|
// add one to this page's count and return the new number
|
|
data = await incrementPageHits(slug as string);
|
|
|
|
// disable caching on both ends
|
|
res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");
|
|
} else {
|
|
// return overall site stats if slug not specified
|
|
data = await getSiteStats();
|
|
|
|
// let Vercel edge cache results for 15 mins
|
|
res.setHeader("Cache-Control", "public, max-age=0, s-maxage=900, stale-while-revalidate");
|
|
}
|
|
|
|
// send result as JSON
|
|
return res.status(200).json(data);
|
|
} catch (error) {
|
|
// extract just the error message to send back to client
|
|
const message = error instanceof Error ? error.message : "Unknown error.";
|
|
|
|
// log full error to console and sentry
|
|
await logServerError(error);
|
|
|
|
// 500 Internal Server Error
|
|
return res.status(500).json({ message });
|
|
}
|
|
};
|
|
|
|
const incrementPageHits = async (slug: string): Promise<PageStats> => {
|
|
const { hits } = await prisma.hits.upsert({
|
|
where: { slug },
|
|
create: { slug },
|
|
update: {
|
|
hits: {
|
|
increment: 1,
|
|
},
|
|
},
|
|
});
|
|
|
|
// send client the *new* hit count
|
|
return { hits };
|
|
};
|
|
|
|
const getSiteStats = async (): Promise<SiteStats> => {
|
|
// simultaneously fetch the entire hits db and notes from the filesystem
|
|
const [hits, notes] = await Promise.all([
|
|
prisma.hits.findMany({
|
|
orderBy: [
|
|
{
|
|
hits: "desc",
|
|
},
|
|
],
|
|
}),
|
|
getAllNotes(),
|
|
]);
|
|
|
|
const pages: DetailedPageStats[] = [];
|
|
const total = { hits: 0 };
|
|
|
|
hits.forEach((record) => {
|
|
// match slugs from getAllNotes() with db results to populate some metadata
|
|
// TODO: add support for pages other than notes.
|
|
const match = notes.find((note) => `${NOTES_DIR}/${note.slug}` === record.slug);
|
|
|
|
// don't reveal via API if the db entry doesn't belong to a valid page
|
|
if (!match) {
|
|
return;
|
|
}
|
|
|
|
// merge record with its matching front matter data
|
|
pages.push({
|
|
...record,
|
|
title: match.title,
|
|
url: match.permalink,
|
|
date: match.date,
|
|
});
|
|
|
|
// add these hits to running tally
|
|
total.hits += record.hits;
|
|
});
|
|
|
|
return { total, pages };
|
|
};
|
|
|
|
export default handler;
|