Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions src/Kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
54 changes: 19 additions & 35 deletions src/controllers/auth.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
const User = require('./security/user');
const
BaseController = require('./base'),
User = require('./security/user');

/**
* Auth controller
*
* @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');
}

/**
Expand All @@ -27,8 +25,7 @@ class AuthController {
* @return {Promise|*|PromiseLike<T>|Promise<T>}
*/
checkToken (token) {
return this.kuzzle.query({
controller: 'auth',
return this.query({
action: 'checkToken',
body: {token}
}, {queuable: false})
Expand All @@ -44,9 +41,8 @@ class AuthController {
* @returns {Promise|*|PromiseLike<T>|Promise<T>}
*/
createMyCredentials (strategy, credentials, options = {}) {
return this.kuzzle.query({
return this.query({
strategy,
controller: 'auth',
action: 'createMyCredentials',
body: credentials
}, options)
Expand All @@ -60,9 +56,8 @@ class AuthController {
* @returns {Promise|*|PromiseLike<T>|Promise<T>}
*/
credentialsExist (strategy, options = {}) {
return this.kuzzle.query({
return this.query({
strategy,
controller: 'auth',
action: 'credentialsExist'
}, options)
.then(response => response.result);
Expand All @@ -76,9 +71,8 @@ class AuthController {
* @returns {Promise|*|PromiseLike<T>|Promise<T>}
*/
deleteMyCredentials (strategy, options = {}) {
return this.kuzzle.query({
return this.query({
strategy,
controller: 'auth',
action: 'deleteMyCredentials'
}, options)
.then(response => response.result.acknowledged);
Expand All @@ -90,8 +84,7 @@ class AuthController {
* @returns {Promise|*|PromiseLike<T>|Promise<T>}
*/
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));
Expand All @@ -104,9 +97,8 @@ class AuthController {
* @returns {Promise|*|PromiseLike<T>|Promise<T>}
*/
getMyCredentials(strategy, options = {}) {
return this.kuzzle.query({
return this.query({
strategy,
controller: 'auth',
action: 'getMyCredentials'
}, options)
.then(response => response.result);
Expand All @@ -119,8 +111,7 @@ class AuthController {
* @returns {Promise|*|PromiseLike<T>|Promise<T>}
*/
getMyRights (options = {}) {
return this.kuzzle.query({
controller: 'auth',
return this.query({
action: 'getMyRights'
}, options)
.then(response => response.result.hits);
Expand All @@ -133,8 +124,7 @@ class AuthController {
* @returns {Promise|*|PromiseLike<T>|Promise<T>}
*/
getStrategies (options = {}) {
return this.kuzzle.query({
controller: 'auth',
return this.query({
action: 'getStrategies'
}, options)
.then(response => response.result);
Expand All @@ -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;
Expand All @@ -186,8 +175,7 @@ class AuthController {
* @returns {Promise|*|PromiseLike<T>|Promise<T>}
*/
logout () {
return this.kuzzle.query({
controller: 'auth',
return this.query({
action: 'logout'
}, {queuable: false})
.then(() => {
Expand All @@ -204,10 +192,9 @@ class AuthController {
* @returns {Promise|*|PromiseLike<T>|Promise<T>}
*/
updateMyCredentials (strategy, credentials, options = {}) {
return this.kuzzle.query({
return this.query({
strategy,
body: credentials,
controller: 'auth',
action: 'updateMyCredentials'
}, options)
.then(response => response.result);
Expand All @@ -221,9 +208,8 @@ class AuthController {
* @returns {Promise|*|PromiseLike<T>|Promise<T>}
*/
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));
Expand All @@ -238,10 +224,9 @@ class AuthController {
* @returns {Promise|*|PromiseLike<T>|Promise<T>}
*/
validateMyCredentials (strategy, credentials, options = {}) {
return this.kuzzle.query({
return this.query({
strategy,
body: credentials,
controller: 'auth',
action: 'validateMyCredentials'
}, options)
.then(response => response.result);
Expand All @@ -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;

Expand Down
14 changes: 7 additions & 7 deletions src/controllers/base.js
Original file line number Diff line number Diff line change
@@ -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;
}

/**
Expand All @@ -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);
}
}

Expand Down
13 changes: 5 additions & 8 deletions src/controllers/bulk.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading