Rezept: Webhook-Integration
Übersicht
Abschnitt betitelt „Übersicht“Verwenden Sie die Callbacks afterSave und afterDelete, um externe Dienste zu benachrichtigen, wenn sich Daten in Rebase ändern.
Slack-Benachrichtigung bei neuer Bestellung
Abschnitt betitelt „Slack-Benachrichtigung bei neuer Bestellung“const ordersCollection: EntityCollection = { slug: "orders", name: "Orders", table: "orders", callbacks: { afterSave: async ({ values, entityId, status }) => { if (status === "new") { await fetch(process.env.SLACK_WEBHOOK_URL!, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ text: `🛒 New order #${entityId}\nCustomer: ${values.customer_name}\nTotal: $${values.total}` }) }); } } }, properties: { /* ... */ }};Synchronisation mit externer API
Abschnitt betitelt „Synchronisation mit externer API“callbacks: { afterSave: async ({ values, entityId }) => { // Sync product to Shopify await fetch("https://your-shop.myshopify.com/admin/api/2024-01/products.json", { method: "PUT", headers: { "Content-Type": "application/json", "X-Shopify-Access-Token": process.env.SHOPIFY_TOKEN! }, body: JSON.stringify({ product: { id: values.shopify_id, title: values.name, body_html: values.description, variants: [{ price: values.price }] } }) }); },
afterDelete: async ({ entityId, entity }) => { // Remove from Shopify if (entity.values.shopify_id) { await fetch( `https://your-shop.myshopify.com/admin/api/2024-01/products/${entity.values.shopify_id}.json`, { method: "DELETE", headers: { "X-Shopify-Access-Token": process.env.SHOPIFY_TOKEN! } } ); } }}Fehlerbehandlung
Abschnitt betitelt „Fehlerbehandlung“Verwenden Sie afterSaveError, um Fehler elegant zu behandeln:
callbacks: { afterSave: async ({ values, entityId }) => { // This might fail await syncToExternalService(values); }, afterSaveError: async ({ entityId, error }) => { // Log the error, send alert, or retry console.error(`Webhook failed for entity ${entityId}:`, error); await sendErrorAlert(entityId, error); }}Nächste Schritte
Abschnitt betitelt „Nächste Schritte“- Entity-Callbacks — Vollständige Callback-Referenz
- Blog-CMS-Rezept — Vollständiges Blog-Beispiel