-
Notifications
You must be signed in to change notification settings - Fork 82
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
Feature Request: First Class API Stubs #318
Comments
I am in favor of this and I recall an old feature request being opened to Should we consider placing the stubs under On Wed, Sep 3, 2014 at 9:31 AM, David Mosher [email protected]
|
Since these stubs would be mostly/entirely used for the dev server and not for tests, I don't think it makes sense for them to be stored under spec. Since our testing facility only covers unit tests, I don't want to encourage people to start using fixtures in external files. |
As I re-read Dave's ticket, it sounds like this is a way to auto-break up the server file into n smaller server files, and not necessarily an approach for stubbings at a particular route. But I'm pretty sure I'm just not clear. Dave could you put together a gist or small repo of files/folders of how you'd like to use this feature and then explain what they'd do (e.g. " |
@searls good call on the fixtures |
@searls, what you describe in breaking up the
In a simpler scenario, all I really want at this point is:
|
So in your simpler example what does stubs/user.js export? A function that takes an express app and does [THINGS] to it? Or something more constrained? On Thu, Sep 4, 2014 at 12:32 PM, David Mosher [email protected]
|
stubs/user.js is the same in the simpler example, i just manually manage routing in config/server.js as per normal. |
I'm still not getting it, then. |
Could you post an example listing of |
var users = require('./stubs/users.js');
module.exports = {
drawRoutes: function(app) {
app.get('/api/users', function(req, res) { res.json(users); });
}
};
module.exports = {
users: [
{ id:1, name: 'Dave' },
{ id:2, name: 'Justin' }
]
} |
oh, well in that case I think what you're asking for is much simpler and less opinionated than what I thought you were asking for. I think the default behavior absolutely should explode on errors, though, to the same extent a compilation failure does. If one particular API starts barfing and we don't tell people, they won't know where to look. And there's no good reason for them to start exploding. (If all you're saying is you don't want the process to blow up, I agree) Error handling generally should probably give feedback in the browser with a lineman error page and not just in the terminal. |
Here's a thing that's somewhat related. worth a look: http://webpro.github.io/dyson/ |
I'm going to post some of the api stubs we're using in a current project for reference. There are some interesting usage patterns I've discovered working through these;
var _ = require('underscore');
// var drillingData = require('./stubs/execution/drilling_data.js');
// var upToBatData = require('./stubs/uptobat/uptobat_data.js');
// var upToBatGridsData = require('./stubs/uptobat/uptobat_grids_data.js');
// var upToBatFilterData = require('./stubs/uptobat/uptobat_filter_data.js');
// var projectIssuesOptionsData = require('./stubs/issues/project_issues_options_data.js');
// var drillingExecutionGridWidgetOptions = require('./stubs/execution/drilling_grid_widget_options.js');
// var includesCopyButtonJson = require('./stubs/uptobat/includes_copy_button_configuration.js');
var parameters = require('./stubs/admin/parameters.js');
var notifications = require('./stubs/admin/notifications.js');
var notificationRecipients = require("./stubs/admin/notification_recipients.js");
var notificationTriggers = require("./stubs/admin/notification_triggers.js");
var uptobatCounts = require('./stubs/uptobat/uptobat_counts.js');
module.exports = {
drawRoutes: function(app) {
// ADMIN SCREEN STUBS
// uptobat counts
app.get('/api/uptobats/counts', function (req, res) {
console.log("\n *** LINEMAN STUB: GET /api/uptobat/counts");
_.each(uptobatCounts, function(v, k){
uptobatCounts[k] = v + 1;
});
res.json(uptobatCounts);
});
// parameters
app.get('/api/admin/parameters/:buid', function(req, res) {
console.log("\n *** LINEMAN STUB: GET /api/admin/parameters/" + req.params.buid);
res.json(parameters);
});
app.post('/api/admin/parameters/:buid', function (req, res) {
console.log("\n *** LINEMAN STUB: POST /api/admin/parameters/" + req.params.buid);
_.extend(parameters, req.body.data);
res.json(parameters);
});
var logEvents = [];
app.post('/api/log', function(req, res) {
logEvents.push(req.body);
console.log("\n *** LOG EVENT LINEMAN STUB ***\n", req.body);
res.json({status: "ok"});
});
app.get('/api/log', function(req, res) {
res.json(logEvents);
});
// notifications
app.post('/api/admin/notifications/:buid', function (req, res) {
console.log("\n *** LINEMAN STUB: POST /api/admin/notifications/" + req.params.buid);
res.json(notifications);
});
app.get('/api/admin/notifications/:nid', function (req, res) {
console.log("\n *** LINEMAN STUB: GET /api/notifications//" + req.params.nid);
var notification = getNotificationById(req.params.nid);
res.json(notification);
});
app.get('/api/admin/notifications/:nid/recipients', function (req, res) {
console.log("\n *** LINEMAN STUB: GET /api/notifications/" + req.params.nid + "/recipients");
res.json(notificationRecipients);
});
app.get('/api/admin/notifications/:nid/triggers', function (req, res) {
console.log("\n *** LINEMAN STUB: GET /api/notifications/" + req.params.nid + "/triggers");
res.json(notificationTriggers);
});
app.put('/api/admin/notifications/:nid/recipients/update', function (req, res) {
console.log("\n *** LINEMAN STUB: ---> NOTIFICATION RECIPIENT SAVED", req.body.form.nid);
res.json(req.body.form);
});
app.post('/api/admin.notifications/:nid', function(req, res) {
console.log("\n *** LINEMAN STUB: ---> EMAIL TEMPLATE SAVED");
var notification = getNotificationById(req.params.nid);
notification.subject = req.body.subject;
notification.name = req.body.name;
notification.body = req.body.body;
notification.triggers = req.body.triggers;
notification.recipients = req.body.recipients;
res.json(notification);
});
function getNotificationById(id) {
for (var key in notifications.data) {
var notification = notifications.data[key];
if (notification.id == id) {
return notification;
}
}
}
// app.put('/api/data/uptobat/:statenamespace/:uptobatId/:buid', function(req, res){
// console.log('\n *** UPDATE up to bat row ***');
// console.log('\n *** LINEMAN STUB: PUT /api/data/uptobat/:statenamespace/:uptobatId/:buid ***');
// var objToUpdate = _.find(upToBatGridsData, function(obj){
// return obj.id == req.body.models[0].id;
// });
// _.extend(objToUpdate, req.body.models[0]);
// res.json(objToUpdate);
// });
// app.post('/api/data/uptobat/:statenamespace/:uptobatId/:buid', function(req, res){
// console.log('\n *** LINEMAN STUB: POST /api/data/uptobat/' + req.params.statenamespace + '/' + req.params.uptobatId + '/' + req.params.buid + ' *** \n');
// res.json(upToBatGridsData);
// });
// app.put('/api/data/execution.drilling/:buid', function(req, res) {
// console.log('\n *** LINEMAN STUB: PUT /api/data/execution.drilling/' + req.params.buid + ' *** \n');
// res.json(req.body);
// });
// app.get('/api/display/widget/options/execution.drilling/drilling-grid/:buid', function (req, res) {
// console.log('\n *** LINEMAN STUB: GET /api/display/widget/options/execution.drilling/drilling-grid/' + req.params.buId + ' *** \n');
// res.json(drillingExecutionGridWidgetOptions);
// });
// app.post('/api/data/execution.drilling/:buid', function(req, res){
// console.log('\n *** LINEMAN STUB: POST /api/data/execution.drilling/' + req.params.buid + ' *** \n');
// res.json(drillingData);
// });
// app.get('/api/session', function(req, res) {
// console.log('\n *** LINEMAN STUB: GET /api/session *** \n');
// res.json(session);
// });
// app.post('/api/user/profile', function(req, res) {
// _.extend(session.user.profile, req.body);
// console.log('\n *** LINEMAN STUB: POST /api/userprofile: persisting session.user.profile in memory *** \n');
// console.log(session.user.profile);
// res.send(200);
// });
}
}; |
Agreed. I'm not sure how to configure lineman to watch an additional folder for changes, but I edited
to
This allows for file changes without restarting lineman, but for some reason livereload doesn't trigger. I'd love to know how to put this change into |
I wanted to create this to capture the thoughts while they are fresh in my mind after context on a client project where this would have saved some time:
I think we should formalize a
config/stubs
directory which has the following properties:module.exports = {}
for now), we don't need to get too complicated hereconfig/server.{js,coffee}
either as a local in the scope or on the node global objectThe first two would be mostly quality of life improvements to the way I use the existing API stubbing feature, the latter two are things that would be really cool if we could make them not suck in implementation ;)
If anyone is willing to pair with me on this, I'd like to start with the first two which are captured in #268.
The text was updated successfully, but these errors were encountered: