Skip to content

Commit 93fbfb4

Browse files
committed
Added the callback endpoint (no API rate limit)
1 parent b9edd2e commit 93fbfb4

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { type ActionFunctionArgs, json } from "@remix-run/server-runtime";
2+
import {
3+
type CompleteWaitpointTokenResponseBody,
4+
conditionallyExportPacket,
5+
stringifyIO,
6+
} from "@trigger.dev/core/v3";
7+
import { WaitpointId } from "@trigger.dev/core/v3/isomorphic";
8+
import { z } from "zod";
9+
import { $replica } from "~/db.server";
10+
import { logger } from "~/services/logger.server";
11+
import { engine } from "~/v3/runEngine.server";
12+
13+
const paramsSchema = z.object({
14+
waitpointFriendlyId: z.string(),
15+
});
16+
17+
export async function action({ request, params }: ActionFunctionArgs) {
18+
if (request.method.toUpperCase() !== "POST") {
19+
return json({ error: "Method not allowed" }, { status: 405, headers: { Allow: "POST" } });
20+
}
21+
22+
const { waitpointFriendlyId } = paramsSchema.parse(params);
23+
const waitpointId = WaitpointId.toId(waitpointFriendlyId);
24+
25+
try {
26+
//check permissions
27+
const waitpoint = await $replica.waitpoint.findFirst({
28+
where: {
29+
id: waitpointId,
30+
},
31+
});
32+
33+
if (!waitpoint) {
34+
throw json({ error: "Waitpoint not found" }, { status: 404 });
35+
}
36+
37+
if (waitpoint.status === "COMPLETED") {
38+
return json<CompleteWaitpointTokenResponseBody>({
39+
success: true,
40+
});
41+
}
42+
43+
const body = await request.json();
44+
45+
const stringifiedData = await stringifyIO(body);
46+
const finalData = await conditionallyExportPacket(
47+
stringifiedData,
48+
`${waitpointId}/waitpoint/http-callback`
49+
);
50+
51+
const result = await engine.completeWaitpoint({
52+
id: waitpointId,
53+
output: finalData.data
54+
? { type: finalData.dataType, value: finalData.data, isError: false }
55+
: undefined,
56+
});
57+
58+
return json<CompleteWaitpointTokenResponseBody>(
59+
{
60+
success: true,
61+
},
62+
{ status: 200 }
63+
);
64+
} catch (error) {
65+
logger.error("Failed to complete waitpoint token", { error });
66+
throw json({ error: "Failed to complete waitpoint token" }, { status: 500 });
67+
}
68+
}

apps/webapp/app/services/apiRateLimit.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const apiRateLimiter = authorizationRateLimitMiddleware({
5959
"/api/v1/usage/ingest",
6060
"/api/v1/auth/jwt/claims",
6161
/^\/api\/v1\/runs\/[^\/]+\/attempts$/, // /api/v1/runs/$runFriendlyId/attempts
62+
/^\/api\/v1\/waitpoints\/http-callback\/[^\/]+\/callback$/, // /api/v1/waitpoints/http-callback/$waitpointFriendlyId/callback
6263
],
6364
log: {
6465
rejections: env.API_RATE_LIMIT_REJECTION_LOGS_ENABLED === "1",

0 commit comments

Comments
 (0)