// Source: https://github.com/strapi/strapi/blob/master/packages/strapi/lib/middlewares/router/index.js // Strapi version: 3.6.0
Last active
April 27, 2021 09:15
-
-
Save flaki/7db47d57fec5234479659b24e877188a to your computer and use it in GitHub Desktop.
Patched Strapi router to honor prefix routes for the Admin interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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