|
| 1 | +import { ActionFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { webhooks } from "@trigger.dev/sdk/v3"; |
| 3 | +import { WebhookError } from "@trigger.dev/sdk/v3"; |
| 4 | +import { logger } from "~/services/logger.server"; |
| 5 | + |
| 6 | +/* |
| 7 | + This route is for testing our webhooks |
| 8 | +*/ |
| 9 | +export async function action({ request }: ActionFunctionArgs) { |
| 10 | + // Make sure this is a POST request |
| 11 | + if (request.method !== "POST") { |
| 12 | + return json({ error: "[Webhook Internal Test] Method not allowed" }, { status: 405 }); |
| 13 | + } |
| 14 | + |
| 15 | + const clonedRequest = request.clone(); |
| 16 | + const rawBody = await clonedRequest.text(); |
| 17 | + logger.log("[Webhook Internal Test] Raw body:", { rawBody }); |
| 18 | + |
| 19 | + try { |
| 20 | + // Construct and verify the webhook event |
| 21 | + const event = await webhooks.constructEvent(request, process.env.INTERNAL_TEST_WEBHOOK_SECRET!); |
| 22 | + |
| 23 | + // Handle the webhook event |
| 24 | + logger.log("[Webhook Internal Test] Received verified webhook:", event); |
| 25 | + |
| 26 | + // Process the event based on its type |
| 27 | + switch (event.type) { |
| 28 | + default: |
| 29 | + logger.log(`[Webhook Internal Test] Unhandled event type: ${event.type}`); |
| 30 | + } |
| 31 | + |
| 32 | + // Return a success response |
| 33 | + return json({ received: true }, { status: 200 }); |
| 34 | + } catch (err) { |
| 35 | + // Handle webhook errors |
| 36 | + if (err instanceof WebhookError) { |
| 37 | + logger.error("[Webhook Internal Test] Webhook error:", { message: err.message }); |
| 38 | + return json({ error: err.message }, { status: 400 }); |
| 39 | + } |
| 40 | + |
| 41 | + if (err instanceof Error) { |
| 42 | + logger.error("[Webhook Internal Test] Error processing webhook:", { message: err.message }); |
| 43 | + return json({ error: err.message }, { status: 400 }); |
| 44 | + } |
| 45 | + |
| 46 | + // Handle other errors |
| 47 | + logger.error("[Webhook Internal Test] Error processing webhook:", { err }); |
| 48 | + return json({ error: "Internal server error" }, { status: 500 }); |
| 49 | + } |
| 50 | +} |
0 commit comments