all files / express-stormpath/lib/helpers/ handle-accept-request.js

95.24% Statements 20/21
80% Branches 8/10
100% Functions 3/3
95.24% Lines 20/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                                        48×     48× 48×       48× 48×       48×       48×   48× 48×     45× 45× 45×       48×       48×    
'use strict';
 
var _ = require('lodash');
var spaResponseHandler = require('./spa-response-handler');
 
/**
 * Determines which handler should be used to fulfill a response, given the
 * Accept header of the request and the content types that are allowed by the
 * `stormpath.web.produces` configuration.  Also handles the serving of the SPA
 * root page, if needed.
 *
 * @method
 * @private
 *
 * @param {Object} req - HTTP request.
 * @param {Object} res - HTTP response.
 * @param {Object} handlers - Object where the keys are content types and the
 * functions are handlers that will be called, if needed, to fulfill the
 * response as that type.
 * @param {Function} fallbackHandler - Handler to call when an acceptable
 * content type could not be resolved.
 */
function handleAcceptRequest(req, res, handlers, fallbackHandler) {
  var config = req.app.get('stormpathConfig');
 
  // Accepted is an ordered list of preferred types, as specified by the request.
  var accepted = req.accepts();
  var produces = config.web.produces;
 
  // Our default response is HTML, if the client does not specify something more
  // specific.  As such, map the wildcard type to html.
  accepted = accepted.map(function (contentType) {
    return contentType === '*/*' ? 'text/html' : contentType;
  });
 
  // Of the accepted types, find the ones that are allowed by the configuration.
  var allowedResponseTypes = _.intersection(produces, accepted);
 
  // Of the allowed response types, find the first handler that matches. But
  // always override with the SPA handler if SPA is enabled.
  var handler;
 
  allowedResponseTypes.some(function (contentType) {
    if (config.web.spa.enabled && contentType === 'text/html') {
      handler = spaResponseHandler(config);
      return true;
    }
 
    Eif (contentType in handlers) {
      handler = handlers[contentType];
      return true;
    }
  });
 
  Iif (!handler) {
    return fallbackHandler();
  }
 
  handler(req, res);
}
 
module.exports = handleAcceptRequest;