0
v-firecrawl/apps/api/src/services/webhook.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-04-15 17:01:47 -04:00
import { supabase_service } from "./supabase";
export const callWebhook = async (teamId: string, data: any) => {
2024-04-20 22:37:45 -04:00
try {
2024-04-15 17:01:47 -04:00
const { data: webhooksData, error } = await supabase_service
.from('webhooks')
.select('url')
.eq('team_id', teamId)
.limit(1);
if (error) {
console.error(`Error fetching webhook URL for team ID: ${teamId}`, error.message);
return null;
}
if (!webhooksData || webhooksData.length === 0) {
return null;
}
let dataToSend = [];
if (data.result.links && data.result.links.length !== 0) {
for (let i = 0; i < data.result.links.length; i++) {
dataToSend.push({
content: data.result.links[i].content.content,
markdown: data.result.links[i].content.markdown,
metadata: data.result.links[i].content.metadata,
});
}
}
await fetch(webhooksData[0].url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
success: data.success,
data: dataToSend,
error: data.error || undefined,
}),
2024-04-20 22:37:45 -04:00
});
} catch (error) {
console.error(`Error sending webhook for team ID: ${teamId}`, error.message);
}
};