|
| 1 | +import { ActionFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { FinalizeDeploymentRequestBody } from "@trigger.dev/core/v3"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { authenticateApiRequest } from "~/services/apiAuth.server"; |
| 5 | +import { logger } from "~/services/logger.server"; |
| 6 | +import { ServiceValidationError } from "~/v3/services/baseService.server"; |
| 7 | +import { FinalizeDeploymentV2Service } from "~/v3/services/finalizeDeploymentV2.server"; |
| 8 | + |
| 9 | +const ParamsSchema = z.object({ |
| 10 | + deploymentId: z.string(), |
| 11 | +}); |
| 12 | + |
| 13 | +export async function action({ request, params }: ActionFunctionArgs) { |
| 14 | + // Ensure this is a POST request |
| 15 | + if (request.method.toUpperCase() !== "POST") { |
| 16 | + return { status: 405, body: "Method Not Allowed" }; |
| 17 | + } |
| 18 | + |
| 19 | + const parsedParams = ParamsSchema.safeParse(params); |
| 20 | + |
| 21 | + if (!parsedParams.success) { |
| 22 | + return json({ error: "Invalid params" }, { status: 400 }); |
| 23 | + } |
| 24 | + |
| 25 | + // Next authenticate the request |
| 26 | + const authenticationResult = await authenticateApiRequest(request); |
| 27 | + |
| 28 | + if (!authenticationResult) { |
| 29 | + logger.info("Invalid or missing api key", { url: request.url }); |
| 30 | + return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 31 | + } |
| 32 | + |
| 33 | + const authenticatedEnv = authenticationResult.environment; |
| 34 | + |
| 35 | + const { deploymentId } = parsedParams.data; |
| 36 | + |
| 37 | + const rawBody = await request.json(); |
| 38 | + const body = FinalizeDeploymentRequestBody.safeParse(rawBody); |
| 39 | + |
| 40 | + if (!body.success) { |
| 41 | + return json({ error: "Invalid body", issues: body.error.issues }, { status: 400 }); |
| 42 | + } |
| 43 | + |
| 44 | + try { |
| 45 | + // Create a text stream chain |
| 46 | + const stream = new TransformStream(); |
| 47 | + const encoder = new TextEncoderStream(); |
| 48 | + const writer = stream.writable.getWriter(); |
| 49 | + |
| 50 | + const service = new FinalizeDeploymentV2Service(); |
| 51 | + |
| 52 | + // Chain the streams: stream -> encoder -> response |
| 53 | + const response = new Response(stream.readable.pipeThrough(encoder), { |
| 54 | + headers: { |
| 55 | + "Content-Type": "text/event-stream", |
| 56 | + "Cache-Control": "no-cache", |
| 57 | + Connection: "keep-alive", |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + service |
| 62 | + .call(authenticatedEnv, deploymentId, body.data, writer) |
| 63 | + .then(async () => { |
| 64 | + await writer.write(`event: complete\ndata: ${JSON.stringify({ id: deploymentId })}\n\n`); |
| 65 | + await writer.close(); |
| 66 | + }) |
| 67 | + .catch(async (error) => { |
| 68 | + let errorMessage; |
| 69 | + |
| 70 | + if (error instanceof ServiceValidationError) { |
| 71 | + errorMessage = { error: error.message }; |
| 72 | + } else if (error instanceof Error) { |
| 73 | + logger.error("Error finalizing deployment", { error: error.message }); |
| 74 | + errorMessage = { error: `Internal server error: ${error.message}` }; |
| 75 | + } else { |
| 76 | + logger.error("Error finalizing deployment", { error: String(error) }); |
| 77 | + errorMessage = { error: "Internal server error" }; |
| 78 | + } |
| 79 | + |
| 80 | + await writer.write(`event: error\ndata: ${JSON.stringify(errorMessage)}\n\n`); |
| 81 | + await writer.close(); |
| 82 | + }); |
| 83 | + |
| 84 | + return response; |
| 85 | + } catch (error) { |
| 86 | + if (error instanceof ServiceValidationError) { |
| 87 | + return json({ error: error.message }, { status: 400 }); |
| 88 | + } else if (error instanceof Error) { |
| 89 | + logger.error("Error finalizing deployment", { error: error.message }); |
| 90 | + return json({ error: `Internal server error: ${error.message}` }, { status: 500 }); |
| 91 | + } else { |
| 92 | + logger.error("Error finalizing deployment", { error: String(error) }); |
| 93 | + return json({ error: "Internal server error" }, { status: 500 }); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments