feat: extracts some repeated logic out into lib/ files
This commit is contained in:
parent
38653f2aa1
commit
99c1539aad
22 changed files with 200 additions and 336 deletions
19
www/src/lib/api.ts
Normal file
19
www/src/lib/api.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { isAdmin } from './auth';
|
||||
|
||||
export function jsonResponse(data: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(data), {
|
||||
status,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
export function errorResponse(message: string, status: number): Response {
|
||||
return jsonResponse({ error: message }, status);
|
||||
}
|
||||
|
||||
export function requireAdmin(session: { user?: { id?: string } } | null): Response | null {
|
||||
if (!session?.user?.id || !isAdmin(session.user.id)) {
|
||||
return errorResponse('Unauthorized', 403);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue