remove Joi validation, add createClient, update tests
This commit is contained in:
@@ -28,7 +28,6 @@
|
|||||||
"sinon": "~1.17.3"
|
"sinon": "~1.17.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"elasticsearch": "~11.0.1",
|
"elasticsearch": "~11.0.1"
|
||||||
"joi": "~8.0.5"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/helpers/create_client.js
Normal file
13
src/helpers/create_client.js
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import elasticsearch from 'elasticsearch';
|
||||||
|
|
||||||
|
export default function createClient(options) {
|
||||||
|
let client;
|
||||||
|
|
||||||
|
if (options instanceof elasticsearch.Client) {
|
||||||
|
client = options;
|
||||||
|
} else {
|
||||||
|
client = new elasticsearch.Client(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
return client;
|
||||||
|
};
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
import Joi from 'joi';
|
|
||||||
|
|
||||||
var schema = Joi.object({
|
|
||||||
url: Joi.string().uri({ scheme: ['http', 'https'] }).default('http://localhost:9200'),
|
|
||||||
index: Joi.string().required(),
|
|
||||||
interval: Joi.string().default('1w'),
|
|
||||||
timeout: Joi.number().min(10000).default(10000),
|
|
||||||
}).default();
|
|
||||||
|
|
||||||
const validateSchema = (options) => new Promise(function (resolve, reject) {
|
|
||||||
schema.validate(options, (err, settings) => {
|
|
||||||
if (err) return reject(err);
|
|
||||||
resolve(settings);
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
export default validateSchema;
|
|
||||||
26
src/index.js
26
src/index.js
@@ -1,24 +1,26 @@
|
|||||||
import events from 'events';
|
import events from 'events';
|
||||||
import validateSchema from './helpers/validate_schema';
|
import createClient from './helpers/create_client';
|
||||||
|
|
||||||
export default class Elastique extends events.EventEmitter {
|
export default class Elastique extends events.EventEmitter {
|
||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
|
if (!options.index) throw new Error('Must specify an index to write to');
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.ready = true;
|
this.ready = true;
|
||||||
|
|
||||||
// initalize the module
|
this.settings = {};
|
||||||
Promise.all([
|
Object.keys(options, (key) => {
|
||||||
validateSchema(options)
|
if (key !== 'client') this.settings[key] = options[key];
|
||||||
])
|
|
||||||
.then(([ settings ]) => {
|
|
||||||
this.settings = settings;
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
this.ready = false;
|
|
||||||
this.emit('error', err);
|
|
||||||
throw err;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.client = createClient(options.client);
|
||||||
|
}
|
||||||
|
|
||||||
|
add(type, payload, opts = {}) {
|
||||||
|
const options = Object.assign({
|
||||||
|
timeout: this.settings.timeout
|
||||||
|
}, opts);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,28 +1,29 @@
|
|||||||
import Elastique from '../lib/index'
|
import Elastique from '../lib/index';
|
||||||
import expect from 'expect.js';
|
import expect from 'expect.js';
|
||||||
|
|
||||||
describe('Elastique class', function () {
|
describe('Elastique class', function () {
|
||||||
describe('Option validation', function () {
|
describe('Option validation', function () {
|
||||||
it('should emit error without an index', function (done) {
|
it('should throw without an index', function () {
|
||||||
const queue = new Elastique();
|
const init = () => new Elastique();
|
||||||
|
expect(init).to.throwException(/must.+specify.+index/i);
|
||||||
queue.on('error', (err) => {
|
|
||||||
expect(err).to.be.an(Error);
|
|
||||||
expect(err.message).to.match(/index/);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should emit error with an invalid URL', function (done) {
|
it('should throw with an invalid host', function () {
|
||||||
const queue = new Elastique({
|
const init = () => new Elastique({
|
||||||
index: 'elastique',
|
index: 'elastique',
|
||||||
url: 'nope://nope'
|
client: { host: 'nope://nope' }
|
||||||
});
|
|
||||||
queue.on('error', (err) => {
|
|
||||||
expect(err).to.be.an(Error);
|
|
||||||
expect(err.message).to.match(/url/);
|
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
expect(init).to.throwException(/invalid.+protocol/i);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw with invalid hosts', function () {
|
||||||
|
const init = () => new Elastique({
|
||||||
|
index: 'elastique',
|
||||||
|
client: { hosts: [{ host: 'localhost', protocol: 'nope' }] }
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(init).to.throwException(/invalid.+protocol/i);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user