Skip to content

Commit d59460a

Browse files
committed
port stav mealtime reports to koa-zod-router
1 parent 0d6a08e commit d59460a

3 files changed

Lines changed: 21 additions & 31 deletions

File tree

source/ccci-stolaf-college/v1/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ api.register(streams.getUpcomingRoute)
8484
api.register(printing.getColorPrintersRoute)
8585

8686
// reports
87-
api.get('/reports/stav', reports.stavMealtimeReport)
87+
api.register(reports.getStavMealtimeReportRoute)
8888

8989
// utilities
9090
api.register(util.htmlToMarkdownRoute)

source/ccci-stolaf-college/v1/majors.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
import {get} from '../../ccc-lib/http.js'
2-
import {ONE_HOUR} from '../../ccc-lib/constants.js'
32
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'
65

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+
)
812

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())
1315
}
1416

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

Comments
 (0)