Comparing version 3.0.0-beta2 to 3.0.0-beta3
@@ -9,3 +9,3 @@ const path = require('path'); | ||
const thinkLoader = require('./loader.js'); | ||
const ThinkLoader = require('./loader.js'); | ||
@@ -19,7 +19,7 @@ /** | ||
*/ | ||
constructor(options = {}){ | ||
constructor(options = {}) { | ||
assert(options.ROOT_PATH, 'options.ROOT_PATH must be set'); | ||
if(!options.APP_PATH){ | ||
if (!options.APP_PATH) { | ||
let appPath = path.join(options.ROOT_PATH, 'app'); | ||
if(!options.transpiler && !helper.isDirectory(appPath)){ | ||
if (!options.transpiler && !helper.isDirectory(appPath)) { | ||
appPath = path.join(options.ROOT_PATH, 'src'); | ||
@@ -34,6 +34,6 @@ } | ||
*/ | ||
notifier(err){ | ||
if(!this.options.notifier) return; | ||
notifier(err) { | ||
if (!this.options.notifier) return; | ||
let notifier = this.options.notifier; | ||
if(!helper.isArray(notifier)){ | ||
if (!helper.isArray(notifier)) { | ||
notifier = [notifier]; | ||
@@ -49,9 +49,9 @@ } | ||
*/ | ||
_watcherCallBack(fileInfo){ | ||
_watcherCallBack(fileInfo) { | ||
let transpiler = this.options.transpiler; | ||
if(transpiler){ | ||
if(!helper.isArray(transpiler)){ | ||
if (transpiler) { | ||
if (!helper.isArray(transpiler)) { | ||
transpiler = [transpiler]; | ||
} | ||
let ret = transpiler[0]({ | ||
const ret = transpiler[0]({ | ||
srcPath: fileInfo.path, | ||
@@ -62,3 +62,3 @@ outPath: this.options.APP_PATH, | ||
}); | ||
if(helper.isError(ret)){ | ||
if (helper.isError(ret)) { | ||
console.error(ret.stack); | ||
@@ -68,8 +68,8 @@ this.notifier(ret); | ||
} | ||
if(think.logger){ | ||
if (think.logger) { | ||
think.logger.info(`transpile file ${fileInfo.file} success`); | ||
} | ||
} | ||
//reload all workers | ||
if(this.masterInstance){ | ||
// reload all workers | ||
if (this.masterInstance) { | ||
this.masterInstance.forceReloadWorkers(); | ||
@@ -81,5 +81,6 @@ } | ||
*/ | ||
startWatcher(){ | ||
if(!this.options.watcher) return; | ||
const instance = new this.options.watcher({ | ||
startWatcher() { | ||
const Watcher = this.options.watcher; | ||
if (!Watcher) return; | ||
const instance = new Watcher({ | ||
srcPath: path.join(this.options.ROOT_PATH, 'src'), | ||
@@ -93,15 +94,15 @@ diffPath: this.options.APP_PATH | ||
*/ | ||
startServer(port, host){ | ||
startServer(port, host) { | ||
const callback = () => { | ||
think.app.emit('appReady'); | ||
} | ||
}; | ||
const createServer = () => { | ||
const createServerFn = think.config('createServer'); | ||
if(createServerFn){ | ||
if (createServerFn) { | ||
assert(helper.isFunction(createServerFn), 'config.createServer must be a function'); | ||
think.app.server = createServerFn(think.app, port, host, callback) | ||
}else{ | ||
think.app.server = createServerFn(think.app, port, host, callback); | ||
} else { | ||
think.app.server = think.app.listen(port, host, callback); | ||
} | ||
} | ||
}; | ||
return think.beforeStartServer().catch(err => { | ||
@@ -114,7 +115,7 @@ think.logger.error(err); | ||
*/ | ||
parseArgv(){ | ||
parseArgv() { | ||
const argv2 = process.argv[2]; | ||
const portRegExp = /^\d{2,5}$/; | ||
if(argv2){ | ||
if(!portRegExp.test(argv2)){ | ||
if (argv2) { | ||
if (!portRegExp.test(argv2)) { | ||
return {path: argv2}; | ||
@@ -129,3 +130,3 @@ } | ||
*/ | ||
runInMaster(){ | ||
runInMaster() { | ||
const instance = new thinkCluster.Master({ | ||
@@ -145,4 +146,4 @@ workers: think.config('workers'), | ||
*/ | ||
runInWorker(argv){ | ||
let port = argv.port || think.config('port'); | ||
runInWorker(argv) { | ||
const port = argv.port || think.config('port'); | ||
const host = think.config('host'); | ||
@@ -157,3 +158,3 @@ const instance = new thinkCluster.Worker({ | ||
think.app.once('appReady', () => { | ||
if(thinkCluster.isFirstWorker()){ | ||
if (thinkCluster.isFirstWorker()) { | ||
think.logger.info(`Server running at http://${host || '127.0.0.1'}:${port}`); | ||
@@ -163,3 +164,3 @@ think.logger.info(`ThinkJS version: ${think.version}`); | ||
think.logger.info(`Workers: ${instance.getWorkers()}`); | ||
//think.logger.info(`Agent Worker: ${instance.options.enableAgent}`); | ||
// think.logger.info(`Agent Worker: ${instance.options.enableAgent}`); | ||
} | ||
@@ -174,3 +175,3 @@ instance.options.server = think.app.server; | ||
*/ | ||
runInCli(cliPath){ | ||
runInCli(cliPath) { | ||
think.app.emit('appReady'); | ||
@@ -185,3 +186,3 @@ mockHttp({ | ||
*/ | ||
runInAgent(){ | ||
runInAgent() { | ||
const instance = new thinkCluster.Agent(); | ||
@@ -193,32 +194,31 @@ instance.createServer(); | ||
*/ | ||
run(){ | ||
if(pm2.isClusterMode){ | ||
run() { | ||
if (pm2.isClusterMode) { | ||
throw new Error('can not use pm2 cluster mode, please change exec_mode to fork'); | ||
} | ||
//start file watcher | ||
if(cluster.isMaster){ | ||
// start file watcher | ||
if (cluster.isMaster) { | ||
this.startWatcher(); | ||
} | ||
const instance = new thinkLoader(this.options); | ||
const instance = new ThinkLoader(this.options); | ||
const argv = this.parseArgv(); | ||
try{ | ||
if(argv.path){ | ||
try { | ||
if (argv.path) { | ||
think.isCli = true; | ||
instance.loadAll('worker'); | ||
return this.runInCli(argv.path); | ||
}else if(cluster.isMaster){ | ||
} else if (cluster.isMaster) { | ||
instance.loadAll('master'); | ||
this.runInMaster(); | ||
}else if(thinkCluster.isAgent()){ | ||
} else if (thinkCluster.isAgent()) { | ||
instance.loadAll('agent'); | ||
this.runInAgent(); | ||
}else{ | ||
} else { | ||
instance.loadAll('worker'); | ||
this.runInWorker(argv); | ||
} | ||
}catch(e){ | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
} | ||
}; |
@@ -18,4 +18,4 @@ const path = require('path'); | ||
filename: path.join(think.ROOT_PATH, 'logs/file.log'), | ||
maxLogSize: 50 * 1024, //50M | ||
backups: 10 //max chunk number | ||
maxLogSize: 50 * 1024, // 50M | ||
backups: 10 // max chunk number | ||
}, | ||
@@ -29,2 +29,2 @@ dateFile: { | ||
} | ||
} | ||
}; |
@@ -6,2 +6,2 @@ /** | ||
type: 'dateFile' | ||
} | ||
}; |
@@ -5,18 +5,18 @@ /** | ||
module.exports = { | ||
port: 8360, //server port | ||
host: '127.0.0.1', //server host | ||
workers: 0, //server workers num, if value is 0 then get cpus num | ||
createServer: undefined, //create server function | ||
startServerTimeout: 3000, //before start server time | ||
reloadSignal: 'SIGUSR2', //reload process signal | ||
onUnhandledRejection: err => think.logger.error(err), //unhandledRejection handle | ||
onUncaughtException: err => think.logger.error(err), //uncaughtException handle | ||
processKillTimeout: 10 * 1000, //process kill timeout, default is 10s | ||
enableAgent: false, //enable agent worker | ||
port: 8360, // server port | ||
host: '127.0.0.1', // server host | ||
workers: 0, // server workers num, if value is 0 then get cpus num | ||
createServer: undefined, // create server function | ||
startServerTimeout: 3000, // before start server time | ||
reloadSignal: 'SIGUSR2', // reload process signal | ||
onUnhandledRejection: err => think.logger.error(err), // unhandledRejection handle | ||
onUncaughtException: err => think.logger.error(err), // uncaughtException handle | ||
processKillTimeout: 10 * 1000, // process kill timeout, default is 10s | ||
enableAgent: false, // enable agent worker | ||
jsonpCallbackField: 'callback', // jsonp callback field | ||
jsonContentType: 'application/json', // json content type | ||
errnoField: 'errno', //errno field | ||
errmsgField: 'errmsg', //errmsg field | ||
errnoField: 'errno', // errno field | ||
errmsgField: 'errmsg', // errmsg field | ||
defaultErrno: 1000, // default errno | ||
validateDefaultErrno: 1001, //validate default errno | ||
} | ||
validateDefaultErrno: 1001 // validate default errno | ||
}; |
@@ -7,2 +7,2 @@ const cache = require('think-cache'); | ||
session | ||
]; | ||
]; |
const helper = require('think-helper'); | ||
const assert = require('assert'); | ||
const cookies = require('cookies'); | ||
const Cookies = require('cookies'); | ||
const url = require('url'); | ||
@@ -9,2 +9,3 @@ | ||
const FILE = Symbol('context-file'); | ||
const COOKIE_STORE = Symbol('cookie-store'); | ||
@@ -18,3 +19,3 @@ /** | ||
*/ | ||
get userAgent(){ | ||
get userAgent() { | ||
return this.header['user-agent']; | ||
@@ -25,3 +26,3 @@ }, | ||
*/ | ||
referer(onlyHost){ | ||
referer(onlyHost) { | ||
return this.referrer(onlyHost); | ||
@@ -32,5 +33,5 @@ }, | ||
*/ | ||
referrer(onlyHost){ | ||
let referrer = this.header['referer']; | ||
if(!referrer || !onlyHost) return referrer; | ||
referrer(onlyHost) { | ||
const referrer = this.header['referer']; | ||
if (!referrer || !onlyHost) return referrer; | ||
return url.parse(referrer).hostname; | ||
@@ -41,3 +42,3 @@ }, | ||
*/ | ||
get isGet(){ | ||
get isGet() { | ||
return this.method === 'GET'; | ||
@@ -48,3 +49,3 @@ }, | ||
*/ | ||
get isPost(){ | ||
get isPost() { | ||
return this.method === 'POST'; | ||
@@ -55,3 +56,3 @@ }, | ||
*/ | ||
isMethod(method){ | ||
isMethod(method) { | ||
return this.method === method.toUpperCase(); | ||
@@ -62,3 +63,3 @@ }, | ||
*/ | ||
get isCli(){ | ||
get isCli() { | ||
return think.isCli; | ||
@@ -69,4 +70,4 @@ }, | ||
*/ | ||
isAjax(method){ | ||
if(method && !this.isMethod(method)) return false; | ||
isAjax(method) { | ||
if (method && !this.isMethod(method)) return false; | ||
return this.header['x-requested-with'] === 'XMLHttpRequest'; | ||
@@ -77,3 +78,3 @@ }, | ||
*/ | ||
isJsonp(callbackField = this.config('jsonpCallbackField')){ | ||
isJsonp(callbackField = this.config('jsonpCallbackField')) { | ||
return !!this.param(callbackField); | ||
@@ -84,7 +85,7 @@ }, | ||
*/ | ||
jsonp(data, callbackField = this.config('jsonpCallbackField')){ | ||
jsonp(data, callbackField = this.config('jsonpCallbackField')) { | ||
let field = this.param(callbackField); | ||
//remove unsafe chars | ||
field = (field || '').replace(/[^\w\.]/g, ''); | ||
if(field){ | ||
// remove unsafe chars | ||
field = (field || '').replace(/[^\w.]/g, ''); | ||
if (field) { | ||
data = `${field}(${JSON.stringify(data)})`; | ||
@@ -99,3 +100,3 @@ } | ||
*/ | ||
json(data){ | ||
json(data) { | ||
this.type = this.config('jsonContentType'); | ||
@@ -108,3 +109,3 @@ this.body = data; | ||
*/ | ||
success(data = '', message = ''){ | ||
success(data = '', message = '') { | ||
const obj = { | ||
@@ -122,11 +123,11 @@ [this.config('errnoField')]: 0, | ||
*/ | ||
fail(errno, errmsg = '', data = ''){ | ||
fail(errno, errmsg = '', data = '') { | ||
let obj; | ||
if(helper.isObject(errno)){ | ||
if (helper.isObject(errno)) { | ||
obj = errno; | ||
}else{ | ||
if(/^[A-Z\_]+$/.test(errno)){ | ||
} else { | ||
if (/^[A-Z_]+$/.test(errno)) { | ||
const messages = think.app.validators.messages || {}; | ||
const msg = messages[errno]; | ||
if(think.isArray(msg)){ | ||
if (think.isArray(msg)) { | ||
[errno, errmsg] = msg; | ||
@@ -136,3 +137,3 @@ } | ||
if (!think.isNumber(errno)) { | ||
[data, errmsg, errno] = [errmsg, errno, this.config('defaultErrno')] | ||
[data, errmsg, errno] = [errmsg, errno, this.config('defaultErrno')]; | ||
} | ||
@@ -143,3 +144,3 @@ obj = { | ||
}; | ||
if(data){ | ||
if (data) { | ||
obj.data = data; | ||
@@ -155,3 +156,3 @@ } | ||
*/ | ||
expires(time){ | ||
expires(time) { | ||
time = helper.ms(time); | ||
@@ -178,7 +179,7 @@ const date = new Date(Date.now() + time); | ||
param(name, value) { | ||
if(!this[PARAM]){ | ||
if (!this[PARAM]) { | ||
this[PARAM] = Object.assign({}, this.query); | ||
} | ||
if(!name) return this[PARAM]; | ||
if(helper.isObject(name)){ | ||
if (!name) return this[PARAM]; | ||
if (helper.isObject(name)) { | ||
this[PARAM] = Object.assign(this[PARAM], name); | ||
@@ -188,6 +189,6 @@ return this; | ||
assert(name && helper.isString(name), 'param.name must be a string'); | ||
if(value === undefined){ | ||
if (value === undefined) { | ||
// this.param('a,b') | ||
if(name.indexOf(',') > -1){ | ||
let value = {}; | ||
if (name.indexOf(',') > -1) { | ||
const value = {}; | ||
name.split(',').forEach(item => { | ||
@@ -208,15 +209,15 @@ value[item] = this[PARAM][item]; | ||
*/ | ||
post(name, value){ | ||
if(!this[POST]){ | ||
post(name, value) { | ||
if (!this[POST]) { | ||
this[POST] = Object.assign({}, this.request.body && this.request.body.post); | ||
} | ||
if(!name) return this[POST]; | ||
if(helper.isObject(name)){ | ||
if (!name) return this[POST]; | ||
if (helper.isObject(name)) { | ||
this[POST] = Object.assign(this[POST], name); | ||
return this; | ||
} | ||
if(value === undefined){ | ||
if (value === undefined) { | ||
// this.param('a,b') | ||
if(name.indexOf(',') > -1){ | ||
let value = {}; | ||
if (name.indexOf(',') > -1) { | ||
const value = {}; | ||
name.split(',').forEach(item => { | ||
@@ -237,12 +238,12 @@ value[item] = this[POST][item]; | ||
*/ | ||
file(name, value){ | ||
if(!this[FILE]){ | ||
file(name, value) { | ||
if (!this[FILE]) { | ||
this[FILE] = Object.assign({}, this.request.body && this.request.body.file); | ||
} | ||
if(!name) return this[FILE]; | ||
if(helper.isObject(name)){ | ||
if (!name) return this[FILE]; | ||
if (helper.isObject(name)) { | ||
this[FILE] = Object.assign(this[FILE], name); | ||
return this; | ||
} | ||
if(value === undefined){ | ||
if (value === undefined) { | ||
return this[FILE][name]; | ||
@@ -259,25 +260,30 @@ } | ||
*/ | ||
cookie(name, value, options = {}){ | ||
cookie(name, value, options = {}) { | ||
assert(name && helper.isString(name), 'cookie.name must be a string'); | ||
options = Object.assign({}, this.config('cookie'), options); | ||
const instance = new cookies(this.req, this.res, { | ||
const instance = new Cookies(this.req, this.res, { | ||
keys: options.keys, | ||
secure: this.req.secure | ||
}); | ||
//get cookie | ||
if(value === undefined){ | ||
if (!this[COOKIE_STORE]) this[COOKIE_STORE] = {}; | ||
// get cookie | ||
if (value === undefined) { | ||
if (this[COOKIE_STORE][name] !== undefined) return this[COOKIE_STORE][name]; | ||
return instance.get(name, options); | ||
} | ||
//remove cookie | ||
if(value === null){ | ||
return instance.set(name, '', { | ||
maxAge: -1 | ||
}); | ||
// remove cookie | ||
if (value === null) { | ||
delete this[COOKIE_STORE][name]; | ||
options.maxAge = -1; | ||
return instance.set(name, '', options); | ||
} | ||
assert(helper.isString(value), 'cookie value must be a string'); | ||
//http://browsercookielimits.squawky.net/ | ||
if(value.length >= 4094){ | ||
// http://browsercookielimits.squawky.net/ | ||
if (value.length >= 4094) { | ||
this.app.emit('cookieLimit', {name, value, ctx: this}); | ||
} | ||
//set cookie | ||
this[COOKIE_STORE][name] = value; | ||
// set cookie | ||
return instance.set(name, value, options); | ||
@@ -290,3 +296,3 @@ }, | ||
*/ | ||
controller(name, m){ | ||
controller(name, m) { | ||
return think.controller(name, this, m); | ||
@@ -299,5 +305,5 @@ }, | ||
*/ | ||
service(name, m){ | ||
service(name, m) { | ||
return think.service(name, m); | ||
} | ||
} | ||
}; |
const helper = require('think-helper'); | ||
const debug = require('debug')('thinkjs'); | ||
const deprecate = require('depd')('thinkjs'); | ||
/** | ||
@@ -12,3 +10,3 @@ * extend controller | ||
*/ | ||
get body(){ | ||
get body() { | ||
return this.ctx.body; | ||
@@ -19,3 +17,3 @@ }, | ||
*/ | ||
set body(value){ | ||
set body(value) { | ||
this.ctx.body = value; | ||
@@ -26,3 +24,3 @@ }, | ||
*/ | ||
get ip(){ | ||
get ip() { | ||
return this.ctx.ip; | ||
@@ -33,3 +31,3 @@ }, | ||
*/ | ||
get ips(){ | ||
get ips() { | ||
return this.ctx.ips; | ||
@@ -40,3 +38,3 @@ }, | ||
*/ | ||
get status(){ | ||
get status() { | ||
return this.ctx.status; | ||
@@ -47,3 +45,3 @@ }, | ||
*/ | ||
set status(status){ | ||
set status(status) { | ||
this.ctx.status = status; | ||
@@ -54,3 +52,3 @@ }, | ||
*/ | ||
get type(){ | ||
get type() { | ||
return this.ctx.type; | ||
@@ -61,3 +59,3 @@ }, | ||
*/ | ||
set type(contentType){ | ||
set type(contentType) { | ||
this.ctx.type = contentType; | ||
@@ -71,3 +69,3 @@ }, | ||
*/ | ||
config(name, value, m = this.ctx.module){ | ||
config(name, value, m = this.ctx.module) { | ||
return think.config(name, value, m); | ||
@@ -78,3 +76,3 @@ }, | ||
*/ | ||
get method(){ | ||
get method() { | ||
return this.ctx.method; | ||
@@ -86,3 +84,3 @@ }, | ||
*/ | ||
isMethod(method){ | ||
isMethod(method) { | ||
return this.ctx.isMethod(method); | ||
@@ -93,3 +91,3 @@ }, | ||
*/ | ||
get isGet(){ | ||
get isGet() { | ||
return this.ctx.isGet; | ||
@@ -100,3 +98,3 @@ }, | ||
*/ | ||
get isPost(){ | ||
get isPost() { | ||
return this.ctx.isPost; | ||
@@ -107,3 +105,3 @@ }, | ||
*/ | ||
get isCli(){ | ||
get isCli() { | ||
return think.isCli; | ||
@@ -115,3 +113,3 @@ }, | ||
*/ | ||
isAjax(method){ | ||
isAjax(method) { | ||
return this.ctx.isAjax(method); | ||
@@ -123,3 +121,3 @@ }, | ||
*/ | ||
isJsonp(callbackField){ | ||
isJsonp(callbackField) { | ||
return this.ctx.isJsonp(callbackField); | ||
@@ -130,3 +128,3 @@ }, | ||
*/ | ||
jsonp(data, callbackField){ | ||
jsonp(data, callbackField) { | ||
return this.ctx.jsonp(data, callbackField); | ||
@@ -137,3 +135,3 @@ }, | ||
*/ | ||
json(data){ | ||
json(data) { | ||
return this.ctx.json(data); | ||
@@ -144,3 +142,3 @@ }, | ||
*/ | ||
success(data, message){ | ||
success(data, message) { | ||
return this.ctx.success(data, message); | ||
@@ -151,3 +149,3 @@ }, | ||
*/ | ||
fail(errno, errmsg, data){ | ||
fail(errno, errmsg, data) { | ||
return this.ctx.fail(errno, errmsg, data); | ||
@@ -159,3 +157,3 @@ }, | ||
*/ | ||
expires(time){ | ||
expires(time) { | ||
return this.ctx.expires(time); | ||
@@ -168,3 +166,3 @@ }, | ||
*/ | ||
get(name, value){ | ||
get(name, value) { | ||
return this.ctx.param(name, value); | ||
@@ -177,3 +175,3 @@ }, | ||
*/ | ||
query(name, value){ | ||
query(name, value) { | ||
return this.ctx.param(name, value); | ||
@@ -186,3 +184,3 @@ }, | ||
*/ | ||
post(name, value){ | ||
post(name, value) { | ||
return this.ctx.post(name, value); | ||
@@ -195,3 +193,3 @@ }, | ||
*/ | ||
file(name, value){ | ||
file(name, value) { | ||
return this.ctx.file(name, value); | ||
@@ -205,3 +203,3 @@ }, | ||
*/ | ||
cookie(name, value, options){ | ||
cookie(name, value, options) { | ||
return this.ctx.cookie(name, value, options); | ||
@@ -214,14 +212,14 @@ }, | ||
*/ | ||
header(name, value){ | ||
if(value === undefined && helper.isString(name)){ | ||
header(name, value) { | ||
if (value === undefined && helper.isString(name)) { | ||
return this.ctx.header[name]; | ||
} | ||
if(this.ctx.res.headersSent){ | ||
if (this.ctx.res.headersSent) { | ||
debug(`headers has already sent, url: ${this.ctx.url}`); | ||
return; | ||
} | ||
if(value !== undefined){ | ||
if (value !== undefined) { | ||
return this.ctx.set(name, value); | ||
} | ||
if(helper.isObject(name)){ | ||
if (helper.isObject(name)) { | ||
return this.ctx.set(name); | ||
@@ -233,3 +231,3 @@ } | ||
*/ | ||
get userAgent(){ | ||
get userAgent() { | ||
return this.ctx.userAgent; | ||
@@ -240,3 +238,3 @@ }, | ||
*/ | ||
referrer(onlyHost){ | ||
referrer(onlyHost) { | ||
return this.ctx.referer(onlyHost); | ||
@@ -247,3 +245,3 @@ }, | ||
*/ | ||
referer(onlyHost){ | ||
referer(onlyHost) { | ||
return this.ctx.referer(onlyHost); | ||
@@ -256,3 +254,3 @@ }, | ||
*/ | ||
redirect(url, alt){ | ||
redirect(url, alt) { | ||
return this.ctx.redirect(url, alt); | ||
@@ -265,3 +263,3 @@ }, | ||
*/ | ||
controller(name, m){ | ||
controller(name, m) { | ||
return think.controller(name, this.ctx, m); | ||
@@ -274,5 +272,34 @@ }, | ||
*/ | ||
service(name, m){ | ||
service(name, m) { | ||
return think.service(name, m); | ||
}, | ||
/** | ||
* execute action | ||
* @param {String} controller | ||
* @param {String} actionName | ||
* @param {String} m | ||
*/ | ||
action(controller, actionName, m) { | ||
const instance = this.controller(controller, m); | ||
let promise = Promise.resolve(); | ||
if (instance.__before) { | ||
promise = Promise.resolve(instance.__before()); | ||
} | ||
return promise.then(data => { | ||
if (data === false) return false; | ||
let method = `${actionName}Action`; | ||
if (!instance[method]) { | ||
method = '__call'; | ||
} | ||
if (instance[method]) { | ||
return instance[method](); | ||
} | ||
}).then(data => { | ||
if (data === false) return false; | ||
if (instance.__after) { | ||
return instance.__after(); | ||
} | ||
return data; | ||
}); | ||
} | ||
} | ||
}; |
@@ -12,10 +12,10 @@ const Validator = require('think-validator'); | ||
*/ | ||
validate(rules, msgs){ | ||
if(helper.isEmpty(rules)) return; | ||
validate(rules, msgs) { | ||
if (helper.isEmpty(rules)) return; | ||
this[VALIDATE_INVOKED] = true; | ||
//add user defined rules | ||
if(!validatorsRuleAdd){ | ||
// add user defined rules | ||
if (!validatorsRuleAdd) { | ||
validatorsRuleAdd = true; | ||
let rules = think.app.validators.rules || {}; | ||
for(let key in rules){ | ||
const rules = think.app.validators.rules || {}; | ||
for (const key in rules) { | ||
Validator.addRule(key, rules[key]); | ||
@@ -27,3 +27,3 @@ } | ||
const ret = instance.validate(rules, msgs); | ||
if(!helper.isEmpty(ret)){ | ||
if (!helper.isEmpty(ret)) { | ||
this.validateErrors = ret; | ||
@@ -37,11 +37,11 @@ return false; | ||
*/ | ||
__after(){ | ||
//check request method is allowed | ||
__after() { | ||
// check request method is allowed | ||
let allowMethods = this.allowMethods; | ||
if(!helper.isEmpty(allowMethods)){ | ||
if(helper.isString(allowMethods)) { | ||
if (!helper.isEmpty(allowMethods)) { | ||
if (helper.isString(allowMethods)) { | ||
allowMethods = allowMethods.split(','); | ||
} | ||
const method = this.method; | ||
if(allowMethods.indexOf(method) === -1){ | ||
if (allowMethods.indexOf(method) === -1) { | ||
this.fail(this.config('validateDefaultErrno'), 'METHOD_NOT_ALLOWED'); | ||
@@ -51,6 +51,6 @@ return false; | ||
} | ||
//check rules | ||
if(!helper.isEmpty(this.rules) && !this[VALIDATE_INVOKED]){ | ||
// check rules | ||
if (!helper.isEmpty(this.rules) && !this[VALIDATE_INVOKED]) { | ||
const flag = this.validate(this.rules); | ||
if(!flag){ | ||
if (!flag) { | ||
this.fail(this.config('validateDefaultErrno'), this.validateErrors); | ||
@@ -61,2 +61,2 @@ return false; | ||
} | ||
} | ||
}; |
@@ -17,3 +17,3 @@ const getConfigFn = require('think-config').getConfigFn; | ||
const thinkLoader = class { | ||
constructor(options = {}){ | ||
constructor(options = {}) { | ||
this.options = options; | ||
@@ -24,11 +24,11 @@ } | ||
*/ | ||
initPath(){ | ||
initPath() { | ||
think.ROOT_PATH = this.options.ROOT_PATH; | ||
think.APP_PATH = this.options.APP_PATH; | ||
//set env | ||
if(this.options.env){ | ||
// set env | ||
if (this.options.env) { | ||
think.app.env = this.options.env; | ||
} | ||
//set proxy | ||
if(this.options.proxy){ | ||
// set proxy | ||
if (this.options.proxy) { | ||
think.app.proxy = this.options.proxy; | ||
@@ -40,4 +40,4 @@ } | ||
*/ | ||
loadData(){ | ||
//add data to koa application | ||
loadData() { | ||
// add data to koa application | ||
think.app.modules = think.loader.modules; | ||
@@ -54,3 +54,3 @@ think.app.controllers = think.loader.loadController(); | ||
*/ | ||
loadMiddleware(){ | ||
loadMiddleware() { | ||
const middlewares = think.loader.loadMiddleware(think.app); | ||
@@ -64,4 +64,4 @@ middlewares.forEach(middleware => { | ||
*/ | ||
loadExtend(){ | ||
let exts = think.loader.loadExtend(); | ||
loadExtend() { | ||
const exts = think.loader.loadExtend(); | ||
const list = [ | ||
@@ -77,3 +77,3 @@ ['think', think], | ||
list.forEach(item => { | ||
if(!exts[item[0]]) return; | ||
if (!exts[item[0]]) return; | ||
Loader.extend(item[1], exts[item[0]]); | ||
@@ -85,3 +85,3 @@ }); | ||
*/ | ||
loadCrontab(){ | ||
loadCrontab() { | ||
const crontab = think.loader.loadCrontab(); | ||
@@ -94,3 +94,3 @@ const instance = new Crontab(crontab, think.app); | ||
*/ | ||
loadAll(type){ | ||
loadAll(type) { | ||
this.initPath(); | ||
@@ -101,7 +101,7 @@ think.loader = new Loader(think.APP_PATH, thinkPath); | ||
if(type !== 'master'){ | ||
if (type !== 'master') { | ||
this.loadExtend(); | ||
this.loadData(); | ||
this.loadMiddleware(); | ||
if(!think.isCli){ | ||
if (!think.isCli) { | ||
this.loadCrontab(); | ||
@@ -112,4 +112,4 @@ } | ||
} | ||
} | ||
}; | ||
module.exports = thinkLoader; |
@@ -1,1 +0,1 @@ | ||
module.exports = require('think-controller'); | ||
module.exports = require('think-controller'); |
@@ -1,1 +0,1 @@ | ||
module.exports = require('think-logic'); | ||
module.exports = require('think-logic'); |
@@ -1,1 +0,1 @@ | ||
module.exports = require('think-meta'); | ||
module.exports = require('think-meta'); |
@@ -1,1 +0,1 @@ | ||
module.exports = require('think-payload'); | ||
module.exports = require('think-payload'); |
@@ -1,1 +0,1 @@ | ||
module.exports = require('think-resource'); | ||
module.exports = require('think-resource'); |
@@ -1,1 +0,1 @@ | ||
module.exports = require('think-router'); | ||
module.exports = require('think-router'); |
@@ -1,1 +0,1 @@ | ||
module.exports = require('think-trace'); | ||
module.exports = require('think-trace'); |
@@ -54,6 +54,6 @@ const Koa = require('koa'); | ||
think.Controller = class Controller { | ||
constructor(ctx){ | ||
constructor(ctx) { | ||
this.ctx = ctx; | ||
} | ||
} | ||
}; | ||
@@ -65,18 +65,17 @@ /** | ||
const getClass = function(type, name, m){ | ||
let mcls = think.app[type]; | ||
const getClass = function(type, name, m) { | ||
const mcls = think.app[type]; | ||
let cls = null; | ||
if(think.app.modules.length){ | ||
if(mcls[m]){ | ||
if (think.app.modules.length) { | ||
if (mcls[m]) { | ||
cls = mcls[m][name]; | ||
} | ||
if(!cls && m !== 'common' && mcls.common){ | ||
if (!cls && m !== 'common' && mcls.common) { | ||
cls = mcls.common[name]; | ||
} | ||
}else{ | ||
} else { | ||
cls = mcls[name]; | ||
} | ||
return cls; | ||
} | ||
}; | ||
/** | ||
@@ -89,6 +88,7 @@ * get controller instance | ||
think.controller = (name, ctx, m = 'common') => { | ||
const cls = getClass('controllers', name, m); | ||
assert(cls, `can not find controller:${name}`); | ||
return new cls(ctx); | ||
} | ||
const Cls = getClass('controllers', name, m); | ||
assert(Cls, `can not find controller:${name}`); | ||
return new Cls(ctx); | ||
}; | ||
/** | ||
@@ -99,8 +99,8 @@ * get service | ||
return getClass('services', name, m); | ||
} | ||
}; | ||
// before start server | ||
let promises = []; | ||
const promises = []; | ||
think.beforeStartServer = fn => { | ||
if(fn) { | ||
if (fn) { | ||
assert(helper.isFunction(fn), 'fn in think.beforeStartServer must be a function'); | ||
@@ -116,2 +116,2 @@ return promises.push(fn()); | ||
return Promise.race([promise, timeoutPromise]); | ||
} | ||
}; |
{ | ||
"name": "thinkjs", | ||
"description": "ThinkJS - Use full ES6/7 features to develop web applications, Support TypeScript", | ||
"version": "3.0.0-beta2", | ||
"version": "3.0.0-beta3", | ||
"author": { | ||
@@ -9,2 +9,5 @@ "name": "welefen", | ||
}, | ||
"pre-commit": [ | ||
"test" | ||
], | ||
"publishConfig": { | ||
@@ -14,2 +17,4 @@ "tag": "next" | ||
"scripts": { | ||
"lint": "eslint lib/", | ||
"lint-fix": "eslint --fix lib/", | ||
"test": "eslint lib/ && nyc ava test/case", | ||
@@ -45,3 +50,2 @@ "coverage": "nyc report --reporter=html" | ||
"debug": "^2.6.1", | ||
"depd": "^1.1.0", | ||
"koa": "^2.2.0", | ||
@@ -69,8 +73,8 @@ "think-cache": "^1.0.0", | ||
"ava": "^0.18.0", | ||
"babel-core": "^6.22.1", | ||
"babel-eslint": "^7.1.1", | ||
"eslint": "2.8.0", | ||
"eslint": "^4.2.0", | ||
"eslint-config-think": "^1.0.0", | ||
"mkdirp": "^0.5.1", | ||
"mock-require": "^2.0.1", | ||
"nyc": "^7.0.0" | ||
"nyc": "^7.0.0", | ||
"pre-commit": "^1.2.2" | ||
}, | ||
@@ -77,0 +81,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
192982
22
28
1081
- Removeddepd@^1.1.0