2016-03-18 11:12:50 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Node.js core.
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
// 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',
|
|
|
|
path: '/' + (scope.humanizeSubId || scope.humanizeId),
|
|
|
|
handler: scope.globalID + '.find',
|
|
|
|
config: {
|
|
|
|
policies: []
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
method: 'GET',
|
|
|
|
path: '/' + (scope.humanizeSubId || scope.humanizeId) + '/:id',
|
|
|
|
handler: scope.globalID + '.findOne',
|
|
|
|
config: {
|
|
|
|
policies: []
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
method: 'POST',
|
|
|
|
path: '/' + (scope.humanizeSubId || scope.humanizeId),
|
|
|
|
handler: scope.globalID + '.create',
|
|
|
|
config: {
|
|
|
|
policies: []
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
method: 'PUT',
|
|
|
|
path: '/' + (scope.humanizeSubId || scope.humanizeId) + '/:id',
|
|
|
|
handler: scope.globalID + '.update',
|
|
|
|
config: {
|
|
|
|
policies: []
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
method: 'DELETE',
|
|
|
|
path: '/' + (scope.humanizeSubId || scope.humanizeId) + '/:id',
|
|
|
|
handler: scope.globalID + '.destroy',
|
|
|
|
config: {
|
|
|
|
policies: []
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to delete current file
|
|
|
|
if (!_.isEmpty(scope.subId)) {
|
|
|
|
const current = require(scope.rootPath);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Remove current routes.json
|
|
|
|
fs.unlinkSync(scope.rootPath);
|
|
|
|
// Merge both array of routes
|
|
|
|
current.routes = _.concat(generateRoutes().routes, current.routes);
|
|
|
|
|
|
|
|
return current;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return generateRoutes();
|
2016-03-18 11:12:50 +01:00
|
|
|
};
|