diff --git a/lib/notification/adapter/http.js b/lib/notification/adapter/http.js new file mode 100644 index 00000000..29a3ca11 --- /dev/null +++ b/lib/notification/adapter/http.js @@ -0,0 +1,57 @@ +import { markdown2Html } from '../../services/markdown.js'; + +const mapListing = (listing) => ({ + address: listing.address, + description: listing.description, + id: listing.id, + imageUrl: listing.image, + price: listing.price, + size: listing.size, + title: listing.title, + url: listing.link, +}); + +export const send = ({ serviceName, newListings, notificationConfig, jobKey }) => { + const { authToken, endpointUrl } = notificationConfig.find((a) => a.id === config.id).fields; + + const listings = newListings.map(mapListing); + const body = { + jobId: jobKey, + timestamp: new Date().toISOString(), + provider: serviceName, + listings, + }; + + const headers = { + 'Content-Type': 'application/json', + }; + if (authToken != null) { + headers['Authorization'] = `Bearer ${authToken}`; + } + + return fetch(endpointUrl, { + method: 'POST', + headers: headers, + body: JSON.stringify(body), + }); +}; + +export const config = { + id: 'http', + name: 'HTTP', + readme: markdown2Html('lib/notification/adapter/http.md'), + description: 'Fredy will send a generic HTTP POST request.', + fields: { + endpointUrl: { + description: "Your application's endpoint URL.", + label: 'Endpoint URL', + type: 'text', + }, + authToken: { + description: "Your application's auth token, if required by your endpoint.", + label: 'Auth token (optional)', + optional: true, + type: 'text', + }, + }, +}; diff --git a/lib/notification/adapter/http.md b/lib/notification/adapter/http.md new file mode 100644 index 00000000..497db02d --- /dev/null +++ b/lib/notification/adapter/http.md @@ -0,0 +1,43 @@ +### HTTP Adapter + +This is a generic adapter for sending notifications via HTTP requests. +You can leverage this adapter to integrate with various webhooks or APIs that accept HTTP requests. (e.g. Supabase +Functions, a Node.js server, etc.) + +HTTP adapter supports a `authToken` field, which can be used to include an authorization token in the request headers. +Your token would be included as a Bearer token in the `Authorization` header, which is a common method for securing API requests. + +Request Details: +
+Request Method: POST + +Headers: + +``` +Content Type: `application/json` +Authorization: Bearer {your-optional-auth-token} +``` + +Body: + +```json +{ + "jobId": "mg1waX4RHmIzL5NDYtYp-", + "provider": "immoscout", + "timestamp": "2024-06-15T12:34:56Z", + "listings": [ + { + "address": "Str. 123, Bielefeld, Germany", + "description": "Möbliert: Einziehen & wohlfühlen: Neu möbliert.", + "id": "123456789", + "imageUrl": "https://.com/listings/123456789.jpg", + "price": "1.240 €", + "size": "38 m²", + "title": "Schöne 1-Zimmer-Wohnung in Bielefeld", + "url": "https://.com/listings/123456789" + } + ] +} +``` + +