Name: bedrock-rest
Owner: Digital Bazaar, Inc.
Description: Bedrock REST support
Created: 2015-01-16 16:04:06.0
Updated: 2016-03-03 14:47:17.0
Pushed: 2017-06-28 01:28:35.0
Homepage: null
Size: 51
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A bedrock module that adds helpers for REST support.
install bedrock-rest
s
bedrock = require('bedrock');
brIdentity = require('bedrock-identity');
brPassport = require('bedrock-passport');
brRest = require('bedrock-rest');
cors = require('cors');
myItems = require('my-items-library');
ock.events.on('bedrock-express.init', function(app) {
p.param('identity', brRest.idParam);
ock.events.on('bedrock-express.configure.routes', addRoutes);
tion addRoutes(app) {
r idPath = bedrock.config.identity.basePath + '/:identity';
p.get(idPath + '/dashboard',
brPassport.ensureAuthenticated,
brRest.makeResourceHandler());
p.options(idPath + '/items', cors());
p.get(idPath + '/items',
cors(),
brRest.makeResourceHandler({
get: function(req, res, callback) {
var identityId = brIdentity.createIdentityId(req.params.identity);
myItems.getItems(identityId, function(err, items) {
if(err) {
return callback(err);
}
callback(null, items);
});
}
}));
p.options(idPath + '/items/:item', cors());
p.get(idPath + '/items/:item',
cors(),
brRest.makeResourceHandler({
get: function(req, res, callback) {
var identityId = brIdentity.createIdentityId(req.params.identity);
var itemId = myItems.createItemId(identityId, req.params.item);
myItems.getItem({id: itemId}, function(err, item) {
callback(err, item);
});
},
template: 'item.html',
templateNeedsResource: true,
updateVars: function(resource, vars, callback) {
vars.item = resource;
callback();
}
}));
Validates an ID from a URL path and, it passes validation, it will be available via req.params. This method is for use with express or bedrock-express:
assed to an express server's param call
er.param(':foo', rest.idParam);
etup with "bedrock-express.init" event
ock.events.on('bedrock-express.init', function(app) {
p.param('foo', rest.idParam);
Make middleware for a type negotiated REST resource. This middleware handles
the details of handling requests for json
, application/ld+json
, and
html
. JSON based requests will just return the data from the get
option.
HTML requests will return a HTML template as needed. It defaults to
main.html
which could be setup to start a single page app that could
include the data with updateVars
or re-call the resource URL and request
JSON based data.
options
:
ResourceHandler({
t: function(req, res, callback) {
myLib.load(..., function(err, resource) {
if(err) {
return callback(err);
}
callback(null, resource);
}
mplate: 'my-template.html',
mplateNeedsResource: true,
dateVars = function(resource, vars, callback) {
vars.resource = resource;
callback();