vue-plugin-kuzzle
Version:
A Vuejs plugin shipping the Kuzzle SDK in your components
29 lines • 1.11 kB
JavaScript
import { Kuzzle, Http, WebSocket } from 'kuzzle-sdk';
import { KuzzleProtocol } from '../types';
import { getBackendFromLocalStorage, getBackendFromWindow, getBackendFromConf } from './getConfig';
function protocolFactory(backend) {
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, sdkOptions) {
const backend = 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);
}
//# sourceMappingURL=instantiateKuzzleSDK.js.map