adds zod API validation

This commit is contained in:
Christian Beutel
2024-12-27 01:56:35 +01:00
parent 1f81779a64
commit 75c94a3ce4
80 changed files with 847 additions and 559 deletions

View File

@@ -1,37 +1,30 @@
import { CommentCreateSchema } from '$lib/models/api/comment_schema';
import type { Comment } from '$lib/models/comment';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit';
import { Collection, create, handleError, list } from '$lib/util/api_util';
import { json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const filter = event.url.searchParams.get('filter') ?? ""
try {
const r: Comment[] = await pb.collection('comments').getFullList<Comment>({
expand: "author",
filter: filter,
sort: "-created",
})
for (const comment of r) {
if(!comment.expand?.author) {
const r = await list<Comment>(event, Collection.comments);
for (const comment of r.items) {
if (!comment.expand?.author) {
comment.expand = {
author: await pb.collection('users_anonymous').getOne(comment.author)
}
}
}
return json(r)
} catch (e: any) {
throw error(e.status, e);
} catch (e) {
throw handleError(e)
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('comments').create<Comment>(data)
const r = await create<Comment>(event, CommentCreateSchema, Collection.comments)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}