-
Notifications
You must be signed in to change notification settings - Fork 4.4k
docs: trigger webhook update for new payload received #2017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anmol098
wants to merge
5
commits into
ComposioHQ:next
Choose a base branch
from
anmol098:docs/triggers-fixes
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+73
−34
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e7f90d4
docs: trigger webhook update for new payload received
anmol098 5edfc22
Merge branch 'next' into docs/triggers-fixes
Sushmithamallesh 25e1d73
Merge branch 'next' into docs/triggers-fixes
anmol098 0edc52e
Merge branch 'next' into docs/triggers-fixes
anmol098 71b28a7
Merge branch 'next' into docs/triggers-fixes
anmol098 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| 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', | ||
| }); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.datais directly assignedpayload.data. This assumes the incoming webhook payload always has adatafield and that it includes all necessary properties, such asconnection_nano_id,trigger_nano_id, anduser_id, which were previously extracted from other payload parts. This could lead to missing data or runtime errors.