Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ES6 classes - v2.x #605

Closed
wants to merge 11 commits into from
36 changes: 3 additions & 33 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@
const isGeneratorFunction = require('is-generator-function');
const debug = require('debug')('koa:application');
const onFinished = require('on-finished');
const response = require('./response');
const compose = require('koa-compose');
const isJSON = require('koa-is-json');
const context = require('./context');
const request = require('./request');
const statuses = require('statuses');
const Cookies = require('cookies');
const accepts = require('accepts');
const Emitter = require('events');
const assert = require('assert');
const Stream = require('stream');
const http = require('http');
const only = require('only');

const Context = require('./context');

/**
* Expose `Application` class.
* Inherits from `Emitter.prototype`.
Expand All @@ -42,9 +39,6 @@ module.exports = class Application extends Emitter {
this.middleware = [];
this.subdomainOffset = 2;
this.env = process.env.NODE_ENV || 'development';
this.context = Object.create(context);
this.request = Object.create(request);
this.response = Object.create(response);
}

/**
Expand Down Expand Up @@ -121,36 +115,12 @@ module.exports = class Application extends Emitter {

return (req, res) => {
res.statusCode = 404;
const ctx = this.createContext(req, res);
const ctx = new Context(this, req, res);
onFinished(res, ctx.onerror);
fn(ctx).then(() => respond(ctx)).catch(ctx.onerror);
};
}

/**
* Initialize a new context.
*
* @api private
*/

createContext(req, res) {
const context = Object.create(this.context);
const request = context.request = Object.create(this.request);
const response = context.response = Object.create(this.response);
context.app = request.app = response.app = this;
context.req = request.req = response.req = req;
context.res = request.res = response.res = res;
request.ctx = response.ctx = context;
request.response = response;
response.request = request;
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);
context.state = {};
return context;
}

/**
* Default error handler.
*
Expand Down
65 changes: 43 additions & 22 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,52 @@
* Module dependencies.
*/

const accepts = require('accepts');
const createError = require('http-errors');
const httpAssert = require('http-assert');
const delegate = require('delegates');
const httpAssert = require('http-assert');
const statuses = require('statuses');
const Cookies = require('cookies');

const Request = require('./request');
const Response = require('./response');

/**
* Context prototype.
*/

const proto = module.exports = {
const Context = module.exports = class Context {

/**
* Initialize a new context.
*
* @api private
*/

constructor(app, req, res) {
const request = this.request = new Request();
const response = this.response = new Response();
this.app = request.app = response.app = app;
this.req = request.req = response.req = req;
this.res = request.res = response.res = res;
request.ctx = response.ctx = this;
request.response = response;
response.request = request;
this.onerror = this.onerror.bind(this);
this.originalUrl = request.originalUrl = req.url;
this.cookies = new Cookies(req, res, app.keys);
this.accept = request.accept = accepts(req);
this.state = {};

/**
* Similar to .throw(), adds assertion.
*
* ctx.assert(ctx.user, 401, 'Please login!');
*
* See: https://github.com/jshttp/http-assert
*/
this.assert = httpAssert;
}

/**
* util.inspect() implementation, which
Expand All @@ -26,7 +62,7 @@ const proto = module.exports = {

inspect() {
return this.toJSON();
},
}

/**
* Return JSON representation.
Expand All @@ -50,22 +86,7 @@ const proto = module.exports = {
res: '<original node res>',
socket: '<original node socket>'
};
},

/**
* Similar to .throw(), adds assertion.
*
* this.assert(this.user, 401, 'Please login!');
*
* See: https://github.com/jshttp/http-assert
*
* @param {Mixed} test
* @param {Number} status
* @param {String} message
* @api public
*/

assert: httpAssert,
}

/**
* Throw an error with `msg` and optional `status`
Expand All @@ -89,7 +110,7 @@ const proto = module.exports = {

throw() {
throw createError.apply(null, arguments);
},
}

/**
* Default error handling.
Expand Down Expand Up @@ -142,7 +163,7 @@ const proto = module.exports = {
* Response delegation.
*/

delegate(proto, 'response')
delegate(Context.prototype, 'response')
.method('attachment')
.method('redirect')
.method('remove')
Expand All @@ -163,7 +184,7 @@ delegate(proto, 'response')
* Request delegation.
*/

delegate(proto, 'request')
delegate(Context.prototype, 'request')
.method('acceptsLanguages')
.method('acceptsEncodings')
.method('acceptsCharsets')
Expand Down
Loading