mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
- Update schema from z.url().max() to use .pipe() with length check. - Properly validates URL format while preventing oversized input.
70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import { z } from 'zod';
|
|
import { uuid } from '@/lib/crypto';
|
|
import { getQueryFilters, parseRequest } from '@/lib/request';
|
|
import { json, unauthorized } from '@/lib/response';
|
|
import { pagingParams, searchParams } from '@/lib/schema';
|
|
import { canCreateTeamWebsite, canCreateWebsite } from '@/permissions';
|
|
import { createLink, getUserLinks } from '@/queries/prisma';
|
|
|
|
export async function GET(request: Request) {
|
|
const schema = z.object({
|
|
...pagingParams,
|
|
...searchParams,
|
|
});
|
|
|
|
const { auth, query, error } = await parseRequest(request, schema);
|
|
|
|
if (error) {
|
|
return error();
|
|
}
|
|
|
|
const filters = await getQueryFilters(query);
|
|
|
|
const links = await getUserLinks(auth.user.id, filters);
|
|
|
|
return json(links);
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const schema = z.object({
|
|
name: z.string().max(100),
|
|
url: z.string().max(500),
|
|
slug: z.string().min(4).max(100),
|
|
ogTitle: z.string().max(500).optional(),
|
|
ogDescription: z.string().max(500).optional(),
|
|
ogImageUrl: z.union([z.string().max(500).pipe(z.url()), z.literal('')]).optional(),
|
|
teamId: z.string().nullable().optional(),
|
|
id: z.uuid().nullable().optional(),
|
|
});
|
|
|
|
const { auth, body, error } = await parseRequest(request, schema);
|
|
|
|
if (error) {
|
|
return error();
|
|
}
|
|
|
|
const { id, name, url, slug, ogTitle, ogDescription, ogImageUrl, teamId } = body;
|
|
|
|
if ((teamId && !(await canCreateTeamWebsite(auth, teamId))) || !(await canCreateWebsite(auth))) {
|
|
return unauthorized();
|
|
}
|
|
|
|
const data: any = {
|
|
id: id ?? uuid(),
|
|
name,
|
|
url,
|
|
slug,
|
|
ogTitle,
|
|
ogDescription,
|
|
ogImageUrl,
|
|
teamId,
|
|
};
|
|
|
|
if (!teamId) {
|
|
data.userId = auth.user.id;
|
|
}
|
|
|
|
const result = await createLink(data);
|
|
|
|
return json(result);
|
|
}
|