Converted admin, auth, me and realtime routes.

This commit is contained in:
Mike Cao 2025-01-28 22:29:03 -08:00
parent 6c9f1ad06b
commit 5205551ca8
25 changed files with 346 additions and 7 deletions

View file

@ -13,7 +13,11 @@ export async function getJsonBody(request: Request) {
}
}
export async function parseRequest(request: Request, schema?: ZodObject<any>) {
export async function parseRequest(
request: Request,
schema?: ZodObject<any>,
options?: { skipAuth: boolean },
): Promise<any> {
const url = new URL(request.url);
let query = Object.fromEntries(url.searchParams);
let body = await getJsonBody(request);
@ -32,7 +36,7 @@ export async function parseRequest(request: Request, schema?: ZodObject<any>) {
}
}
const auth = !error ? await checkAuth(request) : null;
const auth = !error && !options?.skipAuth ? await checkAuth(request) : null;
if (!error && !auth) {
error = () => unauthorized();

View file

@ -12,14 +12,18 @@ export function badRequest(message?: any) {
return Response.json({ error: 'Bad request', message }, { status: 400 });
}
export function notFound(message?: any) {
return Response.json({ error: 'Not found', message, status: 404 });
}
export function unauthorized(message?: any) {
return Response.json({ error: 'Unauthorized', message }, { status: 401 });
}
export function forbidden(message?: any) {
return Response.json({ error: 'Forbidden', message, status: 403 });
}
export function notFound(message?: any) {
return Response.json({ error: 'Not found', message, status: 404 });
}
export function serverError(error?: any) {
return Response.json({ error: 'Server error', message: serializeError(error), status: 500 });
}