digitalbazaar/bedrock-rest

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

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

bedrock-rest

A bedrock module that adds helpers for REST support.

Requirements
Quick Examples
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();
  }
}));

API
idParam(req, res, next, id)

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);

makeResourceHandler(options)

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();



This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.