Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// load the Kuzzle SDK module
/* snippet:start:1 */
import { Kuzzle, WebSocket } from 'kuzzle-sdk';
/* snippet:end */
// instantiate a Kuzzle client
/* snippet:start:2 */
const kuzzle = new Kuzzle(new WebSocket('kuzzle'));
/* snippet:end */
// add a listener to detect any connection problems
/* snippet:start:3 */
kuzzle.on('networkError', error => {
console.error(`Network Error: ${error}`);
});
/* snippet:end */
/* snippet:start:4 */
const run = async () => {
try {
// Connect to Kuzzle server
await kuzzle.connect();
// Create an index
const config = {
host: options.host || 'localhost',
port: options.port || 7512
};
if (options.username && options.password) {
config.login = {
strategy: 'local',
credentials: {
username: options.username,
password: options.password
}
};
}
const kuzzle = new Kuzzle(new WebSocket(config.host, { port: config.port }));
return kuzzle.connect()
.then(() => {
if (config.login) {
return kuzzle.auth.login(config.login.strategy, config.login.credentials);
}
})
.then(() => kuzzle.query(query));
}
_getSdk () {
let protocol;
switch (this.protocol) {
case 'http':
protocol = new Http(this.host, { port: this.port });
break;
case 'websocket':
protocol = new WebSocket(this.host, { port: this.port });
break;
default:
throw new Error(`Unknown protocol "${this.protocol}".`);
}
return new Kuzzle(protocol);
}
}
function getProtocol (world) {
let protocol;
switch (world.protocol) {
case 'http':
protocol = new Http(world.host, { port: world.port });
break;
case 'websocket':
protocol = new WebSocket(world.host, { port: world.port });
break;
default:
throw new Error(`Unknown protocol "${world.protocol}".`);
}
return protocol;
}