Add support for a global context option (#1256)

This commit is contained in:
Tomas Della Vedova
2020-07-13 14:36:53 +02:00
committed by delvedor
parent 28f2be397c
commit d73cb1a29b
16 changed files with 2995 additions and 1776 deletions

View File

@ -190,6 +190,11 @@ _Default:_ `null`
|`object` - A set of custom headers to send in every request. +
_Default:_ `{}`
|`context`
|`object` - A custom object that you can use for observability in yoru events.
It will be merged with the API level context option. +
_Default:_ `null`
|`cloud`
a|`object` - Custom configuration for connecting to
https://cloud.elastic.co[Elastic Cloud]. See https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/auth-reference.html[Authentication]

View File

@ -229,6 +229,44 @@ client.search({
})
----
The context object can also be configured as a global option in the client
configuration. If you provide both, the two context object will be shallow merged,
and the API level object will take precedece.
[source,js]
----
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
node: 'http://localhost:9200',
context: { winter: 'is coming' }
})
client.on('request', (err, result) => {
const { id } = result.meta.request
const { context } = result.meta
if (err) {
console.log({ error: err, reqId: id, context })
}
})
client.on('response', (err, result) => {
const { id } = result.meta.request
const { winter } = result.meta.context
if (err) {
console.log({ error: err, reqId: id, winter })
}
})
client.search({
index: 'my-index',
body: { foo: 'bar' }
}, {
context: { winter: 'has come' }
}, (err, result) => {
if (err) console.log(err)
})
----
=== Client name