Skip to content

Endpoint improvements #1122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ var AngularFullstackGenerator = yeoman.generators.Base.extend({

saveSettings: function() {
if(this.skipConfig) return;
this.config.set('endpointDirectory', 'server/api/');
this.config.set('insertRoutes', true);
this.config.set('registerRoutesFile', 'server/routes.js');
this.config.set('routesNeedle', '// Insert routes below');
Expand Down Expand Up @@ -399,11 +400,19 @@ var AngularFullstackGenerator = yeoman.generators.Base.extend({
},

generateEndpoint: function() {
var name = this.name = this.cameledName = 'thing';
this.classedName = name.charAt(0).toUpperCase() + name.slice(1);
this.route = '/api/' + name + 's';
this.sourceRoot(path.join(__dirname, '..', 'endpoint', 'templates'));
genUtils.processDirectory(this, '.', 'server/api/' + name);
var models;
if (this.filters.mongooseModels) {
models = 'mongoose';
} else if (this.filters.sequelizeModels) {
models = 'sequelize';
}
this.composeWith('angular-fullstack:endpoint', {
options: {
route: '/api/things',
models: models
},
args: ['thing']
});
}

},
Expand Down
2 changes: 1 addition & 1 deletion app/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"mongoose": "~4.0.3",
"mongoose-bird": "~0.0.1",
"connect-mongo": "^0.8.1",<% } %><% if (filters.sequelize) { %>
"sequelize": "^2.0.0-rc2",
"sequelize": "^3.5.1",
"sqlite3": "~3.0.2",<% } %><% if (filters.auth) { %>
"jsonwebtoken": "^5.0.0",
"express-jwt": "^3.0.0",
Expand Down
21 changes: 3 additions & 18 deletions app/templates/server/sqldb(sequelize)/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,8 @@ var db = {
Sequelize: Sequelize,
sequelize: new Sequelize(config.sequelize.uri, config.sequelize.options)
};
<% if (filters.sequelizeModels) { %>
db.Thing = db.sequelize.import(path.join(
config.root,
'server',
'api',
'thing',
'thing.model'
));
<% if (filters.auth) { %>
db.User = db.sequelize.import(path.join(
config.root,
'server',
'api',
'user',
'user.model'
));
<% } %><% } %>
// Insert models below

// Insert models below<% if (filters.sequelizeModels && filters.auth) { %>
db.User = db.sequelize.import('../api/user/user.model');<% } %>

module.exports = db;
130 changes: 81 additions & 49 deletions endpoint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,59 @@ var ScriptBase = require('../script-base.js');

var Generator = module.exports = function Generator() {
ScriptBase.apply(this, arguments);

this.option('route', {
desc: 'URL for the endpoint',
type: String
});

this.option('models', {
desc: 'Specify which model(s) to use',
type: String
});

this.option('endpointDirectory', {
desc: 'Parent directory for enpoints',
type: String
});
};

util.inherits(Generator, ScriptBase);

Generator.prototype.prompting = function askFor() {
var done = this.async();
var promptCb = function (props) {
if(props.route.charAt(0) !== '/') {
props.route = '/' + props.route;
}

this.route = props.route;

if (props.models) {
delete this.filters.mongoose;
delete this.filters.mongooseModels;
delete this.filters.sequelize;
delete this.filters.sequelizeModels;

this.filters[props.models] = true;
this.filters[props.models + 'Models'] = true;
}
done();
}.bind(this);

if (this.options.route) {
if (this.filters.mongoose && this.filters.sequelize) {
if (this.options.models) {
return promptCb(this.options);
}
} else {
if (this.filters.mongooseModels) { this.options.models = 'mongoose'; }
else if (this.filters.sequelizeModels) { this.options.models = 'sequelize'; }
else { delete this.options.models; }
return promptCb(this.options);
}
}

var name = this.name;

var base = this.config.get('routesBase') || '/api/';
Expand Down Expand Up @@ -46,73 +93,58 @@ Generator.prototype.prompting = function askFor() {
}
];

this.prompt(prompts, function (props) {
if(props.route.charAt(0) !== '/') {
props.route = '/' + props.route;
}

this.route = props.route;

if (props.models) {
delete this.filters.mongoose;
delete this.filters.mongooseModels;
delete this.filters.sequelize;
delete this.filters.sequelizeModels;
this.prompt(prompts, promptCb);
};

this.filters[props.models] = true;
this.filters[props.models + 'Models'] = true;
}
done();
}.bind(this));
Generator.prototype.configuring = function config() {
this.routeDest = path.join(this.options.endpointDirectory ||
this.config.get('endpointDirectory') || 'server/api/', this.name);
};

Generator.prototype.writing = function createFiles() {
var dest = this.config.get('endpointDirectory') || 'server/api/' + this.name;
this.sourceRoot(path.join(__dirname, './templates'));
ngUtil.processDirectory(this, '.', dest);
ngUtil.processDirectory(this, '.', this.routeDest);
};

Generator.prototype.end = function registerEndpoint() {
if(this.config.get('insertRoutes')) {
var routesFile = this.config.get('registerRoutesFile');
var reqPath = this.relativeRequire(this.routeDest, routesFile);
var routeConfig = {
file: this.config.get('registerRoutesFile'),
file: routesFile,
needle: this.config.get('routesNeedle'),
splicable: [
"app.use(\'" + this.route +"\', require(\'./api/" + this.name + "\'));"
"app.use(\'" + this.route +"\', require(\'" + reqPath + "\'));"
]
};
ngUtil.rewriteFile(routeConfig);
}

if (this.filters.socketio) {
if(this.config.get('insertSockets')) {
var socketConfig = {
file: this.config.get('registerSocketsFile'),
needle: this.config.get('socketsNeedle'),
splicable: [
"require(\'../api/" + this.name + '/' + this.name + ".socket\').register(socket);"
]
};
ngUtil.rewriteFile(socketConfig);
}
if (this.filters.socketio && this.config.get('insertSockets')) {
var socketsFile = this.config.get('registerSocketsFile');
var reqPath = this.relativeRequire(this.routeDest + '/' + this.basename +
'.socket', socketsFile);
var socketConfig = {
file: socketsFile,
needle: this.config.get('socketsNeedle'),
splicable: [
"require(\'" + reqPath + "\').register(socket);"
]
};
ngUtil.rewriteFile(socketConfig);
}

if (this.filters.sequelize) {
if (this.config.get('insertModels')) {
var modelConfig = {
file: this.config.get('registerModelsFile'),
needle: this.config.get('modelsNeedle'),
splicable: [
"db." + this.classedName + " = db.sequelize.import(path.join(\n" +
" config.root,\n" +
" 'server',\n" +
" 'api',\n" +
" '" + this.name + "',\n" +
" '" + this.name + ".model'\n" +
"));"
]
};
ngUtil.rewriteFile(modelConfig);
}
if (this.filters.sequelize && this.config.get('insertModels')) {
var modelsFile = this.config.get('registerModelsFile');
var reqPath = this.relativeRequire(this.routeDest + '/' + this.basename +
'.model', modelsFile);
var modelConfig = {
file: modelsFile,
needle: this.config.get('modelsNeedle'),
splicable: [
"db." + this.classedName + " = db.sequelize.import(\'" + reqPath +"\');"
]
};
ngUtil.rewriteFile(modelConfig);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
'use strict';<% if (filters.models) { %>

var _ = require('lodash');<% if (filters.mongooseModels) { %>
var <%= classedName %> = require('./<%= name %>.model');<% } if (filters.sequelizeModels) { %>
var sqldb = require('../../sqldb');
var <%= classedName %> = require('./<%= basename %>.model');<% } if (filters.sequelizeModels) { %>
var sqldb = require('<%= relativeRequire(config.get('registerModelsFile')) %>');
var <%= classedName %> = sqldb.<%= classedName %>;<% } %>

function handleError(res, statusCode) {
Expand Down Expand Up @@ -64,7 +64,7 @@ function removeEntity(res) {
};
}<% } %>

// Gets a list of <%= name %>s
// Gets a list of <%= classedName %>s
exports.index = function(req, res) {<% if (!filters.models) { %>
res.json([]);<% } else { %>
<% if (filters.mongooseModels) { %><%= classedName %>.findAsync()<% }
Expand All @@ -73,7 +73,7 @@ exports.index = function(req, res) {<% if (!filters.models) { %>
.catch(handleError(res));<% } %>
};<% if (filters.models) { %>

// Gets a single <%= name %> from the DB
// Gets a single <%= classedName %> from the DB
exports.show = function(req, res) {
<% if (filters.mongooseModels) { %><%= classedName %>.findByIdAsync(req.params.id)<% }
if (filters.sequelizeModels) { %><%= classedName %>.find({
Expand All @@ -86,15 +86,15 @@ exports.show = function(req, res) {
.catch(handleError(res));
};

// Creates a new <%= name %> in the DB
// Creates a new <%= classedName %> in the DB
exports.create = function(req, res) {
<% if (filters.mongooseModels) { %><%= classedName %>.createAsync(req.body)<% }
if (filters.sequelizeModels) { %><%= classedName %>.create(req.body)<% } %>
.then(responseWithResult(res, 201))
.catch(handleError(res));
};

// Updates an existing <%= name %> in the DB
// Updates an existing <%= classedName %> in the DB
exports.update = function(req, res) {
if (req.body._id) {
delete req.body._id;
Expand All @@ -111,7 +111,7 @@ exports.update = function(req, res) {
.catch(handleError(res));
};

// Deletes a <%= name %> from the DB
// Deletes a <%= classedName %> from the DB
exports.destroy = function(req, res) {
<% if (filters.mongooseModels) { %><%= classedName %>.findByIdAsync(req.params.id)<% }
if (filters.sequelizeModels) { %><%= classedName %>.find({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
'use strict';

var EventEmitter = require('events').EventEmitter;<% if (filters.mongooseModels) { %>
var <%= classedName %> = require('./<%= name %>.model');<% } if (filters.sequelizeModels) { %>
var <%= classedName %> = require('../../sqldb').<%= classedName %>;<% } %>
var <%= classedName %> = require('./<%= basename %>.model');<% } if (filters.sequelizeModels) { %>
var <%= classedName %> = require('<%= relativeRequire(config.get('registerModelsFile')) %>').<%= classedName %>;<% } %>
var <%= classedName %>Events = new EventEmitter();

// Set max event listeners (0 == unlimited)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var app = require('../../app');
var app = require('<%= relativeRequire('server/app') %>');
var request = require('supertest');<% if(filters.models) { %>

var new<%= classedName %>;<% } %>
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('<%= classedName %> API:', function() {
.post('<%= route %>')
.send({
name: 'New <%= classedName %>',
info: 'This is the brand new <%= name %>!!!'
info: 'This is the brand new <%= cameledName %>!!!'
})
.expect(201)
.expect('Content-Type', /json/)
Expand All @@ -49,9 +49,9 @@ describe('<%= classedName %> API:', function() {
});
});

it('should respond with the newly created <%= name %>', function() {
it('should respond with the newly created <%= cameledName %>', function() {
new<%= classedName %>.name.should.equal('New <%= classedName %>');
new<%= classedName %>.info.should.equal('This is the brand new <%= name %>!!!');
new<%= classedName %>.info.should.equal('This is the brand new <%= cameledName %>!!!');
});

});
Expand All @@ -77,9 +77,9 @@ describe('<%= classedName %> API:', function() {
<%= cameledName %> = {};
});

it('should respond with the requested <%= name %>', function() {
it('should respond with the requested <%= cameledName %>', function() {
<%= cameledName %>.name.should.equal('New <%= classedName %>');
<%= cameledName %>.info.should.equal('This is the brand new <%= name %>!!!');
<%= cameledName %>.info.should.equal('This is the brand new <%= cameledName %>!!!');
});

});
Expand All @@ -92,7 +92,7 @@ describe('<%= classedName %> API:', function() {
.put('<%= route %>/' + new<%= classedName %>._id)
.send({
name: 'Updated <%= classedName %>',
info: 'This is the updated <%= name %>!!!'
info: 'This is the updated <%= cameledName %>!!!'
})
.expect(200)
.expect('Content-Type', /json/)
Expand All @@ -109,9 +109,9 @@ describe('<%= classedName %> API:', function() {
updated<%= classedName %> = {};
});

it('should respond with the updated <%= name %>', function() {
it('should respond with the updated <%= cameledName %>', function() {
updated<%= classedName %>.name.should.equal('Updated <%= classedName %>');
updated<%= classedName %>.info.should.equal('This is the updated <%= name %>!!!');
updated<%= classedName %>.info.should.equal('This is the updated <%= cameledName %>!!!');
});

});
Expand All @@ -130,7 +130,7 @@ describe('<%= classedName %> API:', function() {
});
});

it('should respond with 404 when <%= name %> does not exist', function(done) {
it('should respond with 404 when <%= cameledName %> does not exist', function(done) {
request(app)
.delete('<%= route %>/' + new<%= classedName %>._id)
.expect(404)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

'use strict';

var <%= classedName %>Events = require('./<%= name %>.events');
var <%= classedName %>Events = require('./<%= basename %>.events');

// Model events to emit
var events = ['save', 'remove'];
Expand All @@ -13,7 +13,7 @@ exports.register = function(socket) {
// Bind model events to socket events
for (var i = 0, eventsLength = events.length; i < eventsLength; i++) {
var event = events[i];
var listener = createListener('<%= name %>:' + event, socket);
var listener = createListener('<%= cameledName %>:' + event, socket);

<%= classedName %>Events.on(event, listener);
socket.on('disconnect', removeListener(event, listener));
Expand Down
Loading