Skip to content

Commit a0c212f

Browse files
committed
refactor: fix code review issues
1 parent 19d2313 commit a0c212f

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

src/insight.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = function(AV) {
77
* 包含了使用了 LeanCloud
88
* <a href='/docs/leaninsight_guide.html'>离线数据分析功能</a>的函数。
99
* <p><strong><em>
10-
* 部分函数仅在云引擎运行环境下有效
10+
* 仅在云引擎运行环境下有效
1111
* </em></strong></p>
1212
* @namespace
1313
*/

src/request.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const ajax = (method, resourceUrl, data, headers = {}, onprogress) => {
8282
}
8383
req.end((err, res) => {
8484
if (res) {
85-
debug(`response(${count})`, res.status, res.body && res.text, res.header);
85+
debug(`response(${count})`, res.status, res.body || res.text, res.header);
8686
}
8787
if (err) {
8888
if (res) {
@@ -117,20 +117,20 @@ const setHeaders = (sessionToken) => {
117117
headers['User-Agent'] = AV._config.userAgent || `AV/${AV.version}; Node.js/${process.version}`;
118118
}
119119

120-
return new Promise((resolve) => {
120+
return Promise.resolve().then(() => {
121121
// Pass the session token
122122
if (sessionToken) {
123123
headers['X-LC-Session'] = sessionToken;
124-
resolve(headers);
124+
return headers;
125125
} else if (!AV._config.disableCurrentUser) {
126-
AV.User.currentAsync().then((currentUser) => {
126+
return AV.User.currentAsync().then((currentUser) => {
127127
if (currentUser && currentUser._sessionToken) {
128128
headers['X-LC-Session'] = currentUser._sessionToken;
129129
}
130-
resolve(headers);
130+
return headers;
131131
});
132132
} else {
133-
resolve(headers);
133+
return headers;
134134
}
135135
});
136136
};

src/status.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module.exports = function(AV) {
5757
*/
5858
destroy: function(options){
5959
if(!this.id)
60-
return AV.Promise.reject('The status id is not exists.');
60+
return AV.Promise.reject(new Error('The status id is not exists.'));
6161
var request = AVRequest('statuses', null, this.id, 'DELETE', options && options.sessionToken);
6262
return request;
6363
},
@@ -93,7 +93,7 @@ module.exports = function(AV) {
9393
* });
9494
*/
9595
send: function(options = {}){
96-
if(!AV.User.current() && !options.sessionToken) {
96+
if(!options.sessionToken && !AV.User.current()) {
9797
throw new Error('Please signin an user.');
9898
}
9999
if(!this.query){
@@ -149,7 +149,7 @@ module.exports = function(AV) {
149149
* });
150150
*/
151151
AV.Status.sendStatusToFollowers = function(status, options = {}) {
152-
if(!AV.User.current() && !options.sessionToken){
152+
if(!options.sessionToken && !AV.User.current()){
153153
throw new Error('Please signin an user.');
154154
}
155155
return getUserPointer(options).then(currUser => {
@@ -192,7 +192,7 @@ module.exports = function(AV) {
192192
* });
193193
*/
194194
AV.Status.sendPrivateStatus = function(status, target, options = {}) {
195-
if(!AV.User.current() && !options.sessionToken){
195+
if(!options.sessionToken && !AV.User.current()){
196196
throw new Error('Please signin an user.');
197197
}
198198
if(!target){
@@ -240,7 +240,7 @@ module.exports = function(AV) {
240240
AV.Status.countUnreadStatuses = function(owner){
241241
var options = (!_.isString(arguments[1]) ? arguments[1] : arguments[2]) || {};
242242
var inboxType = !_.isString(arguments[1]) ? 'default' : arguments[1];
243-
if(!AV.User.current() && !options.sessionToken && owner == null){
243+
if(!options.sessionToken && owner == null && !AV.User.current()){
244244
throw new Error('Please signin an user or pass the owner objectId.');
245245
}
246246
return getUser(options).then(owner => {

test/status.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
'use strict';
22

33
describe("AV.Status",function(){
4+
before(function() {
5+
var userName = this.userName = 'StatusTest' + Date.now();
6+
return AV.User.signUp(userName, userName).then(user => {
7+
this.user = user;
8+
});
9+
});
10+
11+
after(() => AV.User.logOut());
412

513
describe("Send status.",function(){
614
it("should send status to followers.",function(){

0 commit comments

Comments
 (0)