| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Public node modules.
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2019-06-08 18:50:07 +02:00
										 |  |  | const swaggerUi = require('swagger-ui-dist'); | 
					
						
							|  |  |  | const koaStatic = require('koa-static'); | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Variables.
 | 
					
						
							|  |  |  | const initialRoutes = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = strapi => { | 
					
						
							|  |  |  |   return { | 
					
						
							| 
									
										
										
										
											2019-06-08 18:50:07 +02:00
										 |  |  |     beforeInitialize() { | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |       strapi.config.middleware.load.before.push('documentation'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 13:50:00 +02:00
										 |  |  |       initialRoutes.push(..._.cloneDeep(strapi.plugins.documentation.config.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() { | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |       // Find the plugins routes.
 | 
					
						
							| 
									
										
										
										
											2019-06-08 18:50:07 +02:00
										 |  |  |       strapi.plugins.documentation.config.routes = strapi.plugins.documentation.config.routes.map( | 
					
						
							|  |  |  |         (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.
 | 
					
						
							| 
									
										
										
										
											2020-05-08 13:50:00 +02:00
										 |  |  |           if (_.get(strapi.plugins, ['documentation', 'config', 'x-strapi-config', 'path'])) { | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |             route.config.prefix = ''; | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  |             route.path = `/${strapi.plugins.documentation.config['x-strapi-config'].path}${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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  |       strapi.router.get('/plugins/documentation/*', async (ctx, next) => { | 
					
						
							|  |  |  |         ctx.url = path.basename(ctx.url); | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  |         return await koaStatic(swaggerUi.getAbsoluteFSPath(), { | 
					
						
							|  |  |  |           maxage: strapi.config.middleware.settings.public.maxAge, | 
					
						
							|  |  |  |           defer: true, | 
					
						
							|  |  |  |         })(ctx, next); | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-06-08 18:50:07 +02:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-04-15 18:57:58 +02:00
										 |  |  | }; |