Skip to content

Commit d0ce540

Browse files
authored
Add protocol support (Http, Websocket) (#8)
1 parent d26883d commit d0ce540

File tree

6 files changed

+34
-48
lines changed

6 files changed

+34
-48
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
index.js
3-
index.js.map
3+
index.js.map
4+
index.d.ts

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ An object of available backends to connect to. Backends are POJOs of the followi
3535
options.backends = {
3636
local: {
3737
host: 'localhost',
38+
protocol: 'websocket',
3839
options: {
3940
port: 7512,
4041
sslConnection: false
@@ -58,6 +59,7 @@ Aside from the `backends` option, you can define the backend to connect to entir
5859
* `VUE_APP_BACKEND_HOST` contains the hostname (e.g. `kuzzle.mydomain.com`),
5960
* `VUE_APP_BACKEND_PORT` contains the port (e.g. `443`),
6061
* `VUE_APP_BACKEND_SSL` can be set to `true` if the connection supports the SSL layer (do not set this variable if SSL is not supported).
62+
* `VUE_APP_BACKEND_PROTO` can be set to either `http` or `websocket`. If left blank, Websocket protocol is used by default.
6163

6264
For example, you can build your up to connect the Websocket to `wss://kuzzle.mydomain.com:443` like the following
6365

index.d.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

index.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
import { Kuzzle, WebSocket } from 'kuzzle-sdk';
1+
import { Http, Kuzzle, KuzzleAbstractProtocol, WebSocket } from 'kuzzle-sdk';
22
import _Vue from 'vue';
33

44
const LS_KEY = 'kuzzle-backend'
55
const GLOBAL_NAME = 'kuzzleBackend'
66

7-
interface Backend {
7+
export enum KuzzleProtocol {
8+
HTTP = 'http',
9+
WEBSOCKET = 'websocket'
10+
}
11+
export interface Backend {
812
host: string;
13+
protocol: KuzzleProtocol
914
options: {
1015
port: number;
11-
sslConnection: boolean
16+
sslConnection: boolean,
1217
}
1318
}
1419

15-
interface Backends {
20+
export interface Backends {
1621
[name: string]: Backend
1722
}
1823

1924
export function getBackendFromConf(backendsConfig: Backends) {
2025
const backends: Backends = {
2126
default: {
2227
host: process.env.VUE_APP_BACKEND_HOST || 'localhost',
28+
protocol: (process.env.VUE_APP_BACKEND_PROTO as KuzzleProtocol) || KuzzleProtocol.WEBSOCKET,
2329
options: {
2430
port: parseInt(process.env.VUE_APP_BACKEND_PORT || '7512'),
2531
sslConnection: process.env.VUE_APP_BACKEND_SSL === 'true' || false
@@ -67,6 +73,12 @@ export function getBackendFromWindow() {
6773
return backend;
6874
}
6975

76+
/**
77+
* Instantiates the Kuzzle SDK by resolving the backend from the given config.
78+
*
79+
* @param backendsConfig
80+
* @param sdkOptions
81+
*/
7082
export const instantiateKuzzleSDK = (backendsConfig: Backends, sdkOptions: any): Kuzzle => {
7183
const backend:Backend | null = getBackendFromLocalStorage() || getBackendFromWindow() || getBackendFromConf(backendsConfig)
7284

@@ -78,9 +90,20 @@ export const instantiateKuzzleSDK = (backendsConfig: Backends, sdkOptions: any):
7890
throw new Error(`Backend is malformed (missing host)`);
7991
}
8092

81-
return new Kuzzle(new WebSocket(backend.host, backend.options || null), sdkOptions);
93+
return new Kuzzle(protocolFactory(backend), sdkOptions);
8294
};
8395

96+
const protocolFactory = (backend: Backend): KuzzleAbstractProtocol => {
97+
switch (backend.protocol) {
98+
case KuzzleProtocol.HTTP:
99+
return new Http(backend.host, backend.options)
100+
101+
case KuzzleProtocol.WEBSOCKET:
102+
default:
103+
return new WebSocket(backend.host, backend.options)
104+
}
105+
}
106+
84107
/**
85108
* The VueKuzzle plugin. Makes the Kuzzle SDK available in Vue components as
86109
* `this.$kuzzle`.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-plugin-kuzzle",
3-
"version": "4.3.1",
3+
"version": "4.4.0",
44
"description": "A Vuejs plugin shipping the Kuzzle SDK in your components",
55
"main": "index.js",
66
"types": "./index.d.ts",

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"moduleResolution": "node",
88
"skipLibCheck": true,
99
"esModuleInterop": true,
10-
"declaration": false,
10+
"declaration": true,
1111
"sourceMap": true,
1212
"resolveJsonModule": true,
1313
"baseUrl": ".",

0 commit comments

Comments
 (0)