Last active
February 16, 2018 13:08
-
-
Save tobiashm/5773e6c8f234822edcd012ef4fe1c840 to your computer and use it in GitHub Desktop.
Fetch connector for elasticsearch.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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