Skip to content

Commit

Permalink
feat: export context base classes on Application (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored and popomore committed Apr 10, 2017
1 parent ee127ad commit c33523d
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 7 deletions.
10 changes: 3 additions & 7 deletions app/extend/context.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
'use strict';

const delegate = require('delegates');
const ContextLogger = require('egg-logger').EggContextLogger;
const Cookies = require('egg-cookies');
const co = require('co');
const ContextHttpClient = require('../../lib/core/context_httpclient');
const { assign } = require('utility');


const HELPER = Symbol('Context#helper');
const LOCALS = Symbol('Context#locals');
const LOCALS_LIST = Symbol('Context#localsList');
Expand All @@ -18,7 +14,7 @@ const CONTEXT_HTTPCLIENT = Symbol('Context#httpclient');
const proto = module.exports = {
get cookies() {
if (!this[COOKIES]) {
this[COOKIES] = new Cookies(this, this.app.keys);
this[COOKIES] = new this.app.ContextCookies(this, this.app.keys);
}
return this[COOKIES];
},
Expand All @@ -30,7 +26,7 @@ const proto = module.exports = {
*/
get httpclient() {
if (!this[CONTEXT_HTTPCLIENT]) {
this[CONTEXT_HTTPCLIENT] = new ContextHttpClient(this);
this[CONTEXT_HTTPCLIENT] = new this.app.ContextHttpClient(this);
}
return this[CONTEXT_HTTPCLIENT];
},
Expand Down Expand Up @@ -96,7 +92,7 @@ const proto = module.exports = {
if (!appLogger) return null;

// write to cache
cache[name] = new ContextLogger(this, appLogger);
cache[name] = new this.app.ContextLogger(this, appLogger);
return cache[name];
},

Expand Down
8 changes: 8 additions & 0 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const fs = require('fs');
const EggCore = require('egg-core').EggCore;
const cluster = require('cluster-client');
const extend = require('extend2');
const ContextLogger = require('egg-logger').EggContextLogger;
const ContextCookies = require('egg-cookies');
const ContextHttpClient = require('./core/context_httpclient');
const Messenger = require('./core/messenger');
const createHttpClient = require('./core/httpclient');
const createLoggers = require('./core/logger');
Expand Down Expand Up @@ -34,6 +37,11 @@ class EggApplication extends EggCore {
constructor(options) {
super(options);

// export context base classes, let framework can impl sub class and over context extend easily.
this.ContextCookies = ContextCookies;
this.ContextLogger = ContextLogger;
this.ContextHttpClient = ContextHttpClient;

this.loader.loadConfig();

/**
Expand Down
15 changes: 15 additions & 0 deletions test/app/extend/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ describe('test/app/extend/context.test.js', () => {
});
});

describe('app or framework can override ctx.getLogger', () => {
let app;
before(() => {
app = utils.app('apps/custom-context-getlogger');
return app.ready();
});
after(() => app.close());

it('should log with custom logger', () => {
return request(app.callback())
.get('/')
.expect('work, logger: exists');
});
});

describe('properties', () => {
let app;
before(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = function* () {
const logger = this.getLogger('foo');
logger.info('hello');
this.body = 'work, logger: ' + (logger ? 'exists' : 'not exists');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
getLogger(name) {
console.log('get custom %s logger', name);
return new this.app.ContextLogger(this, console);
},
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/custom-context-getlogger/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = app => {
app.get('/', 'home');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'foo';
3 changes: 3 additions & 0 deletions test/fixtures/apps/custom-context-getlogger/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "custom-context-getlogger"
}

0 comments on commit c33523d

Please sign in to comment.