2018-12-06 18:03:56 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
const _ = require('lodash');
|
|
|
|
const path = require('path');
|
2019-04-15 18:57:58 +02:00
|
|
|
const swaggerUi = require('swagger-ui-dist')
|
2018-12-06 18:03:56 +01:00
|
|
|
|
|
|
|
// Variables.
|
|
|
|
const initialRoutes = [];
|
|
|
|
|
|
|
|
module.exports = strapi => {
|
|
|
|
|
|
|
|
return {
|
|
|
|
beforeInitialize: function() {
|
|
|
|
strapi.config.middleware.load.before.push('documentation');
|
|
|
|
|
|
|
|
initialRoutes.push(..._.cloneDeep(strapi.plugins.documentation.config.routes));
|
|
|
|
},
|
2019-04-15 18:57:58 +02:00
|
|
|
|
2018-12-06 18:03:56 +01:00
|
|
|
initialize: function(cb) {
|
|
|
|
// Find the plugins routes.
|
|
|
|
strapi.plugins.documentation.config.routes = strapi.plugins.documentation.config.routes
|
|
|
|
.map((route, index) => {
|
|
|
|
if (route.handler === 'Documentation.getInfos') {
|
|
|
|
return route;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (route.handler === 'Documentation.index' || route.path === '/login') {
|
|
|
|
route.config.policies = initialRoutes[index].config.policies;
|
|
|
|
}
|
2019-04-15 18:57:58 +02:00
|
|
|
|
2018-12-06 18:03:56 +01:00
|
|
|
// Set prefix to empty to be able to customise it.
|
|
|
|
if (_.get(strapi.plugins, ['documentation', 'config', 'x-strapi-config', 'path'])) {
|
|
|
|
route.config.prefix = '';
|
|
|
|
route.path = `/${strapi.plugins.documentation.config['x-strapi-config'].path}${route.path}`.replace('//', '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
return route;
|
|
|
|
});
|
|
|
|
|
|
|
|
strapi.router.route({
|
|
|
|
method: 'GET',
|
|
|
|
path: '/plugins/documentation/*.*',
|
|
|
|
handler: [
|
|
|
|
async (ctx, next) => {
|
|
|
|
ctx.url = path.basename(ctx.url);
|
|
|
|
|
2019-04-15 18:57:58 +02:00
|
|
|
return await strapi.koaMiddlewares.static(swaggerUi.getAbsoluteFSPath(), {
|
2018-12-06 18:03:56 +01:00
|
|
|
maxage: strapi.config.middleware.settings.public.maxAge,
|
|
|
|
defer: true
|
|
|
|
})(ctx, next);
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
};
|
2019-04-15 18:57:58 +02:00
|
|
|
};
|