-
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.
refactor: split code to improve readability
- Loading branch information
Showing
14 changed files
with
309 additions
and
153 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
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
node_modules | ||
index.js | ||
index.js.map | ||
index.d.ts | ||
dist | ||
*.tgz |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Backends, Backend, KuzzleProtocol } from '../types'; | ||
|
||
const LS_KEY = 'kuzzle-backend'; | ||
const GLOBAL_NAME = 'kuzzleBackend'; | ||
|
||
export function getBackendFromConf(backendsConfig: Backends = {}): Backend | null { | ||
/* eslint-disable sort-keys */ | ||
const backends: Backends = { | ||
default: { | ||
host: process.env.VUE_APP_BACKEND_HOST ?? 'localhost', | ||
protocol: | ||
(process.env.VUE_APP_BACKEND_PROTO as KuzzleProtocol | undefined) ?? | ||
KuzzleProtocol.WEBSOCKET, | ||
options: { | ||
port: parseInt(process.env.VUE_APP_BACKEND_PORT ?? '7512'), | ||
sslConnection: process.env.VUE_APP_BACKEND_SSL === 'true' || false, | ||
}, | ||
}, | ||
...backendsConfig, | ||
}; | ||
/* eslint-enable sort-keys */ | ||
|
||
const backendName = process.env.VUE_APP_BACKEND ?? 'default'; | ||
|
||
if (backends[backendName] === undefined) { | ||
throw new Error(`Unable to find backend ${backendName} in configuration.`); | ||
} | ||
|
||
return backends[backendName] ?? null; | ||
} | ||
|
||
export function getBackendFromLocalStorage(): Backend | null { | ||
const lsItem = localStorage.getItem(LS_KEY); | ||
if (lsItem === null) { | ||
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(): Backend | null { | ||
if ((window as any)[GLOBAL_NAME] === undefined) { | ||
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; | ||
} |
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,2 @@ | ||
export * from './getConfig'; | ||
export * from './instantiateKuzzleSDK'; |
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,35 @@ | ||
import { Kuzzle, KuzzleAbstractProtocol, Http, WebSocket } from 'kuzzle-sdk'; | ||
import { Backends, Backend, KuzzleProtocol, SDKOptions } from '../types'; | ||
import { getBackendFromLocalStorage, getBackendFromWindow, getBackendFromConf } from './getConfig'; | ||
|
||
function 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); | ||
} | ||
} | ||
|
||
/** | ||
* Instantiates the Kuzzle SDK by resolving the backend from the given config. | ||
* | ||
* @param backendsConfig | ||
* @param sdkOptions | ||
*/ | ||
export function instantiateKuzzleSDK(backendsConfig?: Backends, sdkOptions?: SDKOptions): Kuzzle { | ||
const backend: Backend | null = | ||
getBackendFromLocalStorage() ?? getBackendFromWindow() ?? getBackendFromConf(backendsConfig); | ||
|
||
if (backend === null) { | ||
throw new Error('No backend resolved.'); | ||
} | ||
|
||
if (backend.host === undefined) { | ||
throw new Error(`Backend is malformed (missing host)`); | ||
} | ||
|
||
return new Kuzzle(protocolFactory(backend), sdkOptions); | ||
} |
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,4 @@ | ||
import '../vue-kuzzle.d'; | ||
export * from './types'; | ||
export * from './helpers'; | ||
export * from './plugin'; |
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,22 @@ | ||
import { PluginFunction } from 'vue'; | ||
import { instantiateKuzzleSDK } from './helpers'; | ||
import { Backends, SDKOptions } from './types'; | ||
|
||
export interface VueKuzzleOptions { | ||
backends: Backends; | ||
sdkOptions: 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: PluginFunction<VueKuzzleOptions> = (Vue, options) => { | ||
const sdkOptions = options?.sdkOptions ?? {}; | ||
Vue.prototype.$kuzzle = instantiateKuzzleSDK(options?.backends, sdkOptions); | ||
}; |
Oops, something went wrong.