-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ types for new contact endpoints
- Loading branch information
1 parent
5822b60
commit 7a45294
Showing
4 changed files
with
32 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<typeof ContactInput>) => | ||
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<ReturnType<typeof update>>; | ||
|
||
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<ReturnType<typeof deleteContact>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { PatchContact, DeleteContact } from "./+server"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export type { GetContacts } from "./+server"; | ||
export type { GetContacts, PostContact } from "./+server"; |