33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
|
|
import type { RequestHandler } from './$types';
|
||
|
|
import { json, error } from '@sveltejs/kit';
|
||
|
|
import { z } from 'zod';
|
||
|
|
import { getDb } from '$lib/server/db';
|
||
|
|
import { addDomain, listDomains } from '$lib/server/domains/repository';
|
||
|
|
|
||
|
|
const CreateSchema = z.object({
|
||
|
|
domain: z.string().min(3).max(253),
|
||
|
|
display_name: z.string().max(100).nullable().optional(),
|
||
|
|
added_by_profile_id: z.number().int().positive().nullable().optional()
|
||
|
|
});
|
||
|
|
|
||
|
|
export const GET: RequestHandler = async () => {
|
||
|
|
return json(listDomains(getDb()));
|
||
|
|
};
|
||
|
|
|
||
|
|
export const POST: RequestHandler = async ({ request }) => {
|
||
|
|
const body = await request.json().catch(() => null);
|
||
|
|
const parsed = CreateSchema.safeParse(body);
|
||
|
|
if (!parsed.success) error(400, { message: 'Invalid body' });
|
||
|
|
try {
|
||
|
|
const d = addDomain(
|
||
|
|
getDb(),
|
||
|
|
parsed.data.domain,
|
||
|
|
parsed.data.display_name ?? null,
|
||
|
|
parsed.data.added_by_profile_id ?? null
|
||
|
|
);
|
||
|
|
return json(d, { status: 201 });
|
||
|
|
} catch (e) {
|
||
|
|
error(409, { message: (e as Error).message });
|
||
|
|
}
|
||
|
|
};
|