Skip to content

Commit 1c1e274

Browse files
authored
Release 6.0.0-beta-1 (#311)
* KZL 174 - Update README for SDK V6 (#308) * Draft readme * Update README for sdk v6 * Update sdk deploy script to build * Use 6-beta links in github url * Apply change from @ballinette and @scottinet * typo * Release 6.0.0-beta-1 * dependencies update
1 parent ba27a31 commit 1c1e274

File tree

5 files changed

+973
-1552
lines changed

5 files changed

+973
-1552
lines changed

.travis.yml

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- 6
4+
- 6
5+
6+
env:
7+
global:
8+
secure: ATb4QFGG6sPkfquAoye6BeN7u4uQ4abP8qatVK8XG3Qof+pECZwRkFPVoopO1Gb/4bQSPfapc1aGPzGuKi409v633vpDnpcgM7cHX9CPT+YQYcTePMFtq/m+5YY3WIYWM1zCFmJ+bBwQO/MtsubfGfA9EmSAAxtnJV+1JDD3Y6+NQRkQN7K+gxQozec0u1W0Oe1sJXLSKlW3JPL8iRtKcLvmIDysKtBK6X/Wg1qP2Me4reb449a0yGej3QvZDbDYJbdlh7vuFLjXJAz3yExfED8QAm8wf0p9F8emILHnkLxjumTTnGUUqDJLLu8WgkahFiyFo4jMtryhNQ0+p6WhXmEbpNIO+yMGSZPsCGbnpkhWJ1aMAT6ZhgpHf4jDZQd4ZJi02+n9tOpe+Lm2Jk2LXY8SwMk3xGtqm5sk03weTc019TUGj/NLoHNjO/vfNz3xOImD5Gck3bXWNxdrX3bjiyVsr/u77enoWu9drPCEl3JOTIwVP3mTF8R3axSFOQ8M4SI1orCMChkGMB5qExl+Gfx+MHa/jL7QKO1SFRgl1O3DbhsyFE6xiabYzwLcdbI50ypy6VJjmDd5RxK7BkDa8RumlHT1uY1FK9yRXu4XFrrw9FjF2s+xjWbliRZqXRReXk0W1Hz4zj5au3Y9YH5a/UPtB72TY+AUUWHTUPQl3SU=
9+
510
addons:
611
apt:
712
sources:
8-
- ubuntu-toolchain-r-test
13+
- ubuntu-toolchain-r-test
914
packages:
10-
- gcc-4.9
11-
- g++-4.9
12-
script:
13-
- npm run build && npm test && cat ./coverage/lcov.info | ./node_modules/.bin/codecov
14-
- chmod 755 ${TRAVIS_BUILD_DIR}/deploy.sh && sh -c "${TRAVIS_BUILD_DIR}/deploy.sh"
15+
- gcc-4.9
16+
- g++-4.9
17+
1518
before_install:
16-
- export CC="gcc-4.9" CXX="g++-4.9"
19+
- export CC="gcc-4.9" CXX="g++-4.9"
20+
1721
install:
18-
- npm install
19-
env:
20-
global:
21-
secure: ATb4QFGG6sPkfquAoye6BeN7u4uQ4abP8qatVK8XG3Qof+pECZwRkFPVoopO1Gb/4bQSPfapc1aGPzGuKi409v633vpDnpcgM7cHX9CPT+YQYcTePMFtq/m+5YY3WIYWM1zCFmJ+bBwQO/MtsubfGfA9EmSAAxtnJV+1JDD3Y6+NQRkQN7K+gxQozec0u1W0Oe1sJXLSKlW3JPL8iRtKcLvmIDysKtBK6X/Wg1qP2Me4reb449a0yGej3QvZDbDYJbdlh7vuFLjXJAz3yExfED8QAm8wf0p9F8emILHnkLxjumTTnGUUqDJLLu8WgkahFiyFo4jMtryhNQ0+p6WhXmEbpNIO+yMGSZPsCGbnpkhWJ1aMAT6ZhgpHf4jDZQd4ZJi02+n9tOpe+Lm2Jk2LXY8SwMk3xGtqm5sk03weTc019TUGj/NLoHNjO/vfNz3xOImD5Gck3bXWNxdrX3bjiyVsr/u77enoWu9drPCEl3JOTIwVP3mTF8R3axSFOQ8M4SI1orCMChkGMB5qExl+Gfx+MHa/jL7QKO1SFRgl1O3DbhsyFE6xiabYzwLcdbI50ypy6VJjmDd5RxK7BkDa8RumlHT1uY1FK9yRXu4XFrrw9FjF2s+xjWbliRZqXRReXk0W1Hz4zj5au3Y9YH5a/UPtB72TY+AUUWHTUPQl3SU=
22+
- npm install
23+
24+
script:
25+
- npm run build && npm test && cat ./coverage/lcov.info | ./node_modules/.bin/codecov
26+
27+
after_success:
28+
- chmod 755 ${TRAVIS_BUILD_DIR}/deploy.sh && sh -c "${TRAVIS_BUILD_DIR}/deploy.sh"
29+
2230
deploy:
2331
provider: npm
2432
skip_cleanup: true
@@ -28,6 +36,7 @@ deploy:
2836
on:
2937
repo: kuzzleio/sdk-javascript
3038
branch: master
39+
3140
notifications:
3241
webhooks:
3342
urls:

README.md

Lines changed: 220 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,37 @@ A backend software, self-hostable and ready to use to power modern apps.
1010

1111
You can access the Kuzzle repository on [Github](https://github.com/kuzzleio/kuzzle)
1212

13+
This is the documentation for the beta-6 SDK. We are currently refactoring in deep our documentation to support many versions of each SDK. You can see it [here](https://docs-v2.kuzzle.io/sdk-reference/kuzzle/constructor/) but notice that this is a beta version of the documentation.
14+
15+
This new documentation will be available soon. In the meantime, you can still use the SDK 5 and the old documentation at https://docs.kuzzle.io/sdk-reference/essentials/.
16+
1317
* [Basic usage](#basic-usage)
14-
* [SDK Documentation](#sdk-documentation)
15-
* [Protocols used](#protocols-used)
1618
* [Installation](#installation)
1719
* [NodeJS](#nodejs)
1820
* [Javascript](#javascript)
21+
* [Protocols used](#protocols-used)
22+
* [SDK Documentation](#sdk-documentation)
1923
* [Building manually](#building-manually)
2024
* [License](#license)
2125

2226
## Basic usage
2327

24-
Follow [Kuzzle Guide](http://docs.kuzzle.io/guide/getting-started/#sdk-play-time)
25-
26-
## SDK Documentation
28+
The SDK supports different protocols. When instantiating, you must choose the protocol to use and fill in the different options needed to connect to Kuzzle.
2729

28-
The complete SDK documentation is available [here](http://docs.kuzzle.io/sdk-reference/)
30+
Example:
31+
```js
32+
const { Kuzzle } = require('kuzzle-sdk');
2933

30-
## Protocols used
34+
const kuzzle = new Kuzzle('websocket', { host: 'localhost', port: 7512 });
3135

32-
The Javascript SDK implements two network protocols: raw WebSocket, and [Socket.IO](http://socket.io/)
33-
The main reason behind this is that while Socket.IO offers better compatibility with older web browsers, our raw WebSocket implementation is about 20% faster
34-
35-
Which protocol is used when you connect to Kuzzle depends on multiple factors:
36-
37-
#### NodeJS
38-
39-
The protocol used is always raw WebSocket.
40-
41-
#### Web Browsers
42-
43-
The SDK will first try to use raw WebSocket to connect to Kuzzle. If the web browser does not support this protocol, then the SDK falls back to Socket.IO
36+
kuzzle.connect()
37+
.then(() => {
38+
// You are now connected to your Kuzzle instance.
39+
return kuzzle.server.now();
40+
})
41+
.then(serverTime => console.log(serverTime))
42+
.catch(error => console.error(error));
43+
```
4444

4545
## Installation
4646

@@ -50,19 +50,21 @@ This SDK can be used either in NodeJS or in a browser.
5050

5151
```
5252
npm install kuzzle-sdk --save
53+
# Beta v6
54+
npm install git://github.com/kuzzleio/sdk-javascript.git#6-beta --save
5355
```
5456

5557
### Browser
5658

57-
To run the SDK in the browser, you need to pick the [built version available here](https://raw.githubusercontent.com/kuzzleio/sdk-javascript/master/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.
59+
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.
5860

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

6466
```html
65-
<script type="text/javascript" src="https://cdn.rawgit.com/kuzzleio/sdk-javascript/master/dist/kuzzle.js"></script>
67+
<script type="text/javascript" src="https://cdn.rawgit.com/kuzzleio/sdk-javascript/tree/6-beta/dist/kuzzle.js"></script>
6668
```
6769

6870
If you want to support older browser versions, you may load `socket.io` before Kuzzle, making the SDK compatible with browsers without websocket support:
@@ -84,9 +86,204 @@ But you'll still need to pick the built version (which ships with the package).
8486

8587
```javascript
8688
// with the classic require...
87-
let Kuzzle = require('kuzzle-sdk/dist/kuzzle.js')
89+
const { Kuzzle } = require('kuzzle-sdk/dist/kuzzle.js')
8890
// ... or with the new import directive.
89-
import Kuzzle from 'kuzzle-sdk/dist/kuzzle.js'
91+
import { Kuzzle } from 'kuzzle-sdk/dist/kuzzle.js'
92+
```
93+
94+
## Protocols used
95+
Actually, the SDK support 3 protocols: `http`, `websocket` et `socketio`.
96+
97+
Websocket and Socket.IO protocols implement the whole Kuzzle API, while HTTP protocol does not implement realtime features (rooms and subscriptions).
98+
While Socket.IO offers better compatibility with older web browsers, our raw WebSocket implementation is about 20% faster.
99+
100+
#### NodeJS
101+
102+
We recommend to use the `websocket` protocol, but you can still use `http`, `socketio` or even a custom protocol if you want.
103+
104+
#### Web Browsers
105+
106+
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.
107+
108+
```js
109+
let kuzzle;
110+
111+
if ('WebSocket' in window && window.WebSocket.CLOSING === 2) {
112+
kuzzle = new Kuzzle('websocket', { host: 'localhost' });
113+
} else {
114+
kuzzle = new Kuzzle('socketio', { host: 'localhost' });
115+
}
116+
```
117+
118+
#### Custom protocol
119+
120+
The SDK also proposes to create custom protocols.
121+
122+
There are two ways to write these protocols, the first is to inherit the `KuzzleAbstractNetwork` class provided with the SDK and implement only the `connect` and `send` methods.
123+
124+
```js
125+
const { KuzzleAbstractNetwork } = require('kuzzle-sdk');
126+
127+
class MyCustomProtocol extends KuzzleAbstractNetwork {
128+
connect() {
129+
// (...) do custom connection steps...
130+
131+
// change state and resolve:
132+
this.state = 'ready';
133+
return Promise.resolve();
134+
}
135+
136+
send(request) {
137+
// (...) here the protocol-specific code to send the request to kuzzle and get the result into `result` variable
138+
139+
// Send back the result to SDK and resolve:
140+
this.emit(request.requestId, { result });
141+
return Promise.resolve();
142+
}
143+
}
144+
```
145+
146+
The second way is to implement the `isReady` and `query` methods as well as javascript [Event API](https://nodejs.org/api/events.html) by inheriting the `KuzzleEventEmitter` class.
147+
148+
```js
149+
const { KuzzleEventEmitter } = require('kuzzle-sdk');
150+
151+
class MyCustomProtocol extends KuzzleEventEmitter {
152+
153+
isReady() {
154+
return true;
155+
}
156+
157+
query (request, options) {
158+
// (...) here the protocol-specific code to send the request to kuzzle and get the result into `result` variable
159+
160+
// Resolves the response:
161+
return Promise.resolve({ result });
162+
}
163+
}
164+
```
165+
166+
These customized protocols can then be used by the SDK by passing them as parameters at instantiation.
167+
168+
```js
169+
const protocol = new MyCustomProtocol();
170+
171+
const kuzzle = new Kuzzle(protocol);
172+
```
173+
174+
## SDK Documentation
175+
176+
### Connection to Kuzzle
177+
178+
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.
179+
180+
It is then possible to interact with the Kuzzle API through the SDK once the Promise returned by `kuzzle.connect()` has been resolved.
181+
182+
```js
183+
// Without async/await
184+
kuzzle.connect()
185+
.then(() => {
186+
// You are now connected to your Kuzzle instance.
187+
return kuzzle.server.now();
188+
})
189+
.then(serverTime => console.log(serverTime))
190+
.catch(error => console.error(error));
191+
192+
// With async/await
193+
try {
194+
await kuzzle.connect();
195+
const serverTime = await kuzzle.server.now();
196+
197+
console.log(serverTime);
198+
} catch (error) {
199+
console.error(error);
200+
}
201+
```
202+
203+
### Match Kuzzle's API
204+
205+
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/).
206+
207+
Each controller is accessible from the Kuzzle object. The controller's actions are named in the same way as in the API.
208+
209+
For example, for the `create` action of the `document` controller ([document#create](https://docs.kuzzle.io/api-documentation/controller-document/create)):
210+
```js
211+
const options = { refresh: 'wait_for' };
212+
const documentBody = { hello: 'world' };
213+
kuzzle.document.create('my-index', 'my-collection', 'my-uniq-id', documentBody, options)
214+
```
215+
216+
The parameters of each method differ according to the parameters expected in the API.
217+
If you want to get the details of the parameters for each method, it is necessary for the moment to see the code of each controller on [Github](https://github.com/kuzzleio/sdk-javascript/tree/6-beta/src/controllers).
218+
219+
#### Query method
220+
221+
This SDK also expose a low level `query` method to access the API even if the controller is not available inside the SDK.
222+
223+
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.
224+
225+
Example with the [Admin](https://docs.kuzzle.io/api-documentation/controller-admin/) controller:
226+
```js
227+
const query = {
228+
controller: 'admin',
229+
action: 'resetCache',
230+
database: 'memoryStorage'
231+
};
232+
const options = {
233+
refresh: 'wait_for'
234+
};
235+
236+
try {
237+
const response = await kuzzle.query(query, options);
238+
/*
239+
{
240+
requestId: '6526be09-330d-4183-be2b-8c30183db0f0',
241+
status: 200,
242+
error: null,
243+
controller: 'admin',
244+
action: 'resetCache',
245+
collection: null,
246+
index: null,
247+
volatile:
248+
{ sdkInstanceId: 'c6fbe345-1d8b-4324-bf1f-2be3cc1a7d27',
249+
sdkVersion: '6.0.0' },
250+
result: { acknowledge: true },
251+
room: '6526be09-330d-4183-be2b-8c30183db0f0'
252+
}
253+
*/
254+
console.log('Kuzzle memory storage successfully reset');
255+
} catch (error) {
256+
console.error(error);
257+
}
258+
```
259+
260+
### Promise based
261+
262+
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.
263+
264+
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.
265+
266+
Any error must be caught either at the end of the Promise chain, or by using `async/await` and a `try...catch`.
267+
268+
```js
269+
const mapping = {
270+
properties: {
271+
hello: { type: 'text' }
272+
}
273+
};
274+
// Without async/await
275+
kuzzle.collection.create('my-index', 'my-collection', mapping)
276+
.then(result => console.log('Success'))
277+
.catch(error => console.error(`Hu oh, we've got some error: ${error.message}`));
278+
279+
// With async/await
280+
try {
281+
const result = await kuzzle.collection.create('my-index', 'my-collection', mapping);
282+
// result contain { "acknowledged": true }
283+
console.log(result);
284+
} catch (error) {
285+
console.error(`Hu oh, we've got some error: ${error.message}`)
286+
}
90287
```
91288

92289
## License

deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /bin/bash
22
set -e
33

4-
if [ "${TRAVIS_BRANCH}" = "master" -a "${TRAVIS_PULL_REQUEST}" = "false" ]
4+
if ([ "${TRAVIS_BRANCH}" = "master" ] || [ "${TRAVIS_BRANCH}" = "6-beta" ]) && [ "${TRAVIS_PULL_REQUEST}" = "false" ]
55
then
66
echo "Push generated dist folder"
77
git config --global user.email "support@kuzzle.io"

0 commit comments

Comments
 (0)