Skip to content

Commit

Permalink
refactor: ♻️ types for new contact endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderl19 committed Feb 20, 2024
1 parent 5822b60 commit 7a45294
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
13 changes: 8 additions & 5 deletions src/routes/api/contacts/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,22 @@ export const GET: RequestHandler = async (event) => {

export type GetContacts = Awaited<ReturnType<typeof findMany>>;

const create = (data: z.infer<typeof ContactInput>) =>
prisma.contact.create({
data,
});

export const POST: 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.create({
data: input.data,
}),
);
return json(await create(input.data));
} else {
throw error(500, input.error);
}
};

export type PostContact = Awaited<ReturnType<typeof create>>;
31 changes: 22 additions & 9 deletions src/routes/api/contacts/[id]/+server.ts
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>>;
1 change: 1 addition & 0 deletions src/routes/api/contacts/[id]/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { PatchContact, DeleteContact } from "./+server";
2 changes: 1 addition & 1 deletion src/routes/api/contacts/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type { GetContacts } from "./+server";
export type { GetContacts, PostContact } from "./+server";

0 comments on commit 7a45294

Please sign in to comment.