Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
fix lint and replace foreach() with some()
Browse files Browse the repository at this point in the history
  • Loading branch information
typerandom committed Feb 19, 2016
1 parent bd9a9ee commit 5b3221c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
40 changes: 14 additions & 26 deletions lib/helpers/handle-accept-request.js
@@ -1,8 +1,7 @@
'use strict';

var _ = require('lodash');

var SpaResponseHandler = require('./spa-handler');
var spaResponseHandler = require('./spa-response-handler');

/**
* Determines which handler should be used to fulfill a response, given the
Expand All @@ -24,46 +23,35 @@ var SpaResponseHandler = require('./spa-handler');
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

// 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.
*/

// 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

// 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.
*/

// 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.forEach(function (contentType) {
if (handler) {
return;
allowedResponseTypes.some(function (contentType) {
if (config.web.spa.enabled && contentType === 'text/html') {
handler = spaResponseHandler(config);
return true;
}

if (contentType === 'text/html' && config.web.spa.enabled) {
handler = new SpaResponseHandler(config);
return;
if (contentType in handlers) {
handler = handlers[contentType];
return true;
}
handler = handlers[contentType];
});



if (!handler) {
return fallbackHandler();
}
Expand Down
Expand Up @@ -8,8 +8,8 @@
* @return {Function} spaResponseHandler Middleware function that writes the SPA
* response from the configured file.
*/
module.exports = function SpaResponseHandler(stormpathConfig) {
return function spaResponseHandler(req, res) {
module.exports = function spaResponseHandler(stormpathConfig) {
return function spaResponseHandlerMiddleware(req, res) {

This comment has been minimized.

Copy link
@robertjd

robertjd Feb 19, 2016

Member

Thanks, I like this better. I tried a few different ideas that I didn't like, including a SpaResponseHandlerBuilder :)

res.sendFile(stormpathConfig.web.spa.view);
};
};
2 changes: 0 additions & 2 deletions test/produces.js
@@ -1,11 +1,9 @@
'use strict';

var helpers = require('./helpers');

var ProducesFixture = require('./fixtures/produces-fixture');

describe('produces option', function () {

var stormpathApplication;

before(function (done) {
Expand Down

0 comments on commit 5b3221c

Please sign in to comment.