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,18 +1,13 @@
import type { Activity } from "$lib/models/activity";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, list } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
const page = event.url.searchParams.get("page") ?? "0";
const perPage = event.url.searchParams.get("per-page") ?? "10";
const filter = event.url.searchParams.get("filter") ?? "";
try {
const r = await pb.collection('activities')
.getList<Activity>(parseInt(page), parseInt(perPage), { filter: filter ?? "" })
const r = await list<Activity>(event, Collection.activities);
return json(r)
} catch (e: any) {
throw error(e.status, e);
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,17 +1,16 @@
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { ClientResponseError } from "pocketbase";
import { z } from "zod";
export async function POST(event: RequestEvent) {
try {
let data;
try {
data = await event.request.json()
} catch (e) {
throw new ClientResponseError({ status: 400, response: { message: "Invalid json" } })
}
const r = await pb.collection('users').confirmPasswordReset(data.token, data.password, data.passwordConfirm);
const data = await event.request.json()
const safeData = z.object({
token: z.string(),
password: z.string(),
passwordConfirm: z.string()
}).refine(d => d.password === d.passwordConfirm).parse(data)
const r = await pb.collection('users').confirmPasswordReset(safeData.token, safeData.password, safeData.passwordConfirm);
return json(r);
} catch (e: any) {
throw error(e.status, e);

View File

@@ -1,17 +1,18 @@
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { ClientResponseError } from "pocketbase";
import { z } from "zod";
export async function POST(event: RequestEvent) {
try {
let data;
try {
data = await event.request.json()
} catch (e) {
throw new ClientResponseError({ status: 400, response: { message: "Invalid json" } })
}
const data = await event.request.json();
const safeData = z.object({
email: z.string().email().optional(),
username: z.string().optional(),
password: z.string()
}).refine(d => d.email !== undefined || d.username !== undefined).parse(data);
const r = await pb.collection('users').authWithPassword(data.email ?? data.username!, data.password);
const r = await pb.collection('users').authWithPassword(safeData.email ?? safeData.username!, data.password);
return json(r);
} catch (e: any) {
throw error(e.status, e);

View File

@@ -3,6 +3,7 @@ import { env as private_env } from "$env/dynamic/private";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { z } from "zod";
const redirectURL = private_env.ORIGIN + "/login/redirect"
@@ -23,12 +24,18 @@ export async function GET(event: RequestEvent) {
}
export async function POST(event: RequestEvent) {
const data = await event.request.json();
try {
const data = await event.request.json();
const safeData = z.object({
name: z.string(),
code: z.string(),
codeVerifier: z.string()
}).parse(data)
const r = await pb.collection('users').authWithOAuth2Code(
data.name,
data.code,
data.codeVerifier,
safeData.name,
safeData.code,
safeData.codeVerifier,
redirectURL,
)
return json(r)

View File

@@ -1,20 +1,20 @@
import { pb } from "$lib/pocketbase";
import { handleError } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { ClientResponseError } from "pocketbase";
import { z } from "zod";
export async function POST(event: RequestEvent) {
try {
let data;
try {
data = await event.request.json()
} catch (e) {
throw new ClientResponseError({ status: 400, response: { message: "Invalid json" } })
}
const data = await event.request.json()
const safeData = z.object({
email: z.string().email()
}).parse(data)
const r = await pb.collection('users').requestPasswordReset(data.email);
const r = await pb.collection('users').requestPasswordReset(safeData.email);
return json(r);
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}

View File

@@ -1,13 +1,13 @@
import type { Category } from "$lib/models/category";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, list } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r: Category[] = await pb.collection('categories').getFullList<Category>()
const r = await list<Category>(event, Collection.categories);
return json(r)
} catch (e: any) {
throw error(e.status, e);
} catch (e) {
throw handleError(e)
}
}

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)
}
}

View File

@@ -1,33 +1,35 @@
import { CommentUpdateSchema } 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, 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('comments')
.getOne<Comment>(event.params.id as string, { expand: "author" })
const r = await show<Comment>(event, Collection.comments)
r.expand = {
author: await pb.collection('users_anonymous').getOne(r.author)
}
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('comments').update<Comment>(event.params.id as string, data)
const r = await update<Comment>(event, CommentUpdateSchema, Collection.comments)
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('comments').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.comments)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,22 +1,32 @@
import { pb } from "$lib/pocketbase";
import { error, type RequestEvent } from "@sveltejs/kit";
import { z } from "zod";
export async function GET(event: RequestEvent) {
const safeParams = z.object({
collection: z.string().length(15),
record: z.string().length(15),
file: z.string()
}).safeParse(event.params)
if (!safeParams.success) {
throw error(400, 'Invalid params')
}
const parts = [];
parts.push("api");
parts.push("files");
parts.push(encodeURIComponent(event.params.collection as string));
parts.push(encodeURIComponent(event.params.record as string));
parts.push(encodeURIComponent(event.params.file as string));
parts.push(encodeURIComponent(safeParams.data.collection));
parts.push(encodeURIComponent(safeParams.data.record));
parts.push(encodeURIComponent(safeParams.data.file));
let fileURL = pb.buildUrl(parts.join("/"));
try {
const r = await event.fetch(fileURL)
return new Response(r.body, {headers: r.headers});
return new Response(r.body, { headers: r.headers });
} catch (e: any) {
console.log(e);
throw error(500, e);
}

View File

@@ -1,45 +1,31 @@
import { FollowCreateSchema } from '$lib/models/api/follow_schema';
import type { Follow } from '$lib/models/follow';
import type { UserAnonymous } from '$lib/models/user';
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 page = event.url.searchParams.get("page") ?? "0";
const perPage = event.url.searchParams.get("per-page") ?? "10";
const sort = event.url.searchParams.get('sort') ?? ""
const filter = event.url.searchParams.get("filter") ?? "";
try {
let r;
if (parseInt(perPage) < 0) {
r = {
items: await pb.collection('follows')
.getFullList<Follow>({ sort: sort, filter: filter, requestKey: filter })
}
} else {
r = await pb.collection('follows')
.getList<Follow>(parseInt(page), parseInt(perPage), { sort: sort ?? "", filter: filter ?? "", requestKey: filter })
}
const r = await list<Follow>(event, Collection.follows);
for (const follow of r.items) {
const follower = await pb.collection('users_anonymous').getOne<UserAnonymous>(follow.follower, { requestKey: filter })
const followee = await pb.collection('users_anonymous').getOne<UserAnonymous>(follow.followee, { requestKey: filter })
const follower = await pb.collection('users_anonymous').getOne<UserAnonymous>(follow.follower, { requestKey: null })
const followee = await pb.collection('users_anonymous').getOne<UserAnonymous>(follow.followee, { requestKey: null })
follow.expand = {
follower, followee
}
}
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('follows').create<Follow>(data)
const r = await create<Follow>(event, FollowCreateSchema, Collection.follows)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

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

View File

@@ -1,12 +1,11 @@
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, show } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('follow_counts')
.getOne(event.params.id as string)
const r = await show(event, Collection.follow_counts)
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e)
}
}

View File

@@ -1,18 +1,15 @@
import { ListShareCreateSchema } from '$lib/models/api/list_share_schema';
import type { ListShare } from '$lib/models/list_share';
import type { User } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { Collection, create, handleError, list } from '$lib/util/api_util';
import { error, 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: ListShare[] = await pb.collection('list_share').getFullList<ListShare>({
sort: sort,
filter: filter
})
for (const share of r) {
const r = await list<ListShare>(event, Collection.list_share);
for (const share of r.items) {
const anonymous_user = await pb.collection('users_anonymous').getOne<User>(share.user)
share.expand = {
user: anonymous_user
@@ -20,17 +17,15 @@ export async function GET(event: RequestEvent) {
}
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('list_share').create<ListShare>(data)
const r = await create<ListShare>(event, ListShareCreateSchema, Collection.list_share)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,33 +1,31 @@
import { ListShareUpdateSchema } from "$lib/models/api/list_share_schema";
import type { ListShare } from "$lib/models/list_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('list_share')
.getOne<ListShare>(event.params.id as string)
const r = await show<ListShare>(event, Collection.list_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('list_share').update<ListShare>(event.params.id as string, data)
const r = await update<ListShare>(event, ListShareUpdateSchema, Collection.list_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('list_share').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.list_share)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,27 +1,12 @@
import { ListCreateSchema } from '$lib/models/api/list_schema';
import type { List } from '$lib/models/list';
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 page = event.url.searchParams.get("page") ?? "0";
const perPage = event.url.searchParams.get("per-page") ?? "10";
const sort = event.url.searchParams.get('sort') ?? ""
const filter = event.url.searchParams.get("filter") ?? "";
const expand = "trails,trails.waypoints,trails.category,list_share_via_list";
try {
let r;
if (parseInt(perPage) < 0) {
r = {
items: await pb.collection('lists')
.getFullList<List>({ expand: expand, sort: sort, filter: filter })
}
} else {
r = await pb.collection('lists')
.getList<List>(parseInt(page), parseInt(perPage), { expand: expand, sort: sort ?? "", filter: filter ?? "" })
}
const r = await list<List>(event, Collection.lists);
for (const t of r.items) {
if (!t.author || !pb.authStore.model) {
continue;
@@ -31,20 +16,17 @@ export async function GET(event: RequestEvent) {
}
t.expand!.author = await pb.collection("users_anonymous").getOne(t.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('lists').create<List>(data)
const r = await create<List>(event, ListCreateSchema, Collection.lists)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,33 +1,36 @@
import { ListUpdateSchema } from "$lib/models/api/list_schema";
import type { List } from "$lib/models/list";
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('lists')
.getOne<List>(event.params.id as string, { expand: "trails,trails.waypoints,trails.category,list_share_via_list" })
const r = await show<List>(event, Collection.lists)
r.expand = {
author: await pb.collection('users_anonymous').getOne(r.author!)
}
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('lists').update<List>(event.params.id as string, data)
const r = await update<List>(event, ListUpdateSchema, Collection.lists)
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('lists').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.lists)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,13 +1,13 @@
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, upload } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
import type { List } from "postcss/lib/list";
export async function POST(event: RequestEvent) {
const data = await event.request.formData()
try {
const r = await pb.collection("lists").update<List>(event.params.id as string, data,);
const r = await upload<List>(event, Collection.lists);
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,28 +1,16 @@
import type { Notification } from '$lib/models/notification';
import type { UserAnonymous } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { Collection, list } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const page = event.url.searchParams.get("page") ?? "0";
const perPage = event.url.searchParams.get("per-page") ?? "10";
const sort = event.url.searchParams.get('sort') ?? ""
const filter = event.url.searchParams.get("filter") ?? "";
try {
let r;
if (parseInt(perPage) < 0) {
r = {
items: await pb.collection('notifications')
.getFullList<Notification>({ sort: sort, filter: filter, requestKey: filter })
}
} else {
r = await pb.collection('notifications')
.getList<Notification>(parseInt(page), parseInt(perPage), { sort: sort ?? "", filter: filter ?? "", requestKey: filter })
}
const r = await list<Notification>(event, Collection.notifications);
for (const notification of r.items) {
const recipient = await pb.collection('users_anonymous').getOne<UserAnonymous>(notification.recipient, { requestKey: filter })
const author = await pb.collection('users_anonymous').getOne<UserAnonymous>(notification.author, { requestKey: filter })
const recipient = await pb.collection('users_anonymous').getOne<UserAnonymous>(notification.recipient, { requestKey: null })
const author = await pb.collection('users_anonymous').getOne<UserAnonymous>(notification.author, { requestKey: null })
notification.expand = {
recipient, author
}

View File

@@ -1,15 +1,13 @@
import type { Follow } from "$lib/models/follow";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { NotificationUpdateSchema } from "$lib/models/api/notification_schema";
import { Collection, handleError, update } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
const r = await pb.collection('notifications').update<Notification>(event.params.id as string, { ...data, seen: true })
const r = await update<Comment>(event, NotificationUpdateSchema, Collection.notifications)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,12 +1,11 @@
import type { Settings } from '$lib/models/settings';
import { pb } from '$lib/pocketbase';
import { SettingsCreateSchema } from '$lib/models/api/settings_schema';
import { Collection, create } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('settings').create<Settings>(data)
const r = await create<Comment>(event, SettingsCreateSchema, Collection.settings)
return json(r);
} catch (e: any) {
throw error(e.status, e)

View File

@@ -1,32 +1,33 @@
import { SettingsCreateSchema } from '$lib/models/api/settings_schema';
import type { Settings } from "$lib/models/settings";
import { pb } from "$lib/pocketbase";
import { Collection, handleError, remove, show, update } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('settings').getFirstListItem<Settings>(`user="${event.params.id}"`)
const r = await show<Settings>(event, Collection.settings)
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 await pb.collection("settings").update<Settings>(event.params.id as string, data);
const r = await update<Settings>(event, SettingsCreateSchema, Collection.settings)
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('settings').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.settings)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e);
}
}

View File

@@ -1,37 +1,32 @@
import { SummitLogCreateSchema } from '$lib/models/api/summit_log_schema';
import type { SummitLog } from '$lib/models/summit_log';
import { pb } from '$lib/pocketbase';
import { create, handleError } from '$lib/util/api_util';
import { Collection, list } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const filter = event.url.searchParams.get('filter') ?? ""
try {
const r: SummitLog[] = await pb.collection('summit_logs').getFullList<SummitLog>({
expand: "trails_via_summit_logs.category",
sort: "+date",
filter: filter
})
const r = await list<SummitLog>(event, Collection.summit_logs);
for (const t of r) {
for (const t of r.items) {
if (!t.author || !pb.authStore.model) {
continue;
}
if (!t.expand) {
t.expand = {} as any
}
t.expand.author = await pb.collection("users_anonymous").getOne(t.author);
t.expand!.author = await pb.collection("users_anonymous").getOne(t.author);
}
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('summit_logs').create<SummitLog>(data)
const r = await create<SummitLog>(event, SummitLogCreateSchema, Collection.summit_logs)
return json(r);
} catch (e: any) {
throw error(e.status, e)

View File

@@ -1,39 +1,39 @@
import { SummitLogUpdateSchema } from "$lib/models/api/summit_log_schema";
import type { SummitLog } from "$lib/models/summit_log";
import { pb } from "$lib/pocketbase";
import { Collection, handleError, remove, show, update } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('summit_logs')
.getOne<SummitLog>(event.params.id as string)
const r = await show<SummitLog>(event, Collection.summit_logs)
if (!r.expand) {
r.expand = {} as any
}
r.expand.author = await pb.collection("users_anonymous").getOne(r.author!);
r.expand!.author = await pb.collection("users_anonymous").getOne(r.author!);
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 await pb.collection("summit_logs").update<SummitLog>(event.params.id as string, data);
const r = await update<SummitLog>(event, SummitLogUpdateSchema, Collection.summit_logs)
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('summit_logs').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.summit_logs)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e);
}
}

View File

@@ -1,11 +1,10 @@
import type { SummitLog } from "$lib/models/summit_log";
import { pb } from "$lib/pocketbase";
import { Collection, upload } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
export async function POST(event: RequestEvent) {
const data = await event.request.formData()
try {
const r = await pb.collection("summit_logs").update<SummitLog>(event.params.id as string, data,);
const r = await upload<SummitLog>(event, Collection.summit_logs);
return json(r);
} catch (e: any) {
throw error(e.status, e)

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)
}
}

View File

@@ -1,49 +1,33 @@
import { TrailCreateSchema } from '$lib/models/api/trail_schema';
import type { Trail } from '$lib/models/trail';
import { pb } from '$lib/pocketbase';
import { Collection, create, handleError, list } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const page = event.url.searchParams.get("page") ?? "0";
const perPage = event.url.searchParams.get("per-page") ?? "21";
const expand = event.url.searchParams.get("expand") ?? ""
const sort = event.url.searchParams.get("sort") ?? ""
const filter = event.url.searchParams.get("filter") ?? "";
try {
let r;
if (parseInt(perPage) < 0) {
r = {
items: await pb.collection('trails')
.getFullList<Trail>({ expand: expand, sort: sort, filter: filter })
}
} else {
r = await pb.collection('trails')
.getList<Trail>(parseInt(page), parseInt(perPage), { expand: expand ?? "", sort: sort ?? "", filter: filter ?? "" })
}
const r = await list<Trail>(event, Collection.trails);
for (const t of r.items) {
if (!t.author || !pb.authStore.model) {
continue;
}
if(!t.expand) {
if (!t.expand) {
t.expand = {} as any
}
t.expand.author = await pb.collection("users_anonymous").getOne(t.author);
t.expand!.author = await pb.collection("users_anonymous").getOne(t.author);
}
return json(r)
} catch (e: any) {
console.error(e)
throw error(e.status, e);
throw handleError(e);
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('trails').create<Trail>(data)
const r = await create<Trail>(event, TrailCreateSchema, Collection.trails)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,20 +1,18 @@
import type { Trail } from "$lib/models/trail";
import { pb } from "$lib/pocketbase";
import { Collection, handleError, remove, show, update } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { TrailUpdateSchema } from '$lib/models/api/trail_schema';
export async function GET(event: RequestEvent) {
const expand = event.url.searchParams.get("expand") ?? ""
try {
const r = await pb.collection('trails')
.getOne<Trail>(event.params.id as string, { expand: expand ?? "" })
const r = await show<Trail>(event, Collection.trails)
if (pb.authStore.model) {
if (!r.expand) {
r.expand = {} as any
}
r.expand.author = await pb.collection("users_anonymous").getOne(r.author!);
r.expand!.author = await pb.collection("users_anonymous").getOne(r.author!);
}
// remove time from dates
@@ -25,33 +23,31 @@ export async function GET(event: RequestEvent) {
if (!log.expand) {
log.expand = {} as any
}
if(log.author) {
log.expand.author = await pb.collection("users_anonymous").getOne(log.author);
if (log.author) {
log.expand!.author = await pb.collection("users_anonymous").getOne(log.author);
}
}
return json(r)
} catch (e: any) {
console.error(e)
throw error(e.status || 500, e);
throw handleError(e)
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
const r = await pb.collection("trails").update<Trail>(event.params.id as string, data, { expand: "category,waypoints,summit_logs" });
const r = await update<Trail>(event, TrailUpdateSchema, Collection.trails)
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('trails').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.trails)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,11 +1,11 @@
import type { Trail } from "$lib/models/trail";
import { pb } from "$lib/pocketbase";
import { Collection, upload } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
export async function POST(event: RequestEvent) {
const data = await event.request.formData()
try {
const r = await pb.collection("trails").update<Trail>(event.params.id as string, data,);
const r = await upload<Trail>(event, Collection.trails);
return json(r);
} catch (e: any) {
throw error(e.status, e)

View File

@@ -24,10 +24,10 @@ export async function PUT(event: RequestEvent) {
elevation_loss: trail.elevation_loss,
duration: trail.duration ? trail.duration * 60 : undefined,
})
log.expand.gpx_data = gpxData;
log.expand!.gpx_data = gpxData;
log._gpx = new File([gpxFile], (data.get("name") as string | null) ?? "file");
trail.expand.summit_logs.push(log);
trail.expand!.summit_logs.push(log);
} catch (e: any) {
throw new ClientResponseError({ status: 400, response: { message: "Invalid file" } })
}

View File

@@ -1,19 +1,20 @@
import { env } from '$env/dynamic/public';
import { UserCreateSchema } from '$lib/models/api/user_schema';
import type { User } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit'
import { Collection, create, handleError } from '$lib/util/api_util';
import { json, type RequestEvent } from '@sveltejs/kit';
import { ClientResponseError } from 'pocketbase';
export async function PUT(event: RequestEvent) {
const data = await event.request.json()
try {
if (env.PUBLIC_DISABLE_SIGNUP === "true") {
throw new ClientResponseError({ status: 401, response: { messgage: "Forbidden" } })
}
const r = await pb.collection('users').create<User>(data)
const r = await create<User>(event, UserCreateSchema, Collection.users)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e);
}
}

View File

@@ -1,41 +1,49 @@
import { RecordIdSchema } from '$lib/models/api/base_schema';
import { UserUpdateSchema } from '$lib/models/api/user_schema';
import type { User } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { Collection, handleError, remove, show } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit'
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('users')
.getOne<User>(event.params.id as string)
const r = await show<User>(event, Collection.users)
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 {
if (data.email != pb.authStore.model!.email) {
const r = await pb.collection('users').requestEmailChange(data.email);
pb.authStore.model!.email = data.email;
const params = event.params
const safeParams = RecordIdSchema.parse(params);
const safeData = UserUpdateSchema.parse(data);
if (safeData.email && safeData.email != pb.authStore.model!.email) {
const r = await pb.collection('users').requestEmailChange(safeData.email);
pb.authStore.model!.email = safeData.email;
}
const r = await pb.collection('users').update<User>(event.params.id as string, data)
if (data.password) {
const r = await pb.collection('users').authWithPassword(data.email ?? data.username!, data.password);
const r = await pb.collection('users').update<User>(safeParams.id, safeData)
if (safeData.password) {
const r = await pb.collection('users').authWithPassword(safeData.email ?? safeData.username!, safeData.password);
return json(r.record)
} else {
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('users').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.users)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e);
}
}

View File

@@ -1,11 +1,10 @@
import { pb } from "$lib/pocketbase";
import type { User } from "$lib/stores/user_store";
import type { User } from "$lib/models/user";
import { Collection, upload } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
export async function POST(event: RequestEvent) {
const data = await event.request.formData()
try {
const r = await pb.collection("users").update<User>(event.params.id as string, data,);
const r = await upload<User>(event, Collection.users);
return json(r);
} catch (e: any) {
throw error(e.status, e)

View File

@@ -1,25 +1,24 @@
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 } from '$lib/util/api_util';
import { json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const page = event.url.searchParams.get("page") ?? "0";
const perPage = event.url.searchParams.get("per-page") ?? "10";
const sort = event.url.searchParams.get("sort") ?? ""
let filter = event.url.searchParams.get("filter") ?? "";
let filter = event.url.searchParams.get("filter");
if (!filter.length) {
if (!filter) {
filter = "private=false"
} else {
filter += "&&private=false"
}
event.url.searchParams.set("filter", filter);
try {
const r = await pb.collection('users_anonymous')
.getList<User>(parseInt(page), parseInt(perPage), { sort: sort ?? "", filter: filter ?? "" })
const r = await list<UserAnonymous>(event, Collection.users_anonymous);
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}

View File

@@ -1,13 +1,13 @@
import type { User } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit'
import type { UserAnonymous } from "$lib/models/user";
import { Collection, handleError, show } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('users_anonymous')
.getOne<User>(event.params.id as string)
const r = await show<UserAnonymous>(event, Collection.users_anonymous)
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}

View File

@@ -1,23 +1,22 @@
import { WaypointCreateSchema } from '$lib/models/api/waypoint_schema';
import type { Waypoint } from '$lib/models/waypoint';
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) {
try {
const r: Waypoint[] = await pb.collection('waypoints').getFullList<Waypoint>()
const r = await list<Waypoint>(event, Collection.waypoints);
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('waypoints').create<Waypoint>(data)
const r = await create<Waypoint>(event, WaypointCreateSchema, Collection.waypoints)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,32 +1,31 @@
import { WaypointUpdateSchema } from "$lib/models/api/waypoint_schema";
import type { Waypoint } from "$lib/models/waypoint";
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('waypoints')
.getOne<Waypoint>(event.params.id as string)
const r = await show<Waypoint>(event, Collection.waypoints)
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()
export async function POST(event: RequestEvent) {
try {
const r = await await pb.collection("waypoints").update<Waypoint>(event.params.id as string, data);
const r = await update<Waypoint>(event, WaypointUpdateSchema, Collection.waypoints)
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('waypoints').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.waypoints)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,13 +1,12 @@
import type { Waypoint } from "$lib/models/waypoint";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, upload } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function POST(event: RequestEvent) {
const data = await event.request.formData()
try {
const r = await pb.collection("waypoints").update<Waypoint>(event.params.id as string, data,);
const r = await upload<Waypoint>(event, Collection.waypoints);
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e);
}
}