-
Notifications
You must be signed in to change notification settings - Fork 1
/
webflowSyndicationRouter.js
108 lines (97 loc) · 2.94 KB
/
webflowSyndicationRouter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const express = require("express");
const Webflow = require("webflow-api");
const Bottleneck = require("bottleneck");
require("dotenv").config();
const router = express.Router();
const limiter = new Bottleneck({
minTime: 1000,
});
// Array of sites with siteId, collectionId, and siteApiKey
const sites = [
{
siteId: "64dbf04907aed84bec098152",
collectionId: "64dbf04907aed84bec09817c",
siteApiKey: process.env.SITE_ONE_API_KEY,
},
{
siteId: "64dbf0fe3e4ebd3032bc5a36",
collectionId: "64dbf0fe3e4ebd3032bc5a60",
siteApiKey: process.env.SITE_TWO_API_KEY,
},
{
siteId: "64dbf12b8aaca4193c5c5595",
collectionId: "64dbf12b8aaca4193c5c55bf",
siteApiKey: process.env.SITE_THREE_API_KEY,
},
// ... add more sites as needed
];
router.post("/syndicate-post", async (req, res) => {
console.log("Webhook received");
const postData = req.body;
const tasks = sites.map((site) => {
console.log(site.siteId, site.collectionId, site.siteApiKey);
const postBodyHtml = convertToHtml(postData["post-body"]);
const newItem = {
_archived: false,
_draft: false,
name: postData.name,
slug: postData.slug,
"post-summary": postData["post-summary"],
"post-body": postBodyHtml,
featured: postData.featured,
"thumbnail-image": postData["thumbnail-image"].url,
"main-image": postData["main-image"].url,
color: postData.color,
};
const siteWebflow = new Webflow({
token: site.siteApiKey,
});
return limiter
.schedule(() =>
siteWebflow.post(`collections/${site.collectionId}/items?live=true`, {
fields: newItem,
})
)
.then((response) => {
console.log(
`Created post in site with ID: ${site.siteId} and collection ID: ${site.collectionId}`
);
return response;
})
.catch((error) => {
console.error(
`Failed to add to Webflow CMS for site with ID: ${site.siteId} and collection ID: ${site.collectionId}`,
error.response.data
);
throw error; // This will reject the Promise returned by tasks.
});
});
try {
await Promise.all(tasks);
res.status(200).send("Post syndicated across sites");
} catch (error) {
res.status(500).send("Failed to syndicate post across all sites");
}
});
function convertToHtml(data) {
let htmlContent = "";
data.forEach((block) => {
switch (block.type) {
case "Heading":
const headingText = data.find(
(item) => item._id === block.children[0]
).v;
htmlContent += `<${block.tag}>${headingText}</${block.tag}>`;
break;
case "Paragraph":
const paraText = data.find((item) => item._id === block.children[0]).v;
htmlContent += `<${block.tag}>${paraText}</${block.tag}>`;
break;
// Handle other block types as needed
default:
break;
}
});
return htmlContent;
}
module.exports = router;