Skip to content

Commit 55cbbfa

Browse files
authored
[feat] backend from global variable
1 parent c6482ce commit 55cbbfa

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ For example, you can build your up to connect the Websocket to `wss://kuzzle.myd
6565
VUE_APP_BACKEND_HOST=kuzzle.mydomain.com VUE_APP_BACKEND_PORT=443 VUE_APP_BACKEND_SSL=true npm run build
6666
```
6767

68+
## Specify the backend via a global variable
69+
70+
You can also specify the backend config as a JSON-stringified POJO living in a global variable called `kuzzleBackend`. This variable must be available _before_ the Vue app is started, specifically before the `instantiateKuzzleSDK` public function is called. Here is an example of how to declare it
71+
72+
```javascript
73+
const kuzzleBackend = JSON.stringify({
74+
host: 'myhost.mydomain.com',
75+
options: {
76+
port: 443,
77+
ssl: true
78+
}
79+
})
80+
```
81+
6882
## Specify the backend via localStorage
6983

7084
_Purely for debug purposes_, you can override all the backend configuration by setting your backend as stringified JSON in the `kuzzle-backend` Local Storage item, e.g.
@@ -75,6 +89,15 @@ _Purely for debug purposes_, you can override all the backend configuration by s
7589

7690
**Beware that Local Storage is persistent and it is fairly easy to forget you set this item.** Use it consciously and keep in mind it is a good practice to unset it as soon as your debug session is over.
7791

92+
## Backend declaration precedence order
93+
94+
The plugin makes a choice of the available backend declaration by setting a preference order.
95+
96+
* Local storage (`kuzzle-backend`)
97+
* Global variable (`kuzzleBackend`)
98+
* Environment variables
99+
* Options passed to `Vue.use`
100+
78101
## Accessing the Kuzzle SDK instance within the app
79102

80103
You'll be able to access the Kuzzle SDK instance from the components as

index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Kuzzle, WebSocket } from 'kuzzle-sdk';
22
import _Vue from 'vue';
33

44
const LS_KEY = 'kuzzle-backend'
5+
const GLOBAL_NAME = 'kuzzleBackend'
56

67
interface Backend {
78
host: string;
@@ -52,8 +53,22 @@ export function getBackendFromLocalStorage() {
5253
return backend;
5354
}
5455

56+
export function getBackendFromWindow() {
57+
if (!window[GLOBAL_NAME]) {
58+
return null
59+
}
60+
61+
const backend = JSON.parse(window[GLOBAL_NAME])
62+
63+
if (typeof backend !== 'object') {
64+
throw new Error(`Item found in global (${GLOBAL_NAME}) is malformed. Expected an object, found ${backend}`)
65+
}
66+
67+
return backend;
68+
}
69+
5570
export const instantiateKuzzleSDK = (backendsConfig: Backends, sdkOptions: any): Kuzzle => {
56-
const backend:Backend | null = getBackendFromLocalStorage() || getBackendFromConf(backendsConfig)
71+
const backend:Backend | null = getBackendFromLocalStorage() || getBackendFromWindow() || getBackendFromConf(backendsConfig)
5772

5873
if (!backend) {
5974
throw new Error('No backend resolved.');

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.2.0",
3+
"version": "4.3.0",
44
"description": "A Vuejs plugin shipping the Kuzzle SDK in your components",
55
"main": "index.js",
66
"types": "./index.d.ts",

0 commit comments

Comments
 (0)