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
'nyc-open-data-plugin/driver': {
enroll: { verb: 'POST', url: '/_plugin/nyc-open-data-plugin/drivers' },
remove: { verb: 'DELETE', url: '/_plugin/nyc-open-data-plugin/drivers/:driverId' }
}
};
const options = {
customRoutes,
sslConnection: false
};
// Instantiates the Http protocol
const httpProtocol = new Http('kuzzle', options);
// Use it with Kuzzle
const kuzzle = new Kuzzle(httpProtocol);
} = require('kuzzle-sdk');
const customRoutes = {
'nyc-open-data-plugin/driver': {
enroll: { verb: 'POST', url: '/_plugin/nyc-open-data-plugin/drivers' },
remove: { verb: 'DELETE', url: '/_plugin/nyc-open-data-plugin/drivers/:driverId' }
}
};
const options = {
customRoutes,
sslConnection: false
};
// Instantiates the Http protocol
const httpProtocol = new Http('kuzzle', options);
// Use it with Kuzzle
const kuzzle = new Kuzzle(httpProtocol);
BeforeAll(({ timeout: 10 * 1000 }), async function () {
const world = new World({});
console.log(`Start tests with ${world.protocol.toLocaleUpperCase()} protocol.`);
world.sdk = new Kuzzle(getProtocol(world));
await world.sdk.connect();
console.log('Loading default securities..');
await world.sdk.query({
controller: 'admin',
action: 'loadSecurities',
body: testSecurities,
refresh: 'wait_for'
});
world.sdk.disconnect();
});
Reflect.defineProperty(this.accessors, 'strategies', {
enumerable: true,
get: () => {
return {
add: curryAddStrategy(kuzzle, pluginName),
remove: curryRemoveStrategy(kuzzle, pluginName)
};
}
});
Reflect.defineProperty(this.accessors, 'trigger', {
enumerable: true,
get: () => curryTrigger(kuzzle, pluginName)
});
const kuzzleSdk = new KuzzleSDK(new FunnelProtocol(kuzzle.funnel));
const throwNotAvailable = name => () => {
contextError.throw('unavailable_realtime', name);
};
const getPluginSdk = sdk => ({
query: sdk.query.bind(sdk),
auth: sdk.auth,
bulk: sdk.bulk,
collection: sdk.collection,
document: sdk.document,
index: sdk.index,
ms: sdk.ms,
realtime: Object.assign(sdk.realtime, {
subscribe: throwNotAvailable('realtime:subscribe'),
unsubscribe: throwNotAvailable('realtime:unsubscribe')
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);
}
}
pluginSdk.as = user => {
if (!(user instanceof User)) {
contextError.throw('invalid_user');
}
return getPluginSdk(
new KuzzleSDK(new FunnelProtocol(kuzzle.funnel, user)));
};
Before(({ timeout: 10 * 1000 }), async function () {
this.sdk = new Kuzzle(getProtocol(this));
await this.sdk.connect();
await this.sdk.auth.login(
'local',
{ username: 'test-admin', password: 'password' });
await this.sdk.query({
controller: 'admin',
action: 'resetDatabase',
refresh: 'wait_for'
});
});
_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);
}
}