@digicms/cms
Version:
An open source headless CMS solution to create and manage your own API. It provides a powerful dashboard and features to make your life easier. Databases supported: MySQL, MariaDB, PostgreSQL, SQLite
38 lines (28 loc) • 1.07 kB
JavaScript
;
const { join, extname, basename } = require('path');
const fse = require('fs-extra');
const { importDefault } = require('@strapi/utils');
// TODO:: allow folders with index.js inside for bigger policies
module.exports = async function loadMiddlewares(strapi) {
const localMiddlewares = await loadLocalMiddlewares(strapi);
const internalMiddlewares = require('../../middlewares');
strapi.container.get('middlewares').add(`global::`, localMiddlewares);
strapi.container.get('middlewares').add(`strapi::`, internalMiddlewares);
};
const loadLocalMiddlewares = async (strapi) => {
const dir = strapi.dirs.dist.middlewares;
if (!(await fse.pathExists(dir))) {
return {};
}
const middlewares = {};
const paths = await fse.readdir(dir, { withFileTypes: true });
for (const fd of paths) {
const { name } = fd;
const fullPath = join(dir, name);
if (fd.isFile() && extname(name) === '.js') {
const key = basename(name, '.js');
middlewares[key] = importDefault(fullPath);
}
}
return middlewares;
};