| 
									
										
										
										
											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() { | 
					
						
							| 
									
										
										
										
											2017-10-11 12:41:10 +02:00
										 |  |  |     const routes = { | 
					
						
							| 
									
										
										
										
											2019-04-11 16:19:15 +02:00
										 |  |  |       routes: [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           method: 'GET', | 
					
						
							|  |  |  |           path: '/' + scope.idPluralized, | 
					
						
							|  |  |  |           handler: scope.globalID + '.find', | 
					
						
							|  |  |  |           config: { | 
					
						
							|  |  |  |             policies: [], | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           method: 'GET', | 
					
						
							|  |  |  |           path: '/' + scope.idPluralized + '/count', | 
					
						
							|  |  |  |           handler: scope.globalID + '.count', | 
					
						
							|  |  |  |           config: { | 
					
						
							|  |  |  |             policies: [], | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           method: 'GET', | 
					
						
							| 
									
										
										
										
											2019-08-13 16:31:29 +02:00
										 |  |  |           path: '/' + scope.idPluralized + '/:id', | 
					
						
							| 
									
										
										
										
											2019-04-11 16:19:15 +02:00
										 |  |  |           handler: scope.globalID + '.findOne', | 
					
						
							|  |  |  |           config: { | 
					
						
							|  |  |  |             policies: [], | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           method: 'POST', | 
					
						
							|  |  |  |           path: '/' + scope.idPluralized, | 
					
						
							|  |  |  |           handler: scope.globalID + '.create', | 
					
						
							|  |  |  |           config: { | 
					
						
							|  |  |  |             policies: [], | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           method: 'PUT', | 
					
						
							| 
									
										
										
										
											2019-08-13 16:31:29 +02:00
										 |  |  |           path: '/' + scope.idPluralized + '/:id', | 
					
						
							| 
									
										
										
										
											2019-04-11 16:19:15 +02:00
										 |  |  |           handler: scope.globalID + '.update', | 
					
						
							|  |  |  |           config: { | 
					
						
							|  |  |  |             policies: [], | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           method: 'DELETE', | 
					
						
							| 
									
										
										
										
											2019-08-13 16:31:29 +02:00
										 |  |  |           path: '/' + scope.idPluralized + '/:id', | 
					
						
							| 
									
										
										
										
											2019-04-11 16:19:15 +02:00
										 |  |  |           handler: scope.globalID + '.delete', | 
					
						
							|  |  |  |           config: { | 
					
						
							|  |  |  |             policies: [], | 
					
						
							|  |  |  |           }, | 
					
						
							| 
									
										
										
										
											2019-08-13 16:31:29 +02:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-04-11 16:19:15 +02:00
										 |  |  |       ], | 
					
						
							| 
									
										
										
										
											2016-12-20 20:41:42 +01:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-10-11 12:41:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return routes; | 
					
						
							| 
									
										
										
										
											2016-12-20 20:41:42 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 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 = { | 
					
						
							| 
									
										
										
										
											2019-04-11 16:19:15 +02:00
										 |  |  |         routes: [], | 
					
						
							| 
									
										
										
										
											2017-01-04 11:43:32 +01:00
										 |  |  |       }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const newest = generateRoutes().routes; | 
					
						
							|  |  |  |       // Merge both array of routes, and remove identical routes
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:19:15 +02:00
										 |  |  |       _.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
										 |  |  | }; |