Skip to content

Commit 3a61d9b

Browse files
authored
Deep merge options on client instantiation (#2890) (#2898)
1 parent 8166b3b commit 3a61d9b

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/client.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ export default class Client extends API {
260260
}
261261
}
262262

263+
const headers: Record<string, any> = Object.assign({}, {
264+
'user-agent': `elasticsearch-js/${clientVersion} (${os.platform()} ${os.release()}-${os.arch()}; Node.js ${nodeVersion}; Transport ${transportVersion})`
265+
}, opts.headers ?? {})
266+
267+
const redaction = Object.assign({}, { type: 'replace', additionalKeys: [] }, opts.redaction ?? {})
268+
263269
const options: Required<ClientOptions> = Object.assign({}, {
264270
Connection: UndiciConnection,
265271
Transport: SniffingTransport,
@@ -277,9 +283,6 @@ export default class Client extends API {
277283
tls: null,
278284
caFingerprint: null,
279285
agent: null,
280-
headers: {
281-
'user-agent': `elasticsearch-js/${clientVersion} (${os.platform()} ${os.release()}-${os.arch()}; Node.js ${nodeVersion}; Transport ${transportVersion})`
282-
},
283286
nodeFilter: null,
284287
generateRequestId: null,
285288
name: 'elasticsearch-js',
@@ -289,12 +292,8 @@ export default class Client extends API {
289292
proxy: null,
290293
enableMetaHeader: true,
291294
maxResponseSize: null,
292-
maxCompressedResponseSize: null,
293-
redaction: {
294-
type: 'replace',
295-
additionalKeys: []
296-
}
297-
}, opts)
295+
maxCompressedResponseSize: null
296+
}, opts, { headers, redaction })
298297

299298
if (options.caFingerprint != null && isHttpConnection(opts.node ?? opts.nodes)) {
300299
throw new errors.ConfigurationError('You can\'t configure the caFingerprint with a http connection')

test/unit/client.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,28 @@ test('Custom headers', t => {
111111
t.end()
112112
})
113113

114+
test('Custom headers should merge, not overwrite', t => {
115+
const client = new Client({
116+
node: 'http://localhost:9200',
117+
headers: { foo: 'bar' }
118+
})
119+
t.ok(client.transport[symbols.kHeaders]['user-agent']?.startsWith('elasticsearch-js/'))
120+
t.end()
121+
})
122+
123+
test('Redaction options should merge, not overwrite', t => {
124+
const client = new Client({
125+
node: 'http://localhost:9200',
126+
// @ts-expect-error
127+
redaction: {
128+
additionalKeys: ['foo'],
129+
}
130+
})
131+
t.equal(client.transport[symbols.kRedaction].type, 'replace')
132+
t.match(client.transport[symbols.kRedaction].additionalKeys, ['foo'])
133+
t.end()
134+
})
135+
114136
test('Basic auth', async t => {
115137
t.plan(1)
116138

0 commit comments

Comments
 (0)