Skip to content

Deep merge options on client instantiation #2890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,13 @@ export default class Client extends API {
}
}

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

const redaction = Object.assign({}, { type: 'replace', additionalKeys: [] }, opts.redaction ?? {})

const options: Required<ClientOptions> = Object.assign({}, {
Connection: UndiciConnection,
Transport: opts.serverMode === 'serverless' ? Transport : SniffingTransport,
Expand All @@ -277,7 +279,6 @@ export default class Client extends API {
tls: null,
caFingerprint: null,
agent: null,
headers,
nodeFilter: null,
generateRequestId: null,
name: 'elasticsearch-js',
Expand All @@ -288,12 +289,8 @@ export default class Client extends API {
enableMetaHeader: true,
maxResponseSize: null,
maxCompressedResponseSize: null,
redaction: {
type: 'replace',
additionalKeys: []
},
serverMode: 'stack'
}, opts)
}, opts, { headers, redaction })

if (options.caFingerprint != null && isHttpConnection(opts.node ?? opts.nodes)) {
throw new errors.ConfigurationError('You can\'t configure the caFingerprint with a http connection')
Expand Down
22 changes: 22 additions & 0 deletions test/unit/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ test('Custom headers', t => {
t.end()
})

test('Custom headers should merge, not overwrite', t => {
const client = new Client({
node: 'http://localhost:9200',
headers: { foo: 'bar' }
})
t.ok(client.transport[symbols.kHeaders]['user-agent']?.startsWith('elasticsearch-js/'))
t.end()
})

test('Redaction options should merge, not overwrite', t => {
const client = new Client({
node: 'http://localhost:9200',
// @ts-expect-error
redaction: {
additionalKeys: ['foo'],
}
})
t.equal(client.transport[symbols.kRedaction].type, 'replace')
t.match(client.transport[symbols.kRedaction].additionalKeys, ['foo'])
t.end()
})

test('Basic auth', async t => {
t.plan(1)

Expand Down