Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 73 additions & 34 deletions fern/snippets/triggers/typescript/trigger-webhook.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,96 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { TriggerEvent } from '@composio/core';

// Define type-safe payload for GitHub Star Added event
export type GitHubStarAddedEventPayload = {
action: "created";
repository_id: number;
repository_name: string;
repository_url: string;
starred_at: string;
starred_by: string;
// Type definition for Gmail Webhook event
export type GmailNewEmailEventPayload = {
type: string;
timestamp: string;
data: {
attachment_list: Array<any>;
id: string;
label_ids: Array<string>;
message_id: string;
message_text: string;
message_timestamp: string;
payload: {
body: {
size: number;
};
filename: string;
headers: Array<{
name: string;
value: string;
}>;
mimeType: string;
partId: string;
parts: Array<{
body: {
data: string;
size: number;
};
filename: string;
headers: Array<{
name: string;
value: string;
}>;
mimeType: string;
partId: string;
}>;
};
preview: {
body: string;
subject: string;
};
sender: string;
subject: string;
thread_id: string;
to: string;
connection_id: string;
connection_nano_id: string;
trigger_nano_id: string;
trigger_id: string;
user_id: string;
};
};

// Type-safe handler function
function handleGitHubStarAddedEvent(event: TriggerEvent<GitHubStarAddedEventPayload>) {
console.log(`⭐ ${event.data.repository_name} starred by ${event.data.starred_by}`);
// Handler for Gmail "new email received" event
function handleGmailNewEmailEvent(event: TriggerEvent<GmailNewEmailEventPayload['data']>) {
console.log(`📧 New email received from: ${event.data.sender}`);
console.log(`Subject: ${event.data.subject}`);
console.log(`Preview: ${event.data.preview.body}`);
console.log(`Message ID: ${event.data.message_id}`);
}

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'POST') {
return res.status(405).json({
status: 'error',
message: 'Method not allowed. Only POST requests are accepted.'
return res.status(405).json({
status: 'error',
message: 'Method not allowed. Only POST requests are accepted.',
});
}

try {
const payload = req.body;

// Type-safe webhook payload processing
if (payload.triggerSlug === 'GITHUB_STAR_ADDED_EVENT') {
const starEvent: TriggerEvent<GitHubStarAddedEventPayload> = {
type: payload.triggerSlug,
// Ensure we're processing Gmail webhook events
if (payload.type === 'gmail_new_email_event') {
const gmailEvent: TriggerEvent<GmailNewEmailEventPayload['data']> = {
type: payload.type,
timestamp: new Date().toISOString(),
data: {
...payload.payload as GitHubStarAddedEventPayload,
connection_nano_id: payload.metadata?.connectedAccount?.id || '',
trigger_nano_id: payload.id || '',
user_id: payload.userId || '',
}
data: payload.data,
};
handleGitHubStarAddedEvent(starEvent);

handleGmailNewEmailEvent(gmailEvent);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Webhook Payload Data Assignment Issue

The gmailEvent.data is directly assigned payload.data. This assumes the incoming webhook payload always has a data field and that it includes all necessary properties, such as connection_nano_id, trigger_nano_id, and user_id, which were previously extracted from other payload parts. This could lead to missing data or runtime errors.

Fix in Cursor Fix in Web

}
res.status(200).json({
status: 'success',
message: 'Webhook received and processed successfully'

res.status(200).json({
status: 'success',
message: 'Webhook received and processed successfully',
});
} catch (error) {
console.error('Error processing webhook:', error);
res.status(500).json({
status: 'error',
message: 'Internal server error while processing webhook'
res.status(500).json({
status: 'error',
message: 'Internal server error while processing webhook',
});
}
}