app.context: extend the context with your own properties

master
Jonathan Ong 2013-08-17 15:36:51 -07:00
parent cdba6f87ed
commit 978f581099
4 changed files with 160 additions and 39 deletions

View File

@ -6,7 +6,7 @@
var debug = require('debug')('koa:app');
var Emitter = require('events').EventEmitter;
var compose = require('koa-compose');
var Context = require('./context');
var context = require('./context');
var Stream = require('stream');
var http = require('http');
var co = require('co');
@ -37,6 +37,12 @@ function Application() {
this.poweredBy = true;
this.jsonSpaces = 2;
this.middleware = [];
this._Context = function Context(app, req, res){
this.app = app;
this.req = req;
this.res = res;
}
this.context(context);
}
/**
@ -74,6 +80,35 @@ app.use = function(fn){
return this;
};
/**
* Mixin an app's context with your own object.
*
* app.context({
* get something(){
* return 'hi';
* },
* set something(val){
* this._something = val;
* },
* render: function(){
* this.body = '<html></html>';
* }
* })
*
* @param {Object} obj
* @return {Application} self
* @api public
*/
app.context = function(obj){
var ctx = this._Context.prototype;
Object.getOwnPropertyNames(obj).forEach(function(name){
var descriptor = Object.getOwnPropertyDescriptor(obj, name);
Object.defineProperty(ctx, name, descriptor);
});
return this;
};
/**
* Return a request handler callback
* for node's native htto server.
@ -88,7 +123,7 @@ app.callback = function(){
var self = this;
return function(req, res){
var ctx = new Context(self, req, res);
var ctx = new self._Context(self, req, res);
function done(err) {
if (err) ctx.error(err);

View File

@ -16,35 +16,12 @@ var url = require('url');
var qs = require('qs');
var parse = url.parse;
var stringify = url.format;
var ctx = Context.prototype;
/**
* Expose `Context`.
*/
module.exports = Context;
/**
* Initialize a new Context with raw node
* request and response objects.
*
* @param {Application} app
* @param {Request} req
* @param {Request} res
* @api public
*/
function Context(app, req, res) {
this.app = app;
this.req = req;
this.res = res;
}
/**
* Prototype.
*/
Context.prototype = {
module.exports = {
/**
* Return request header.

View File

@ -253,3 +253,104 @@ describe('app.respond', function(){
})
})
})
describe('app.context', function(){
it('should merge regular object properties', function(done){
var app = koa();
app.context({
a: 1,
b: 2
});
app.use(function(next){
return function *(){
this.a.should.equal(1);
this.b.should.equal(2);
this.status = 204;
}
});
var server = http.createServer(app.callback());
request(server)
.get('/')
.expect(204)
.end(done);
})
it('should merge accessor properties', function(done){
var app = koa();
app.context({
get something() {
return this._something || 'hi';
},
set something(value) {
this._something = value;
}
});
app.use(function(next){
return function *(){
this.something.should.equal('hi');
this.something = 'hello';
this.something.should.equal('hello');
this.status = 204;
}
});
var server = http.createServer(app.callback());
request(server)
.get('/')
.expect(204)
.end(done);
})
it('should merge multiple objects', function(done){
var app = koa();
app.context({
a: 1
});
app.context({
b: 2
});
app.use(function(next){
return function *(){
this.a.should.equal(1);
this.b.should.equal(2);
this.status = 204;
}
});
var server = http.createServer(app.callback());
request(server)
.get('/')
.expect(204)
.end(done);
})
it('should not butcher the original prototype', function(done){
var app1 = koa();
var app2 = koa();
app1.context({
a: 1
});
app2.use(function(next){
return function *(){
assert.equal(this.a, undefined);
this.status = 204;
}
});
var server = http.createServer(app2.callback());
request(server)
.get('/')
.expect(204)
.end(done);
})
})

View File

@ -1,9 +1,17 @@
var Context = require('../lib/context');
var _context = require('../lib/context');
var assert = require('assert');
var koa = require('..');
var fs = require('fs');
function Context(app, req, res) {
this.app = app;
this.req = req;
this.res = res;
}
Context.prototype = _context;
function context(req, res) {
req = req || { headers: {} };
res = res || { _headers: {} };