refactor: move content negotiation to accepts

This commit is contained in:
Jonathan Ong 2013-12-27 13:56:21 -08:00
parent 3891e1787b
commit 7fe4133b4a
3 changed files with 10 additions and 41 deletions

View file

@ -11,6 +11,7 @@ var request = require('./request');
var response = require('./response');
var Cookies = require('cookies');
var Keygrip = require('keygrip');
var accepts = require('accepts');
var assert = require('assert');
var http = require('http');
var co = require('co');
@ -157,6 +158,7 @@ app.createContext = function(req, res){
context.onerror = context.onerror.bind(context);
context.originalUrl = request.originalUrl = req.url;
context.cookies = new Cookies(req, res, this.keys);
context.accept = request.accept = accepts(req);
return context;
};

View file

@ -4,7 +4,6 @@
*/
var debug = require('debug')('koa:context');
var Negotiator = require('negotiator');
var qs = require('querystring');
var fresh = require('fresh');
var mime = require('mime');
@ -394,16 +393,8 @@ module.exports = {
* @api public
*/
accepts: function(types){
if (!Array.isArray(types)) types = [].slice.call(arguments);
var n = new Negotiator(this.req);
if (!types.length) return n.preferredMediaTypes();
if (!this.header.accept) return types[0];
var mimes = types.map(extToMime);
var accepts = n.preferredMediaTypes(mimes);
var first = accepts[0];
if (!first) return false;
return types[mimes.indexOf(first)];
accepts: function(){
return this.accept.types.apply(this.accept, arguments);
},
/**
@ -419,11 +410,8 @@ module.exports = {
* @api public
*/
acceptsEncodings: function(encodings) {
if (!Array.isArray(encodings)) encodings = [].slice.call(arguments);
var n = new Negotiator(this.req);
if (!encodings.length) return n.preferredEncodings();
return n.preferredEncodings(encodings)[0] || 'identity';
acceptsEncodings: function(){
return this.accept.encodings.apply(this.accept, arguments);
},
/**
@ -439,12 +427,8 @@ module.exports = {
* @api public
*/
acceptsCharsets: function(charsets) {
if (!Array.isArray(charsets)) charsets = [].slice.call(arguments);
var n = new Negotiator(this.req);
if (!charsets.length) return n.preferredCharsets();
if (!this.header['accept-charset']) return charsets[0];
return n.preferredCharsets(charsets)[0] || false;
acceptsCharsets: function(){
return this.accept.charsets.apply(this.accept, arguments);
},
/**
@ -461,11 +445,7 @@ module.exports = {
*/
acceptsLanguages: function(langs) {
if (!Array.isArray(langs)) langs = [].slice.call(arguments);
var n = new Negotiator(this.req);
if (!langs.length) return n.preferredLanguages();
if (!this.header['accept-language']) return langs[0];
return n.preferredLanguages(langs)[0] || false;
return this.accept.languages.apply(this.accept, arguments);
},
/**
@ -614,19 +594,6 @@ module.exports = {
}
};
/**
* Convert extnames to mime.
*
* @param {String} type
* @return {String}
* @api private
*/
function extToMime(type) {
if (~type.indexOf('/')) return type;
return mime.lookup(type);
}
/**
* Check if `exected` mime type
* matches `actual` mime type with

View file

@ -21,12 +21,12 @@
],
"license": "MIT",
"dependencies": {
"accepts": "~1.0.0",
"on-socket-error": "~1.0.1",
"co": "~3.0.1",
"debug": "*",
"mime": "~1.2.11",
"fresh": "~0.2.0",
"negotiator": "~0.3.0",
"koa-compose": "~2.1.0",
"cookies": "~0.3.7",
"keygrip": "~1.0.0"