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,36 +1,30 @@
import { TrailShareCreateSchema } from '$lib/models/api/trail_share_schema';
import type { TrailShare } from '$lib/models/trail_share';
import type { User } from '$lib/models/user';
import type { UserAnonymous } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit';
import { Collection, handleError, list, create } from '$lib/util/api_util';
import { json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const sort = event.url.searchParams.get('sort') ?? ""
const filter = event.url.searchParams.get("filter") ?? "";
try {
const r: TrailShare[] = await pb.collection('trail_share').getFullList<TrailShare>({
sort: sort,
filter: filter
})
for (const share of r) {
const anonymous_user = await pb.collection('users_anonymous').getOne<User>(share.user)
const r = await list<TrailShare>(event, Collection.trail_share);
for (const share of r.items) {
const anonymous_user = await pb.collection('users_anonymous').getOne<UserAnonymous>(share.user)
share.expand = {
user: anonymous_user
}
}
return json(r)
} catch (e: any) {
throw error(e.status, e);
handleError(e)
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('trail_share').create<TrailShare>(data)
const r = await create<TrailShare>(event, TrailShareCreateSchema, Collection.trail_share)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,33 +1,31 @@
import { TrailShareUpdateSchema } from "$lib/models/api/trail_share_schema";
import type { TrailShare } from "$lib/models/trail_share";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, remove, show, update } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('trail_share')
.getOne<TrailShare>(event.params.id as string)
const r = await show<TrailShare>(event, Collection.trail_share)
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e)
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
const r = await pb.collection('trail_share').update<TrailShare>(event.params.id as string, data)
const r = await update<TrailShare>(event, TrailShareUpdateSchema, Collection.trail_share)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('trail_share').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.trail_share)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}