fix: handled api error responses
This commit is contained in:
@@ -28,6 +28,7 @@ export async function POST(request: Request) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const message: z.infer<typeof ResponseSchema> = {
|
const message: z.infer<typeof ResponseSchema> = {
|
||||||
|
success: true,
|
||||||
message: `Thank you for confirming the subscription, ${user.email}!`
|
message: `Thank you for confirming the subscription, ${user.email}!`
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -35,7 +36,8 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const message: z.infer<typeof ResponseSchema> = {
|
const message: z.infer<typeof ResponseSchema> = {
|
||||||
message: `Nothing to see here...`
|
success: false,
|
||||||
|
message: `It was not possible to confirm the subscription.`
|
||||||
};
|
};
|
||||||
|
|
||||||
return ApiResponse(200, JSON.stringify(message));
|
return ApiResponse(200, JSON.stringify(message));
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const message: z.infer<typeof ResponseSchema> = {
|
const message: z.infer<typeof ResponseSchema> = {
|
||||||
|
success: true,
|
||||||
message: `Thank you for subscribing!`
|
message: `Thank you for subscribing!`
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -67,6 +68,7 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const message: z.infer<typeof ResponseSchema> = {
|
const message: z.infer<typeof ResponseSchema> = {
|
||||||
|
success: true,
|
||||||
message: `Thank you! You will now receive an email to ${email} to confirm the subscription.`
|
message: `Thank you! You will now receive an email to ${email} to confirm the subscription.`
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const message: z.infer<typeof ResponseSchema> = {
|
const message: z.infer<typeof ResponseSchema> = {
|
||||||
|
success: true,
|
||||||
message: `${email} unsubscribed.`
|
message: `${email} unsubscribed.`
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,13 @@ export default function Confirmation() {
|
|||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
router.push('/');
|
router.push('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
const response: z.infer<typeof ResponseSchema> = await res.json();
|
const response: z.infer<typeof ResponseSchema> = await res.json();
|
||||||
|
|
||||||
|
if (!response.success) {
|
||||||
|
router.push('/');
|
||||||
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
|||||||
@@ -59,6 +59,10 @@ export default function Home() {
|
|||||||
const formResponse: z.infer<typeof ResponseSchema> =
|
const formResponse: z.infer<typeof ResponseSchema> =
|
||||||
await response.json();
|
await response.json();
|
||||||
|
|
||||||
|
if (!formResponse.success) {
|
||||||
|
throw Error(formResponse.message);
|
||||||
|
}
|
||||||
|
|
||||||
setMessage(formResponse.message);
|
setMessage(formResponse.message);
|
||||||
setCompleted(true);
|
setCompleted(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -59,6 +59,10 @@ export default function Unsubscribe() {
|
|||||||
const formResponse: z.infer<typeof ResponseSchema> =
|
const formResponse: z.infer<typeof ResponseSchema> =
|
||||||
await response.json();
|
await response.json();
|
||||||
|
|
||||||
|
if (!formResponse.success) {
|
||||||
|
throw Error(formResponse.message);
|
||||||
|
}
|
||||||
|
|
||||||
setMessage(formResponse.message);
|
setMessage(formResponse.message);
|
||||||
setCompleted(true);
|
setCompleted(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -1,21 +1,22 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
export const ResponseSchema = z.object({
|
export const ResponseSchema = z.object({
|
||||||
message: z.string(),
|
success: z.boolean(),
|
||||||
|
message: z.string()
|
||||||
});
|
});
|
||||||
|
|
||||||
export const SubscribeFormSchema = z.object({
|
export const SubscribeFormSchema = z.object({
|
||||||
email: z.string().email(),
|
email: z.string().email(),
|
||||||
name: z.string().optional(),
|
name: z.string().optional()
|
||||||
});
|
});
|
||||||
|
|
||||||
export const ConfirmationSchema = z.object({
|
export const ConfirmationSchema = z.object({
|
||||||
code: z.string(),
|
code: z.string()
|
||||||
});
|
});
|
||||||
|
|
||||||
export const UnsubscribeFormSchema = z.object({
|
export const UnsubscribeFormSchema = z.object({
|
||||||
email: z.string().email(),
|
email: z.string().email(),
|
||||||
name: z.string().optional(),
|
name: z.string().optional()
|
||||||
});
|
});
|
||||||
|
|
||||||
export const NewsDatabaseSchema = z.object({
|
export const NewsDatabaseSchema = z.object({
|
||||||
@@ -26,7 +27,7 @@ export const NewsDatabaseSchema = z.object({
|
|||||||
by: z.string(),
|
by: z.string(),
|
||||||
time: z.number(),
|
time: z.number(),
|
||||||
url: z.string().optional(),
|
url: z.string().optional(),
|
||||||
score: z.number(),
|
score: z.number()
|
||||||
});
|
});
|
||||||
|
|
||||||
export const NewsSchema = z.object({
|
export const NewsSchema = z.object({
|
||||||
@@ -38,5 +39,5 @@ export const NewsSchema = z.object({
|
|||||||
time: z.number(),
|
time: z.number(),
|
||||||
url: z.string().nullable(),
|
url: z.string().nullable(),
|
||||||
score: z.number(),
|
score: z.number(),
|
||||||
createdAt: z.date(),
|
createdAt: z.date()
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user