127 lines
2.9 KiB
JavaScript
Raw Normal View History

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
*/
module.exports = scope => {
function generateRoutes() {
2017-10-11 13:11:16 +02:00
const tokenID = scope.args.tpl && scope.args.tpl !== 'mongoose' ? 'id' : '_id';
const routes = {
routes: [{
method: 'GET',
path: '/' + scope.humanizeId,
handler: scope.globalID + '.find',
config: {
policies: []
}
}, {
method: 'GET',
2018-06-08 22:52:41 +02:00
path: '/' + scope.humanizeId + '/count',
handler: scope.globalID + '.count',
config: {
policies: []
}
2018-05-29 15:48:07 +03:00
}, {
method: 'GET',
2018-06-08 22:52:41 +02:00
path: '/' + scope.humanizeId + '/:' + tokenID,
handler: scope.globalID + '.findOne',
2018-05-29 15:48:07 +03:00
config: {
policies: []
}
}, {
method: 'POST',
path: '/' + scope.humanizeId,
handler: scope.globalID + '.create',
config: {
policies: []
}
}, {
method: 'PUT',
2017-10-11 13:11:16 +02:00
path: '/' + scope.humanizeId + '/:' + tokenID,
handler: scope.globalID + '.update',
config: {
policies: []
}
}, {
method: 'DELETE',
2017-10-11 13:11:16 +02:00
path: '/' + scope.humanizeId + '/:' + tokenID,
handler: scope.globalID + '.destroy',
config: {
policies: []
}
}]
};
if (scope.args.tpl && scope.args.tpl !== 'mongoose') {
routes.routes.push({
method: 'POST',
2017-10-11 13:11:16 +02:00
path: '/' + scope.humanizeId + '/:' + tokenID + '/relationships/:relation',
handler: scope.globalID + '.createRelation',
config: {
policies: []
}
}, {
method: 'PUT',
2017-10-11 13:11:16 +02:00
path: '/' + scope.humanizeId + '/:' + tokenID + '/relationships/:relation',
handler: scope.globalID + '.updateRelation',
config: {
policies: []
}
}, {
method: 'DELETE',
2017-10-11 13:11:16 +02:00
path: '/' + scope.humanizeId + '/:' + tokenID + '/relationships/:relation',
handler: scope.globalID + '.destroyRelation',
config: {
policies: []
}
});
}
return routes;
}
// We have to delete current file
if (!_.isEmpty(scope.parentId)) {
2017-01-04 11:43:32 +01:00
let current;
try {
2017-01-04 11:43:32 +01:00
// Copy current routes.json
current = require(scope.rootPath);
// 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)));
return current;
} catch (e) {
console.error(e);
return;
}
}
return generateRoutes();
2016-03-18 11:12:50 +01:00
};