MCPcopy
hub / github.com/triggerdotdev/trigger.dev / queueEvent

Function queueEvent

apps/proxy/src/events/queueEvent.ts:10–87  ·  view source on GitHub ↗
(request: Request, env: Env)

Source from the content-addressed store, hash-verified

8
9/** Adds the event to an AWS SQS queue, so it can be consumed from the main Trigger.dev API */
10export async function queueEvent(request: Request, env: Env): Promise<Response> {
11 //check there's a private API key
12 const apiKeyResult = getApiKeyFromRequest(request);
13 if (!apiKeyResult || apiKeyResult.type !== "PRIVATE") {
14 return json(
15 { error: "Invalid or Missing API key" },
16 {
17 status: 401,
18 }
19 );
20 }
21
22 //parse the request body
23 try {
24 const anyBody = await request.json();
25 const body = SendEventBodySchema.safeParse(anyBody);
26 if (!body.success) {
27 return json(
28 { error: generateErrorMessage(body.error.issues) },
29 {
30 status: 422,
31 }
32 );
33 }
34
35 // The AWS SDK tries to use crypto from off of the window,
36 // so we need to trick it into finding it where it expects it
37 globalThis.global = globalThis;
38
39 const client = new SQSClient({
40 region: env.AWS_SQS_REGION,
41 credentials: {
42 accessKeyId: env.AWS_SQS_ACCESS_KEY_ID,
43 secretAccessKey: env.AWS_SQS_SECRET_ACCESS_KEY,
44 },
45 });
46
47 const timestamp = body.data.event.timestamp ?? new Date();
48
49 //add the event to the queue
50 const send = new SendMessageCommand({
51 // use wrangler secrets to provide this global variable
52 QueueUrl: env.AWS_SQS_QUEUE_URL,
53 MessageBody: JSON.stringify({
54 event: { ...body.data.event, timestamp },
55 options: body.data.options,
56 apiKey: apiKeyResult.apiKey,
57 }),
58 });
59
60 const queuedEvent = await client.send(send);
61 console.log("Queued event", queuedEvent);
62
63 //respond with the event
64 const event: ApiEventLog = {
65 id: body.data.event.id,
66 name: body.data.event.name,
67 payload: body.data.event.payload,

Callers 1

fetchFunction · 0.90

Calls 7

getApiKeyFromRequestFunction · 0.90
jsonFunction · 0.90
calculateDeliverAtFunction · 0.90
jsonMethod · 0.80
sendMethod · 0.65
logMethod · 0.65
errorMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…