Skip to content

Commit 267c899

Browse files
authored
🔀 Merge pull request #102 from leancloud/next
Node SDK 2.0
2 parents 742a4ee + f8b4bd0 commit 267c899

32 files changed

Lines changed: 1278 additions & 1467 deletions

.jshintrc

Lines changed: 0 additions & 14 deletions
This file was deleted.

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
language: node_js
22

33
node_js:
4-
- 0.12
54
- 4
65
- 6
6+
- node
77

88
script:
99
- npm test
10-
- node -v | grep 'v0.12' || npm run test-koa
10+
- npm run test-koa

API.md

Lines changed: 38 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ AV.Cloud.define(name: string, func: function)
4949
AV.Cloud.define(name: string, options: object, func: function)
5050
```
5151

52-
定义云函数有两种签名,其中 options 是一个可选的参数,`func` 的签名:`function(request: Request, response: Response)`
52+
定义云函数有两种签名,其中 `options` 是一个可选的参数,`func` 是接受一个 Request 对象作为参数,返回 Promise 的函数,Promise 的值即为云函数的响应。在 Promise 中可以抛出使用 `AV.Cloud.Error` 构造的异常表示客户端错误,如参数不合法;如果抛出其他类型的异常则视作服务器端错误,会打印错误到标准输出
5353

5454
`options` 的属性包括:
5555

@@ -63,10 +63,18 @@ AV.Cloud.define(name: string, options: object, func: function)
6363
* `meta: {remoteAddress}``meta.remoteAddress` 是客户端的 IP.
6464
* `sessionToken?: string`:客户端发来的 sessionToken(`X-LC-Session` 头)。
6565
66-
`Response` 上的属性包括:
66+
1.x 兼容模式:在早期版本中,云函数和 before 类的 Hook 是接受两个参数(`request``response`)的,**我们会继续兼容这种用法到下一个大版本,希望开发者尽快迁移到 Promise 风格的云函数上**。
6767
68-
* `success: function(result?)`:向客户端发送结果,可以是包括 AV.Object 在内的各种数据类型或数组,客户端解析方式见各 SDK 文档。
69-
* `error: function(err?: string)`:向客户端返回一个错误。
68+
### AV.Cloud.Error
69+
70+
```javascript
71+
new AV.Cloud.Error(message: string, options?)
72+
```
73+
74+
继承自 `Error`,用于在云函数和 Class Hook 中表示客户端错误,其中第二个参数支持:
75+
76+
- `status?: number`:设置 HTTP 响应代码(默认 400)
77+
- `code?: number`:设置响应正文中的错误代码(默认 1)
7078
7179
### AV.Cloud.run
7280
@@ -98,23 +106,14 @@ AV.Cloud.run(name: string, data: object, options?: object): Promise
98106
* AV.Cloud.beforeDelete
99107
* AV.Cloud.afterDelete
100108
101-
这些函数的签名:`function(className: string, func: function)`。
102-
103-
before 类 Hook 的 `func` 签名:`function(request: Request, response: Response)`,before 类 Hook 需要在执行完成后调用 `response.success` 或 `response.error` 接受或拒绝这次操作。
104-
105-
after 类 Hook 的 `func` 签名:`function(request: Request)`。
109+
这些函数的签名:`function(className: string, func: function)`,其中 `func` 是接受一个 Request 对象作为参数,返回 Promise 的函数。在 before 类 Hook 中如果没有抛出异常则视作接受这次操作。如果抛出使用 `AV.Cloud.Error` 构造的异常表示客户端错误,拒绝本次操作;如果抛出其他类型的异常则视作服务器端错误,返回 500 响应并打印错误到标准输出,也会拒绝本次操作。
106110

107111
`Request` 上的属性包括:
108112

109113
* `object: AV.Object`:被操作的对象。
110114
* `currentUser?: AV.User`:发起操作的用户。
111115
* `user?: AV.User`:同 `currentUser`.
112116

113-
`Response` 上的属性包括:
114-
115-
* `success: function()`:允许这个操作,请在 15 秒内调用 `success`, 否则会认为操作被拒绝。
116-
* `error: function(err: string)`:向客户端返回一个错误并拒绝这个操作。
117-
118117
LeanEngine 中间件会为这些 Hook 函数检查「Hook 签名」,确保调用者的确是 LeanCloud 或本地调试时的命令行工具。
119118

120119
更多有关 Hook 函数的内容请参考文档 [云函数开发指南:Hook 函数](https://leancloud.cn/docs/leanengine_cloudfunction_guide-node.html#Hook_函数)。
@@ -124,83 +123,53 @@ LeanEngine 中间件会为这些 Hook 函数检查「Hook 签名」,确保调
124123
* AV.Cloud.onVerified
125124
* AV.Cloud.onLogin
126125

127-
这两个函数的签名:`function(func: function)`,`func` 签名:`function(request: Request, response: Response)`,Hook 需要在执行完成后调用 `response.success` 或 `response.error` 接受或拒绝这次操作
126+
这两个函数的签名:`function(func: function)`,其中 `func` 是接受一个 Request 对象作为参数,返回 Promise 的函数,如果没有抛出异常则视作接受这次操作
128127

129128
`Request` 上的属性包括:
130129

131130
* `currentUser: AV.User`:被操作的用户。
132131
* `user: AV.User`:同 `currentUser`.
133132

134-
`Response` 上的属性包括:
135-
136-
* `success: function()`:允许这个操作,请在 15 秒内调用 `success`, 否则会认为操作被拒绝。
137-
* `error: function(err: string)`:向客户端返回一个错误并拒绝这个操作。
138-
139-
更多有关 Hook 函数的内容请参考文档 [云函数开发指南:Hook 函数](https://leancloud.cn/docs/leanengine_cloudfunction_guide-node.html#Hook_函数)。
140-
141133
### 实时通信 Hook 函数
142134

143135
包括:
144136

145-
* `_messageReceived`
146-
* `_receiversOffline`
147-
* `_messageSent`
148-
* `_conversationStart`
149-
* `_conversationAdd`
150-
* `_conversationRemove`
151-
* `_conversationUpdate`
137+
* `onIMMessageReceived`
138+
* `onIMReceiversOffline`
139+
* `onIMMessageSent`
140+
* `onIMConversationStart`
141+
* `onIMConversationStarted`
142+
* `onIMConversationAdd`
143+
* `onIMConversationRemove`
144+
* `onIMConversationUpdate`
152145

153146
LeanEngine 中间件会为这些 Hook 函数检查「Hook 签名」,确保调用者的确是 LeanCloud 或本地调试时的命令行工具。
154147

155-
这些 Hook 需要用 `AV.Cloud.define` 来定义,详见文档 [实时通信概览:云引擎 Hook](https://leancloud.cn/docs/realtime_v2.html#云引擎_Hook)
148+
这些 Hook 函数签名是 `function(func: function)`,其中 `func` 是接受一个 Request 对象作为参数,返回 Promise 的函数,详见文档 [实时通信概览:云引擎 Hook](https://leancloud.cn/docs/realtime_v2.html#云引擎_Hook)
149+
150+
## Middlewares
156151

157-
### AV.Cloud.httpRequest
152+
### leancloud-headers
158153

159-
注意:该 API 已不再维护且可能在之后的版本中去除,请使用 [request](https://www.npmjs.com/package/request) 发起 HTTP 请求。
154+
该中间件会将 `X-LC` 系列的头解析为 request.AV 上的属性,在 Express 中:
160155

161156
```javascript
162-
AV.Cloud.httpRequest(options: object);
157+
app.use(AV.Cloud.LeanCloudHeaders());
163158
```
164159

165-
options 的属性包括:
166-
167-
* `url`:被请求的 Url, 例如 `https://api.leancloud.cn/1.1/ping`。
168-
* `success: function(response: Response)`:成功回调,接受一个 HTTP 响应作为参数。
169-
* `error: function(response: Response)`:失败回调,接受一个 HTTP 响应作为参数。
170-
* `method: string`:HTTP 方法,默认为 `GET`。
171-
* `params`:Query String,可以是对象 `{q : 'Sean Plott'}` 也可以是字符串 `q=Sean Plott`
172-
* `headers: object`:HTTP 头,例如 `{'Content-Type': 'application/json'}`
173-
* `body: object`:HTTP 请求正文,默认使用 urlencode 编码,如果指定了 `Content-Type``application/json` 则发送 JSON 格式的正文;不适用于 `GET``HEAD` 请求。
174-
* `timeout: number`:超时时间,单位秒,默认 `10000`
175-
176-
Response 的属性包括:
177-
178-
* `status: number`:HTTP 响应状态码。
179-
* `headers: object`:HTTP 响应头。
180-
* `text: string`:HTTP 响应正文。
181-
* `buffer: Buffer`:HTTP 响应正文。
182-
* `data` 解析后的 HTTP 响应正文,例如对于 `Content-Type``application/json` 时,会将响应正文解析为一个对象。
183-
184-
示例:
160+
在 Koa 中(添加 `framework: 'koa'` 参数):
185161

186162
```javascript
187-
AV.Cloud.httpRequest({
188-
method: 'POST',
189-
url: 'http://www.example.com/create_post',
190-
body: {
191-
title: 'Vote for Pedro',
192-
body: 'If you vote for Pedro, your wildest dreams will come true'
193-
},
194-
success: function(httpResponse) {
195-
console.log(httpResponse.text);
196-
},
197-
error: function(httpResponse) {
198-
console.error('Request failed with response code ' + httpResponse.status);
199-
}
200-
});
163+
app.use(AV.Cloud.LeanCloudHeaders({framework: 'koa'}));
201164
```
202165

203-
## Middlewares
166+
express 的 `Request`(或 koa 的 `ctx.request`)上会有这些属性可用:
167+
168+
* `AV.id?`:App ID
169+
* `AV.key?`:App Key
170+
* `AV.masterKey?`:App Master Key
171+
* `AV.prod`:`0` 或 `1`
172+
* `AV.sessionToken?`:Session Token
204173

205174
### cookie-session
206175

@@ -216,9 +185,8 @@ app.use(AV.Cloud.CookieSession({secret: 'my secret', maxAge: 3600000, fetchUser:
216185
app.use(AV.Cloud.CookieSession({framework: 'koa', secret: 'my secret', maxAge: 3600000, fetchUser: true}));
217186
```
218187
219-
参数包括
188+
其他参数包括
220189
221-
* `koa?: boolean`:返回一个 koa(而不是 express)中间件。
222190
* `secret: string`:对 Cookie 进行签名的密钥,请选用一个随机字符串。
223191
* `name?: string`:Cookie 名称,默认为 `avos.sess`
224192
* `maxAge?: number`:Cookie 过期时间。

CHANGELOG.md

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

3+
## v2.0.0
4+
5+
- **不兼容改动** 升级 JavaScript SDK 到 [leancloud-storage 2.1.0](https://github.com/leancloud/javascript-sdk/releases/tag/v2.0.0)(请检查 JS SDK 2.0 引入的不兼容改动)。
6+
- **不兼容改动** 仅兼容至 Node.js 4.4 以上版本。
7+
- **不兼容改动** 重复定义云函数或 Hook 时会抛出一个异常。
8+
- **不兼容改动** 移除 `AV.Cloud.httpRequest`(请改用 `request` 模块)。
9+
- **不兼容改动** 移除 `app.use(AV.Cloud)` 的用法(请改用 `app.use(AV.express())`)。
10+
- **不兼容改动** 移除基于 [domain](https://nodejs.org/api/domain.html) 的异常处理,开发者需要自行捕捉云函数异步代码中的异常。
11+
- **不兼容改动** `AV.Cloud.run` 不再支持 Backbone 风格的回调(`success``error`)、在运行失败时不再向标准输出打印日志,请从返回的 Promise 中获取错误。
12+
- **不兼容改动** `AV.Insight.on` 注册的回调函数改为只接受一个 `result` 参数(去掉了 err 参数,请从 `result.status` 判断成功或失败)。
13+
- **不兼容改动** 一些错误提示被调整过,如果你依赖于对错误信息进行字符串匹配,请注意测试你的错误处理逻辑。
14+
- 新增 TypeScript 类型定义文件,位于 `leanengine.d.ts`
15+
- 新增 `AV.Cloud.onIMMessageReceived` 系列方法,用于更方便地定义实时通讯 Hook 函数。
16+
17+
**新增云函数、Class Hook 和 User Hook 的 Promise 模式**,会使用 Promise 的值作为响应内容。如果在 Promise 中抛了使用新增的 `AV.Cloud.Error` 构造的异常则作为错误返回给客户端,`AV.Cloud.Error` 的第二个参数可以指定 HTTP Status Code 和 Error Code(`AV.Cloud.Error('posts is empty', {status: 422, code: 422})`);如果抛出了其他错误类型则视作服务器端错误,返回 500 响应并打印错误到标准输出。
18+
19+
```javascript
20+
AV.Cloud.define(function(request) {
21+
return new AV.Query('Post').find().then( posts => {
22+
if (posts.length > 0) {
23+
return posts[0];
24+
} else {
25+
throw new AV.Cloud.Error('posts is empty');
26+
}
27+
});
28+
});
29+
```
30+
31+
对于原本不需要响应的 after 类 Hook(还包括 onVerified),现在也会按照同样的方式等待 Promise 完成再发送响应、结束链接,如果你希望收到请求后立刻结束链接,请不要在这类 Hook 中返回 Promise(或提前返回一个已经 resolve 的 Promise)。
32+
33+
如果传入 `AV.Cloud.define` 的函数有两个参数(`request``response`)则继续兼容原定义方式,需要使用 `response.success()` 发送响应。我们会继续兼容这种用法到下一个大版本,希望开发者尽快迁移到 Promise 风格的云函数上。
34+
35+
**新增了 LeanCloudHeaders 中间件**,用于在 Express 或 Koa 应用中解析 `X-LC` 开头的头,获取 Session Token 等信息:
36+
37+
```javascript
38+
// 注册中间件
39+
app.use(AV.Cloud.LeanCloudHeaders());
40+
41+
// 获取 Session Token
42+
app.get('/', (req, res) => {
43+
res.json({
44+
sessionToken: req.AV.sessionToken
45+
})
46+
});
47+
```
48+
49+
## v1.2.4
50+
51+
暂时地锁定 leancloud-storage 的版本,控制 [1.5.5](https://github.com/leancloud/javascript-sdk/releases/tag/v1.5.5) 中 disableCurrentUser 变动的影响。
52+
53+
- 锁定 leancloud-storage 的版本到 1.5.4
54+
355
## v1.2.3
456

557
* 升级 JavaScript SDK 到 [leancloud-storage 1.5.0](https://github.com/leancloud/javascript-sdk/releases)

README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# LeanEngine Node.js SDK
22

3-
[![Build Status](https://travis-ci.org/leancloud/leanengine-node-sdk.svg?branch=master)](https://travis-ci.org/leancloud/leanengine-node-sdk)
4-
53
## 安装
64

75
```bash
86
npm install leanengine --save
97
```
108

11-
建议使用 Node.js 4.0 以上的版本(可在 `package.json` 中设置 `engines.node``4.x`)。
9+
建议使用 Node.js 6.0 以上的版本(可在 `package.json` 中设置 `engines.node``6.x`)。
1210

1311
## 文档
1412

@@ -29,20 +27,14 @@ npm install leanengine --save
2927

3028
[Releases](https://github.com/leancloud/leanengine-node-sdk/releases)
3129

30+
安装 1.x 版本(建议尽快升级,见 [v2.0.0](https://github.com/leancloud/leanengine-node-sdk/releases/tag/v2.0.0)):
31+
32+
```
33+
npm install leanengine@1.x --save
34+
```
35+
3236
安装早期的 0.x 版本(请尽快 [升级到云引擎 Node.js SDK 1.0](https://leancloud.cn/docs/leanengine-node-sdk-upgrade-1.html)):
3337

3438
```bash
3539
npm install leanengine@0.x --save
3640
```
37-
38-
## 贡献者
39-
40-
在此表示感谢!
41-
42-
* [filod](https://github.com/filod)
43-
44-
## 许可协议
45-
46-
许可协议: MIT
47-
48-
作者: wchen (wchen@leancloud.rocks)

0 commit comments

Comments
 (0)