mirror your GitHub repos to tangled.org automatically
1import type { H3Event } from 'h3'
2import type { SynchubAccount } from './server-session'
3import { requireSession } from './server-session'
4
5/**
6 * Shared preamble for `/api/repos/[id]/*` handlers: require an authenticated
7 * session and parse the `:id` route param to a mapping id, throwing a 400 if
8 * it isn't a finite number. Ownership is enforced downstream by scoping the
9 * query to `session.installationId`.
10 */
11export async function requireSessionAndMappingId(
12 event: H3Event,
13): Promise<{ session: SynchubAccount, mappingId: number }> {
14 const session = await requireSession(event)
15 const mappingId = Number(getRouterParam(event, 'id'))
16 if (!Number.isFinite(mappingId)) {
17 throw createError({ statusCode: 400, statusMessage: 'invalid mapping id' })
18 }
19 return { session, mappingId }
20}