Skip to content

Commit fb122fc

Browse files
authored
🔀 Merge pull request #123 from leancloud/error
✨ Add `onError` and `ignoreInvalidSessionToken` option
2 parents dbd8906 + 5e8b2b8 commit fb122fc

8 files changed

Lines changed: 77 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# 更新日志
22

3+
## v3.1.0
4+
5+
- `AV.Express()` 添加 `onError`(全局云函数错误处理)和 `ignoreInvalidSessionToken`(忽略不正确的 Session Token 而不是返回错误)选项。
6+
- 修复改进 TypeScript 定义文件
7+
- 修复发出请求的 User Agent
8+
39
## v3.0.2
410

511
- 修复 Promise 写法的 beforeHook 无法正确工作的问题。

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
## 安装
44

55
```bash
6-
npm install leanengine --save
7-
npm install leancloud-storage@3 --save
6+
npm install --save leanengine leancloud-storage@3 --save
87
```
98

109
建议使用 Node.js 6.0 以上的版本(可在 `package.json` 中设置 `engines.node``6.x`)。
@@ -20,7 +19,7 @@ npm install leancloud-storage@3 --save
2019
## 项目示例
2120

2221
* 项目模板:<https://github.com/leancloud/node-js-getting-started>
23-
* Todo 列表示例<https://github.com/leancloud/leanengine-todo-demo>
22+
* Node.js Demo 汇总<https://github.com/leancloud/leanengine-nodejs-demos>
2423
* 微信公众平台:<https://leancloud.cn/docs/webhosting_weixin.html>
2524
* 微博 OAuth 授权验证:<https://leancloud.cn/docs/webhosting_oauth.html>
2625

leanengine.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ declare namespace Express {
2323
}
2424
}
2525

26-
2726
interface InitializeOptions {
2827
appId: string,
2928
appKey: string,
@@ -32,7 +31,9 @@ interface InitializeOptions {
3231

3332
interface MiddlewareOptions {
3433
timeout?: string,
35-
printFullStack?: boolean
34+
printFullStack?: boolean,
35+
onError?(err: Error),
36+
ignoreInvalidSessionToken?: boolean
3637
}
3738

3839
export function init(options: InitializeOptions): void;

lib/leanengine.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function createCloudFunctionRouter(options) {
9393
const hookName = urlParams[2];
9494

9595
if (!hookName) {
96-
return callCloudFunction(req, functionOrClass);
96+
return callCloudFunction(req, functionOrClass, options);
9797
} else if (functionOrClass === 'onVerified') {
9898
return callUserHook(req, 'onVerified', hookName);
9999
} else if (functionOrClass === '_User' && hookName === 'onLogin') {
@@ -131,6 +131,8 @@ function createCloudFunctionRouter(options) {
131131
error: err.message || err.responseText || err || 'unknown error'
132132
}));
133133
}
134+
135+
options.onError && options.onError(err);
134136
});
135137
});
136138

@@ -146,7 +148,7 @@ function createCloudFunctionRouter(options) {
146148
return cloudFunctions;
147149
}
148150

149-
function callCloudFunction(req, funcName) {
151+
function callCloudFunction(req, funcName, options) {
150152
const cloudFunction = Cloud.functions[funcName];
151153

152154
if (!cloudFunction) {
@@ -165,7 +167,11 @@ function callCloudFunction(req, funcName) {
165167

166168
return promiseTry( () => {
167169
if (cloudFunction.fetchUser !== false && req.AV.sessionToken && req.AV.sessionToken !== '') {
168-
return AV.User.become(req.AV.sessionToken);
170+
return AV.User.become(req.AV.sessionToken).catch( err => {
171+
if (!options.ignoreInvalidSessionToken) {
172+
throw err;
173+
}
174+
});
169175
}
170176
}).then( user => {
171177
const request = utils.prepareRequestObject({req, user, params});
@@ -245,8 +251,8 @@ function callClassHook(req, className, hookName) {
245251
});
246252
}
247253

248-
function callUserHook(req, hookName, verifType) {
249-
const userHookFunction = Cloud.functions[utils.hookNameMapping[hookName] + verifType];
254+
function callUserHook(req, hookName, verifyType) {
255+
const userHookFunction = Cloud.functions[utils.hookNameMapping[hookName] + verifyType];
250256

251257
checkHookKey(req);
252258

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "leanengine",
3-
"version": "3.0.2",
3+
"version": "3.1.0",
44
"description": "LeanCloud LeanEngine Node.js SDK.",
55
"repository": {
66
"type": "git",
@@ -18,8 +18,10 @@
1818
},
1919
"devDependencies": {
2020
"blanket": "^1.2.3",
21+
"es6-promise": "^4.2.4",
2122
"express": "^4.14.1",
2223
"mocha": "^3.2.0",
24+
"npm-run-all": "^4.1.2",
2325
"should": "^11.2.0",
2426
"supertest": "^3.0.0"
2527
},
@@ -48,9 +50,10 @@
4850
"email": "support@leancloud.rocks"
4951
},
5052
"scripts": {
51-
"test": "npm install leancloud-storage@3 && npm run test-express && npm run test-koa1 && npm run test-koa2",
53+
"test": "npm install --no-save leancloud-storage@3 && npm-run-all test-tsd test-express test-koa1 test-koa2",
5254
"test-express": "mocha test test/express",
53-
"test-koa1": "env FRAMEWORK=koa npm install koa@1 koa-bodyparser@2 && mocha test test/koa",
54-
"test-koa2": "env FRAMEWORK=koa npm install koa@2 koa-bodyparser@4 && mocha test test/koa test/koa2"
55+
"test-tsd": "tsc leanengine.d.ts",
56+
"test-koa1": "env FRAMEWORK=koa npm install --no-save leancloud-storage@3 koa@1 koa-bodyparser@2 && mocha test test/koa",
57+
"test-koa2": "env FRAMEWORK=koa npm install --no-save leancloud-storage@3 koa@2 koa-bodyparser@4 && mocha test test/koa test/koa2"
5558
}
5659
}

test/constructor-test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const request = require('supertest');
2+
const should = require('should');
3+
4+
require('./fixtures/functions');
5+
6+
const {appId, appKey} = require('./fixtures/app-info');
7+
8+
describe('constructor', function() {
9+
it('onError option', function(done) {
10+
const app = require('./fixtures/app')({
11+
onError: (err) => {
12+
err.message.should.be.equal('some message');
13+
err.status.should.be.equal(400);
14+
done();
15+
}
16+
});
17+
18+
request(app)
19+
.post('/1.1/functions/clientErrorPromise')
20+
.set('X-AVOSCloud-Application-Id', appId)
21+
.set('X-AVOSCloud-Application-Key', appKey)
22+
.expect(400, (err) => {
23+
if (err) {
24+
done(err);
25+
}
26+
});
27+
});
28+
29+
it('ignoreInvalidSessionToken option', function(done) {
30+
const app = require('./fixtures/app')({
31+
ignoreInvalidSessionToken: true
32+
});
33+
34+
request(app)
35+
.post('/1.1/functions/whoami')
36+
.set('X-AVOSCloud-Application-Id', appId)
37+
.set('X-AVOSCloud-Application-Key', appKey)
38+
.set('X-LC-Session', '00000000000000000000')
39+
.expect(200, done);
40+
});
41+
});

test/fixtures/app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const AV = require('../..');
22

3-
module.exports = function() {
3+
module.exports = function(options) {
44
if (process.env.FRAMEWORK == 'koa') {
55
const Koa = require('koa');
66
var app = new Koa();
7-
app.use(AV.koa());
7+
app.use(AV.koa(options));
88
return app.listen();
99
} else {
10-
return AV.express();
10+
return AV.express(options);
1111
}
1212
}

test/fixtures/functions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ AV.Cloud.define('testUser', function(request, response) {
123123
response.success('ok');
124124
});
125125

126+
AV.Cloud.define('whoami', function(request) {
127+
return request.currentUser;
128+
});
129+
126130
AV.Cloud.define('dontFetchUser', {fetchUser: false}, function(req, res) {
127131
should.not.exist(res.user);
128132
should.not.exist(res.currentUser);

0 commit comments

Comments
 (0)