Created
May 25, 2024 20:04
-
-
Save decentration/5f1e1af6281924609ea5923723468b6b to your computer and use it in GitHub Desktop.
Process webhook
This file contains 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
socket is kind of over kill | |
because we can just fetch in intervals of 5 seconds or something like i am doing on the frontend | |
Define the function: | |
```js | |
async fetchLatestFromWebhookSite(uuid) { | |
try { | |
console.log('[fetchLatestFromWebhookSite] Fetching the latest webhook data with webhook id UUID:', uuid); | |
const response = await axios.get(`${baseURL}/api/webhook/fetchWebhookData/${uuid}`); // Replace with your actual API endpoint | |
console.log('Response Webhook Data:', response); | |
console.log('Latest Webhook Data:', response.data); // Log the data | |
console.log('Webhook event received:', response.data.data); | |
return response.data; // Return the data for further processing | |
} catch (error) { | |
console.error('Error fetching latest webhook data:', error); | |
throw error; // Rethrow the error for handling in the calling function | |
} | |
} | |
``` | |
2.function to wait for new webhook event: | |
``` | |
export const waitForNewWebhookEvent = async (uuid, webhookFetchStartTime, nodeId) => { | |
let foundNewEvent = false; | |
let processedEventData = null; | |
while (!foundNewEvent && useAppStore.getState().isExecuting && useAppStore.getState().nodeLoadingStates[nodeId]) { | |
try { | |
const webhookData = await WebhooksService.fetchLatestFromWebhookSite(uuid); | |
const { processedEventData: newEventData, isNewEvent } = processWebhookEvent(webhookData, webhookFetchStartTime); | |
if (isNewEvent) { | |
foundNewEvent = true; | |
processedEventData = newEventData; | |
break; // Exit the loop if a new event is found | |
} | |
} catch (error) { | |
console.error('Error fetching new webhook event:', error); | |
} | |
// Wait for a specified interval before polling again | |
await new Promise(resolve => setTimeout(resolve, 2000)); | |
} | |
return processedEventData; | |
}; | |
``` | |
3. the function to process event: | |
export const processWebhookEvent = (webhookEventData, webhookFetchStartTime) => { | |
const fetchStartTimeUTC = webhookFetchStartTime.getTime() + (webhookFetchStartTime.getTimezoneOffset() * 60000); | |
console.log('Comparing against fetch start time:', fetchStartTimeUTC); | |
console.log('processWebhookEvent webhookEventData:', webhookEventData); | |
const eventCreatedAtUTC = new Date(webhookEventData.created_at).getTime(); | |
console.log('Event created at:', eventCreatedAtUTC); | |
if (eventCreatedAtUTC > fetchStartTimeUTC) { | |
const processedEventData = { | |
query: webhookEventData.query || null, | |
content: parseWebhookContent(webhookEventData.content || null), | |
headers: webhookEventData.headers || null, | |
method: webhookEventData.method || null, | |
createdAt: webhookEventData.created_at || null, | |
}; | |
return { processedEventData, isNewEvent: true }; | |
} else { | |
return { processedEventData: null, isNewEvent: false }; | |
} | |
}; | |
Ramsey (Decentration) — Today at 21:02 | |
then the general processing area for webhook: | |
case 'webhook': | |
const webhookFetchStartTime = new Date(); | |
setIsLoadingNode( currentNode.id, true); | |
console.log('Webhook fetch start time:', webhookFetchStartTime.toISOString()); | |
try { | |
const webhookData = await WebhooksService.fetchLatestFromWebhookSite(currentNode.formData.uuid); | |
const { processedEventData, isNewEvent } = processWebhookEvent(webhookData, webhookFetchStartTime); | |
if (!isNewEvent) { | |
console.log('Waiting for new webhook event...', currentNode.id); | |
// Directly start waiting for a new event without user input | |
const newEventData = await waitForNewWebhookEvent(currentNode.formData.uuid, webhookFetchStartTime, currentNode.id); | |
if (newEventData) { | |
// Process the new event data | |
updateNodeResponseData(activeScenarioId, updatedExecutionId, currentNode.id, { eventData: newEventData }); | |
} | |
} else { | |
// If the event is already new, process it immediately | |
updateNodeResponseData(activeScenarioId, updatedExecutionId, currentNode.id, { eventData: processedEventData }); | |
} | |
} catch (error) { | |
console.error('Error waiting for webhook data:', error); | |
updateNodeResponseData(activeScenarioId, updatedExecutionId, currentNode.id, { error: error.message }); | |
} finally { | |
} | |
executionCycleFinished = index === orderedList.length - 1; | |
break; | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment