23 lines
675 B
TypeScript
23 lines
675 B
TypeScript
import { db } from "#server/db";
|
|
import * as tables from "#server/db/schema";
|
|
import { z } from "zod";
|
|
|
|
const bodySchema = z.object({
|
|
name: z.string().min(1),
|
|
path: z.string().min(1).startsWith("/"),
|
|
url: z.url(),
|
|
disabled: z.boolean().optional(),
|
|
});
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readValidatedBody(event, bodySchema.parse);
|
|
|
|
const conflicts = await findConflictingFields(body);
|
|
if (conflicts.length > 0) {
|
|
throw createError({ statusCode: 409, message: `A link with this ${joinFields(conflicts)} already exists` });
|
|
}
|
|
|
|
const [link] = await db.insert(tables.links).values(body).returning();
|
|
return link;
|
|
});
|