| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Node.js core.
 | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Public node modules.
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Expose main routes of the generated API | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-11 13:03:35 +02:00
										 |  |  | module.exports = scope => { | 
					
						
							| 
									
										
										
										
											2016-12-20 20:41:42 +01:00
										 |  |  |   function generateRoutes() { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       routes: [{ | 
					
						
							|  |  |  |         method: 'GET', | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |         path: '/' + scope.humanizeId, | 
					
						
							| 
									
										
										
										
											2016-12-20 20:41:42 +01:00
										 |  |  |         handler: scope.globalID + '.find', | 
					
						
							|  |  |  |         config: { | 
					
						
							|  |  |  |           policies: [] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, { | 
					
						
							|  |  |  |         method: 'GET', | 
					
						
							| 
									
										
										
										
											2017-08-30 14:12:59 +02:00
										 |  |  |         path: '/' + scope.humanizeId + '/:_id', | 
					
						
							| 
									
										
										
										
											2016-12-20 20:41:42 +01:00
										 |  |  |         handler: scope.globalID + '.findOne', | 
					
						
							|  |  |  |         config: { | 
					
						
							|  |  |  |           policies: [] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, { | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |         path: '/' + scope.humanizeId, | 
					
						
							| 
									
										
										
										
											2016-12-20 20:41:42 +01:00
										 |  |  |         handler: scope.globalID + '.create', | 
					
						
							|  |  |  |         config: { | 
					
						
							|  |  |  |           policies: [] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, { | 
					
						
							|  |  |  |         method: 'PUT', | 
					
						
							| 
									
										
										
										
											2017-08-30 14:12:59 +02:00
										 |  |  |         path: '/' + scope.humanizeId + '/:_id', | 
					
						
							| 
									
										
										
										
											2016-12-20 20:41:42 +01:00
										 |  |  |         handler: scope.globalID + '.update', | 
					
						
							|  |  |  |         config: { | 
					
						
							|  |  |  |           policies: [] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, { | 
					
						
							|  |  |  |         method: 'DELETE', | 
					
						
							| 
									
										
										
										
											2017-08-30 14:12:59 +02:00
										 |  |  |         path: '/' + scope.humanizeId + '/:_id', | 
					
						
							| 
									
										
										
										
											2016-12-20 20:41:42 +01:00
										 |  |  |         handler: scope.globalID + '.destroy', | 
					
						
							|  |  |  |         config: { | 
					
						
							|  |  |  |           policies: [] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }] | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // We have to delete current file
 | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |   if (!_.isEmpty(scope.parentId)) { | 
					
						
							| 
									
										
										
										
											2017-01-04 11:43:32 +01:00
										 |  |  |     let current; | 
					
						
							| 
									
										
										
										
											2016-12-20 20:41:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2017-01-04 11:43:32 +01:00
										 |  |  |       // Copy current routes.json
 | 
					
						
							|  |  |  |       current = require(scope.rootPath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-20 20:41:42 +01:00
										 |  |  |       // Remove current routes.json
 | 
					
						
							|  |  |  |       fs.unlinkSync(scope.rootPath); | 
					
						
							| 
									
										
										
										
											2017-01-04 11:43:32 +01:00
										 |  |  |     } catch (e) { | 
					
						
							|  |  |  |       // Fake existing routes
 | 
					
						
							|  |  |  |       current = { | 
					
						
							|  |  |  |         routes: [] | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const newest = generateRoutes().routes; | 
					
						
							|  |  |  |       // Merge both array of routes, and remove identical routes
 | 
					
						
							|  |  |  |       _.set(current, 'routes', _.concat(newest, _.differenceWith(current.routes, newest, _.isEqual))); | 
					
						
							| 
									
										
										
										
											2016-12-20 20:41:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       return current; | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |       console.error(e); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return generateRoutes(); | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | }; |