diff --git a/src/Kuzzle.js b/src/Kuzzle.js index 569c8fc0d..9382ddb96 100644 --- a/src/Kuzzle.js +++ b/src/Kuzzle.js @@ -62,15 +62,15 @@ class Kuzzle extends KuzzleEventEmitter { this.volatile = typeof options.volatile === 'object' ? options.volatile : {}; // controllers - this.auth = new AuthController(this); - this.bulk = new BulkController(this); - this.collection = new CollectionController(this); - this.document = new DocumentController(this); - this.index = new IndexController(this); - this.ms = new MemoryStorageController(this); - this.realtime = new RealtimeController(this); - this.security = new SecurityController(this); - this.server = new ServerController(this); + this.useController(AuthController, 'auth'); + this.useController(BulkController, 'bulk'); + this.useController(CollectionController, 'collection'); + this.useController(DocumentController, 'document'); + this.useController(IndexController, 'index'); + this.useController(MemoryStorageController, 'ms'); + this.useController(RealtimeController, 'realtime'); + this.useController(SecurityController, 'security'); + this.useController(ServerController, 'server'); // offline queue this._autoQueue = typeof options.autoQueue === 'boolean' ? options.autoQueue : false; @@ -443,28 +443,34 @@ Discarded request: ${JSON.stringify(request)}`)); /** * Adds a new controller and make it available in the SDK. * - * @param {BaseController} controller + * @param {BaseController} ControllerClass + * @param {string} accessor * @returns {Kuzzle} */ - useController (controller) { - if (!(controller instanceof BaseController)) { + useController (ControllerClass, accessor) { + if (!(ControllerClass.prototype instanceof BaseController)) { throw new Error('Controllers must inherits from the BaseController class.'); } - if (!(controller.name && controller.name.length > 0)) { - throw new Error('Controllers must have a name.'); + if (!(accessor && accessor.length > 0)) { + throw new Error('You must provide a valid accessor.'); + } + + if (this[accessor]) { + throw new Error(`There is already a controller with the accessor '${accessor}'. Please use another one.`); } - if (!(controller.accessor && controller.accessor.length > 0)) { - throw new Error('Controllers must have an accessor.'); + const controller = new ControllerClass(this); + + if (!(controller.name && controller.name.length > 0)) { + throw new Error('Controllers must have a name.'); } - if (this[controller.accessor]) { - throw new Error(`There is already a controller with the accessor '${controller.accessor}'. Please use another one.`); + if (controller.kuzzle !== this) { + throw new Error('You must pass the Kuzzle SDK instance to the parent constructor.'); } - controller.kuzzle = this; - this[controller.accessor] = controller; + this[accessor] = controller; return this; } diff --git a/src/controllers/auth.js b/src/controllers/auth.js index 1c93a2746..342a150db 100644 --- a/src/controllers/auth.js +++ b/src/controllers/auth.js @@ -1,4 +1,6 @@ -const User = require('./security/user'); +const + BaseController = require('./base'), + User = require('./security/user'); /** * Auth controller @@ -6,18 +8,14 @@ const User = require('./security/user'); * @param kuzzle * @constructor */ -class AuthController { +class AuthController extends BaseController { /** * constructor * @param kuzzle */ constructor (kuzzle) { - this._kuzzle = kuzzle; - } - - get kuzzle () { - return this._kuzzle; + super(kuzzle, 'auth'); } /** @@ -27,8 +25,7 @@ class AuthController { * @return {Promise|*|PromiseLike|Promise} */ checkToken (token) { - return this.kuzzle.query({ - controller: 'auth', + return this.query({ action: 'checkToken', body: {token} }, {queuable: false}) @@ -44,9 +41,8 @@ class AuthController { * @returns {Promise|*|PromiseLike|Promise} */ createMyCredentials (strategy, credentials, options = {}) { - return this.kuzzle.query({ + return this.query({ strategy, - controller: 'auth', action: 'createMyCredentials', body: credentials }, options) @@ -60,9 +56,8 @@ class AuthController { * @returns {Promise|*|PromiseLike|Promise} */ credentialsExist (strategy, options = {}) { - return this.kuzzle.query({ + return this.query({ strategy, - controller: 'auth', action: 'credentialsExist' }, options) .then(response => response.result); @@ -76,9 +71,8 @@ class AuthController { * @returns {Promise|*|PromiseLike|Promise} */ deleteMyCredentials (strategy, options = {}) { - return this.kuzzle.query({ + return this.query({ strategy, - controller: 'auth', action: 'deleteMyCredentials' }, options) .then(response => response.result.acknowledged); @@ -90,8 +84,7 @@ class AuthController { * @returns {Promise|*|PromiseLike|Promise} */ getCurrentUser (options = {}) { - return this.kuzzle.query({ - controller: 'auth', + return this.query({ action: 'getCurrentUser' }, options) .then(response => new User(this.kuzzle, response.result._id, response.result._source, response.result._meta)); @@ -104,9 +97,8 @@ class AuthController { * @returns {Promise|*|PromiseLike|Promise} */ getMyCredentials(strategy, options = {}) { - return this.kuzzle.query({ + return this.query({ strategy, - controller: 'auth', action: 'getMyCredentials' }, options) .then(response => response.result); @@ -119,8 +111,7 @@ class AuthController { * @returns {Promise|*|PromiseLike|Promise} */ getMyRights (options = {}) { - return this.kuzzle.query({ - controller: 'auth', + return this.query({ action: 'getMyRights' }, options) .then(response => response.result.hits); @@ -133,8 +124,7 @@ class AuthController { * @returns {Promise|*|PromiseLike|Promise} */ getStrategies (options = {}) { - return this.kuzzle.query({ - controller: 'auth', + return this.query({ action: 'getStrategies' }, options) .then(response => response.result); @@ -159,11 +149,10 @@ class AuthController { strategy, expiresIn, body: credentials, - controller: 'auth', action: 'login' }; - return this.kuzzle.query(request, {queuable: false}) + return this.query(request, {queuable: false}) .then(response => { try { this.kuzzle.jwt = response.result.jwt; @@ -186,8 +175,7 @@ class AuthController { * @returns {Promise|*|PromiseLike|Promise} */ logout () { - return this.kuzzle.query({ - controller: 'auth', + return this.query({ action: 'logout' }, {queuable: false}) .then(() => { @@ -204,10 +192,9 @@ class AuthController { * @returns {Promise|*|PromiseLike|Promise} */ updateMyCredentials (strategy, credentials, options = {}) { - return this.kuzzle.query({ + return this.query({ strategy, body: credentials, - controller: 'auth', action: 'updateMyCredentials' }, options) .then(response => response.result); @@ -221,9 +208,8 @@ class AuthController { * @returns {Promise|*|PromiseLike|Promise} */ updateSelf (body, options = {}) { - return this.kuzzle.query({ + return this.query({ body, - controller: 'auth', action: 'updateSelf' }, options) .then(response => new User(this.kuzzle, response.result._id, response.result._source, response.result._meta)); @@ -238,10 +224,9 @@ class AuthController { * @returns {Promise|*|PromiseLike|Promise} */ validateMyCredentials (strategy, credentials, options = {}) { - return this.kuzzle.query({ + return this.query({ strategy, body: credentials, - controller: 'auth', action: 'validateMyCredentials' }, options) .then(response => response.result); @@ -255,12 +240,11 @@ class AuthController { */ refreshToken(options = {}) { const query = { - controller: 'auth', action: 'refreshToken', expiresIn: options.expiresIn }; - return this.kuzzle.query(query, options) + return this.query(query, options) .then(response => { this.kuzzle.jwt = response.result.jwt; diff --git a/src/controllers/base.js b/src/controllers/base.js index de61e8421..33ca42450 100644 --- a/src/controllers/base.js +++ b/src/controllers/base.js @@ -1,20 +1,20 @@ class BaseController { /** + * @param {Kuzzle} kuzzle - Kuzzle SDK object. * @param {string} name - Controller full name for API request. - * @param {string} accessor - Controller accessor name on Kuzzle object. */ - constructor (name, accessor) { - this.name = name; - this.accessor = accessor; + constructor (kuzzle, name) { + this._kuzzle = kuzzle; + this._name = name; } get kuzzle () { return this._kuzzle; } - set kuzzle (kuzzle) { - this._kuzzle = kuzzle; + get name () { + return this._name; } /** @@ -25,7 +25,7 @@ class BaseController { query (request = {}, options = {}) { request.controller = request.controller || this.name; - return this.kuzzle.query(request, options); + return this._kuzzle.query(request, options); } } diff --git a/src/controllers/bulk.js b/src/controllers/bulk.js index d6a647b5f..43cf6b390 100644 --- a/src/controllers/bulk.js +++ b/src/controllers/bulk.js @@ -1,15 +1,12 @@ -class BulkController { - constructor (kuzzle) { - this._kuzzle = kuzzle; - } +const BaseController = require('./base'); - get kuzzle () { - return this._kuzzle; +class BulkController extends BaseController { + constructor (kuzzle) { + super(kuzzle, 'bulk'); } import (data, options) { - return this.kuzzle.query({ - controller: 'bulk', + return this.query({ action: 'import', body: { bulkData: data diff --git a/src/controllers/collection.js b/src/controllers/collection.js index 7bbf315a1..6d4e6332a 100644 --- a/src/controllers/collection.js +++ b/src/controllers/collection.js @@ -1,17 +1,14 @@ const + BaseController = require('./base'), SpecificationsSearchResult = require('./searchResult/specifications'); -class CollectionController { +class CollectionController extends BaseController { /** * @param {Kuzzle} kuzzle */ constructor (kuzzle) { - this._kuzzle = kuzzle; - } - - get kuzzle () { - return this._kuzzle; + super(kuzzle, 'collection'); } create (index, collection, body = {}, options = {}) { @@ -22,11 +19,10 @@ class CollectionController { throw new Error('Kuzzle.collection.create: collection is required'); } - return this.kuzzle.query({ + return this.query({ index, collection, body, - controller: 'collection', action: 'create' }, options) .then(response => response.result); @@ -40,10 +36,9 @@ class CollectionController { throw new Error('Kuzzle.collection.deleteSpecifications: collection is required'); } - return this.kuzzle.query({ + return this.query({ index, collection, - controller: 'collection', action: 'deleteSpecifications' }, options) .then(response => response.result); @@ -57,10 +52,9 @@ class CollectionController { throw new Error('Kuzzle.collection.exists: collection is required'); } - return this.kuzzle.query({ + return this.query({ index, collection, - controller: 'collection', action: 'exists' }, options) .then(response => response.result); @@ -74,10 +68,9 @@ class CollectionController { throw new Error('Kuzzle.collection.getMapping: collection is required'); } - return this.kuzzle.query({ + return this.query({ index, collection, - controller: 'collection', action: 'getMapping' }, options) .then(response => response.result); @@ -91,10 +84,9 @@ class CollectionController { throw new Error('Kuzzle.collection.getSpecifications: collection is required'); } - return this.kuzzle.query({ + return this.query({ index, collection, - controller: 'collection', action: 'getSpecifications' }, options) .then(response => response.result); @@ -107,7 +99,6 @@ class CollectionController { const request = { index, - controller: 'collection', action: 'list', from: options.from, size: options.size @@ -115,14 +106,13 @@ class CollectionController { delete options.from; delete options.size; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } searchSpecifications (body = {}, options = {}) { const request = { body, - controller: 'collection', action: 'searchSpecifications' }; for (const opt of ['from', 'size', 'scroll']) { @@ -130,7 +120,7 @@ class CollectionController { delete options[opt]; } - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new SpecificationsSearchResult(this.kuzzle, request, options, response.result)); } @@ -142,10 +132,9 @@ class CollectionController { throw new Error('Kuzzle.collection.truncate: collection is required'); } - return this.kuzzle.query({ + return this.query({ index, collection, - controller: 'collection', action: 'truncate', refresh: options.refresh }, options) @@ -160,11 +149,10 @@ class CollectionController { throw new Error('Kuzzle.collection.updateMapping: collection is required'); } - return this.kuzzle.query({ + return this.query({ index, collection, body, - controller: 'collection', action: 'updateMapping' }, options) .then(response => response.result); @@ -187,9 +175,8 @@ class CollectionController { } }; - return this.kuzzle.query({ + return this.query({ body, - controller: 'collection', action: 'updateSpecifications' }, options) .then(response => response.result[index][collection]); @@ -212,9 +199,8 @@ class CollectionController { } }; - return this.kuzzle.query({ + return this.query({ body, - controller: 'collection', action: 'validateSpecifications' }, options) .then(response => response.result); diff --git a/src/controllers/document.js b/src/controllers/document.js index ecdeabf7c..be77c1543 100644 --- a/src/controllers/document.js +++ b/src/controllers/document.js @@ -1,17 +1,14 @@ const + BaseController = require('./base'), DocumentSearchResult = require('./searchResult/document'); -class DocumentController { +class DocumentController extends BaseController { /** * @param {Kuzzle} kuzzle */ constructor (kuzzle) { - this._kuzzle = kuzzle; - } - - get kuzzle () { - return this._kuzzle; + super(kuzzle, 'document'); } count (index, collection, body, options = {}) { @@ -26,13 +23,12 @@ class DocumentController { index, collection, body, - controller: 'document', action: 'count', includeTrash: options.includeTrash }; delete options.includeTrash; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result.count); } @@ -52,13 +48,12 @@ class DocumentController { collection, _id, body: document, - controller: 'document', action: 'create', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -81,13 +76,12 @@ class DocumentController { collection, _id, body, - controller: 'document', action: 'createOrReplace', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -106,13 +100,12 @@ class DocumentController { index, collection, _id, - controller: 'document', action: 'delete', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result._id); } @@ -128,13 +121,12 @@ class DocumentController { index, collection, body, - controller: 'document', action: 'deleteByQuery', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result.ids); } @@ -153,11 +145,10 @@ class DocumentController { index, collection, _id, - controller: 'document', action: 'exists' }; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -176,13 +167,12 @@ class DocumentController { index, collection, _id, - controller: 'document', action: 'get', includeTrash: options.includeTrash }; delete options.includeTrash; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -201,13 +191,12 @@ class DocumentController { index, collection, body: {documents}, - controller: 'document', action: 'mCreate', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -226,13 +215,12 @@ class DocumentController { index, collection, body: {documents}, - controller: 'document', action: 'mCreateOrReplace', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -251,13 +239,12 @@ class DocumentController { index, collection, body: {ids}, - controller: 'document', action: 'mDelete', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -276,13 +263,12 @@ class DocumentController { index, collection, body: {ids}, - controller: 'document', action: 'mGet', includeTrash: options.includeTrash }; delete options.includeTrash; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -301,12 +287,11 @@ class DocumentController { index, collection, body: {documents}, - controller: 'document', action: 'mReplace', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -325,13 +310,12 @@ class DocumentController { index, collection, body: {documents}, - controller: 'document', action: 'mUpdate', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -354,13 +338,12 @@ class DocumentController { collection, _id, body, - controller: 'document', action: 'replace', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -376,7 +359,6 @@ class DocumentController { index, collection, body, - controller: 'document', action: 'search', }; for (const opt of ['from', 'size', 'scroll', 'includeTrash']) { @@ -384,7 +366,7 @@ class DocumentController { delete options[opt]; } - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new DocumentSearchResult(this.kuzzle, request, options, response.result)); } @@ -407,7 +389,6 @@ class DocumentController { collection, _id, body, - controller: 'document', action: 'update', refresh: options.refresh, retryOnConflict: options.retryOnConflict @@ -415,7 +396,7 @@ class DocumentController { delete options.refresh; delete options.retryOnConflict; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -430,11 +411,10 @@ class DocumentController { throw new Error('Kuzzle.document.validate: body is required'); } - return this.kuzzle.query({ + return this.query({ index, collection, body, - controller: 'document', action: 'validate' }, options) .then(response => response.result); diff --git a/src/controllers/index.js b/src/controllers/index.js index afadeaa78..e20e53778 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -1,14 +1,12 @@ -class IndexController { +const BaseController = require('./base'); + +class IndexController extends BaseController { /** * @param {Kuzzle} kuzzle */ constructor (kuzzle) { - this._kuzzle = kuzzle; - } - - get kuzzle () { - return this._kuzzle; + super(kuzzle, 'index'); } create (index, options) { @@ -16,9 +14,8 @@ class IndexController { throw new Error('Kuzzle.index.create: index is required'); } - return this.kuzzle.query({ + return this.query({ index, - controller: 'index', action : 'create' }, options) .then(response => response.result); @@ -29,9 +26,8 @@ class IndexController { throw new Error('Kuzzle.index.delete: index is required'); } - return this.kuzzle.query({ + return this.query({ index, - controller: 'index', action : 'delete' }, options) .then(response => response.result.acknowledged); @@ -42,9 +38,8 @@ class IndexController { throw new Error('Kuzzle.index.exists: index is required'); } - return this.kuzzle.query({ + return this.query({ index, - controller: 'index', action : 'exists' }, options) .then(response => response.result); @@ -55,17 +50,15 @@ class IndexController { throw new Error('Kuzzle.index.getAutoRefresh: index is required'); } - return this.kuzzle.query({ + return this.query({ index, - controller: 'index', action: 'getAutoRefresh' }, options) .then(response => response.result); } list (options) { - return this.kuzzle.query({ - controller: 'index', + return this.query({ action: 'list' }, options) .then(response => response.result.indexes); @@ -76,8 +69,7 @@ class IndexController { throw new Error('Kuzzle.index.mDelete: indexes must be an array'); } - return this.kuzzle.query({ - controller: 'index', + return this.query({ action: 'mDelete', body: { indexes @@ -91,17 +83,15 @@ class IndexController { throw new Error('Kuzzle.index.refresh: index is required'); } - return this.kuzzle.query({ + return this.query({ index, - controller: 'index', action: 'refresh' }, options) .then(response => response.result._shards); } refreshInternal (options) { - return this.kuzzle.query({ - controller: 'index', + return this.query({ action: 'refreshInternal' }, options) .then(response => response.result.acknowledged); @@ -116,9 +106,8 @@ class IndexController { throw new Error('Kuzzle.index.setAutoRefresh: autoRefresh must be a boolean'); } - return this.kuzzle.query({ + return this.query({ index, - controller: 'index', action: 'setAutoRefresh', body: { autoRefresh diff --git a/src/controllers/memoryStorage.js b/src/controllers/memoryStorage.js index 2904912ef..cf05bf64d 100644 --- a/src/controllers/memoryStorage.js +++ b/src/controllers/memoryStorage.js @@ -1,3 +1,5 @@ +const BaseControler = require('./base'); + // Parameter mutualization const getId = {getter: true, required: ['_id']}, @@ -187,14 +189,10 @@ const * @param {object} kuzzle - Kuzzle instance to inherit from * @constructor */ -class MemoryStorageController { +class MemoryStorageController extends BaseControler { constructor (kuzzle) { - this._kuzzle = kuzzle; - } - - get kuzzle () { - return this._kuzzle; + super(kuzzle, 'ms'); } } @@ -206,8 +204,7 @@ for (const action of Object.keys(commands)) { const command = commands[action], request = { - action, - controller: 'ms' + action }, options = {}; @@ -254,7 +251,7 @@ for (const action of Object.keys(commands)) { command.opts(request, options); } - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => { if (command.mapResults) { return command.mapResults(response.result); diff --git a/src/controllers/realtime/index.js b/src/controllers/realtime/index.js index 42a23af87..9913fc583 100644 --- a/src/controllers/realtime/index.js +++ b/src/controllers/realtime/index.js @@ -1,21 +1,19 @@ -const Room = require('./room'); +const + BaseController = require('../base'), + Room = require('./room'); const expirationThrottleDelay = 1000; -class RealTimeController { +class RealTimeController extends BaseController { /** * @param {Kuzzle} kuzzle */ constructor (kuzzle) { - this._kuzzle = kuzzle; - this.lastExpirationTimestamp = 0; + super(kuzzle, 'realtime'); - this.subscriptions = { - }; - } + this.lastExpirationTimestamp = 0; - get kuzzle () { - return this._kuzzle; + this.subscriptions = {}; } count (roomId, options = {}) { @@ -23,8 +21,7 @@ class RealTimeController { throw new Error('Kuzzle.realtime.count: roomId is required'); } - return this.kuzzle.query({ - controller: 'realtime', + return this.query({ action: 'count', body: {roomId} }, options) @@ -46,11 +43,10 @@ class RealTimeController { index, collection, body: message, - controller: 'realtime', action: 'publish' }; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result.published); } @@ -96,8 +92,7 @@ class RealTimeController { } delete this.subscriptions[roomId]; - return this.kuzzle.query({ - controller: 'realtime', + return this.query({ action: 'unsubscribe', body: {roomId} }, options) diff --git a/src/controllers/security/index.js b/src/controllers/security/index.js index 555cea34f..0acb7c0ea 100644 --- a/src/controllers/security/index.js +++ b/src/controllers/security/index.js @@ -1,4 +1,5 @@ const + BaseController = require('../base'), Role = require('./role'), RoleSearchResult = require('../searchResult/role'), Profile = require('./profile'), @@ -6,16 +7,12 @@ const User = require('./user'), UserSearchResult = require('../searchResult/user'); -class SecurityController { +class SecurityController extends BaseController { /** * @param {Kuzzle} kuzzle */ constructor (kuzzle) { - this._kuzzle = kuzzle; - } - - get kuzzle () { - return this._kuzzle; + super(kuzzle, 'security'); } createCredentials (strategy, _id, body, options = {}) { @@ -29,11 +26,10 @@ class SecurityController { throw new Error('Kuzzle.security.createCredentials: body is required'); } - return this.kuzzle.query({ + return this.query({ _id, strategy, body, - controller: 'security', action: 'createCredentials' }, options) .then(response => response.result); @@ -50,13 +46,12 @@ class SecurityController { const request = { _id, body, - controller: 'security', action: 'createFirstAdmin', reset: options.reset }; delete options.reset; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new User(this.kuzzle, response.result._id, response.result._source, response.result._meta)); } @@ -71,13 +66,12 @@ class SecurityController { const request = { _id, body, - controller: 'security', action: 'createOrReplaceProfile', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new Profile(this.kuzzle, response.result._id, response.result._source.policies)); } @@ -92,13 +86,12 @@ class SecurityController { const request = { _id, body, - controller: 'security', action: 'createOrReplaceRole', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new Role(this.kuzzle, response.result._id, response.result._source.controllers)); } @@ -113,13 +106,12 @@ class SecurityController { const request = { _id, body, - controller: 'security', action: 'createProfile', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new Profile(this.kuzzle, response.result._id, response.result._source.policies)); } @@ -134,13 +126,12 @@ class SecurityController { const request = { _id, body, - controller: 'security', action: 'createRestrictedUser', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new User(this.kuzzle, response.result._id, response.result._source, response.result._meta)); } @@ -155,13 +146,12 @@ class SecurityController { const request = { _id, body, - controller: 'security', action: 'createRole', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new Role(this.kuzzle, response.result._id, response.result._source.controllers)); } @@ -182,13 +172,12 @@ class SecurityController { const request = { _id, body, - controller: 'security', action: 'createUser', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new User(this.kuzzle, response.result._id, response.result._source, response.result._meta)); } @@ -200,10 +189,9 @@ class SecurityController { throw new Error('Kuzzle.security.deleteCredentials: strategy is required'); } - return this.kuzzle.query({ + return this.query({ strategy, _id, - controller: 'security', action: 'deleteCredentials' }, options) .then(response => response.result); @@ -214,9 +202,8 @@ class SecurityController { throw new Error('Kuzzle.security.deleteProfile: _id is required'); } - return this.kuzzle.query({ + return this.query({ _id, - controller: 'security', action: 'deleteProfile' }, options) .then(response => response.result); @@ -227,9 +214,8 @@ class SecurityController { throw new Error('Kuzzle.security.deleteRole: _id is required'); } - return this.kuzzle.query({ + return this.query({ _id, - controller: 'security', action: 'deleteRole' }, options) .then(response => response.result); @@ -240,17 +226,15 @@ class SecurityController { throw new Error('Kuzzle.security.deleteUser: _id is required'); } - return this.kuzzle.query({ + return this.query({ _id, - controller: 'security', action: 'deleteUser' }, options) .then(response => response.result); } getAllCredentialFields (options = {}) { - return this.kuzzle.query({ - controller: 'security', + return this.query({ action: 'getAllCredentialFields' }, options) .then(response => response.result); @@ -261,9 +245,8 @@ class SecurityController { throw new Error('Kuzzle.security.getCredentialFields: strategy is required'); } - return this.kuzzle.query({ + return this.query({ strategy, - controller: 'security', action: 'getCredentialFields' }, options) .then(response => response.result); @@ -277,10 +260,9 @@ class SecurityController { throw new Error('Kuzzle.security.getCredentials: _id is required'); } - return this.kuzzle.query({ + return this.query({ strategy, _id, - controller: 'security', action: 'getCredentials' }, options) .then(response => response.result); @@ -294,10 +276,9 @@ class SecurityController { throw new Error('Kuzzle.security.getCredentialsById: _id is required'); } - return this.kuzzle.query({ + return this.query({ strategy, _id, - controller: 'security', action: 'getCredentialsById' }, options) .then(response => response.result); @@ -308,17 +289,15 @@ class SecurityController { throw new Error('Kuzzle.security.getProfile: _id is required'); } - return this.kuzzle.query({ + return this.query({ _id, - controller: 'security', action: 'getProfile' }, options) .then(response => new Profile(this.kuzzle, response.result._id, response.result._source.policies)); } getProfileMapping (options = {}) { - return this.kuzzle.query({ - controller: 'security', + return this.query({ action: 'getProfileMapping' }, options) .then(response => response.result); @@ -329,9 +308,8 @@ class SecurityController { throw new Error('Kuzzle.security.getProfileRights: _id is required'); } - return this.kuzzle.query({ + return this.query({ _id, - controller: 'security', action: 'getProfileRights' }, options) .then(response => response.result.hits); @@ -342,17 +320,15 @@ class SecurityController { throw new Error('Kuzzle.security.getRole: _id is required'); } - return this.kuzzle.query({ + return this.query({ _id, - controller: 'security', action: 'getRole' }, options) .then(response => new Role(this.kuzzle, response.result._id, response.result._source.controllers)); } getRoleMapping (options = {}) { - return this.kuzzle.query({ - controller: 'security', + return this.query({ action: 'getRoleMapping' }, options) .then(response => response.result); @@ -363,17 +339,15 @@ class SecurityController { throw new Error('Kuzzle.security.getUser: _id is required'); } - return this.kuzzle.query({ + return this.query({ _id, - controller: 'security', action: 'getUser' }, options) .then(response => new User(this.kuzzle, response.result._id, response.result._source, response.result._meta)); } getUserMapping (options = {}) { - return this.kuzzle.query({ - controller: 'security', + return this.query({ action: 'getUserMapping' }, options) .then(response => response.result); @@ -384,9 +358,8 @@ class SecurityController { throw new Error('Kuzzle.security.getUserRights: _id is required'); } - return this.kuzzle.query({ + return this.query({ _id, - controller: 'security', action: 'getUserRights' }, options) .then(response => response.result.hits); @@ -400,10 +373,9 @@ class SecurityController { throw new Error('Kuzzle.security.hasCredentials: _id is required'); } - return this.kuzzle.query({ + return this.query({ strategy, _id, - controller: 'security', action: 'hasCredentials' }, options) .then(response => response.result); @@ -415,14 +387,13 @@ class SecurityController { } const request = { - controller: 'security', action: 'mDeleteProfiles', body: {ids}, refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -432,14 +403,13 @@ class SecurityController { } const request = { - controller: 'security', action: 'mDeleteRoles', body: {ids}, refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -449,14 +419,13 @@ class SecurityController { } const request = { - controller: 'security', action: 'mDeleteUsers', body: {ids}, refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => response.result); } @@ -465,8 +434,7 @@ class SecurityController { throw new Error('Kuzzle.security.mGetProfiles: ids must be an array'); } - return this.kuzzle.query({ - controller: 'security', + return this.query({ action: 'mGetProfiles', body: {ids} }, options) @@ -478,8 +446,7 @@ class SecurityController { throw new Error('Kuzzle.security.mGetRoles: ids must be an array'); } - return this.kuzzle.query({ - controller: 'security', + return this.query({ action: 'mGetRoles', body: {ids} }, options) @@ -497,20 +464,18 @@ class SecurityController { const request = { _id, body, - controller: 'security', action: 'replaceUser', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new User(this.kuzzle, response.result._id, response.result._source, response.result._meta)); } searchProfiles (body, options= {}) { const request = { body, - controller: 'security', action: 'searchProfiles' }; for (const opt of ['from', 'size', 'scroll']) { @@ -518,14 +483,13 @@ class SecurityController { delete options[opt]; } - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new ProfileSearchResult(this.kuzzle, request, options, response.result)); } searchRoles (body, options = {}) { const request = { body, - controller: 'security', action: 'searchRoles' }; for (const opt of ['from', 'size']) { @@ -533,14 +497,13 @@ class SecurityController { delete options[opt]; } - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new RoleSearchResult(this.kuzzle, request, options, response.result)); } searchUsers (body, options = {}) { const request = { body, - controller: 'security', action: 'searchUsers' }; for (const opt of ['from', 'size', 'scroll']) { @@ -548,7 +511,7 @@ class SecurityController { delete options[opt]; } - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new UserSearchResult(this.kuzzle, request, options, response.result)); } @@ -563,11 +526,10 @@ class SecurityController { throw new Error('Kuzzle.security.updateCredentials: body is required'); } - return this.kuzzle.query({ + return this.query({ strategy, _id, body, - controller: 'security', action: 'updateCredentials' }, options) .then(response => response.result); @@ -584,20 +546,18 @@ class SecurityController { const request = { _id, body, - controller: 'security', action: 'updateProfile', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new Profile(this.kuzzle, response.result._id, response.result._source.policies)); } updateProfileMapping (body, options = {}) { - return this.kuzzle.query({ + return this.query({ body, - controller: 'security', action: 'updateProfileMapping' }, options) .then(response => response.result); @@ -614,20 +574,18 @@ class SecurityController { const request = { _id, body, - controller: 'security', action: 'updateRole', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new Role(this.kuzzle, response.result._id, response.result._source.controllers)); } updateRoleMapping (body, options = {}) { - return this.kuzzle.query({ + return this.query({ body, - controller: 'security', action: 'updateRoleMapping' }, options) .then(response => response.result); @@ -644,20 +602,18 @@ class SecurityController { const request = { _id, body, - controller: 'security', action: 'updateUser', refresh: options.refresh }; delete options.refresh; - return this.kuzzle.query(request, options) + return this.query(request, options) .then(response => new User(this.kuzzle, response.result._id, response.result._source, response.result._meta)); } updateUserMapping (body, options = {}) { - return this.kuzzle.query({ + return this.query({ body, - controller: 'security', action: 'updateUserMapping' }, options) .then(response => response.result); @@ -674,11 +630,10 @@ class SecurityController { throw new Error('Kuzzle.security.validateCredentials: body is required'); } - return this.kuzzle.query({ + return this.query({ _id, strategy, body, - controller: 'security', action: 'validateCredentials' }, options) .then(response => response.result); diff --git a/src/controllers/server.js b/src/controllers/server.js index 43c56cc59..d90961e75 100644 --- a/src/controllers/server.js +++ b/src/controllers/server.js @@ -1,18 +1,16 @@ +const BaseControler = require('./base'); + /** * @class ServerController * @property {Kuzzle} kuzzle - The Kuzzle SDK Instance */ -class ServerController { +class ServerController extends BaseControler { /** * @param {Kuzzle} kuzzle - The Kuzzle SDK Instance */ constructor (kuzzle) { - this._kuzzle = kuzzle; - } - - get kuzzle () { - return this._kuzzle; + super(kuzzle, 'server'); } /** @@ -22,8 +20,7 @@ class ServerController { * @returns {Promise} */ adminExists (options) { - return this.kuzzle.query({ - controller: 'server', + return this.query({ action: 'adminExists' }, options) .then(response => { @@ -45,8 +42,7 @@ class ServerController { * @returns {Promise} */ getAllStats (options) { - return this.kuzzle.query({ - controller: 'server', + return this.query({ action: 'getAllStats' }, options) .then(response => response.result); @@ -59,8 +55,7 @@ class ServerController { * @returns {Promise} */ getConfig (options) { - return this.kuzzle.query({ - controller: 'server', + return this.query({ action: 'getConfig' }, options) .then(response => response.result); @@ -73,8 +68,7 @@ class ServerController { * @returns {Promise} */ getLastStats (options) { - return this.kuzzle.query({ - controller: 'server', + return this.query({ action: 'getLastStats' }, options) .then(response => response.result); @@ -89,8 +83,7 @@ class ServerController { * @returns {Promise} */ getStats(startTime, stopTime, options) { - return this.kuzzle.query({ - controller: 'server', + return this.query({ action: 'getStats', startTime, stopTime @@ -105,8 +98,7 @@ class ServerController { * @returns {Promise} */ info (options) { - return this.kuzzle.query({ - controller: 'server', + return this.query({ action: 'info' }, options) .then(response => response.result); @@ -119,8 +111,7 @@ class ServerController { * @returns {Promise} */ now (options) { - return this.kuzzle.query({ - controller: 'server', + return this.query({ action: 'now' }, options) .then(response => { diff --git a/test/controllers/server.test.js b/test/controllers/server.test.js index 47c807369..905b7a1fb 100644 --- a/test/controllers/server.test.js +++ b/test/controllers/server.test.js @@ -25,7 +25,7 @@ describe('Server Controller', () => { return kuzzle.server.adminExists() .then(res => { should(kuzzle.query).be.calledOnce(); - should(kuzzle.query).be.calledWith(expectedQuery, undefined); + should(kuzzle.query).be.calledWith(expectedQuery, {}); should(res).be.a.Boolean().and.be.true(); kuzzle.query.resetHistory(); @@ -106,7 +106,7 @@ describe('Server Controller', () => { return kuzzle.server.getAllStats() .then(res => { should(kuzzle.query).be.calledOnce(); - should(kuzzle.query).be.calledWith(expectedQuery, undefined); + should(kuzzle.query).be.calledWith(expectedQuery, {}); should(res).match(result); }) .then(() => { @@ -159,7 +159,7 @@ describe('Server Controller', () => { return kuzzle.server.getConfig() .then(res => { should(kuzzle.query).be.calledOnce(); - should(kuzzle.query).be.calledWith(expectedQuery, undefined); + should(kuzzle.query).be.calledWith(expectedQuery, {}); should(res).match(result); }) .then(() => { @@ -219,7 +219,7 @@ describe('Server Controller', () => { return kuzzle.server.getLastStats() .then(res => { should(kuzzle.query).be.calledOnce(); - should(kuzzle.query).be.calledWith(expectedQuery, undefined); + should(kuzzle.query).be.calledWith(expectedQuery, {}); should(res).match(result); }) .then(() => { @@ -280,7 +280,7 @@ describe('Server Controller', () => { return kuzzle.server.getStats(1234, 9876) .then(res => { should(kuzzle.query).be.calledOnce(); - should(kuzzle.query).be.calledWith(expectedQuery, undefined); + should(kuzzle.query).be.calledWith(expectedQuery, {}); should(res).match(result); }) .then(() => { @@ -300,7 +300,7 @@ describe('Server Controller', () => { should(kuzzle.query).be.calledOnce(); should(kuzzle.query).be.calledWith( {controller: 'server', action: 'getStats', startTime: 1234, stopTime: null}, - undefined + {} ); should(res).match(result); }) @@ -312,7 +312,7 @@ describe('Server Controller', () => { should(kuzzle.query).be.calledOnce(); should(kuzzle.query).be.calledWith( {controller: 'server', action: 'getStats', startTime: null, stopTime: 9876}, - undefined + {} ); should(res).match(result); }) @@ -324,7 +324,7 @@ describe('Server Controller', () => { should(kuzzle.query).be.calledOnce(); should(kuzzle.query).be.calledWith( {controller: 'server', action: 'getStats', startTime: null, stopTime: null}, - undefined + {} ); should(res).match(result); }); @@ -379,7 +379,7 @@ describe('Server Controller', () => { return kuzzle.server.info() .then(res => { should(kuzzle.query).be.calledOnce(); - should(kuzzle.query).be.calledWith(expectedQuery, undefined); + should(kuzzle.query).be.calledWith(expectedQuery, {}); should(res).match(result); }) .then(() => { @@ -413,7 +413,7 @@ describe('Server Controller', () => { return kuzzle.server.now() .then(res => { should(kuzzle.query).be.calledOnce(); - should(kuzzle.query).be.calledWith(expectedQuery, undefined); + should(kuzzle.query).be.calledWith(expectedQuery, {}); should(res).be.exactly(12345); }) .then(() => { diff --git a/test/kuzzle/useController.test.js b/test/kuzzle/useController.test.js index 80192405d..43a0c6371 100644 --- a/test/kuzzle/useController.test.js +++ b/test/kuzzle/useController.test.js @@ -6,8 +6,8 @@ const Kuzzle = require('../../src/Kuzzle'); class CustomController extends BaseController { - constructor (accessor) { - super('custom-plugin/custom', accessor); + constructor (kuzzle) { + super(kuzzle, 'custom-plugin/custom'); } sayHello (message) { @@ -23,16 +23,27 @@ class CustomController extends BaseController { } } -class WrongController { - constructor (accessor) { - this.accessor = accessor; +class NotInheritingController { + constructor (kuzzle) { + this._kuzzle = kuzzle; + } +} + +class UnamedController extends BaseController { + constructor (kuzzle) { + super(kuzzle); + } +} + +class WrongConstructorController extends BaseController { + constructor (kuzzle) { + super({}, 'wrongConstructor', kuzzle); } } describe('Kuzzle custom controllers management', () => { describe('#useController', () => { let - customController, response, kuzzle; @@ -42,26 +53,25 @@ describe('Kuzzle custom controllers management', () => { }; const protocol = new ProtocolMock('somewhere'); - customController = new CustomController('custom'); kuzzle = new Kuzzle(protocol); kuzzle.protocol.query.resolves(response); }); it('should add the controller to the controllers list', () => { - kuzzle.useController(customController); + kuzzle.useController(CustomController, 'custom'); - should(kuzzle.custom).be.eql(customController); + should(kuzzle.custom).be.instanceOf(CustomController); }); it('should set the kuzzle object in the controller', () => { - kuzzle.useController(customController); + kuzzle.useController(CustomController, 'custom'); - should(customController.kuzzle).be.eql(kuzzle); + should(kuzzle.custom.kuzzle).be.eql(kuzzle); }); it('should use the controller name by default for query()', () => { - kuzzle.useController(customController); + kuzzle.useController(CustomController, 'custom'); return kuzzle.custom.sayHello('Wake up, and smell the ashes') .then(() => { @@ -73,40 +83,34 @@ describe('Kuzzle custom controllers management', () => { }); it('should throw if the controller does not inherits from BaseController', () => { - const wrongController = new WrongController('wrong'); - wrongController.name = 'wrong-plugin/wrong'; - - should(function () { - kuzzle.useController(wrongController); + should(() => { + kuzzle.useController(NotInheritingController, 'wrong'); }).throw('Controllers must inherits from the BaseController class.'); }); it('should throw if the controller does not have a name', () => { - customController = new CustomController('wrong'); - customController.name = ''; - - should(function () { - kuzzle.useController(customController); + should(() => { + kuzzle.useController(UnamedController, 'unamed'); }).throw('Controllers must have a name.'); }); - it('should throw if the controller does not have an accessor', () => { - customController = new CustomController(''); + it('should throw if the controller does not call the parent with the Kuzzle sdk instance', () => { + should(() => { + kuzzle.useController(WrongConstructorController, 'unamed'); + }).throw('You must pass the Kuzzle SDK instance to the parent constructor.'); + }); - should(function () { - kuzzle.useController(customController); - }).throw('Controllers must have an accessor.'); + it('should throw if the controller does not have an accessor', () => { + should(() => { + kuzzle.useController(CustomController); + }).throw('You must provide a valid accessor.'); }); it('should throw if the accessor is already taken', () => { - const - customController1 = new CustomController('custom'), - customController2 = new CustomController('custom'); - - kuzzle.useController(customController1); + kuzzle.useController(CustomController, 'custom'); - should(function () { - kuzzle.useController(customController2); + should(() => { + kuzzle.useController(CustomController, 'custom'); }).throw('There is already a controller with the accessor \'custom\'. Please use another one.'); }); });