feat: normalization middleware to ensure incoming objects' data types are as expected (simplifies later logic)

This commit is contained in:
Julian Lam
2024-10-17 13:45:56 -04:00
parent c34407d8b8
commit 13c048a515
2 changed files with 16 additions and 0 deletions

View File

@@ -137,6 +137,21 @@ middleware.resolveObjects = async function (req, res, next) {
next(); next();
}; };
middleware.normalize = async function (req, res, next) {
// Normalizes the received data structure
const { body } = req;
const { object } = body;
// Ensure `to` and `cc` are arrays in the object
['to', 'cc'].forEach((prop) => {
if (object[prop] && typeof object[prop] === 'string') {
object[prop] = [object[prop]];
}
});
next();
};
middleware.configureResponse = async function (req, res, next) { middleware.configureResponse = async function (req, res, next) {
res.header('Content-Type', 'application/activity+json'); res.header('Content-Type', 'application/activity+json');
next(); next();

View File

@@ -21,6 +21,7 @@ module.exports = function (app, middleware, controllers) {
const inboxMiddlewares = [ const inboxMiddlewares = [
middleware.activitypub.assertPayload, middleware.activitypub.assertPayload,
middleware.activitypub.resolveObjects, middleware.activitypub.resolveObjects,
middleware.activitypub.normalize,
]; ];
app.get('/actor', middlewares, controllers.activitypub.actors.application); app.get('/actor', middlewares, controllers.activitypub.actors.application);