Skip to content

Commit df7b93d

Browse files
committed
assignRole response value must be void (#28)
* fix (users) assignRole and unassignRole must be void in async - fix (ajax) empty string in async callback if server response has no body in node env. Must be undefined * fixed typescript definition according to change
1 parent 921fd75 commit df7b93d

File tree

3 files changed

+26
-37
lines changed

3 files changed

+26
-37
lines changed

libs/backendless.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,11 @@ declare module __Backendless {
773773
restorePassword(email:string):Backendless.User ;
774774
restorePassword(email:string, async:Backendless.Async):XMLHttpRequest;
775775

776-
assignRole(userName:string, roleName:string):Backendless.User ;
777-
assignRole(userName:string, roleName:string, async:Backendless.Async):XMLHttpRequest;
776+
assignRole(identity:string, roleName:string):Backendless.User ;
777+
assignRole(identity:string, roleName:string, async:Backendless.Async):XMLHttpRequest;
778778

779-
unassignRole(userName:string, roleName:string):Backendless.User ;
780-
unassignRole(userName:string, roleName:string, async:Backendless.Async):XMLHttpRequest;
779+
unassignRole(identity:string, roleName:string):Backendless.User ;
780+
unassignRole(identity:string, roleName:string, async:Backendless.Async):XMLHttpRequest;
781781

782782
login(userName:string, password:string):Backendless.User ;
783783
login(userName:string, password:string, stayLoggedIn:boolean):Backendless.User ;

libs/backendless.js

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,6 @@
409409
}
410410
};
411411

412-
var buffer = '';
413-
414412
if (currentUser != null && !!currentUser["user-token"]) {
415413
options.headers["user-token"] = currentUser["user-token"];
416414
}
@@ -419,23 +417,23 @@
419417
throw new Error('Use Async type of request using Backendless with NodeJS. Add Backendless.Async(successCallback, errorCallback) as last argument');
420418
}
421419

420+
var buffer;
422421
var httpx = require(https ? 'https' : 'http');
423-
424422
var req = httpx.request(options, function(res) {
425423
res.setEncoding('utf8');
426424
res.on('data', function(chunk) {
427-
buffer += chunk;
425+
buffer = buffer ? buffer + chunk : chunk;
428426
});
429427
res.on('end', function() {
430-
var contentType = res.headers['content-type'];
431-
432-
if (contentType && contentType.indexOf('application/json') !== -1) {
433-
buffer = tryParseJSON(buffer);
434-
}
435-
436428
var callback = config.asyncHandler[res.statusCode >= 200 && res.statusCode < 300 ? "success" : "fault"];
437429

438430
if (Utils.isFunction(callback)) {
431+
var contentType = res.headers['content-type'];
432+
433+
if (buffer !== undefined && contentType && contentType.indexOf('application/json') !== -1) {
434+
buffer = tryParseJSON(buffer);
435+
}
436+
439437
callback(buffer);
440438
}
441439
});
@@ -1499,42 +1497,32 @@
14991497
return isAsync ? result : this._parseResponse(result);
15001498
},
15011499

1502-
roleHelper: function(username, rolename, async, operation) {
1503-
if (!username) {
1504-
throw new Error('Username can not be empty');
1500+
roleHelper: function(identity, rolename, async, operation) {
1501+
if (!identity) {
1502+
throw new Error('User identity can not be empty');
15051503
}
15061504

15071505
if (!rolename) {
15081506
throw new Error('Rolename can not be empty');
15091507
}
15101508

15111509
var responder = extractResponder(arguments);
1512-
var isAsync = responder != null;
1513-
1514-
if (responder) {
1515-
responder = this._wrapAsync(responder);
1516-
}
1517-
1518-
var data = {
1519-
user : username,
1520-
roleName: rolename
1521-
};
15221510

15231511
return Backendless._ajax({
15241512
method : 'POST',
15251513
url : this.restUrl + '/' + operation,
1526-
isAsync : isAsync,
1514+
isAsync : !!responder,
15271515
asyncHandler: responder,
1528-
data : JSON.stringify(data)
1516+
data : JSON.stringify({user : identity, roleName: rolename})
15291517
});
15301518
},
15311519

1532-
assignRole: function(username, rolename, async) {
1533-
return this.roleHelper(username, rolename, async, 'assignRole');
1520+
assignRole: function(identity, rolename, async) {
1521+
return this.roleHelper(identity, rolename, async, 'assignRole');
15341522
},
15351523

1536-
unassignRole: function(username, rolename, async) {
1537-
return this.roleHelper(username, rolename, async, 'unassignRole');
1524+
unassignRole: function(identity, rolename, async) {
1525+
return this.roleHelper(identity, rolename, async, 'unassignRole');
15381526
},
15391527

15401528
login: function(username, password, stayLoggedIn, async) {

tests/tsd.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ function testUser() {
303303

304304
function testUserService() {
305305
var userName:string = 'userName';
306+
var identity:string = 'identity';
306307
var roleName:string = 'rolename';
307308
var password:string = 'password';
308309
var div:HTMLElement = document.createElement('div');
@@ -326,11 +327,11 @@ function testUserService() {
326327
newUser = Backendless.UserService.getUserRoles();
327328
resultXHR = Backendless.UserService.getUserRoles(async);
328329

329-
newUser = Backendless.UserService.assignRole(userName, roleName);
330-
resultXHR = Backendless.UserService.assignRole(userName, roleName, async);
330+
newUser = Backendless.UserService.assignRole(identity, roleName);
331+
resultXHR = Backendless.UserService.assignRole(identity, roleName, async);
331332

332-
newUser = Backendless.UserService.unassignRole(userName, roleName);
333-
resultXHR = Backendless.UserService.unassignRole(userName, roleName, async);
333+
newUser = Backendless.UserService.unassignRole(identity, roleName);
334+
resultXHR = Backendless.UserService.unassignRole(identity, roleName, async);
334335

335336
newUser = Backendless.UserService.login(userName, password);
336337
newUser = Backendless.UserService.login(userName, password, bol);

0 commit comments

Comments
 (0)