Skip to content

Commit 1e6cf8b

Browse files
authored
Deep merge options on client instantiation (#2890)
1 parent 7be0f90 commit 1e6cf8b

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
@@ -98,6 +98,28 @@ test('Custom headers', t => {
9898
t.end()
9999
})
100100

101+
test('Custom headers should merge, not overwrite', t => {
102+
const client = new Client({
103+
node: 'http://localhost:9200',
104+
headers: { foo: 'bar' }
105+
})
106+
t.ok(client.transport[symbols.kHeaders]['user-agent']?.startsWith('elasticsearch-js/'))
107+
t.end()
108+
})
109+
110+
test('Redaction options should merge, not overwrite', t => {
111+
const client = new Client({
112+
node: 'http://localhost:9200',
113+
// @ts-expect-error
114+
redaction: {
115+
additionalKeys: ['foo'],
116+
}
117+
})
118+
t.equal(client.transport[symbols.kRedaction].type, 'replace')
119+
t.match(client.transport[symbols.kRedaction].additionalKeys, ['foo'])
120+
t.end()
121+
})
122+
101123
test('Basic auth', async t => {
102124
t.plan(1)
103125

0 commit comments

Comments
 (0)