Skip to content

Commit 4fa6c12

Browse files
committed
Add elastic-api-version header to all requests
1 parent d2d11c4 commit 4fa6c12

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ if (clientVersion.includes('-')) {
5252
// clean prerelease
5353
clientVersion = clientVersion.slice(0, clientVersion.indexOf('-')) + 'p'
5454
}
55-
const clientVersionNoMeta = clientVersion.split('+')[0]
55+
const [clientVersionNoMeta, apiVersion] = clientVersion.split('+')
5656
let transportVersion: string = require('@elastic/transport/package.json').version // eslint-disable-line
5757
/* istanbul ignore next */
5858
if (transportVersion.includes('-')) {
@@ -162,7 +162,8 @@ export default class Client extends API {
162162
caFingerprint: null,
163163
agent: null,
164164
headers: {
165-
'user-agent': `elasticsearch-js/${clientVersion} Node.js ${nodeVersion}; Transport ${transportVersion}; (${os.platform()} ${os.release()} ${os.arch()})`
165+
'user-agent': `elasticsearch-js/${clientVersion} Node.js ${nodeVersion}; Transport ${transportVersion}; (${os.platform()} ${os.release()} ${os.arch()})`,
166+
'elastic-api-version': `${apiVersion.slice(0, 4)}-${apiVersion.slice(4, 6)}-${apiVersion.slice(6, 8)}`
166167
},
167168
generateRequestId: null,
168169
name: 'elasticsearch-js',

test/unit/client.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,27 @@ test('Meta header disabled', async t => {
404404
await client.transport.request({ method: 'GET', path: '/' })
405405
})
406406

407+
test('elastic-api-version header exists on all requests', async t => {
408+
t.plan(1)
409+
410+
const Connection = connection.buildMockConnection({
411+
onRequest (opts) {
412+
t.equal(opts.headers?.['elastic-api-version'], '2023-10-31')
413+
return {
414+
statusCode: 200,
415+
body: { hello: 'world' }
416+
}
417+
}
418+
})
419+
420+
const client = new Client({
421+
node: 'http://localhost:9200',
422+
Connection,
423+
})
424+
425+
await client.transport.request({ method: 'GET', path: '/' })
426+
})
427+
407428
test('caFingerprint', t => {
408429
const client = new Client({
409430
node: 'https://localhost:9200',

0 commit comments

Comments
 (0)