From 5118f55c0cd8cd2d2f46b5a2ed5216c3c0da20f7 Mon Sep 17 00:00:00 2001 From: Riccardo Date: Sun, 17 Dec 2023 19:17:32 +0100 Subject: [PATCH] fix: handled api error responses --- app/api/confirmation/route.ts | 4 +++- app/api/subscribe/route.ts | 2 ++ app/api/unsubscribe/route.ts | 1 + app/confirmation/page.tsx | 6 ++++++ app/page.tsx | 4 ++++ app/unsubscribe/page.tsx | 4 ++++ utils/schemas.ts | 13 +++++++------ 7 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/api/confirmation/route.ts b/app/api/confirmation/route.ts index fb51dea..cc0a5e6 100644 --- a/app/api/confirmation/route.ts +++ b/app/api/confirmation/route.ts @@ -28,6 +28,7 @@ export async function POST(request: Request) { }); const message: z.infer = { + success: true, message: `Thank you for confirming the subscription, ${user.email}!` }; @@ -35,7 +36,8 @@ export async function POST(request: Request) { } const message: z.infer = { - message: `Nothing to see here...` + success: false, + message: `It was not possible to confirm the subscription.` }; return ApiResponse(200, JSON.stringify(message)); diff --git a/app/api/subscribe/route.ts b/app/api/subscribe/route.ts index 317ac16..eab4c1f 100644 --- a/app/api/subscribe/route.ts +++ b/app/api/subscribe/route.ts @@ -36,6 +36,7 @@ export async function POST(request: Request) { } const message: z.infer = { + success: true, message: `Thank you for subscribing!` }; @@ -67,6 +68,7 @@ export async function POST(request: Request) { } const message: z.infer = { + success: true, message: `Thank you! You will now receive an email to ${email} to confirm the subscription.` }; diff --git a/app/api/unsubscribe/route.ts b/app/api/unsubscribe/route.ts index 94ed3de..13dc3bf 100644 --- a/app/api/unsubscribe/route.ts +++ b/app/api/unsubscribe/route.ts @@ -39,6 +39,7 @@ export async function POST(request: Request) { } const message: z.infer = { + success: true, message: `${email} unsubscribed.` }; diff --git a/app/confirmation/page.tsx b/app/confirmation/page.tsx index 545aefe..68309b4 100644 --- a/app/confirmation/page.tsx +++ b/app/confirmation/page.tsx @@ -31,7 +31,13 @@ export default function Confirmation() { if (!res.ok) { router.push('/'); } + const response: z.infer = await res.json(); + + if (!response.success) { + router.push('/'); + } + return response; }) .then(response => { diff --git a/app/page.tsx b/app/page.tsx index f674de4..760786a 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -59,6 +59,10 @@ export default function Home() { const formResponse: z.infer = await response.json(); + if (!formResponse.success) { + throw Error(formResponse.message); + } + setMessage(formResponse.message); setCompleted(true); } catch (error) { diff --git a/app/unsubscribe/page.tsx b/app/unsubscribe/page.tsx index 34c66ac..88563fa 100644 --- a/app/unsubscribe/page.tsx +++ b/app/unsubscribe/page.tsx @@ -59,6 +59,10 @@ export default function Unsubscribe() { const formResponse: z.infer = await response.json(); + if (!formResponse.success) { + throw Error(formResponse.message); + } + setMessage(formResponse.message); setCompleted(true); } catch (error) { diff --git a/utils/schemas.ts b/utils/schemas.ts index 43331ca..e94175a 100644 --- a/utils/schemas.ts +++ b/utils/schemas.ts @@ -1,21 +1,22 @@ import { z } from 'zod'; export const ResponseSchema = z.object({ - message: z.string(), + success: z.boolean(), + message: z.string() }); export const SubscribeFormSchema = z.object({ email: z.string().email(), - name: z.string().optional(), + name: z.string().optional() }); export const ConfirmationSchema = z.object({ - code: z.string(), + code: z.string() }); export const UnsubscribeFormSchema = z.object({ email: z.string().email(), - name: z.string().optional(), + name: z.string().optional() }); export const NewsDatabaseSchema = z.object({ @@ -26,7 +27,7 @@ export const NewsDatabaseSchema = z.object({ by: z.string(), time: z.number(), url: z.string().optional(), - score: z.number(), + score: z.number() }); export const NewsSchema = z.object({ @@ -38,5 +39,5 @@ export const NewsSchema = z.object({ time: z.number(), url: z.string().nullable(), score: z.number(), - createdAt: z.date(), + createdAt: z.date() });