diff --git a/lib/Router/responseType.js b/lib/Router/responseType.js index a456abc..4c57be7 100644 --- a/lib/Router/responseType.js +++ b/lib/Router/responseType.js @@ -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 * @@ -11,7 +17,7 @@ module.exports = function responseType(req, res, next) { if (!req.headers) req.headers = {}; - req.headers.accept = parseResponseType(req); + parseResponseType(req); next(); }; @@ -25,8 +31,6 @@ 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 @@ -34,11 +38,10 @@ function parseResponseType(req) { 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; } \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 9982caf..042e16a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; } } } diff --git a/lib/utils/index.js b/lib/utils/index.js index f483b7c..2154aca 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -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 diff --git a/plugin.js b/plugin.js index df43e0c..7db25ad 100644 --- a/plugin.js +++ b/plugin.js @@ -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' diff --git a/src/Router/responseType.js b/src/Router/responseType.js index 989da66..16018ad 100644 --- a/src/Router/responseType.js +++ b/src/Router/responseType.js @@ -1,3 +1,5 @@ +import mime from 'mime' + /** * Parse response type middleware * @@ -9,7 +11,7 @@ module.exports = function responseType (req, res, next){ if (!req.headers) req.headers = {}; - req.headers.accept = parseResponseType(req); + parseResponseType(req); next(); } @@ -23,8 +25,6 @@ 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 @@ -32,11 +32,11 @@ function parseResponseType (req) { 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; } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 588939e..5db2703 100644 --- a/src/index.js +++ b/src/index.js @@ -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 } } } diff --git a/src/utils/index.js b/src/utils/index.js index cf03476..43fcf3b 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -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