2018-12-06 18:03:56 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const path = require('path');
|
2020-10-27 11:27:17 +01:00
|
|
|
const _ = require('lodash');
|
2019-06-08 18:50:07 +02:00
|
|
|
const koaStatic = require('koa-static');
|
2018-12-06 18:03:56 +01:00
|
|
|
|
|
|
|
const initialRoutes = [];
|
|
|
|
|
2021-09-29 10:47:10 +02:00
|
|
|
// 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() {
|
2018-12-06 18:03:56 +01:00
|
|
|
strapi.config.middleware.load.before.push('documentation');
|
|
|
|
|
2021-07-29 16:39:26 +02:00
|
|
|
initialRoutes.push(..._.cloneDeep(strapi.plugins.documentation.routes));
|
2018-12-06 18:03:56 +01:00
|
|
|
},
|
2019-04-15 18:57:58 +02:00
|
|
|
|
2019-06-08 18:50:07 +02:00
|
|
|
initialize() {
|
2021-09-03 11:11:37 +02:00
|
|
|
const swaggerUi = require('swagger-ui-dist');
|
|
|
|
|
2018-12-06 18:03:56 +01:00
|
|
|
// 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) => {
|
2018-12-06 18:03:56 +01:00
|
|
|
if (route.handler === 'Documentation.getInfos') {
|
|
|
|
return route;
|
|
|
|
}
|
|
|
|
|
2020-05-08 13:50:00 +02:00
|
|
|
if (route.handler === 'Documentation.index' || route.path === '/login') {
|
2018-12-06 18:03:56 +01:00
|
|
|
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.
|
2021-07-20 12:12:30 +02:00
|
|
|
if (strapi.config.has('plugins.documentation.x-strapi-config.path')) {
|
2018-12-06 18:03:56 +01:00
|
|
|
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('//', '/');
|
2018-12-06 18:03:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return route;
|
2019-06-08 18:50:07 +02:00
|
|
|
}
|
|
|
|
);
|
2018-12-06 18:03:56 +01:00
|
|
|
|
2021-09-01 19:55:16 +02:00
|
|
|
strapi.server.routes([
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
path: '/plugins/documentation/(.*)',
|
2021-09-13 12:03:12 +02:00
|
|
|
async handler(ctx, next) {
|
2021-09-01 19:55:16 +02:00
|
|
|
ctx.url = path.basename(ctx.url);
|
|
|
|
|
2021-09-13 12:03:12 +02:00
|
|
|
return koaStatic(swaggerUi.getAbsoluteFSPath(), {
|
2021-09-01 19:55:16 +02:00
|
|
|
maxage: strapi.config.middleware.settings.public.maxAge,
|
|
|
|
defer: true,
|
|
|
|
})(ctx, next);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
2019-06-08 18:50:07 +02:00
|
|
|
},
|
2021-07-29 16:39:26 +02:00
|
|
|
},
|
2019-04-15 18:57:58 +02:00
|
|
|
};
|