-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Repatriates last update and add "install" function for Vue-2 compatib…
…ility (#7)
- Loading branch information
Showing
8 changed files
with
240 additions
and
4,341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,4 @@ declare module 'vue-kuzzle' { | |
$kuzzle: any; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}; |
Oops, something went wrong.