|
1 | 1 | import {get} from '../../ccc-lib/http.js' |
2 | | -import {ONE_HOUR} from '../../ccc-lib/constants.js' |
3 | 2 | import {GH_PAGES_FROM_REPO} from './gh-pages.js' |
4 | | -import mem from 'memoize' |
5 | | -import type {Context} from '../../ccc-server/context.js' |
| 3 | +import {z} from 'zod' |
| 4 | +import {createRouteSpec} from 'koa-zod-router' |
6 | 5 |
|
7 | | -const GET = mem(get, {maxAge: ONE_HOUR}) |
| 6 | +const MealTimeSchema = z.array( |
| 7 | + z.object({ |
| 8 | + date: z.string().date(), |
| 9 | + times: z.record(z.string().time(), z.number()), |
| 10 | + }), |
| 11 | +) |
8 | 12 |
|
9 | | -let stavMealtimesUrl = GH_PAGES_FROM_REPO('stav-mealtimes', 'two-weeks.json') |
10 | | - |
11 | | -export function getStavMealtimes() { |
12 | | - return GET(stavMealtimesUrl).json() |
| 13 | +export async function getStavMealtimes() { |
| 14 | + return MealTimeSchema.parse(await get(GH_PAGES_FROM_REPO('stav-mealtimes', 'two-weeks.json')).json()) |
13 | 15 | } |
14 | 16 |
|
15 | | -export async function stavMealtimeReport(ctx: Context) { |
16 | | - ctx.cacheControl(ONE_HOUR) |
17 | | - |
18 | | - ctx.body = await getStavMealtimes() |
19 | | -} |
| 17 | +export const getStavMealtimeReportRoute = createRouteSpec({ |
| 18 | + method: 'get', |
| 19 | + path: '/reports/stav', |
| 20 | + validate: { |
| 21 | + response: MealTimeSchema, |
| 22 | + }, |
| 23 | + handler: async (ctx) => { |
| 24 | + ctx.body = await getStavMealtimes() |
| 25 | + }, |
| 26 | +}) |
0 commit comments