Skip to content

Commit

Permalink
improve response format handler
Browse files Browse the repository at this point in the history
  • Loading branch information
albertosouza committed Jun 16, 2016
1 parent 5667440 commit b106b20
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 22 deletions.
17 changes: 10 additions & 7 deletions lib/Router/responseType.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'use strict';

var _mime = require('mime');

var _mime2 = _interopRequireDefault(_mime);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
* Parse response type middleware
*
Expand All @@ -11,7 +17,7 @@ module.exports = function responseType(req, res, next) {

if (!req.headers) req.headers = {};

req.headers.accept = parseResponseType(req);
parseResponseType(req);

next();
};
Expand All @@ -25,20 +31,17 @@ module.exports = function responseType(req, res, next) {
* @return {String} the response type string
*/
function parseResponseType(req) {
if (req.extension) return req.extension.toLowerCase();

if (req.query && req.query.responseType) {
if (req.query.responseType == 'modal') {
// suport for old we.js contentOnly api
req.query.responseType = req.we.config.defaultResponseType;
req.query.contentOnly = true;
}

return req.query.responseType.toLowerCase();
req.headers.accept = _mime2.default.lookup(req.query.responseType.toLowerCase());
}

var type = req.accepts(req.we.config.responseTypes);
if (type) return type;
if (req.accepts(req.we.config.responseTypes)) return;

return req.we.config.defaultResponseType;
req.headers.accept = req.we.config.defaultResponseType;
}
11 changes: 7 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,15 @@ We.prototype.startServer = function startExpressServer(cb) {
// parse extension if not is public folder
if (!we.router.isPublicFolder(req.url)) {
req.extension = we.router.splitExtensionFromURL(req.url);

if (req.extension) {
// check if is valid this extension and is one of acceptable extensions
if (we.config.responseTypes.indexOf(req.extension) > -1) {
var format = we.utils.mime.lookup(req.extension);

if (req.we.config.responseTypes.indexOf(format) !== -1) {
// update the header response format with extension format
req.headers.accept = format;
// update the old url
req.url = req.url.replace('.' + req.extension, '');
} else {
req.extension = null;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
mkdirp: require('mkdirp'),
string: require('string'),
cookieParser: require('cookie-parser'),
mime: require('mime'),

/**
* Is authenticated method usable if we-plugin-auth not is installed
Expand Down
1 change: 1 addition & 0 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = function loadPlugin(projectPath, Plugin) {
queryDefaultLimit: 25,
queryMaxLimit: 300,
// map reponseType response types, used by Accept headers in response selection
// this is set automaticaly after add new response types
responseTypes: [
'application/json',
'application/vnd.api+json'
Expand Down
14 changes: 7 additions & 7 deletions src/Router/responseType.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import mime from 'mime'

/**
* Parse response type middleware
*
Expand All @@ -9,7 +11,7 @@ module.exports = function responseType (req, res, next){

if (!req.headers) req.headers = {};

req.headers.accept = parseResponseType(req);
parseResponseType(req);

next();
}
Expand All @@ -23,20 +25,18 @@ module.exports = function responseType (req, res, next){
* @return {String} the response type string
*/
function parseResponseType (req) {
if (req.extension) return req.extension.toLowerCase();

if (req.query && req.query.responseType) {
if (req.query.responseType == 'modal') {
// suport for old we.js contentOnly api
req.query.responseType = req.we.config.defaultResponseType;
req.query.contentOnly = true;
}

return req.query.responseType.toLowerCase();
req.headers.accept = mime.lookup(req.query.responseType.toLowerCase());
}

var type = req.accepts(req.we.config.responseTypes);
if (type) return type;
if (req.accepts(req.we.config.responseTypes))
return;

return req.we.config.defaultResponseType;
req.headers.accept = req.we.config.defaultResponseType;
}
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,15 @@ We.prototype.startServer = function startExpressServer(cb) {
// parse extension if not is public folder
if (!we.router.isPublicFolder(req.url)) {
req.extension = we.router.splitExtensionFromURL(req.url)

if (req.extension) {
// check if is valid this extension and is one of acceptable extensions
if (we.config.responseTypes.indexOf(req.extension) >-1) {
let format = we.utils.mime.lookup(req.extension)

if (req.we.config.responseTypes.includes(format)) {
// update the header response format with extension format
req.headers.accept = format
// update the old url
req.url = req.url.replace('.'+req.extension, '')
} else {
req.extension = null
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
mkdirp: require('mkdirp'),
string: require('string'),
cookieParser: require('cookie-parser'),
mime: require('mime'),

/**
* Is authenticated method usable if we-plugin-auth not is installed
Expand Down

0 comments on commit b106b20

Please sign in to comment.