Webhook fixes
This commit is contained in:
parent
84b48cadd4
commit
9bc864a290
3 changed files with 42 additions and 31 deletions
4
src/pages/api/external/ping.ts
vendored
4
src/pages/api/external/ping.ts
vendored
|
@ -6,9 +6,9 @@ export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse
|
res: NextApiResponse
|
||||||
) {
|
) {
|
||||||
const success = await validateRequest(req, res);
|
const isValid = await validateRequest(req, res);
|
||||||
|
|
||||||
if (success) {
|
if (isValid) {
|
||||||
await db.query.votes.findFirst();
|
await db.query.votes.findFirst();
|
||||||
res.status(200).json({ result: "Pong!" });
|
res.status(200).json({ result: "Pong!" });
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import {
|
||||||
onUserCreatedHandler,
|
onUserCreatedHandler,
|
||||||
onUserDeletedHandler,
|
onUserDeletedHandler,
|
||||||
} from "~/server/webhookHelpers";
|
} from "~/server/webhookHelpers";
|
||||||
import { type WebhookEvent, WebhookEvents } from "~/utils/types";
|
import { WebhookEventBodySchema, WebhookEvents } from "~/utils/types";
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
|
@ -16,24 +16,8 @@ export default async function handler(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const requestBody = req.body as {
|
try {
|
||||||
data: {
|
const requestBody = WebhookEventBodySchema.parse(req.body);
|
||||||
id: string;
|
|
||||||
email_addresses:
|
|
||||||
| [
|
|
||||||
{
|
|
||||||
email_address: string;
|
|
||||||
id: string;
|
|
||||||
verification: {
|
|
||||||
status: string;
|
|
||||||
strategy: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
]
|
|
||||||
| null;
|
|
||||||
};
|
|
||||||
type: WebhookEvent;
|
|
||||||
};
|
|
||||||
|
|
||||||
switch (requestBody.type) {
|
switch (requestBody.type) {
|
||||||
case WebhookEvents.USER_CREATED:
|
case WebhookEvents.USER_CREATED:
|
||||||
|
@ -45,7 +29,11 @@ export default async function handler(
|
||||||
return;
|
return;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
res.status(400).json({ error: "INVALID WEBHOOK EVENT" });
|
res.status(400).json({ error: "INVALID WEBHOOK EVENT TYPE" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
res.status(400).json({ error: "INVALID WEBHOOK EVENT BODY" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
type BetterEnum<T> = T[keyof T];
|
type BetterEnum<T> = T[keyof T];
|
||||||
|
|
||||||
export const EventTypes = {
|
export const EventTypes = {
|
||||||
|
@ -14,6 +16,27 @@ export const WebhookEvents = {
|
||||||
} as const;
|
} as const;
|
||||||
export type WebhookEvent = BetterEnum<typeof WebhookEvents>;
|
export type WebhookEvent = BetterEnum<typeof WebhookEvents>;
|
||||||
|
|
||||||
|
export const WebhookEventBodySchema = z.object({
|
||||||
|
data: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
email_addresses: z
|
||||||
|
.array(
|
||||||
|
z.object({
|
||||||
|
email_address: z.string().email(),
|
||||||
|
id: z.string(),
|
||||||
|
verification: z.object({
|
||||||
|
status: z.string(),
|
||||||
|
strategy: z.string(),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.optional(),
|
||||||
|
}),
|
||||||
|
type: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type WebhookEventBody = z.infer<typeof WebhookEventBodySchema>;
|
||||||
|
|
||||||
export interface PresenceItem {
|
export interface PresenceItem {
|
||||||
name: string;
|
name: string;
|
||||||
image: string;
|
image: string;
|
||||||
|
|
Loading…
Add table
Reference in a new issue