From 20615b808df0dd146a7de4feba4dc7625fc90b8c Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Fri, 27 Dec 2013 18:53:19 -0800 Subject: [PATCH] refactor: move .is to type-is --- lib/request.js | 60 ++++------------------------------------------ package.json | 1 + test/request/is.js | 14 +++++------ 3 files changed, 12 insertions(+), 63 deletions(-) diff --git a/lib/request.js b/lib/request.js index 87b87dc..6300530 100644 --- a/lib/request.js +++ b/lib/request.js @@ -5,8 +5,8 @@ var debug = require('debug')('koa:context'); var qs = require('querystring'); +var typeis = require('type-is'); var fresh = require('fresh'); -var mime = require('mime'); var url = require('url'); var parse = url.parse; var stringify = url.format; @@ -486,38 +486,9 @@ module.exports = { */ is: function(types){ - // no request body - if (!(this.length || 'transfer-encoding' in this.header)) return; - - var ct = this.type; - // no content-type - if (!ct) return false; - - // paramless - ct = ct.split(';')[0]; - - if (!types) return ct; - - // ensure array + if (!types) return typeis(this.req); if (!Array.isArray(types)) types = [].slice.call(arguments); - - var mt; - for (var i = 0; i < types.length; i++) { - var type = types[i]; - - switch (type) { - case 'urlencoded': - mt = 'application/x-www-form-urlencoded'; - break; - default: - mt = ~type.indexOf('/') ? type : mime.lookup(type); - } - - if (mimeMatch(mt, ct)) return ct; - } - - // no matches - return false; + return typeis(this.req, types); }, /** @@ -592,27 +563,4 @@ module.exports = { header: this.header } } -}; - -/** - * Check if `exected` mime type - * matches `actual` mime type with - * wildcard support. - * - * @param {String} expected - * @param {String} actual - * @return {Boolean} - * @api private - */ - -function mimeMatch(expected, actual) { - if (expected == actual) return true; - - if (!~expected.indexOf('*')) return false; - - actual = actual.split('/'); - expected = expected.split('/'); - - if ('*' == expected[0] && expected[1] == actual[1]) return true; - if ('*' == expected[1] && expected[0] == actual[0]) return true; -} +}; \ No newline at end of file diff --git a/package.json b/package.json index 178ee12..69650bf 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "license": "MIT", "dependencies": { "accepts": "~1.0.0", + "type-is": "~1.0.0", "on-socket-error": "~1.0.1", "co": "~3.0.1", "debug": "*", diff --git a/test/request/is.js b/test/request/is.js index 057fd7e..415d9e5 100644 --- a/test/request/is.js +++ b/test/request/is.js @@ -49,8 +49,8 @@ describe('ctx.is(type)', function(){ ctx.header['content-type'] = 'image/png'; ctx.header['transfer-encoding'] = 'chunked'; - ctx.is('png').should.equal('image/png'); - ctx.is('.png').should.equal('image/png'); + ctx.is('png').should.equal('png'); + ctx.is('.png').should.equal('.png'); ctx.is('image/png').should.equal('image/png'); ctx.is('image/*').should.equal('image/png'); ctx.is('*/png').should.equal('image/png'); @@ -69,8 +69,8 @@ describe('ctx.is(type)', function(){ ctx.header['content-type'] = 'image/png'; ctx.header['transfer-encoding'] = 'chunked'; - ctx.is('png').should.equal('image/png'); - ctx.is('.png').should.equal('image/png'); + ctx.is('png').should.equal('png'); + ctx.is('.png').should.equal('.png'); ctx.is('text/*', 'image/*').should.equal('image/png'); ctx.is('image/*', 'text/*').should.equal('image/png'); ctx.is('image/*', 'image/png').should.equal('image/png'); @@ -89,9 +89,9 @@ describe('ctx.is(type)', function(){ ctx.header['content-type'] = 'application/x-www-form-urlencoded'; ctx.header['transfer-encoding'] = 'chunked'; - ctx.is('urlencoded').should.equal('application/x-www-form-urlencoded'); - ctx.is('json', 'urlencoded').should.equal('application/x-www-form-urlencoded'); - ctx.is('urlencoded', 'json').should.equal('application/x-www-form-urlencoded'); + ctx.is('urlencoded').should.equal('urlencoded'); + ctx.is('json', 'urlencoded').should.equal('urlencoded'); + ctx.is('urlencoded', 'json').should.equal('urlencoded'); }) }) }) \ No newline at end of file