| 
									
										
										
										
											2015-10-22 17:54:43 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Public node modules.
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |   find: find, | 
					
						
							|  |  |  |   update: update | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Find routes. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @returns {Function|promise} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-10-28 11:23:59 +01:00
										 |  |  | function * find() { | 
					
						
							| 
									
										
										
										
											2015-10-22 17:54:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-27 11:59:38 +01:00
										 |  |  |   const deferred = Promise.defer(); | 
					
						
							| 
									
										
										
										
											2015-10-22 17:54:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     const verbs = ['get', 'put', 'post', 'delete', 'options', 'patch']; | 
					
						
							| 
									
										
										
										
											2015-10-27 11:59:38 +01:00
										 |  |  |     const routes = {}; | 
					
						
							|  |  |  |     const dbRoutes = yield strapi.orm.collections.route.find().populate('roles'); | 
					
						
							|  |  |  |     const apis = strapi.api; | 
					
						
							| 
									
										
										
										
											2015-10-22 17:54:43 +02:00
										 |  |  |     let dbRoute; | 
					
						
							|  |  |  |     let index; | 
					
						
							|  |  |  |     let firstWord; | 
					
						
							|  |  |  |     let routeNameSplitted; | 
					
						
							|  |  |  |     let verb; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Format verb.
 | 
					
						
							|  |  |  |     _.forEach(dbRoutes, function (route) { | 
					
						
							|  |  |  |       // Split the name with `/`.
 | 
					
						
							|  |  |  |       routeNameSplitted = route.name.split('/'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Verb.
 | 
					
						
							|  |  |  |       verb = _.includes(verbs, routeNameSplitted[0] && _.trim(routeNameSplitted[0].toLowerCase())) ? _.trim(routeNameSplitted[0]) : ''; | 
					
						
							|  |  |  |       route.verb = verb; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       route.path = route.verb ? routeNameSplitted.splice(0, 1) && _.trim('/' + routeNameSplitted.join('/')) : _.trim(routeNameSplitted.join('/')); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // For each API.
 | 
					
						
							|  |  |  |     _.forEach(apis, function (api, key) { | 
					
						
							|  |  |  |       // Init the array object.
 | 
					
						
							|  |  |  |       routes[key] = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // For each routes of the current API.
 | 
					
						
							|  |  |  |       _.forEach(api.config.routes, function (route, routeName) { | 
					
						
							|  |  |  |         // Find routes of the APIs in the `routes` object.
 | 
					
						
							|  |  |  |         dbRoute = _.find(dbRoutes, {name: routeName}); | 
					
						
							|  |  |  |         // If the route is found.
 | 
					
						
							|  |  |  |         if (dbRoute) { | 
					
						
							|  |  |  |           // Find the index.
 | 
					
						
							|  |  |  |           index = _.indexOf(dbRoutes, dbRoute); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           // Assign them to the key of the `route` object.
 | 
					
						
							|  |  |  |           routes[key].push(dbRoute); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           // Remove the pushed route from the list of routes.
 | 
					
						
							|  |  |  |           dbRoutes.splice(index, 1); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Then filter by `begin` with.
 | 
					
						
							|  |  |  |     _.forEach(_.clone(dbRoutes), function (route) { | 
					
						
							|  |  |  |       // Prevent errors.
 | 
					
						
							|  |  |  |       if (!route) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Split the name with `/`.
 | 
					
						
							|  |  |  |       routeNameSplitted = route.name.split('/'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Fetch the first word of the URL.
 | 
					
						
							|  |  |  |       firstWord = route.verb ? _.trim(routeNameSplitted[1]) : _.trim(routeNameSplitted[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Set an empty array for this object if it is not
 | 
					
						
							|  |  |  |       // already defined.
 | 
					
						
							|  |  |  |       routes[firstWord] = routes[firstWord] || []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Set the index value.
 | 
					
						
							|  |  |  |       index = _.indexOf(dbRoutes, route); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Assign them to the key of the `route` object.
 | 
					
						
							|  |  |  |       routes[firstWord].push(_.clone(route)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Remove the pushed route from the list of routes.
 | 
					
						
							|  |  |  |       dbRoutes.splice(index, 1); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Set the non-filtered routes in the `others` object.
 | 
					
						
							|  |  |  |     if (dbRoutes.length) { | 
					
						
							| 
									
										
										
										
											2015-10-27 11:59:38 +01:00
										 |  |  |       routes.others = dbRoutes; | 
					
						
							| 
									
										
										
										
											2015-10-22 17:54:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     deferred.resolve(routes); | 
					
						
							|  |  |  |   } catch (err) { | 
					
						
							|  |  |  |     deferred.reject(err); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return deferred.promise; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Update routes. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param routes | 
					
						
							|  |  |  |  * @returns {Function|promise} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-10-28 11:23:59 +01:00
										 |  |  | function * update(routes) { | 
					
						
							| 
									
										
										
										
											2015-10-22 17:54:43 +02:00
										 |  |  |   let id; | 
					
						
							| 
									
										
										
										
											2015-10-27 11:59:38 +01:00
										 |  |  |   const promises = []; | 
					
						
							|  |  |  |   const deferred = Promise.defer(); | 
					
						
							| 
									
										
										
										
											2015-10-22 17:54:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   _.forEach(routes, function (route) { | 
					
						
							|  |  |  |     id = route.id; | 
					
						
							|  |  |  |     promises.push(strapi.orm.collections.route.update({id: id}, route)); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Promise.all(promises) | 
					
						
							|  |  |  |     .then(function (results) { | 
					
						
							|  |  |  |       deferred.resolve(results); | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     .catch(function (error) { | 
					
						
							|  |  |  |       deferred.reject(error); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return deferred.promise; | 
					
						
							|  |  |  | } |