Skip to content

Commit 9d9bc6a

Browse files
Deep merge options on client instantiation (#2890) (#2897)
Co-authored-by: Josh Mock <[email protected]>
1 parent 9b595d5 commit 9d9bc6a

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/client.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,13 @@ export default class Client extends API {
256256
}
257257
}
258258

259-
const headers: Record<string, any> = {
259+
const headers: Record<string, any> = Object.assign({}, {
260260
'user-agent': `elasticsearch-js/${clientVersion} (${os.platform()} ${os.release()}-${os.arch()}; Node.js ${nodeVersion}; Transport ${transportVersion})`
261-
}
261+
}, opts.headers ?? {})
262262
if (opts.serverMode === 'serverless') headers['elastic-api-version'] = serverlessApiVersion
263263

264+
const redaction = Object.assign({}, { type: 'replace', additionalKeys: [] }, opts.redaction ?? {})
265+
264266
const options: Required<ClientOptions> = Object.assign({}, {
265267
Connection: UndiciConnection,
266268
Transport: opts.serverMode === 'serverless' ? Transport : SniffingTransport,
@@ -277,7 +279,6 @@ export default class Client extends API {
277279
tls: null,
278280
caFingerprint: null,
279281
agent: null,
280-
headers,
281282
nodeFilter: null,
282283
generateRequestId: null,
283284
name: 'elasticsearch-js',
@@ -288,12 +289,8 @@ export default class Client extends API {
288289
enableMetaHeader: true,
289290
maxResponseSize: null,
290291
maxCompressedResponseSize: null,
291-
redaction: {
292-
type: 'replace',
293-
additionalKeys: []
294-
},
295292
serverMode: 'stack'
296-
}, opts)
293+
}, opts, { headers, redaction })
297294

298295
if (options.caFingerprint != null && isHttpConnection(opts.node ?? opts.nodes)) {
299296
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
@@ -73,6 +73,28 @@ test('Custom headers', t => {
7373
t.end()
7474
})
7575

76+
test('Custom headers should merge, not overwrite', t => {
77+
const client = new Client({
78+
node: 'http://localhost:9200',
79+
headers: { foo: 'bar' }
80+
})
81+
t.ok(client.transport[symbols.kHeaders]['user-agent']?.startsWith('elasticsearch-js/'))
82+
t.end()
83+
})
84+
85+
test('Redaction options should merge, not overwrite', t => {
86+
const client = new Client({
87+
node: 'http://localhost:9200',
88+
// @ts-expect-error
89+
redaction: {
90+
additionalKeys: ['foo'],
91+
}
92+
})
93+
t.equal(client.transport[symbols.kRedaction].type, 'replace')
94+
t.match(client.transport[symbols.kRedaction].additionalKeys, ['foo'])
95+
t.end()
96+
})
97+
7698
test('Basic auth', async t => {
7799
t.plan(1)
78100

0 commit comments

Comments
 (0)