refactor with delegation utility

This commit is contained in:
TJ Holowaychuk 2014-01-13 06:36:10 -08:00
parent 26fb6a138a
commit 53b1b8133c
2 changed files with 55 additions and 380 deletions

View file

@ -4,13 +4,14 @@
*/ */
var debug = require('debug')('koa:context'); var debug = require('debug')('koa:context');
var delegate = require('delegates');
var http = require('http'); var http = require('http');
/** /**
* Prototype. * Context prototype.
*/ */
module.exports = { var proto = module.exports = {
/** /**
* Inspect implementation. * Inspect implementation.
@ -128,382 +129,55 @@ module.exports = {
var msg = err.expose ? err.message : code; var msg = err.expose ? err.message : code;
this.status = err.status; this.status = err.status;
this.res.end(msg); this.res.end(msg);
},
/**
* Delegate to Request#header.
*/
get header() {
return this.request.header;
},
/**
* Delegate to Request#url.
*/
get url() {
return this.request.url;
},
/**
* Delegate to Request#url=.
*/
set url(val) {
this.request.url = val;
},
/**
* Delegate to Request#method.
*/
get method() {
return this.request.method;
},
/**
* Delegate to Request#method=.
*/
set method(val) {
this.request.method = val;
},
/**
* Delegate to Response#status.
*/
get status() {
return this.response.status;
},
/**
* Delegate to Response#status=.
*/
set status(val) {
this.response.status = val;
},
/**
* Delegate to Response#body.
*/
get body() {
return this.response.body;
},
/**
* Delegate to Response#body=.
*/
set body(val) {
this.response.body = val;
},
/**
* Delegate to Request#path.
*/
get path() {
return this.request.path;
},
/**
* Delegate to Request#path=.
*/
set path(val) {
this.request.path = val;
},
/**
* Delegate to Request#query.
*/
get query() {
return this.request.query;
},
/**
* Delegate to Request#query=.
*/
set query(val) {
this.request.query = val;
},
/**
* Delegate to Request#querystring.
*/
get querystring() {
return this.request.querystring;
},
/**
* Delegate to Request#querystring=.
*/
set querystring(val) {
this.request.querystring = val;
},
/**
* Delegate to Request#search.
*/
get search() {
return this.request.search;
},
/**
* Delegate to Request#search=.
*/
set search(val) {
this.request.search = val;
},
/**
* Delegate to Request#host.
*/
get host() {
return this.request.host;
},
/**
* Delegate to Request#host=.
*/
set host(val) {
this.request.host = val;
},
/**
* Delegate to Request#fresh.
*/
get fresh() {
return this.request.fresh;
},
/**
* Delegate to Request#stale.
*/
get stale() {
return this.request.stale;
},
/**
* Delegate to Request#idempotent.
*/
get idempotent() {
return this.request.idempotent;
},
/**
* Delegate to Request#socket.
*/
get socket() {
return this.request.socket;
},
/**
* Delegate to Request#length.
*/
get length() {
return this.request.length;
},
/**
* Delegate to Request#length.
*/
set length(val) {
this.response.length = val;
},
/**
* Delegate to Request#protocol.
*/
get protocol() {
return this.request.protocol;
},
/**
* Delegate to Request#secure.
*/
get secure() {
return this.request.secure;
},
/**
* Delegate to Request#ip.
*/
get ip() {
return this.request.ip;
},
/**
* Delegate to Request#ips.
*/
get ips() {
return this.request.ips;
},
/**
* Delegate to Request#subdomains.
*/
get subdomains() {
return this.request.subdomains;
},
/**
* Delegate to Response#headerSent.
*/
get headerSent() {
return this.response.headerSent;
},
/**
* Delegate to Response#type=.
*/
set type(val) {
this.response.type = val;
},
/**
* Delegate to Request#type.
*/
get type() {
return this.request.type;
},
/**
* Delegate to Response#lastModified=.
*/
set lastModified(val) {
this.response.lastModified = val;
},
/**
* Delegate to Response#etag=.
*/
set etag(val) {
this.response.etag = val;
},
/**
* Delegate to Request#remove().
*/
remove: function() {
return this.response.remove.apply(this.response, arguments);
},
/**
* Delegate to Request#accepts().
*/
accepts: function() {
return this.request.accepts.apply(this.request, arguments);
},
/**
* Delegate to Request#acceptsCharsets().
*/
acceptsCharsets: function() {
return this.request.acceptsCharsets.apply(this.request, arguments);
},
/**
* Delegate to Request#acceptsEncodings().
*/
acceptsEncodings: function() {
return this.request.acceptsEncodings.apply(this.request, arguments);
},
/**
* Delegate to Request#acceptsLanguages().
*/
acceptsLanguages: function() {
return this.request.acceptsLanguages.apply(this.request, arguments);
},
/**
* Delegate to Response#vary().
*/
vary: function() {
return this.response.vary.apply(this.response, arguments);
},
/**
* Delegate to Request#is().
*/
is: function() {
return this.request.is.apply(this.request, arguments);
},
/**
* Delegate to Response#append().
*/
append: function() {
return this.response.append.apply(this.response, arguments);
},
/**
* Delegate to Request#get().
*/
get: function() {
return this.request.get.apply(this.request, arguments);
},
/**
* Delegate to Response#set().
*/
set: function() {
return this.response.set.apply(this.response, arguments);
},
/**
* Delegate to Response#redirect().
*/
redirect: function() {
return this.response.redirect.apply(this.response, arguments);
},
/**
* Delegate to Response#attachment().
*/
attachment: function() {
return this.response.attachment.apply(this.response, arguments);
} }
}; };
/**
* Response delegation.
*/
delegate(proto, 'response')
.method('attachment')
.method('redirect')
.method('append')
.method('remove')
.method('set')
.method('vary')
.access('status')
.access('body')
.getter('headerSent')
.setter('etag')
.setter('lastModified')
.setter('length')
.setter('type');
/**
* Request delegation.
*/
delegate(proto, 'request')
.method('acceptsLanguages')
.method('acceptsEncodings')
.method('acceptsCharsets')
.method('accepts')
.method('get')
.method('is')
.access('querystring')
.access('idempotent')
.access('socket')
.access('query')
.access('search')
.access('method')
.access('path')
.access('host')
.access('url')
.getter('length')
.getter('subdomains')
.getter('protocol')
.getter('header')
.getter('stale')
.getter('fresh')
.getter('secure')
.getter('ips')
.getter('ip')
.getter('type');

View file

@ -30,7 +30,8 @@
"fresh": "~0.2.0", "fresh": "~0.2.0",
"koa-compose": "~2.1.0", "koa-compose": "~2.1.0",
"cookies": "~0.3.7", "cookies": "~0.3.7",
"keygrip": "~1.0.0" "keygrip": "~1.0.0",
"delegates": "0.0.3"
}, },
"devDependencies": { "devDependencies": {
"bytes": "~0.2.1", "bytes": "~0.2.1",