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
49 changes: 29 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ Example:
```js
const {
Kuzzle,
Websocket,
WebSocket,
Http,
SocketIO
} = require('kuzzle-sdk');

const kuzzle = new Kuzzle(
new Websocket({ host: 'localhost', port: 7512 })
new WebSocket('localhost', { port: 7512 })
);

try {
Expand All @@ -56,22 +56,31 @@ This SDK can be used either in NodeJS or in a browser.
### NodeJS

```
npm install kuzzle-sdk --save
# Beta v6
npm install git://github.com/kuzzleio/sdk-javascript.git#6-beta --save
npm install git://github.com/kuzzleio/sdk-javascript.git#6-beta
```

### Browser

To run the SDK in the browser, you need to pick the [built beta v6 version available here](https://raw.githubusercontent.com/kuzzleio/sdk-javascript/tree/6-beta/dist/kuzzle.js). You can also build it yourself by cloning this repository and running `npm run build`. A `dist` directory will be created, containing a browser version of this SDK.
To run the SDK in the browser, you have to build it yourself by cloning this repository and running `npm run build`. A `dist` directory will be created, containing a browser version of this SDK.

```html
<script type="text/javascript" src="dist/kuzzle.js"></script>
```
The browser version is also available from CDN:

Then the Kuzzle SDK will be available under the `KuzzleSDK` variable:

```html
<script type="text/javascript" src="https://cdn.rawgit.com/kuzzleio/sdk-javascript/tree/6-beta/dist/kuzzle.js"></script>
<script src="dist/kuzzle.js"></script>
<script>
const kuzzle = new KuzzleSDK.Kuzzle(
new KuzzleSDK.WebSocket('localhost')
);
kuzzle.connect();
kuzzle.addListener('connected', () => {
console.log('Hello Kuzzle');
});
</script>
```

If you want to support older browser versions, you may load `socket.io` before Kuzzle, making the SDK compatible with browsers without websocket support:
Expand All @@ -98,27 +107,27 @@ const { Kuzzle } = require('kuzzle-sdk/dist/kuzzle.js')
import { Kuzzle } from 'kuzzle-sdk/dist/kuzzle.js'
```

## Protocols used
Currently, the SDK support 3 protocols: `Http`, `Websocket` and `SocketIO`.
## Available protocols
Currently, the SDK provides 3 protocols: `Http`, `WebSocket` and `SocketIO`.

WebSocket and Socket.IO protocols implement the whole Kuzzle API, while the **HTTP protocol does not implement realtime features** (rooms and subscriptions).
While Socket.IO offers better compatibility with older web browsers, our raw WebSocket implementation is about 20% faster.

#### NodeJS

We recommend to use the `websocket` protocol, but you can still use `http`, `socketio` or even a custom protocol if you want.
We recommend using the `WebSocket` protocol, but you can still use `Http`, `SocketIO` or even a custom protocol if you want.

#### Web Browsers

We also recommend to use the `webSocket` or `http` protocol, but some old browser may not support Websocket, so you have to implement a fallback to `socketio` in that case.
We also recommend to use the `WebSocket` or `Http` protocol, but some old browser may not support WebSocket, so you have to implement a fallback to `SocketIO` in that case.

```js
let kuzzle;

if ('WebSocket' in window && window.WebSocket.CLOSING === 2) {
kuzzle = new Kuzzle(new Websocket({ host: 'localhost' }));
kuzzle = new Kuzzle(new WebSocket('localhost'));
} else {
kuzzle = new Kuzzle(new SocketIO({ host: 'localhost' }));
kuzzle = new Kuzzle(new SocketIO('localhost'));
}
```

Expand Down Expand Up @@ -182,7 +191,7 @@ const kuzzle = new Kuzzle(protocol);

### Connection to Kuzzle

By default, the SDK is not connected to Kuzzle when it is instantiated. You must manually call the `kuzzle.connect()` method before using the SDK.
By default, the SDK is not connected to Kuzzle when it is instantiated. You must manually call the [kuzzle:connect](https://docs-v2.kuzzle.io/sdk-reference/js/6/kuzzle/connect/) method before using the SDK.

It is then possible to interact with the Kuzzle API through the SDK once the Promise returned by `kuzzle.connect()` has been resolved.

Expand All @@ -209,15 +218,15 @@ try {

### Match Kuzzle's API

The version 6 of this SDK involve a massive refactor of the SDK structure to match the [Kuzzle API](https://docs.kuzzle.io/api-documentation/connecting-to-kuzzle/).
The version 6 of this SDK involve a massive refactor of the SDK structure to match the [Kuzzle API](https://docs-v2.kuzzle.io/api/1/essentials/connecting-to-kuzzle/).

Each controller is accessible from the Kuzzle object. The controller's actions are named in the same way as in the API.

For example, for the `create` action of the `document` controller ([document#create](https://docs.kuzzle.io/api-documentation/controller-document/create)):
For example, for the `create` action of the `document` controller ([document:create](https://docs-v2.kuzzle.io/api/1/controller-document/create)):
```js
const options = { refresh: 'wait_for' };
const documentBody = { hello: 'world' };
kuzzle.document.create('my-index', 'my-collection', documentBody, 'my-uniq-id', options)
const document = { hello: 'world' };
kuzzle.document.create('my-index', 'my-collection', document, 'my-uniq-id', options)
```

The parameters of each method differ according to the parameters expected in the API.
Expand All @@ -229,7 +238,7 @@ This SDK also expose a low level `query` method to access the API even if the co

This method take the controller and action name with all parameters needed by the action (`body`, `_id`, etc.) and return the raw Kuzzle response. This is the method used internally for every controller action in this SDK.

Example with the [Admin](https://docs.kuzzle.io/api-documentation/controller-admin/) controller:
Example with the [Admin](https://docs-v2.kuzzle.io/api/1/controller-admin/reset-cache) controller:
```js
const query = {
controller: 'admin',
Expand Down Expand Up @@ -268,7 +277,7 @@ try {

All SDK methods return a promise resolving the `result` part of Kuzzle API responses. If an error occurs, the promise is rejected with an `Error` object embedding the `error` part of the API response.

For example, for the action `create` of the controller `collection` ([collection#create](https://docs.kuzzle.io/api-documentation/controller-collection/create)), the property `result` contains `{ "acknowledged": true} `. This is therefore what will be returned by the SDK method if successful.
For example, for the action `create` of the controller `collection` ([collection:create](https://docs-v2.kuzzle.io/api/1/controller-collection/create)), the property `result` contains `{ "acknowledged": true} `. This is therefore what will be returned by the SDK method if successful.

Any error must be caught either at the end of the Promise chain, or by using `async/await` and a `try...catch`.

Expand Down
4 changes: 2 additions & 2 deletions features/support/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const
{ setWorldConstructor } = require('cucumber'),
{
Kuzzle,
Websocket
WebSocket
} = require('../../index');

class World {
constructor () {
this.kuzzle = new Kuzzle(new Websocket({ host: 'localhost', port: 7512 }));
this.kuzzle = new Kuzzle(new WebSocket('localhost', { port: 7512 }));

this.index = null;
this.collection = null;
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const
Kuzzle = require('./src/Kuzzle'),
{
Http,
Websocket,
WebSocket,
SocketIO
} = require('./src/protocols'),
KuzzleAbstractProtocol = require('./src/protocols/abstract/common'),
Expand All @@ -18,7 +18,7 @@ if (typeof window !== 'undefined' && typeof BUILT === 'undefined') {
module.exports = {
Kuzzle,
Http,
Websocket,
WebSocket,
SocketIO,
KuzzleAbstractProtocol,
KuzzleEventEmitter
Expand Down
4 changes: 2 additions & 2 deletions src/protocols/abstract/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ let

class AbstractWrapper extends KuzzleEventEmitter {

constructor (options = {}) {
constructor (host, options = {}) {
super();

_host = options.host;
_host = host;
_port = typeof options.port === 'number' ? options.port : 7512;
_ssl = typeof options.sslConnection === 'boolean' ? options.sslConnection : false;

Expand Down
4 changes: 2 additions & 2 deletions src/protocols/abstract/realtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const

class RTWrapper extends KuzzleAbstractProtocol {

constructor (options = {}) {
super(options);
constructor (host, options = {}) {
super(host, options);

this._autoReconnect = typeof options.autoReconnect === 'boolean' ? options.autoReconnect : true;
this._reconnectionDelay = typeof options.reconnectionDelay === 'number' ? options.reconnectionDelay : 1000;
Expand Down
8 changes: 4 additions & 4 deletions src/protocols/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ const

class HttpWrapper extends KuzzleAbstractProtocol {

constructor(options = {}) {
super(options);
constructor(host, options = {}) {
super(host, options);

if (typeof this.host !== 'string' || this.host === '') {
throw new Error('options.host is required');
if (typeof host !== 'string' || host === '') {
throw new Error('host is required');
}

// Application-side HTTP route overrides:
Expand Down
4 changes: 2 additions & 2 deletions src/protocols/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const
Http = require('./http'),
Websocket = require('./websocket'),
WebSocket = require('./websocket'),
SocketIO = require('./socketio');

module.exports = {
Http,
Websocket,
WebSocket,
SocketIO
};
8 changes: 4 additions & 4 deletions src/protocols/socketio.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const

class SocketIO extends RTWrapper {

constructor(options = {}) {
super(options);
constructor(host, options = {}) {
super(host, options);

if (typeof this.host !== 'string' || this.host === '') {
throw new Error('options.host is required');
if (typeof host !== 'string' || host === '') {
throw new Error('host is required');
}

this.socket = null;
Expand Down
8 changes: 4 additions & 4 deletions src/protocols/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ let WebSocketClient;

class WSNode extends RTWrapper {

constructor(options = {}) {
super(options);
constructor(host, options = {}) {
super(host, options);

if (typeof this.host !== 'string' || this.host === '') {
throw new Error('options.host is required');
if (typeof host !== 'string' || host === '') {
throw new Error('host is required');
}

WebSocketClient = typeof WebSocket !== 'undefined' ? WebSocket : require('uws');
Expand Down
6 changes: 3 additions & 3 deletions test/kuzzle/connect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const
describe('Kuzzle connect', () => {

const protocols = {
somewhere: new ProtocolMock({host: 'somewhere'}),
somewhereagain: new ProtocolMock({host: 'somewhereagain'}),
nowhere: new ProtocolMock({host: 'nowhere'})
somewhere: new ProtocolMock('somewhere'),
somewhereagain: new ProtocolMock('somewhereagain'),
nowhere: new ProtocolMock('nowhere')
};

it('should return immediately if already connected', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/kuzzle/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const
ProtocolMock = require('../mocks/protocol.mock');

describe('Kuzzle constructor', () => {
const protocolMock = new ProtocolMock({host: 'somewhere'});
const protocolMock = new ProtocolMock('somewhere');

it('should throw an error if no protocol wrapper is provided', () => {
should(function () {
Expand Down
2 changes: 1 addition & 1 deletion test/kuzzle/getters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Kuzzle getters', () => {
let kuzzle;

beforeEach(() => {
const protocol = new ProtocolMock({host: 'somewhere'});
const protocol = new ProtocolMock('somewhere');
kuzzle = new Kuzzle(protocol);
});

Expand Down
2 changes: 1 addition & 1 deletion test/kuzzle/protocol.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Kuzzle protocol methods', () => {
let kuzzle;

beforeEach(() => {
const protocol = new ProtocolMock({host: 'somewhere'});
const protocol = new ProtocolMock('somewhere');

protocol.close = sinon.stub();
kuzzle = new Kuzzle(protocol);
Expand Down
2 changes: 1 addition & 1 deletion test/kuzzle/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Kuzzle query management', () => {
let kuzzle;

beforeEach(() => {
const protocol = new ProtocolMock({host: 'somewhere'});
const protocol = new ProtocolMock('somewhere');

kuzzle = new Kuzzle(protocol);
kuzzle.protocol.query.resolves(response);
Expand Down
2 changes: 1 addition & 1 deletion test/kuzzle/queue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Kuzzle queue', () => {
let kuzzle;

beforeEach(() => {
const protocol = new ProtocolMock({host: 'somewhere'});
const protocol = new ProtocolMock('somewhere');
kuzzle = new Kuzzle(protocol);
});

Expand Down
2 changes: 1 addition & 1 deletion test/kuzzle/setters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Kuzzle setters', () => {
let kuzzle;

beforeEach(() => {
const protocol = new ProtocolMock({host: 'somewhere'});
const protocol = new ProtocolMock('somewhere');
kuzzle = new Kuzzle(protocol);
});

Expand Down
4 changes: 2 additions & 2 deletions test/mocks/protocol.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const

class ProtocolMock extends KuzzleEventEmitter {

constructor (options = {}) {
constructor (host, options = {}) {
super();

this.options = options || {};
this.host = this.options.host;
this.host = host;
this.port = this.options.port || 7512;
this.state = 'offline';
this.connectCalled = false;
Expand Down
11 changes: 4 additions & 7 deletions test/protocol/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ describe('HTTP networking module', () => {
let protocol;

beforeEach(() => {
protocol = new HttpWrapper({
host: 'address',
protocol = new HttpWrapper('address', {
port: 1234
});
});
Expand Down Expand Up @@ -57,8 +56,7 @@ describe('HTTP networking module', () => {
});

it('should initialize http protocol with custom routes', () => {
const customProtocol = new HttpWrapper({
host: 'address',
const customProtocol = new HttpWrapper('address', {
port: 1234,
http: {
customRoutes: {
Expand Down Expand Up @@ -408,7 +406,7 @@ describe('HTTP networking module', () => {
'min-req-promise': {request: httpRequestStub}
});

protocol = new MockHttpWrapper({host: 'address', port: 1234});
protocol = new MockHttpWrapper('address', { port: 1234 });
});

it('should call http.request with empty body', () => {
Expand Down Expand Up @@ -475,8 +473,7 @@ describe('HTTP networking module', () => {
return xhrStub;
};

protocol = new HttpWrapper({
host: 'address',
protocol = new HttpWrapper('address', {
port: 1234
});
});
Expand Down
Loading