Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
2 changes: 1 addition & 1 deletion 2 documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ In the Browser you can connect like this:
console.log('Someone created a Todo', todo);
});

primus.send('todos::create', { description: 'Do something', {}, function() {
primus.send('todos::create', { description: 'Do something' }, {}, function() {
primus.send('todos::find', {}, function(error, todos) {
console.log(todos);
});
Expand Down
82 changes: 0 additions & 82 deletions 82 lib/providers/rest.js

This file was deleted.

51 changes: 51 additions & 0 deletions 51 lib/providers/rest/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

var _ = require('underscore');
var wrappers = require('./wrappers');

module.exports = function (config) {
config = config || {};

var responder = config.responder || function (req, res) {
res.format(_.extend({
'application/json': function () {
res.json(res.data);
}
}, config.formatters));
};

return function () {
var app = this;

app.enable('feathers rest');

app.use(function (req, res, next) {
req.feathers = {};
next();
});

app.rest = wrappers;

// Register the REST provider
app.providers.push(function (path, service) {
if (app.disabled('feathers rest')) {
return;
}

var uri = path.indexOf('/') === 0 ? path : '/' + path;

// GET / -> service.find(cb, params)
app.get(uri, app.rest.find(service))
// GET /:id -> service.get(cb, id, params)
.get(uri + '/:id', app.rest.get(service))
// POST -> service.create(cb, data, params)
.post(uri, app.rest.create(service))
// PUT /:id -> service.update(cb, id, data, params)
.put(uri + '/:id', app.rest.update(service))
// DELETE /:id -> service.remove(cb, id, params)
.del(uri + '/:id', app.rest.remove(service));

app.use(uri, responder);
});
};
};
115 changes: 115 additions & 0 deletions 115 lib/providers/rest/wrappers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
var _ = require('underscore');

/**
* Return a service callback that sets the data in the given
* response and calls next on errors.
*
* @param req
* @param res
* @param next
* @returns {Function}
*/
function wrap(req, res, next) {
return function(error, data) {
if (error) {
return next(error);
}
res.data = data;
return next();
};
}

/**
* Returns the service params, setting params.query
* to an empty object (if not set) and grabbing anything set
* in req.feathers.
*
* @param req The request
* @returns {Object} The service parameters
*/
function getParams(req) {
var query = req.query || {};
return _.extend({
query: query
}, req.feathers);
}

/**
* Checks if the service method is available. If not, sets the response HTTP status to
* 405 (Method not allowed) and returns an error.
*
* @param res The HTTP response
* @param service The wrapped service object
* @param name The method name to check for
* @returns {Error|false} `false` or an error object with the description
*/
function checkMethod(res, service, name) {
if (typeof service[name] !== 'function') {
res.status(405);
return new Error('Can not call service method .' + name);
}

return false;
}

/**
* Returns wrapped middleware for a service method.
*
* @type {{find: find, get: get, create: create, update: update, remove: remove}}
*/
module.exports = {
find: function(service) {
return function(req, res, next) {
var error = checkMethod(res, service, 'find');
if (error) {
return next(error);
}

service.find(getParams(req), wrap(req, res, next));
};
},

get: function(service) {
return function(req, res, next) {
var error = checkMethod(res, service, 'get');
if (error) {
return next(error);
}

service.get(req.params.id, getParams(req), wrap(req, res, next));
};
},

create: function(service) {
return function(req, res, next) {
var error = checkMethod(res, service, 'create');
if (error) {
return next(error);
}

service.create(req.body, getParams(req), wrap(req, res, next));
};
},

update: function(service) {
return function(req, res, next) {
var error = checkMethod(res, service, 'update');
if (error) {
return next(error);
}

service.update(req.params.id, req.body, getParams(req), wrap(req, res, next));
};
},

remove: function(service) {
return function(req, res, next) {
var error = checkMethod(res, service, 'remove');
if (error) {
return next(error);
}

service.remove(req.params.id, getParams(req), wrap(req, res, next));
};
}
};
2 changes: 1 addition & 1 deletion 2 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"grunt": "~0.4.1",
"grunt-release": "~0.5.1",
"mocha": "~1.11.0",
"grunt-contrib-jshint": "~0.6.3",
"grunt-contrib-jshint": "~0.x",
"grunt-simple-mocha": "~0.4.0",
"grunt-jsbeautifier": "~0.2.2"
}
Expand Down
28 changes: 28 additions & 0 deletions 28 test/providers/rest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,32 @@ describe('REST provider', function () {
server.close(done);
});
});

it('throws a 405 for undefined service methods', function(done) {
var app = feathers().use('todo', {
get: function(id, params, callback) {
callback(null, { description: 'You have to do ' + id });
}
});

/* jshint ignore:start */
// Error handler
app.use(function(error, req, res, next) {
assert.equal(error.message, 'Can not call service method .find');
res.json({ message: error.message });
});
/* jshint ignore:end */

var server = app.listen(4777);

request('http://localhost:4777/todo/dishes', function (error, response, body) {
assert.ok(response.statusCode === 200, 'Got OK status code for .get');
assert.deepEqual(JSON.parse(body), { description: 'You have to do dishes' }, 'Got expected object');
request('http://localhost:4777/todo', function (error, response, body) {
assert.ok(response.statusCode === 405, 'Got 405 for .find');
assert.deepEqual(JSON.parse(body), { message: 'Can not call service method .find' }, 'Error serialized as expected');
server.close(done);
});
});
});
});
Morty Proxy This is a proxified and sanitized view of the page, visit original site.