Skip to content

Commit

Permalink
refactor: change accept to getter (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored and dead-horse committed Aug 31, 2016
1 parent 67278aa commit 2b0dbd3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
16 changes: 7 additions & 9 deletions app/extend/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const http = require('http');
const assert = require('assert');
const accepts = require('accepts');
const Keygrip = require('../../lib/core/keygrip');
const Service = require('../../lib/core/base_service');
const view = require('../../lib/core/view');
Expand All @@ -16,19 +15,19 @@ const VIEW = Symbol('Application#View');
const LOCALS = Symbol('Application#locals');
const LOCALS_LIST = Symbol('Application#localsList');

// 空的 instrument 返回,用于生产环境,避免每次创建对象
// empty instrument object, use on prod env, avoid create object every time.
const emptyInstrument = {
end() {},
};

module.exports = {

/**
* 创建 koa 上下文
* Create egg context
* @method Application#createContext
* @param {Req} req node 原生的 Request 对象
* @param {Res} res node 原生的 Response 对象
* @return {Context} 返回 koa 上下文
* @param {Req} req - node native Request object
* @param {Res} res - node native Response object
* @return {Context} context object
*/
createContext(req, res) {
const app = this;
Expand All @@ -43,7 +42,6 @@ module.exports = {
response.request = request;
context.onerror = context.onerror.bind(context);
context.originalUrl = request.originalUrl = req.url;
context.accept = request.accept = accepts(req);

/**
* Request start time
Expand All @@ -54,14 +52,14 @@ module.exports = {
},

/**
* Service 基类
* Service class
* @member {Service} Application#Service
* @since 1.0.0
*/
Service,

/**
* AppWorkerClient 基类
* AppWorkerClient class
* @member {AppWorkerClient} Application#AppWorkerClient
*/
AppWorkerClient,
Expand Down
10 changes: 6 additions & 4 deletions app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,15 @@ const proto = module.exports = {
* @see Request#isAjax
* @since 1.0.0
*/
delegate(proto, 'request')
.getter('isAjax')
.getter('acceptJSON');

/**
* @member {Array} Context#queries
* @see Request#queries
* @since 1.0.0
*/
delegate(proto, 'request').getter('queries');

delegate(proto, 'request')
.getter('isAjax')
.getter('acceptJSON')
.getter('queries')
.getter('accept');
11 changes: 11 additions & 0 deletions app/extend/request.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const querystring = require('querystring');
const accepts = require('accepts');
const _querycache = Symbol('_querycache');
const _queriesCache = Symbol('_queriesCache');
const PROTOCOL = Symbol('PROTOCOL');
const ACCEPTS = Symbol('ACCEPTS');
const RE_ARRAY_KEY = /[^\[\]]+\[\]$/;
const AJAX_EXT_RE = /\.(json|tile|ajax)$/i;

Expand Down Expand Up @@ -214,6 +216,15 @@ module.exports = {
return this._customQuery(_queriesCache, arrayValue);
},

get accept() {
let accept = this[ACCEPTS];
if (accept) {
return accept;
}
accept = this[ACCEPTS] = accepts(this.req);
return accept;
},

/**
* Set query-string as an object.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"egg-session": "^0.0.2",
"egg-static": "^0.1.0",
"egg-userrole": "^0.1.0",
"egg-userservice": "^0.1.0",
"egg-userservice": "^1.0.0",
"egg-validate": "^0.0.2",
"egg-watcher": "^1.0.0",
"graceful": "^1.0.1",
Expand Down
6 changes: 4 additions & 2 deletions test/lib/core/loader/load_plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

const should = require('should');
const path = require('path');
const fs = require('fs');
const mm = require('egg-mock');
const AppWorkerLoader = require('../../../../').AppWorkerLoader;
const utils = require('../../../utils');

const EGG_BASE = path.join(__dirname, '../../../../');

describe('test/lib/core/loader/load_plugin.test.js', () => {
describe('test/lib/core/loader.test.js', () => {
let app;
const logger = console;
before(() => {
app = utils.app('apps/empty');
return app.ready();
});
afterEach(mm.restore);

Expand Down Expand Up @@ -45,7 +47,7 @@ describe('test/lib/core/loader/load_plugin.test.js', () => {
env: [],
path: path.join(baseDir, 'plugins/e'),
});
appLoader.plugins.onerror.path.should.equal(path.join(EGG_BASE, 'node_modules/egg-onerror'));
appLoader.plugins.onerror.path.should.equal(fs.realpathSync(path.join(EGG_BASE, 'node_modules/egg-onerror')));
appLoader.plugins.onerror.package.should.equal('egg-onerror');
appLoader.plugins.onerror.version.should.match(/\d+\.\d+\.\d+/);
appLoader.orderPlugins.should.be.an.Array;
Expand Down

0 comments on commit 2b0dbd3

Please sign in to comment.