Skip to content

Instantly share code, notes, and snippets.

@rileyrichter
Created November 2, 2023 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rileyrichter/0b60368c48760edcf65b4a354c914126 to your computer and use it in GitHub Desktop.
Save rileyrichter/0b60368c48760edcf65b4a354c914126 to your computer and use it in GitHub Desktop.
Keep your app, Webflow, SQLite, and Algolia in sync...
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Global variables and imports
const express = require("express");
const algoliasearch = require("algoliasearch");
const axios = require("axios");
const Webflow = require("webflow-api");
const router = express.Router();
const Bottleneck = require("bottleneck");
const NodeCache = require("node-cache");
// setup Bottleneck to limit API calls to 1 per second (60RPM)
const limiter = new Bottleneck({
minTime: 1000,
});
// initialize Webflow API client
const webflow = new Webflow({
token: process.env.WEBFLOW_API_TOKEN,
});
// Set the ID for the multi-reference field that holds categories
const categoriesCollectionId = process.env.CATEGORIES_COLLECTION_ID;
// setup NodeCache to cache categories for 10 minutes
const myCache = new NodeCache({ stdTTL: 600, checkperiod: 600 });
// fetch categories from Webflow
async function fetchCategories() {
// check if categories are cached
let categories = myCache.get("categories");
if (categories) {
console.log("Using cached categories");
} else {
const webflowLimit = 100;
let currentOffset = 0;
categories = [];
console.log("No cached categories, fetching from Webflow");
// Fetch categories from Webflow
// loop through all categories
while (true) {
try {
const fetchedCategories = await limiter.schedule(() =>
webflow.get(
`/collections/${categoriesCollectionId}/items?limit=${webflowLimit}&offset=${currentOffset}`
)
);
if (!fetchedCategories.data.items) {
console.error("fetchedCategories.data.items is undefined!");
}
categories = categories.concat(fetchedCategories.data.items);
if (fetchedCategories.data.items.length < webflowLimit) {
break;
}
currentOffset += webflowLimit;
console.log(
"Fetching more categories from Webflow at offset",
currentOffset
);
} catch (error) {
console.error("Webflow API error:", error);
}
}
// transform categories into a map
const categoriesMap = transformCategories(categories);
console.log("Categories fetched from Webflow");
myCache.set("categories", categoriesMap);
categories = categoriesMap;
}
return categories;
}
// function to transform categories into a map
const transformCategories = (categories) => {
return categories.reduce((map, category) => {
map[category._id] = category.name;
return map;
}, {});
};
// initialize Algolia client
const client = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY
);
const index = client.initIndex("posts");
// Webhook endpoint
router.post("/webhook-endpoint", async (req, res) => {
try {
console.log("Event received");
const data = req.body;
// Common SOUL endpoints
const soulApiBaseUrl = "http://your-soul-api-url"; // Replace with your SOUL API base URL
const itemsEndpoint = `${soulApiBaseUrl}/items`;
// if the event is a delete event, delete the object from Algolia
if (data.deleted) {
console.log("This is a delete event");
console.log(`Deleting object ${data.itemId} from Algolia`);
index
.deleteObject(data.itemId)
.then(() => {
console.log(`Deleted object ${data.itemId} from Algolia`);
res.status(200).send("OK");
})
.catch((err) => {
console.error("Error deleting object from Algolia: ", err);
res.status(500).send("Error");
});
// Delete item from SQLite via SOUL
await axios
.delete(`${itemsEndpoint}/${data.itemId}`)
.then((response) => {
console.log(`Deleted item ${data.itemId} from SQLite`);
})
.catch((error) => {
console.error("SOUL API error:", error);
});
}
// if the event is creating or updating an item in my specific collection
// fetch categories from Webflow and save the object to Algolia and SQLite
else if (data._cid === process.env.COLLECTION_ID) {
console.log("This is a create or update event");
// fetch categories from Webflow
const categoriesMap = await fetchCategories();
const categories = data.categories
? data.categories.map((id) => categoriesMap[id])
: [];
// create object to save to Algolia
const objectToUpdate = {
objectID: data._id,
name: data.name,
slug: data.slug,
summary: data["post-summary"],
categories: categories,
image: data["main-image"] ? data["main-image"].url : null,
};
console.log(
`Saving object to Algolia: ${JSON.stringify(objectToUpdate)}`
);
// save object to Algolia
index
.saveObject(objectToUpdate)
.then(({ objectID }) => {
console.log(`Saved object ${objectID} to Algolia`);
res.status(200).send("OK");
})
.catch((err) => {
console.error("Error saving object to Algolia: ", err);
res.status(500).send("Error");
});
// Prepare data for SQLite via SOUL
const itemData = {
id: data._id,
name: data.name,
slug: data.slug,
summary: data["post-summary"],
categories: JSON.stringify(categories),
image: data["main-image"] ? data["main-image"].url : null,
};
// Check if item already exists in SQLite via SOUL
axios
.get(`${itemsEndpoint}/${data._id}`)
.then(async (response) => {
if (response.data) {
// Update item in SQLite via SOUL
await axios
.put(`${itemsEndpoint}/${data._id}`, itemData)
.then((response) => {
console.log(`Updated item ${data._id} in SQLite`);
})
.catch((error) => {
console.error("SOUL API error:", error);
});
} else {
// Insert item into SQLite via SOUL
await axios
.post(itemsEndpoint, itemData)
.then((response) => {
console.log(`Inserted item ${data._id} into SQLite`);
})
.catch((error) => {
console.error("SOUL API error:", error);
});
}
})
.catch((error) => {
console.error("SOUL API error:", error);
});
} else {
// if the event is creating or updating an
// item in a different collection do nothing
console.log("Do nothing, this is not the Articles collection");
}
} catch (error) {
console.error("Error in /webhook-endpoint: ", error);
res.status(500).send("Internal server error");
}
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment