Skip to content

Commit 037b2fb

Browse files
committed
fix: shorter promise chains and better bind syntax
1 parent ea9dc67 commit 037b2fb

8 files changed

Lines changed: 44 additions & 53 deletions

File tree

src/plugins/apiKey.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,15 @@ module.exports = function(Api) {
5151
* @return {Promise} A promise which will resolved if everything went ok and rejected otherwise
5252
*/
5353
Api.prototype.clearApiKey = function () {
54-
var that = this;
5554
return new Promise(function (resolve, reject) {
56-
that.execute('USER', null, 'clearApiKey').then(function (result) {
55+
this.execute('USER', null, 'clearApiKey').then(function (result) {
5756
if (result) {
58-
delete that.httpParams.apiKey;
57+
delete this.httpParams.apiKey;
5958
resolve();
6059
} else {
6160
reject();
6261
}
63-
});
64-
});
62+
}.bind(this));
63+
}.bind(this));
6564
}
6665
};

src/plugins/count.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ module.exports = function(Api) {
2626
* @return {Promise}
2727
*/
2828
Api.prototype.count = function (objCode, query) {
29-
var that = this;
30-
return new Promise(function (resolve, reject) {
31-
that.request(objCode + '/count', query, null, Api.Methods.GET)
32-
.then(function (data) {
33-
resolve(data.count);
34-
}, reject);
29+
return this.request(objCode + '/count', query, null, Api.Methods.GET).then(function (data) {
30+
return data.count;
3531
});
3632
};
3733
};

src/plugins/login.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@ module.exports = function(Api) {
2828
* @return {Promise} A promise which will resolved with logged in user data if everything went ok and rejected otherwise
2929
*/
3030
Api.prototype.login = function (username, password) {
31-
var that = this;
32-
return new Promise(function (resolve, reject) {
33-
that.request('login', {username: username, password: password}, null, Api.Methods.POST)
34-
.then(function (data) {
35-
that.httpOptions.headers.sessionID = data.sessionID;
36-
resolve(data);
37-
}, reject);
38-
});
31+
return this.request('login', {username: username, password: password}, null, Api.Methods.POST).then(function (data) {
32+
this.httpOptions.headers.sessionID = data.sessionID;
33+
return data;
34+
}.bind(this));
3935
};
4036
};

src/plugins/logout.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ module.exports = function(Api) {
2525
* @return {Promise} A promise which will resolved if everything went ok and rejected otherwise
2626
*/
2727
Api.prototype.logout = function () {
28-
var that = this;
2928
return new Promise(function (resolve, reject) {
30-
that.request('logout', null, null, Api.Methods.GET).then(function (result) {
29+
this.request('logout', null, null, Api.Methods.GET).then(function (result) {
3130
if (result && result.success) {
32-
delete that.httpOptions.headers.sessionID;
31+
delete this.httpOptions.headers.sessionID;
3332
resolve();
3433
} else {
3534
reject();
3635
}
37-
});
38-
});
36+
}.bind(this));
37+
}.bind(this));
3938
};
4039
};

src/plugins/remove.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ module.exports = function(Api) {
2828
* @returns {Promise} A promise which will resolved if everything went ok and rejected otherwise
2929
*/
3030
Api.prototype.remove = function (objCode, objID, bForce) {
31-
var that = this;
3231
return new Promise(function (resolve, reject) {
3332
var params = bForce ? {force: true} : null;
34-
that.request(objCode + '/' + objID, params, null, Api.Methods.DELETE).then(function (result) {
33+
this.request(objCode + '/' + objID, params, null, Api.Methods.DELETE).then(function (result) {
3534
if (result && result.success) {
3635
resolve();
3736
} else {
3837
reject();
3938
}
4039
}, reject);
41-
});
40+
}.bind(this));
4241
};
4342
};

src/plugins/request.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,32 @@ module.exports = function(Api) {
2727
return method !== Api.Methods.GET && method !== Api.Methods.PUT;
2828
};
2929

30-
Api.prototype._handleResponse = function(resolve, reject){
31-
return function (response) {
32-
var body = '';
33-
if (typeof response.setEncoding === 'function') {
34-
response.setEncoding('utf8');
35-
}
36-
response.on('data', function (chunk) {
37-
body += chunk;
38-
});
39-
response.on('end', function () {
40-
var data;
41-
try {
42-
data = JSON.parse(body);
43-
}
44-
catch(e) {
45-
reject(body);
46-
return;
47-
}
48-
if (data.error) {
49-
reject(data);
50-
} else {
51-
resolve(data.data);
52-
}
53-
});
54-
};
30+
Api.prototype._handleResponse = function (resolve, reject) {
31+
return function (response) {
32+
var body = '';
33+
if (typeof response.setEncoding === 'function') {
34+
response.setEncoding('utf8');
35+
}
36+
response.on('data', function (chunk) {
37+
body += chunk;
38+
});
39+
response.on('end', function () {
40+
console.log('end'); //eslint-disable-line
41+
var data;
42+
try {
43+
data = JSON.parse(body);
44+
}
45+
catch (e) {
46+
reject(body);
47+
return;
48+
}
49+
if (data.error) {
50+
reject(data);
51+
} else {
52+
resolve(data.data);
53+
}
54+
});
55+
};
5556
};
5657

5758
Api.prototype.request = function(path, params, fields, method) {
@@ -73,7 +74,6 @@ module.exports = function(Api) {
7374
options.method = method;
7475
}
7576

76-
util._extend(options, this.httpOptions);
7777
if (path.indexOf('/') === 0) {
7878
options.path = this.httpOptions.path + path;
7979
}

test/plugins/count.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe('Api.count() method', function() {
4141

4242
var objCode = 'baz';
4343

44+
api.request.resolves();
4445
api.count(objCode, query);
4546
expect(api.request).to.have.callCount(1);
4647
expect(api.request).to.have.been.calledWith(objCode + "/count", query, null, "GET");

test/plugins/login.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('Api.login() method', function() {
3636
it('should call request() with proper params', function() {
3737
var username = 'bar',
3838
password = 'baz';
39+
api.request.resolves();
3940
api.login(username, password);
4041
expect(api.request).to.have.callCount(1);
4142
expect(api.request).to.have.been.calledWith("login", {username: username, password: password}, null, "POST");

0 commit comments

Comments
 (0)