Skip to content

Instantly share code, notes, and snippets.

@tobiashm
Last active February 16, 2018 13:08
Show Gist options
  • Save tobiashm/5773e6c8f234822edcd012ef4fe1c840 to your computer and use it in GitHub Desktop.
Save tobiashm/5773e6c8f234822edcd012ef4fe1c840 to your computer and use it in GitHub Desktop.
Fetch connector for elasticsearch.js
import 'abortcontroller-polyfill';
import ConnectionAbstract from 'elasticsearch/src/lib/connection';
/**
* @example
* const client = new elasticsearch.Client({
* connectionClass: FetchConnector
* });
*/
class FetchConnector extends ConnectionAbstract {
request(params, cb) {
const host = this.host;
const url = host.makeUrl(params);
const method = params.method || 'GET'
const headers = host.getHeaders(params.headers);
const body = params.body;
const controller = new window.AbortController();
const signal = controller.signal;
fetch(url, { body, headers, method, signal })
.then(response => response.text())
.then(response => cb(null, response));
return () => controller.abort();
}
}
export default FetchConnector;
{
"dependencies": {
"abortcontroller-polyfill": "^1.1.5",
"elasticsearch": "^14.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment