65 lines
1.8 KiB
JavaScript
Raw Normal View History

'use strict';
const path = require('path');
const _ = require('lodash');
2019-06-08 18:50:07 +02:00
const koaStatic = require('koa-static');
const initialRoutes = [];
// TODO: delete when refactoring documentation plugin for v4
2021-07-29 16:39:26 +02:00
module.exports = {
defaults: { documentation: { enabled: true } },
load: {
2019-06-08 18:50:07 +02:00
beforeInitialize() {
strapi.config.middleware.load.before.push('documentation');
2021-07-29 16:39:26 +02:00
initialRoutes.push(..._.cloneDeep(strapi.plugins.documentation.routes));
},
2019-06-08 18:50:07 +02:00
initialize() {
2021-09-03 11:11:37 +02:00
const swaggerUi = require('swagger-ui-dist');
// Find the plugins routes.
2021-07-29 16:39:26 +02:00
strapi.plugins.documentation.routes = strapi.plugins.documentation.routes.map(
2019-06-08 18:50:07 +02:00
(route, index) => {
if (route.handler === 'Documentation.getInfos') {
return route;
}
if (route.handler === 'Documentation.index' || route.path === '/login') {
route.config.policies = initialRoutes[index].config.policies;
}
// Set prefix to empty to be able to customise it.
2021-07-20 12:12:30 +02:00
if (strapi.config.has('plugins.documentation.x-strapi-config.path')) {
route.config.prefix = '';
2021-08-06 18:09:49 +02:00
route.path = `/${strapi.config.get('plugin.documentation.x-strapi-config').path}${
2021-07-20 12:12:30 +02:00
route.path
}`.replace('//', '/');
}
return route;
2019-06-08 18:50:07 +02:00
}
);
2021-09-01 19:55:16 +02:00
strapi.server.routes([
{
method: 'GET',
path: '/plugins/documentation/(.*)',
async handler(ctx, next) {
2021-09-01 19:55:16 +02:00
ctx.url = path.basename(ctx.url);
return koaStatic(swaggerUi.getAbsoluteFSPath(), {
2021-09-01 19:55:16 +02:00
maxage: strapi.config.middleware.settings.public.maxAge,
defer: true,
})(ctx, next);
},
2021-09-02 11:25:24 +02:00
config: {
auth: false,
},
2021-09-01 19:55:16 +02:00
},
]);
2019-06-08 18:50:07 +02:00
},
2021-07-29 16:39:26 +02:00
},
};