Skip to content

Commit 1b2fc79

Browse files
authored
feat: add http adapter (#231)
* feat: add http adapter
1 parent 0606122 commit 1b2fc79

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

lib/notification/adapter/http.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
};

lib/notification/adapter/http.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
### HTTP Adapter
2+
3+
This is a generic adapter for sending notifications via HTTP requests.
4+
You can leverage this adapter to integrate with various webhooks or APIs that accept HTTP requests. (e.g. Supabase
5+
Functions, a Node.js server, etc.)
6+
7+
HTTP adapter supports a `authToken` field, which can be used to include an authorization token in the request headers.
8+
Your token would be included as a Bearer token in the `Authorization` header, which is a common method for securing API requests.
9+
10+
Request Details:
11+
<details>
12+
Request Method: POST
13+
14+
Headers:
15+
16+
```
17+
Content Type: `application/json`
18+
Authorization: Bearer {your-optional-auth-token}
19+
```
20+
21+
Body:
22+
23+
```json
24+
{
25+
"jobId": "mg1waX4RHmIzL5NDYtYp-",
26+
"provider": "immoscout",
27+
"timestamp": "2024-06-15T12:34:56Z",
28+
"listings": [
29+
{
30+
"address": "Str. 123, Bielefeld, Germany",
31+
"description": "Möbliert: Einziehen & wohlfühlen: Neu möbliert.",
32+
"id": "123456789",
33+
"imageUrl": "https://<target-url>.com/listings/123456789.jpg",
34+
"price": "1.240 €",
35+
"size": "38 m²",
36+
"title": "Schöne 1-Zimmer-Wohnung in Bielefeld",
37+
"url": "https://<target-url>.com/listings/123456789"
38+
}
39+
]
40+
}
41+
```
42+
43+
</details>

0 commit comments

Comments
 (0)