-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathapi.v1.waitpoints.tokens.$waitpointFriendlyId.callback.$hash.ts
More file actions
103 lines (90 loc) · 3.17 KB
/
api.v1.waitpoints.tokens.$waitpointFriendlyId.callback.$hash.ts
File metadata and controls
103 lines (90 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { type ActionFunctionArgs, json } from "@remix-run/server-runtime";
import { type CompleteWaitpointTokenResponseBody, stringifyIO } from "@trigger.dev/core/v3";
import { WaitpointId } from "@trigger.dev/core/v3/isomorphic";
import { z } from "zod";
import { $replica } from "~/db.server";
import { env } from "~/env.server";
import { processWaitpointCompletionPacket } from "~/runEngine/concerns/waitpointCompletionPacket.server";
import type { AuthenticatedEnvironment } from "~/services/apiAuth.server";
import { verifyHttpCallbackHash } from "~/services/httpCallback.server";
import { logger } from "~/services/logger.server";
import { engine } from "~/v3/runEngine.server";
const paramsSchema = z.object({
waitpointFriendlyId: z.string(),
hash: z.string(),
});
export async function action({ request, params }: ActionFunctionArgs) {
if (request.method.toUpperCase() !== "POST") {
return json({ error: "Method not allowed" }, { status: 405, headers: { Allow: "POST" } });
}
const contentLength = request.headers.get("content-length");
if (!contentLength) {
return json({ error: "Content-Length header is required" }, { status: 411 });
}
if (parseInt(contentLength) > env.TASK_PAYLOAD_MAXIMUM_SIZE) {
return json({ error: "Request body too large" }, { status: 413 });
}
const { waitpointFriendlyId, hash } = paramsSchema.parse(params);
const waitpointId = WaitpointId.toId(waitpointFriendlyId);
try {
const waitpoint = await $replica.waitpoint.findFirst({
where: {
id: waitpointId,
},
include: {
environment: {
include: {
project: true,
organization: true,
orgMember: true,
parentEnvironment: {
select: {
apiKey: true,
},
},
},
},
},
});
if (!waitpoint) {
return json({ error: "Waitpoint not found" }, { status: 404 });
}
if (
!verifyHttpCallbackHash(
waitpoint.id,
hash,
waitpoint.environment.parentEnvironment?.apiKey ?? waitpoint.environment.apiKey
)
) {
return json({ error: "Invalid URL, hash doesn't match" }, { status: 401 });
}
if (waitpoint.status === "COMPLETED") {
return json<CompleteWaitpointTokenResponseBody>({
success: true,
});
}
// If the request body is not valid JSON, return an empty object
const body = await request.json().catch(() => ({}));
const stringifiedData = await stringifyIO(body);
const finalData = await processWaitpointCompletionPacket(
stringifiedData,
waitpoint.environment,
`${WaitpointId.toFriendlyId(waitpointId)}/http-callback`
);
const result = await engine.completeWaitpoint({
id: waitpointId,
output: finalData.data
? { type: finalData.dataType, value: finalData.data, isError: false }
: undefined,
});
return json<CompleteWaitpointTokenResponseBody>(
{
success: true,
},
{ status: 200 }
);
} catch (error) {
logger.error("Failed to complete HTTP callback", { error });
throw json({ error: "Failed to complete HTTP callback" }, { status: 500 });
}
}