Skip to content

Commit 1378053

Browse files
Merge branch 'develop' into fix-rc7-110-handle-ws-error
2 parents 2e06df4 + 3fd4c2b commit 1378053

22 files changed

+203
-448
lines changed

.eslintrc renamed to .eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"no-undef": 0,
4040
"no-undef-init": 1,
4141
"no-unreachable": 2,
42-
"no-unused-expressions": 2,
42+
"no-unused-expressions": [2, {"allowShortCircuit": true}],
4343
"no-useless-call": 2,
4444
"no-with": 2,
4545
"quotes": [2, "single"],

dist/kuzzle.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/kuzzle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/kuzzle.min.js

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

dist/kuzzle.min.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/kuzzle.js

Lines changed: 31 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ Kuzzle.prototype.getJwtToken = function() {
447447
* @param credentials
448448
* @param expiresIn
449449
* @param cb
450-
* @returns {Kuzzle}
451450
*/
452451
Kuzzle.prototype.login = function (strategy) {
453452
var
@@ -456,7 +455,7 @@ Kuzzle.prototype.login = function (strategy) {
456455
strategy: strategy
457456
},
458457
credentials,
459-
cb;
458+
cb = null;
460459

461460
// Handle arguments (credentials, expiresIn, cb)
462461
if (arguments[1]) {
@@ -491,20 +490,13 @@ Kuzzle.prototype.login = function (strategy) {
491490
self.setJwtToken(response.result.jwt);
492491
}
493492

494-
if (cb && typeof cb === 'function') {
495-
cb(null, response.result);
496-
}
493+
cb && cb(null, response.result);
497494
}
498495
else {
499-
if (cb && typeof cb === 'function') {
500-
cb(error);
501-
}
502-
496+
cb && cb(error);
503497
self.emitEvent('loginAttempt', {success: false, error: error.message});
504498
}
505499
});
506-
507-
return self;
508500
};
509501

510502
/**
@@ -523,15 +515,12 @@ Kuzzle.prototype.logout = function (cb) {
523515
body: {}
524516
};
525517

526-
this.query({controller: 'auth', action: 'logout'}, request, {queuable: false}, function(error) {
518+
this.query({controller: 'auth', action: 'logout'}, request, {queuable: false}, typeof cb !== 'function' ? null : function(error) {
527519
if (error === null) {
528520
self.jwtToken = undefined;
529-
530-
if (typeof cb === 'function') {
531-
cb(null, self);
532-
}
521+
cb(null, self);
533522
}
534-
else if (typeof cb === 'function') {
523+
else {
535524
cb(error);
536525
}
537526
});
@@ -545,11 +534,9 @@ Kuzzle.prototype.logout = function (cb) {
545534
* @param {string} token The jwt token to check
546535
* @param {function} callback The callback to be called when the response is
547536
* available. The signature is `function(error, response)`.
548-
* @return {Kuzzle} The Kuzzle instance to enable chaining.
549537
*/
550538
Kuzzle.prototype.checkToken = function (token, callback) {
551539
var
552-
self = this,
553540
request = {
554541
body: {
555542
token: token
@@ -565,16 +552,13 @@ Kuzzle.prototype.checkToken = function (token, callback) {
565552

566553
callback(null, response.result);
567554
});
568-
569-
return self;
570555
};
571556

572557
/**
573558
* Fetches the current user.
574559
*
575560
* @param {function} callback The callback to be called when the response is
576561
* available. The signature is `function(error, response)`.
577-
* @return {Kuzzle} The Kuzzle instance to enable chaining.
578562
*/
579563
Kuzzle.prototype.whoAmI = function (callback) {
580564
var self = this;
@@ -588,13 +572,12 @@ Kuzzle.prototype.whoAmI = function (callback) {
588572

589573
callback(null, new KuzzleUser(self.security, response.result._id, response.result._source));
590574
});
591-
592-
return self;
593575
};
594576

595577
/**
596578
* Gets the rights array of the currently logged user.
597579
*
580+
* @param {object} [options] - Optional parameters
598581
* @param {function} cb The callback containing the normalized array of rights.
599582
*/
600583
Kuzzle.prototype.getMyRights = function (options, cb) {
@@ -607,7 +590,7 @@ Kuzzle.prototype.getMyRights = function (options, cb) {
607590

608591
self.callbackRequired('Kuzzle.getMyRights', cb);
609592

610-
self.query({controller: 'auth', action:'getMyRights'}, {}, null, function (err, res) {
593+
self.query({controller: 'auth', action:'getMyRights'}, {}, options, function (err, res) {
611594
if (err) {
612595
return cb(err);
613596
}
@@ -622,6 +605,7 @@ Kuzzle.prototype.getMyRights = function (options, cb) {
622605
* @param {object} content - a plain javascript object representing the user's modification
623606
* @param {object} [options] - (optional) arguments
624607
* @param {responseCallback} [cb] - (optional) Handles the query response
608+
* @returns {Kuzzle} this object
625609
*/
626610
Kuzzle.prototype.updateSelf = function (content, options, cb) {
627611
var
@@ -636,17 +620,11 @@ Kuzzle.prototype.updateSelf = function (content, options, cb) {
636620

637621
data.body = content;
638622

639-
if (cb) {
640-
self.query(queryArgs, data, options, function (err, res) {
641-
if (err) {
642-
return cb(err);
643-
}
623+
self.query(queryArgs, data, options, cb && function (err, res) {
624+
cb(err, err ? undefined : res.result);
625+
});
644626

645-
cb(null, res.result);
646-
});
647-
} else {
648-
self.query(queryArgs, data, options);
649-
}
627+
return this;
650628
};
651629

652630
/**
@@ -719,9 +697,7 @@ function emitRequest (request, cb) {
719697
self.emitEvent('jwtTokenExpired', request, cb);
720698
}
721699

722-
if (cb) {
723-
cb(response.error, response);
724-
}
700+
cb && cb(response.error, response);
725701
});
726702
}
727703

@@ -830,7 +806,6 @@ Kuzzle.prototype.addListener = function(event, listener) {
830806
*
831807
* @param {object} [options] - Optional parameters
832808
* @param {responseCallback} cb - Handles the query response
833-
* @returns {object} this
834809
*/
835810
Kuzzle.prototype.getAllStatistics = function (options, cb) {
836811
if (!cb && typeof options === 'function') {
@@ -847,8 +822,6 @@ Kuzzle.prototype.getAllStatistics = function (options, cb) {
847822

848823
cb(null, res.result.hits);
849824
});
850-
851-
return this;
852825
};
853826

854827
/**
@@ -858,10 +831,11 @@ Kuzzle.prototype.getAllStatistics = function (options, cb) {
858831
* @param {number} timestamp - Epoch time. Starting time from which the frames are to be retrieved
859832
* @param {object} [options] - Optional parameters
860833
* @param {responseCallback} cb - Handles the query response
861-
* @returns {object} this
862834
*/
863835
Kuzzle.prototype.getStatistics = function (timestamp, options, cb) {
864-
var queryCB;
836+
var
837+
queryCB,
838+
body;
865839

866840
if (!cb) {
867841
if (arguments.length === 1) {
@@ -885,22 +859,13 @@ Kuzzle.prototype.getStatistics = function (timestamp, options, cb) {
885859
return cb(err);
886860
}
887861

888-
if (timestamp) {
889-
cb(null, res.result.hits);
890-
} else {
891-
cb(null, [res.result]);
892-
}
862+
cb(null, timestamp ? res.result.hits : [res.result]);
893863
};
894864

895865
this.callbackRequired('Kuzzle.getStatistics', cb);
896866

897-
if (!timestamp) {
898-
this.query({controller: 'admin', action: 'getLastStats'}, {}, options, queryCB);
899-
} else {
900-
this.query({controller: 'admin', action: 'getStats'}, { body: { startTime: timestamp } }, options, queryCB);
901-
}
902-
903-
return this;
867+
body = timestamp ? {body: {startTime: timestamp}} : {};
868+
this.query({controller: 'admin', action: timestamp ? 'getStats' : 'getLastStats'}, body, options, queryCB);
904869
};
905870

906871
/**
@@ -922,12 +887,8 @@ Kuzzle.prototype.dataCollectionFactory = function(collection, index) {
922887
index = this.defaultIndex;
923888
}
924889

925-
if (typeof index !== 'string') {
926-
throw new Error('Invalid "index" argument: string expected, got ' + typeof index);
927-
}
928-
929-
if (typeof collection !== 'string') {
930-
throw new Error('Invalid "collection" argument: string expected, got ' + typeof collection);
890+
if (typeof index !== 'string' || typeof collection !== 'string') {
891+
throw new Error('Invalid index or collection argument: string expected');
931892
}
932893

933894
if (!this.collections[index]) {
@@ -957,7 +918,6 @@ Kuzzle.prototype.flushQueue = function () {
957918
* @param {string} [index] - Index containing collections to be listed
958919
* @param {object} [options] - Optional parameters
959920
* @param {responseCallback} cb - Handles the query response
960-
* @returns {object} this
961921
*/
962922
Kuzzle.prototype.listCollections = function () {
963923
var
@@ -1000,18 +960,15 @@ Kuzzle.prototype.listCollections = function () {
1000960
return cb(err);
1001961
}
1002962

1003-
return cb(null, res.result.collections);
963+
cb(null, res.result.collections);
1004964
});
1005-
1006-
return this;
1007965
};
1008966

1009967
/**
1010968
* Returns the list of existing indexes in Kuzzle
1011969
*
1012970
* @param {object} [options] - Optional arguments
1013971
* @param {responseCallback} cb - Handles the query response
1014-
* @returns {object} this
1015972
*/
1016973
Kuzzle.prototype.listIndexes = function (options, cb) {
1017974
if (!cb && typeof options === 'function') {
@@ -1022,14 +979,8 @@ Kuzzle.prototype.listIndexes = function (options, cb) {
1022979
this.callbackRequired('Kuzzle.listIndexes', cb);
1023980

1024981
this.query({controller: 'read', action: 'listIndexes'}, {}, options, function (err, res) {
1025-
if (err) {
1026-
return cb(err);
1027-
}
1028-
1029-
return cb(null, res.result.indexes);
982+
cb(err, err ? undefined : res.result.indexes);
1030983
});
1031-
1032-
return this;
1033984
};
1034985

1035986
/**
@@ -1055,7 +1006,6 @@ Kuzzle.prototype.disconnect = function () {
10551006
*
10561007
* @param {object} [options] - Optional arguments
10571008
* @param {responseCallback} cb - Handles the query response
1058-
* @returns {object} this
10591009
*/
10601010
Kuzzle.prototype.getServerInfo = function (options, cb) {
10611011
if (!cb && typeof options === 'function') {
@@ -1072,8 +1022,6 @@ Kuzzle.prototype.getServerInfo = function (options, cb) {
10721022

10731023
cb(null, res.result.serverInfo);
10741024
});
1075-
1076-
return this;
10771025
};
10781026

10791027
/**
@@ -1122,7 +1070,6 @@ Kuzzle.prototype.refreshIndex = function () {
11221070
* @param {string} index - The index to get the status from. Defaults to Kuzzle.defaultIndex
11231071
* @param {object} options - Optinal arguments
11241072
* @param {responseCallback} cb - Handles the query response
1125-
* @returns {object} this
11261073
*/
11271074
Kuzzle.prototype.getAutoRefresh = function () {
11281075
var
@@ -1153,8 +1100,6 @@ Kuzzle.prototype.getAutoRefresh = function () {
11531100

11541101
this.callbackRequired('Kuzzle.getAutoRefresh', cb);
11551102
this.query({ index: index, controller: 'admin', action: 'getAutoRefresh'}, {}, options, cb);
1156-
1157-
return this;
11581103
};
11591104

11601105
/**
@@ -1210,7 +1155,6 @@ Kuzzle.prototype.setAutoRefresh = function () {
12101155
* Return the current Kuzzle's UTC Epoch time, in milliseconds
12111156
* @param {object} [options] - Optional parameters
12121157
* @param {responseCallback} cb - Handles the query response
1213-
* @returns {object} this
12141158
*/
12151159
Kuzzle.prototype.now = function (options, cb) {
12161160
if (!cb && typeof options === 'function') {
@@ -1221,14 +1165,8 @@ Kuzzle.prototype.now = function (options, cb) {
12211165
this.callbackRequired('Kuzzle.now', cb);
12221166

12231167
this.query({controller: 'read', action: 'now'}, {}, options, function (err, res) {
1224-
if (err) {
1225-
return cb(err);
1226-
}
1227-
1228-
cb(null, res.result.now);
1168+
cb(err, res && res.result.now);
12291169
});
1230-
1231-
return this;
12321170
};
12331171

12341172
/**
@@ -1334,6 +1272,7 @@ Kuzzle.prototype.query = function (queryArgs, query, options, cb) {
13341272
* Removes all listeners, either from a specific event or from all events
13351273
*
13361274
* @param {string} event - One of the event described in the Event Handling section of this documentation
1275+
* @returns {Kuzzle} this object
13371276
*/
13381277
Kuzzle.prototype.removeAllListeners = function (event) {
13391278
var
@@ -1351,13 +1290,16 @@ Kuzzle.prototype.removeAllListeners = function (event) {
13511290
self.eventListeners[eventName].listeners = [];
13521291
});
13531292
}
1293+
1294+
return this;
13541295
};
13551296

13561297
/**
13571298
* Removes a listener from an event.
13581299
*
13591300
* @param {string} event - One of the event described in the Event Handling section of this documentation
13601301
* @param {string} listenerId - The ID returned by addListener
1302+
* @returns {Kuzzle} this object
13611303
*/
13621304
Kuzzle.prototype.removeListener = function (event, listenerId) {
13631305
var
@@ -1373,6 +1315,8 @@ Kuzzle.prototype.removeListener = function (event, listenerId) {
13731315
self.eventListeners[event].listeners.splice(index, 1);
13741316
}
13751317
});
1318+
1319+
return this;
13761320
};
13771321

13781322
/**

0 commit comments

Comments
 (0)