forked from TheDeveloper/http-aws-es
-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathAmazonTransport.js
43 lines (38 loc) · 1.25 KB
/
AmazonTransport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { Transport } = require('@elastic/elasticsearch')
function awaitAwsCredentials (awsConfig) {
return new Promise((resolve, reject) => {
awsConfig.getCredentials((err) => {
err ? reject(err) : resolve()
})
})
}
module.exports = awsConfig => {
class AmazonTransport extends Transport {
request (params, options = {}, callback = undefined) {
// options is optional, so if it is omitted, options will be the callback
if (typeof options === 'function') {
callback = options
options = {}
}
// check if getCredentials exists, if so this is an aws-sdk v2 global config object
if (typeof awsConfig.getCredentials !== 'function') {
if (typeof callback === 'undefined') {
return super.request(params, options)
} else {
super.request(params, options, callback)
}
} else {
// Promise support
if (typeof callback === 'undefined') {
return awaitAwsCredentials(awsConfig)
.then(() => super.request(params, options))
}
// Callback support
awaitAwsCredentials(awsConfig)
.then(() => super.request(params, options, callback))
.catch(callback)
}
}
}
return AmazonTransport
}