|
| 1 | +import { markdown2Html } from '../../services/markdown.js'; |
| 2 | + |
| 3 | +const mapListing = (listing) => ({ |
| 4 | + address: listing.address, |
| 5 | + description: listing.description, |
| 6 | + id: listing.id, |
| 7 | + imageUrl: listing.image, |
| 8 | + price: listing.price, |
| 9 | + size: listing.size, |
| 10 | + title: listing.title, |
| 11 | + url: listing.link, |
| 12 | +}); |
| 13 | + |
| 14 | +export const send = ({ serviceName, newListings, notificationConfig, jobKey }) => { |
| 15 | + const { authToken, endpointUrl } = notificationConfig.find((a) => a.id === config.id).fields; |
| 16 | + |
| 17 | + const listings = newListings.map(mapListing); |
| 18 | + const body = { |
| 19 | + jobId: jobKey, |
| 20 | + timestamp: new Date().toISOString(), |
| 21 | + provider: serviceName, |
| 22 | + listings, |
| 23 | + }; |
| 24 | + |
| 25 | + const headers = { |
| 26 | + 'Content-Type': 'application/json', |
| 27 | + }; |
| 28 | + if (authToken != null) { |
| 29 | + headers['Authorization'] = `Bearer ${authToken}`; |
| 30 | + } |
| 31 | + |
| 32 | + return fetch(endpointUrl, { |
| 33 | + method: 'POST', |
| 34 | + headers: headers, |
| 35 | + body: JSON.stringify(body), |
| 36 | + }); |
| 37 | +}; |
| 38 | + |
| 39 | +export const config = { |
| 40 | + id: 'http', |
| 41 | + name: 'HTTP', |
| 42 | + readme: markdown2Html('lib/notification/adapter/http.md'), |
| 43 | + description: 'Fredy will send a generic HTTP POST request.', |
| 44 | + fields: { |
| 45 | + endpointUrl: { |
| 46 | + description: "Your application's endpoint URL.", |
| 47 | + label: 'Endpoint URL', |
| 48 | + type: 'text', |
| 49 | + }, |
| 50 | + authToken: { |
| 51 | + description: "Your application's auth token, if required by your endpoint.", |
| 52 | + label: 'Auth token (optional)', |
| 53 | + optional: true, |
| 54 | + type: 'text', |
| 55 | + }, |
| 56 | + }, |
| 57 | +}; |
0 commit comments