koa-lite/lib/context.js

417 lines
6.4 KiB
JavaScript
Raw Normal View History

2013-08-17 07:15:57 +00:00
/**
* Module dependencies.
*/
var debug = require('debug')('koa:context');
var Request = require('./request');
var Response = require('./response');
var Cookies = require('cookies');
2013-08-17 07:15:57 +00:00
var http = require('http');
/**
* Prototype.
*/
module.exports = {
2013-08-17 07:15:57 +00:00
/**
* Delegate to Request#header.
2013-08-17 07:15:57 +00:00
*/
get header() {
return this.request.header;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#url.
2013-08-17 07:15:57 +00:00
*/
get url() {
return this.request.url;
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Request#url=.
*/
2013-08-28 02:30:35 +00:00
set url(val) {
this.request.url = val;
2013-08-28 02:30:35 +00:00
},
2013-08-28 02:30:35 +00:00
/**
* Delegate to Request#method.
2013-08-28 02:30:35 +00:00
*/
get method() {
return this.request.method;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#method=.
2013-08-17 07:15:57 +00:00
*/
set method(val) {
this.request.method = val;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Response#status.
2013-08-17 07:15:57 +00:00
*/
2013-10-24 06:54:07 +00:00
get status() {
return this.response.status;
2013-10-24 06:44:22 +00:00
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Response#status=.
2013-08-17 07:15:57 +00:00
*/
set status(val) {
this.response.status = val;
2013-10-24 06:44:22 +00:00
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Response#body.
2013-08-17 07:15:57 +00:00
*/
get body() {
return this.response.body;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Response#body=.
2013-08-17 07:15:57 +00:00
*/
set body(val) {
this.response.body = val;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#path.
2013-08-17 07:15:57 +00:00
*/
get path() {
return this.request.path;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#path=.
2013-08-17 07:15:57 +00:00
*/
set path(val) {
this.request.path = val;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#query.
2013-08-17 07:15:57 +00:00
*/
get query() {
return this.request.query;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#query=.
2013-08-17 07:15:57 +00:00
*/
set query(val) {
this.request.query = val;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#querystring.
2013-08-17 07:15:57 +00:00
*/
get querystring() {
return this.request.querystring;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#querystring=.
2013-08-17 07:15:57 +00:00
*/
set querystring(val) {
this.request.querystring = val;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#host.
2013-08-17 07:15:57 +00:00
*/
get host() {
return this.request.host;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#fresh.
2013-08-17 07:15:57 +00:00
*/
get fresh() {
return this.request.fresh;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#stale.
2013-08-17 07:15:57 +00:00
*/
get stale() {
return this.request.stale;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#idempotent.
2013-08-17 07:15:57 +00:00
*/
get idempotent() {
return this.request.idempotent;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#socket.
2013-08-17 07:15:57 +00:00
*/
get socket() {
return this.request.socket;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#length.
2013-08-17 07:15:57 +00:00
*/
get length() {
return this.request.length;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#length.
*/
set length(val) {
this.response.length = val;
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Request#protocol.
2013-08-17 07:15:57 +00:00
*/
get protocol() {
return this.request.protocol;
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Request#secure.
*/
2013-08-17 07:15:57 +00:00
get secure() {
return this.request.secure;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#ip.
2013-08-17 07:15:57 +00:00
*/
get ip() {
return this.request.ip;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#ips.
2013-08-17 07:15:57 +00:00
*/
get ips() {
return this.request.ips;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#subdomains.
2013-08-17 07:15:57 +00:00
*/
get subdomains() {
return this.request.subdomains;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Response#headerSent.
2013-08-17 07:15:57 +00:00
*/
get headerSent() {
return this.response.headerSent;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Response#type=.
2013-08-17 07:15:57 +00:00
*/
set type(val) {
this.response.type = val;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#type.
2013-08-17 07:15:57 +00:00
*/
get type() {
return this.request.type;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#accepts().
2013-08-17 07:15:57 +00:00
*/
accepts: function() {
return this.request.accepts.apply(this.request, arguments);
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#acceptsCharsets().
2013-08-17 07:15:57 +00:00
*/
acceptsCharsets: function() {
return this.request.acceptsCharsets.apply(this.request, arguments);
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#acceptsEncodings().
2013-08-17 07:15:57 +00:00
*/
acceptsEncodings: function() {
return this.request.acceptsEncodings.apply(this.request, arguments);
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#acceptsLanguages().
2013-08-17 07:15:57 +00:00
*/
acceptsLanguages: function() {
return this.request.acceptsLanguages.apply(this.request, arguments);
2013-08-17 07:15:57 +00:00
},
2013-10-24 06:54:07 +00:00
2013-11-08 22:38:35 +00:00
/**
* Delegate to Response#vary().
2013-11-08 22:38:35 +00:00
*/
vary: function() {
return this.response.vary.apply(this.response, arguments);
},
2013-08-17 07:15:57 +00:00
/**
* 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);
},
2013-08-28 03:54:13 +00:00
/**
* Throw an error with `msg` and optional `status`
* defaulting to 500. Note that these are user-level
* errors, and the message may be exposed to the client.
2013-08-28 03:54:13 +00:00
*
* this.error(403)
* this.error('name required', 400)
* this.error('something exploded')
*
* @param {String|Number} msg
* @param {Number} status
* @api public
*/
error: function(msg, status){
2013-09-14 04:06:16 +00:00
// TODO: switch order... feels weird now that im used to express
2013-08-28 03:54:13 +00:00
if ('number' == typeof msg) {
var tmp = msg;
msg = http.STATUS_CODES[tmp];
status = tmp;
}
var err = new Error(msg);
err.status = status || 500;
err.expose = true;
2013-08-28 03:54:13 +00:00
throw err;
},
2013-08-17 07:15:57 +00:00
/**
* Default error handling.
*
* @param {Error} err
* @api private
*/
2013-08-22 02:47:56 +00:00
onerror: function(err){
2013-10-09 06:23:14 +00:00
// don't do anything if there is no error.
// this allows you to pass `this.onerror`
// to node-style callbacks.
if (!err) return;
2013-08-22 02:47:56 +00:00
// nothing we can do here other
// than delegate to the app-level
// handler and log.
if (this.headerSent) {
err.headerSent = true;
this.app.emit('error', err, this);
return;
}
// delegate
this.app.emit('error', err, this);
2013-08-22 02:47:56 +00:00
2013-09-08 19:11:02 +00:00
// force text/plain
this.type = 'text';
// ENOENT support
if ('ENOENT' == err.code) err.status = 404;
// default to 500
err.status = err.status || 500;
2013-08-22 02:47:56 +00:00
// respond
var code = http.STATUS_CODES[err.status];
var msg = err.expose ? err.message : code;
2013-09-08 19:11:02 +00:00
this.status = err.status;
this.res.end(msg);
2013-08-17 07:15:57 +00:00
}
};