/** * Serves the figures of the client's `/community` page, which * `community-service.ts` recomputes on a timer. * * - `GET /api/community?period=weekly|monthly` — signed in. Answers the * site's figures over the last week (by default) or month. * * @module */ import { Router } from "@oak/oak"; import type { APIResponse } from "../../shared/api.ts"; import type { CommunityStats } from "../../shared/community.ts"; import { isDigestCadence } from "../../shared/settings.ts"; import { APIException } from "../lib/api-exception.ts"; import { type AuthContext, authMiddleware } from "../middleware/auth.ts"; import { getCommunityStats } from "../services/community-service.ts"; /** * Mounted by `api/main.ts` behind the error, Open Graph and CORS middleware. * An `APIException` thrown by a handler becomes the JSON error response there. */ const router: Router = new Router({ prefix: "/api/community" }); router.get("/", authMiddleware, (ctx: AuthContext) => { const period = ctx.request.url.searchParams.get("period") ?? "weekly"; if (!isDigestCadence(period)) { throw new APIException(400, "period must be weekly or monthly"); } const responseBody: APIResponse = { success: true, data: getCommunityStats(period), }; ctx.response.body = responseBody; }); export default router;