Skip to content

Commit

Permalink
Add protocol support (Http, Websocket) (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
xbill82 authored Jul 27, 2022
1 parent d26883d commit d0ce540
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
index.js
index.js.map
index.js.map
index.d.ts
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ An object of available backends to connect to. Backends are POJOs of the followi
options.backends = {
local: {
host: 'localhost',
protocol: 'websocket',
options: {
port: 7512,
sslConnection: false
Expand All @@ -58,6 +59,7 @@ Aside from the `backends` option, you can define the backend to connect to entir
* `VUE_APP_BACKEND_HOST` contains the hostname (e.g. `kuzzle.mydomain.com`),
* `VUE_APP_BACKEND_PORT` contains the port (e.g. `443`),
* `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).
* `VUE_APP_BACKEND_PROTO` can be set to either `http` or `websocket`. If left blank, Websocket protocol is used by default.

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

Expand Down
40 changes: 0 additions & 40 deletions index.d.ts

This file was deleted.

33 changes: 28 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { Kuzzle, WebSocket } from 'kuzzle-sdk';
import { Http, Kuzzle, KuzzleAbstractProtocol, WebSocket } from 'kuzzle-sdk';
import _Vue from 'vue';

const LS_KEY = 'kuzzle-backend'
const GLOBAL_NAME = 'kuzzleBackend'

interface Backend {
export enum KuzzleProtocol {
HTTP = 'http',
WEBSOCKET = 'websocket'
}
export interface Backend {
host: string;
protocol: KuzzleProtocol
options: {
port: number;
sslConnection: boolean
sslConnection: boolean,
}
}

interface Backends {
export interface Backends {
[name: string]: Backend
}

export function getBackendFromConf(backendsConfig: Backends) {
const backends: Backends = {
default: {
host: process.env.VUE_APP_BACKEND_HOST || 'localhost',
protocol: (process.env.VUE_APP_BACKEND_PROTO as KuzzleProtocol) || KuzzleProtocol.WEBSOCKET,
options: {
port: parseInt(process.env.VUE_APP_BACKEND_PORT || '7512'),
sslConnection: process.env.VUE_APP_BACKEND_SSL === 'true' || false
Expand Down Expand Up @@ -67,6 +73,12 @@ export function getBackendFromWindow() {
return backend;
}

/**
* Instantiates the Kuzzle SDK by resolving the backend from the given config.
*
* @param backendsConfig
* @param sdkOptions
*/
export const instantiateKuzzleSDK = (backendsConfig: Backends, sdkOptions: any): Kuzzle => {
const backend:Backend | null = getBackendFromLocalStorage() || getBackendFromWindow() || getBackendFromConf(backendsConfig)

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

return new Kuzzle(new WebSocket(backend.host, backend.options || null), sdkOptions);
return new Kuzzle(protocolFactory(backend), sdkOptions);
};

const protocolFactory = (backend: Backend): KuzzleAbstractProtocol => {
switch (backend.protocol) {
case KuzzleProtocol.HTTP:
return new Http(backend.host, backend.options)

case KuzzleProtocol.WEBSOCKET:
default:
return new WebSocket(backend.host, backend.options)
}
}

/**
* The VueKuzzle plugin. Makes the Kuzzle SDK available in Vue components as
* `this.$kuzzle`.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-plugin-kuzzle",
"version": "4.3.1",
"version": "4.4.0",
"description": "A Vuejs plugin shipping the Kuzzle SDK in your components",
"main": "index.js",
"types": "./index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"moduleResolution": "node",
"skipLibCheck": true,
"esModuleInterop": true,
"declaration": false,
"declaration": true,
"sourceMap": true,
"resolveJsonModule": true,
"baseUrl": ".",
Expand Down

0 comments on commit d0ce540

Please sign in to comment.