Skip to content

Instantly share code, notes, and snippets.

@flaki
Last active April 27, 2021 09:15
Show Gist options
  • Save flaki/7db47d57fec5234479659b24e877188a to your computer and use it in GitHub Desktop.
Save flaki/7db47d57fec5234479659b24e877188a to your computer and use it in GitHub Desktop.
Patched Strapi router to honor prefix routes for the Admin interface
'use strict';
/**
* Module dependencies
*/
// Public node modules.
const _ = require('lodash');
const Router = require('koa-router');
const createEndpointComposer = require('./utils/composeEndpoint');
/**
* Router hook
*/
module.exports = strapi => {
const composeEndpoint = createEndpointComposer(strapi);
return {
/**
* Initialize the hook
*/
initialize() {
const routerPrefix = strapi.config.get('middleware.settings.router.prefix', '');
_.forEach(strapi.config.routes, value => {
composeEndpoint(value, { router: strapi.router });
});
strapi.router.prefix(routerPrefix);
if (_.has(strapi.admin, 'config.routes')) {
const router = new Router({
prefix: routerPrefix + '/admin',
});
_.get(strapi.admin, 'config.routes', []).forEach(route => {
composeEndpoint(route, { plugin: 'admin', router });
});
// Mount admin router on Strapi router
strapi.app.use(router.routes()).use(router.allowedMethods());
}
if (strapi.plugins) {
// Parse each plugin's routes.
_.forEach(strapi.plugins, (plugin, pluginName) => {
const router = new Router({
prefix: routerPrefix + `/${pluginName}`,
});
(plugin.config.routes || []).forEach(route => {
const hasPrefix = _.has(route.config, 'prefix');
composeEndpoint(route, {
plugin: pluginName,
router: hasPrefix ? strapi.router : router,
});
});
// Mount plugin router
strapi.app.use(router.routes()).use(router.allowedMethods());
});
}
},
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment