Skip to content

Commit 0132b62

Browse files
committed
Merge pull request #41 from kuzzleio/promisifyLogMethods
Promisify log methods
2 parents 3387d49 + ebea953 commit 0132b62

File tree

6 files changed

+78
-29
lines changed

6 files changed

+78
-29
lines changed

dist/kuzzle.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ module.exports = Kuzzle = function (url, options, cb) {
455455
writable: false
456456
},
457457
loginExpiresIn: {
458-
value: (options && typeof options.loginExpiresIn === 'number') ? options.loginExpiresIn : undefined,
458+
value: (options && ['number', 'string'].indexOf(typeof options.loginExpiresIn) !== -1) ? options.loginExpiresIn : undefined,
459459
enumerable: true,
460460
writable: false
461461
},
@@ -529,7 +529,7 @@ module.exports = Kuzzle = function (url, options, cb) {
529529
return this.bluebird.promisifyAll(this, {
530530
suffix: 'Promise',
531531
filter: function (name, func, target, passes) {
532-
var whitelist = ['getAllStatistics', 'getServerInfo', 'getStatistics', 'listCollections', 'listIndexes', 'now', 'query'];
532+
var whitelist = ['getAllStatistics', 'getServerInfo', 'getStatistics', 'listCollections', 'listIndexes', 'login', 'logout', 'now', 'query'];
533533

534534
return passes && whitelist.indexOf(name) !== -1;
535535
}
@@ -657,21 +657,22 @@ Kuzzle.prototype.connect = function () {
657657
Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) {
658658
var
659659
self = this,
660-
request = {};
660+
request = {
661+
strategy: strategy
662+
};
663+
664+
if (!cb && typeof expiresIn === 'function') {
665+
cb = expiresIn;
666+
expiresIn = null;
667+
}
661668

662669
Object.keys(credentials).forEach(function (key) {
663-
if (credentials.hasOwnProperty(key)) {
664-
request[key] = credentials[key];
665-
}
670+
request[key] = credentials[key];
666671
});
667672

668-
if (
669-
typeof expiresIn === 'number' ||
670-
typeof expiresIn === 'string'
671-
) {
673+
if (['number', 'string'].indexOf(typeof expiresIn) !== -1) {
672674
request.expiresIn = expiresIn;
673675
}
674-
request.strategy = strategy;
675676

676677
this.query({controller: 'auth', action: 'login'}, request, {}, function(error, response) {
677678
if (error === null) {

dist/kuzzle.min.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.min.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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "1.3.2",
3+
"version": "1.3.3",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <support@kuzzle.io>",
66
"repository": {

src/kuzzle.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ module.exports = Kuzzle = function (url, options, cb) {
180180
writable: false
181181
},
182182
loginExpiresIn: {
183-
value: (options && typeof options.loginExpiresIn === 'number') ? options.loginExpiresIn : undefined,
183+
value: (options && ['number', 'string'].indexOf(typeof options.loginExpiresIn) !== -1) ? options.loginExpiresIn : undefined,
184184
enumerable: true,
185185
writable: false
186186
},
@@ -254,7 +254,7 @@ module.exports = Kuzzle = function (url, options, cb) {
254254
return this.bluebird.promisifyAll(this, {
255255
suffix: 'Promise',
256256
filter: function (name, func, target, passes) {
257-
var whitelist = ['getAllStatistics', 'getServerInfo', 'getStatistics', 'listCollections', 'listIndexes', 'now', 'query'];
257+
var whitelist = ['getAllStatistics', 'getServerInfo', 'getStatistics', 'listCollections', 'listIndexes', 'login', 'logout', 'now', 'query'];
258258

259259
return passes && whitelist.indexOf(name) !== -1;
260260
}
@@ -382,21 +382,22 @@ Kuzzle.prototype.connect = function () {
382382
Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) {
383383
var
384384
self = this,
385-
request = {};
385+
request = {
386+
strategy: strategy
387+
};
388+
389+
if (!cb && typeof expiresIn === 'function') {
390+
cb = expiresIn;
391+
expiresIn = null;
392+
}
386393

387394
Object.keys(credentials).forEach(function (key) {
388-
if (credentials.hasOwnProperty(key)) {
389-
request[key] = credentials[key];
390-
}
395+
request[key] = credentials[key];
391396
});
392397

393-
if (
394-
typeof expiresIn === 'number' ||
395-
typeof expiresIn === 'string'
396-
) {
398+
if (['number', 'string'].indexOf(typeof expiresIn) !== -1) {
397399
request.expiresIn = expiresIn;
398400
}
399-
request.strategy = strategy;
400401

401402
this.query({controller: 'auth', action: 'login'}, request, {}, function(error, response) {
402403
if (error === null) {

test/kuzzle/constructor.test.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ describe('Kuzzle constructor', () => {
4646
should.exist(kuzzle.getStatistics);
4747
should.exist(kuzzle.listCollections);
4848
should.exist(kuzzle.disconnect);
49+
should.exist(kuzzle.login);
50+
should.exist(kuzzle.logout);
4951
should.exist(kuzzle.now);
5052
should.exist(kuzzle.query);
5153
should.exist(kuzzle.removeAllListeners);
@@ -108,7 +110,10 @@ describe('Kuzzle constructor', () => {
108110
metadata: {foo: ['bar', 'baz', 'qux'], bar: 'foo'},
109111
replayInterval: 99999,
110112
reconnectionDelay: 666,
111-
defaultIndex: 'foobar'
113+
defaultIndex: 'foobar',
114+
loginStrategy: 'some strategy',
115+
loginCredentials: { user: 'foo', password: 'bar'},
116+
loginExpiresIn: 777
112117
},
113118
kuzzle = new Kuzzle('nowhere', options);
114119

@@ -123,6 +128,9 @@ describe('Kuzzle constructor', () => {
123128
should(kuzzle.metadata).be.an.Object().and.match(options.metadata);
124129
should(kuzzle.replayInterval).be.exactly(options.replayInterval);
125130
should(kuzzle.reconnectionDelay).be.exactly(options.reconnectionDelay);
131+
should(kuzzle.loginStrategy).be.exactly(options.loginStrategy);
132+
should(kuzzle.loginCredentials).be.exactly(options.loginCredentials);
133+
should(kuzzle.loginExpiresIn).be.exactly(options.loginExpiresIn);
126134
});
127135

128136
it('should handle the offlineMode option properly', () => {
@@ -180,7 +188,8 @@ describe('Kuzzle constructor', () => {
180188
should.exist(kuzzle.getStatisticsPromise);
181189
should.exist(kuzzle.listCollectionsPromise);
182190
should.exist(kuzzle.listIndexesPromise);
183-
should.not.exist(kuzzle.logoutPromise);
191+
should.exist(kuzzle.loginPromise);
192+
should.exist(kuzzle.logoutPromise);
184193
should.exist(kuzzle.nowPromise);
185194
should.exist(kuzzle.queryPromise);
186195
should.not.exist(kuzzle.removeAllListenersPromise);
@@ -612,7 +621,7 @@ describe('Kuzzle constructor', () => {
612621
var
613622
loginCalled = false,
614623
loginStub = function(strategy, credentials, expiresIn, cb) {
615-
loginCalled = true
624+
loginCalled = true;
616625

617626
if (typeof cb === 'function') {
618627
if (strategy === 'error') {
@@ -762,6 +771,44 @@ describe('Kuzzle constructor', () => {
762771
});
763772
});
764773

774+
it('should handle optional arguments correctly', function (done) {
775+
var
776+
kuzzle,
777+
loginCredentials = {username: 'foo', password: 'bar'};
778+
779+
this.timeout(200);
780+
781+
kuzzle = new Kuzzle('nowhere', {
782+
connect: 'manual'
783+
});
784+
785+
kuzzle.query = function(queryArgs, query, options, cb) {
786+
should(query.expiresIn).be.undefined();
787+
done();
788+
};
789+
790+
kuzzle.login('local', loginCredentials, function () {});
791+
});
792+
793+
it('should handle optional callback correctly', function (done) {
794+
var
795+
kuzzle,
796+
loginCredentials = {username: 'foo', password: 'bar'};
797+
798+
this.timeout(200);
799+
800+
kuzzle = new Kuzzle('nowhere', {
801+
connect: 'manual'
802+
});
803+
804+
kuzzle.query = function(queryArgs, query, options, cb) {
805+
should(query.expiresIn).be.undefined();
806+
done();
807+
};
808+
809+
kuzzle.login('local', loginCredentials);
810+
});
811+
765812
it('should have a empty token in logout callback', function (done) {
766813
var
767814
kuzzle;

0 commit comments

Comments
 (0)