Skip to content

Commit

Permalink
feat: allow custom routes to override default blueprint actions. closes
Browse files Browse the repository at this point in the history
  • Loading branch information
theoomoregbee committed May 29, 2018
1 parent d826129 commit 6b38ae3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports.routes = {
* for configuration options and examples. *
* *
***************************************************************************/
'delete /clients/:client_id/user/:id': 'UserController.destroy',
'post /user': {
controller: 'UserController',
action: 'create',
Expand Down
7 changes: 6 additions & 1 deletion lib/generators.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ function _generatePaths(generated_routes, tags, customBlueprintMaps) {
console.warn("No tag for this identity '" + identity + "'");
}

var parameter = formatter.blueprint_parameter(route.action, customBlueprintMaps);
var parameter;

if(!route.custom){ // if not from routes.js use the default custom blueprint maps
parameter = formatter.blueprint_parameter(route.action, customBlueprintMaps);
}

if (!parameter) //which means is not in our parameters, then use the keys to get and add token for us incase of any route using header information
parameter = _getPathParamsFromKeys(route.keys).concat({$ref: '#/parameters/TokenHeaderParam'});

Expand Down
3 changes: 2 additions & 1 deletion lib/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ function _parseRoutes(routes) {
summary: '',
description: '',
body: {}, //format as sails attributes, {field:{type,validation...}, ...}
query: []//array of the query types
query: [],//array of the query types
custom: true // to differentiate custom route from route.js
});

//we don't need the body if it's not for post or put
Expand Down
10 changes: 10 additions & 0 deletions test/generators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ describe('Generators', function () {
var tags = generators.tags(models);
const modifiedAttr = {nameModified: {type: 'string', required: true}}
var generatedRoutes = generators.routes(controllers, {routes: {
'delete /play/:play_id/user/:user_id': 'UserController.destroy',
'get /user/phone/:phoneNumber': 'UserController.phone',
'get /user/phone/:phoneNumber/call': 'UserController.phone',
'post /user': {
Expand All @@ -488,6 +489,7 @@ describe('Generators', function () {
done();
});


it('should follow the swagger standardized url format of "/route instead of /route/ or route/"', function (done) {
assert.containsAllDeepKeys(actual, ['/user/login', '/user/{id}'], "should have this url pattern");
expect(actual).to.not.have.property('user/login');
Expand Down Expand Up @@ -570,6 +572,14 @@ describe('Generators', function () {
done();
});

it('should not create default paths like create, find, findOne, update and destroy for routes defined in routes.js', function (done) {
expect(_.find(actual['/play/{play_id}/user/{user_id}'].delete.parameters, {
name: 'play_id',
in: 'path'
})).to.be.an('object');
done();
});

});
//for custom parameter addition from routes.js
describe('custom parameters', function(done) {
Expand Down
22 changes: 14 additions & 8 deletions test/parsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ describe('Parser', function () {
summary: '',
description: '',
body: {},
query: []
query: [],
custom: true
}]
};

// Going to fix it soon to follow Sails Routes Address which means for a path allow any CRUD method (GET, PUT, POST, DELETE or PATCH) when a method is not specified
// TODO: Going to fix it soon to follow Sails Routes Address which means for a path allow any CRUD method (GET, PUT, POST, DELETE or PATCH) when a method is not specified
var expected_route2 = {
user: [{
http_method: 'post',
Expand All @@ -115,7 +116,8 @@ describe('Parser', function () {
summary: '',
description: '',
body: {},
query: []
query: [],
custom: true
},
{
http_method: 'get',
Expand All @@ -124,7 +126,8 @@ describe('Parser', function () {
keys: [],
summary: '',
description: '',
query: []
query: [],
custom: true
},
{
http_method: 'put',
Expand All @@ -134,7 +137,8 @@ describe('Parser', function () {
summary: '',
description: '',
body: {},
query: []
query: [],
custom: true
},
{
http_method: 'delete',
Expand All @@ -143,7 +147,8 @@ describe('Parser', function () {
keys: [],
summary: '',
description: '',
query: []
query: [],
custom: true
},
{
http_method: 'patch',
Expand All @@ -152,7 +157,8 @@ describe('Parser', function () {
keys: [],
summary: '',
description: '',
query: []
query: [],
custom: true
}

]
Expand Down Expand Up @@ -256,4 +262,4 @@ describe('Parser', function () {


})
;
;

0 comments on commit 6b38ae3

Please sign in to comment.