Skip to content

Commit

Permalink
Repatriates last update and add "install" function for Vue-2 compatib…
Browse files Browse the repository at this point in the history
…ility (#7)
  • Loading branch information
Njuelle authored Mar 23, 2022
1 parent a7d9a45 commit ef4be76
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 4,341 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Node.js Package

on:
release:
types: [published]

jobs:
publish-npm:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
registry-url: https://registry.npmjs.org/
- run: |
npm ci
npm run build
npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# CONTRIBUTING

* `master` - Last stable version (4.x compatible with Kuzzle v2 and Vue 3)
* `1-dev`, `1-stable` - dev and stable branches for version 1.x, compatible with Kuzzle v1 and Vue 2
* `2-dev`, `2-stable` - dev and stable branches for version 2.x, compatible with Kuzzle v2 and Vue 2
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ This plugin simply exposes the Kuzzle SDK in your Vuejs components.
## Compatibility matrice

| Kuzzle Version | Vue.js Kuzzle Plugin Version |
| -------------- | -------------- |
| 1.x.x | 1.x.x |
| 2.x.x | 2.x.x |
| -------------- | ----------------- |
| 1.x.x | 1.x.x |
| 2.x.x | 2.x.x and higher |

## Getting started

Expand Down
3 changes: 1 addition & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ declare module 'vue-kuzzle' {
$kuzzle: any;
}
}
}

}
43 changes: 0 additions & 43 deletions index.js

This file was deleted.

111 changes: 111 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { Kuzzle, WebSocket } from 'kuzzle-sdk';
import _Vue from 'vue';

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

interface Backend {
host: string;
options: {
port: number;
sslConnection: boolean;
};
}

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

export function getBackendFromConf(backendsConfig: Backends) {
const backends: Backends = {
default: {
host: process.env.VUE_APP_BACKEND_HOST || 'localhost',
options: {
port: parseInt(process.env.VUE_APP_BACKEND_PORT || '7512'),
sslConnection: process.env.VUE_APP_BACKEND_SSL === 'true' || false,
},
},
...backendsConfig,
};

const backendName: string = process.env.VUE_APP_BACKEND
? process.env.VUE_APP_BACKEND
: 'default';

if (!backends[backendName]) {
throw new Error(`Unable to find backend ${backendName} in configuration.`);
}

return backends[backendName] ? backends[backendName] : null;
}

export function getBackendFromLocalStorage() {
const lsItem = localStorage.getItem(LS_KEY);
if (!lsItem) {
return null;
}
const backend = JSON.parse(lsItem);

if (typeof backend !== 'object') {
throw new Error(
`Item found in localStorage (${LS_KEY}) is malformed. Expected an object, found ${backend}`
);
}

return backend;
}

export function getBackendFromWindow() {
if (!(window as any)[GLOBAL_NAME]) {
return null;
}

const backend = JSON.parse((window as any)[GLOBAL_NAME]);

if (typeof backend !== 'object') {
throw new Error(
`Item found in global (${GLOBAL_NAME}) is malformed. Expected an object, found ${backend}`
);
}

return backend;
}

export const instantiateKuzzleSDK = (
backendsConfig: Backends,
sdkOptions: any
): Kuzzle => {
const backend: Backend | null =
getBackendFromLocalStorage() ||
getBackendFromWindow() ||
getBackendFromConf(backendsConfig);

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

if (!backend.host) {
throw new Error(`Backend is malformed (missing host)`);
}

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

/**
* The VueKuzzle plugin. Makes the Kuzzle SDK available in Vue components as
* `this.$kuzzle`.
*
* @param Vue The Vue application to apply the plugin to
* @param options Options passed to the Kuzzle SDK constructor
*
* @see https://docs.kuzzle.io/sdk/js/7/core-classes/kuzzle/constructor/#options
*/
export const VueKuzzle = {
install(Vue, options) {
const sdkOptions = options.sdkOptions ? options.sdkOptions : {};
Vue.prototype.$kuzzle = instantiateKuzzleSDK(options.backends, sdkOptions);
},
};
Loading

0 comments on commit ef4be76

Please sign in to comment.