From 7a45294c0e3e8d37a36fe58fcc28e79afd699cdb Mon Sep 17 00:00:00 2001 From: Alexander Liu Date: Mon, 19 Feb 2024 17:57:31 -0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E2=99=BB=EF=B8=8F=20types=20for=20?= =?UTF-8?q?new=20contact=20endpoints?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/api/contacts/+server.ts | 13 +++++++---- src/routes/api/contacts/[id]/+server.ts | 31 ++++++++++++++++++------- src/routes/api/contacts/[id]/index.ts | 1 + src/routes/api/contacts/index.ts | 2 +- 4 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 src/routes/api/contacts/[id]/index.ts diff --git a/src/routes/api/contacts/+server.ts b/src/routes/api/contacts/+server.ts index b38fdcc..973373a 100644 --- a/src/routes/api/contacts/+server.ts +++ b/src/routes/api/contacts/+server.ts @@ -66,6 +66,11 @@ export const GET: RequestHandler = async (event) => { export type GetContacts = Awaited>; +const create = (data: z.infer) => + prisma.contact.create({ + data, + }); + export const POST: RequestHandler = async (event) => { const session = await auth.handleRequest(event).validate(); if (!session || session.user.role === "UNAUTHORIZED") { @@ -73,12 +78,10 @@ export const POST: RequestHandler = async (event) => { } const input = ContactInput.safeParse(event.request.json()); if (input.success) { - return json( - await prisma.contact.create({ - data: input.data, - }), - ); + return json(await create(input.data)); } else { throw error(500, input.error); } }; + +export type PostContact = Awaited>; diff --git a/src/routes/api/contacts/[id]/+server.ts b/src/routes/api/contacts/[id]/+server.ts index 936633e..433849f 100644 --- a/src/routes/api/contacts/[id]/+server.ts +++ b/src/routes/api/contacts/[id]/+server.ts @@ -1,30 +1,43 @@ import { error, json, type RequestHandler } from "@sveltejs/kit"; +import type { z } from "zod"; import { ContactInput } from "$lib/schema/types"; import { auth } from "$lib/server/lucia"; import { prisma } from "$lib/server/prisma"; +const update = (id: string, data: z.infer) => + prisma.contact.update({ where: { id }, data }); + export const PATCH: RequestHandler = async (event) => { const session = await auth.handleRequest(event).validate(); if (!session || session.user.role === "UNAUTHORIZED") { throw error(401); } + const input = ContactInput.safeParse(event.request.json()); - if (input.success) { - return json( - await prisma.contact.update({ - where: { id: event.params.id }, - data: input.data, - }), - ); + if (!input.success) { + throw error(500, input.error); } - throw error(500, input.error); + + const id = event.params.id; + if (!id) throw error(500, "ID is a required parameter."); + return json(await update(id, input.data)); }; +export type PatchContact = Awaited>; + +const deleteContact = (id: string) => prisma.contact.delete({ where: { id } }); + export const DELETE: RequestHandler = async (event) => { const session = await auth.handleRequest(event).validate(); if (!session || session.user.role === "UNAUTHORIZED") { throw error(401); } - return json(await prisma.contact.delete({ where: { id: event.params.id } })); + + const id = event.params.id; + if (!id) throw error(500, "ID is a required parameter."); + + return json(await deleteContact(id)); }; + +export type DeleteContact = Awaited>; diff --git a/src/routes/api/contacts/[id]/index.ts b/src/routes/api/contacts/[id]/index.ts new file mode 100644 index 0000000..1066052 --- /dev/null +++ b/src/routes/api/contacts/[id]/index.ts @@ -0,0 +1 @@ +export type { PatchContact, DeleteContact } from "./+server"; diff --git a/src/routes/api/contacts/index.ts b/src/routes/api/contacts/index.ts index 5c28a82..80e680b 100644 --- a/src/routes/api/contacts/index.ts +++ b/src/routes/api/contacts/index.ts @@ -1 +1 @@ -export type { GetContacts } from "./+server"; +export type { GetContacts, PostContact } from "./+server";