[Backport 7.x] Allow the client name to be a symbol (#1257)

Co-authored-by: Tomas Della Vedova <delvedor@users.noreply.github.com>
This commit is contained in:
github-actions[bot]
2020-07-13 12:38:59 +02:00
committed by GitHub
parent 0dd5c3c186
commit 28f2be397c
6 changed files with 59 additions and 4 deletions

View File

@ -178,7 +178,7 @@ function generateRequestId (params, options) {
---- ----
|`name` |`name`
|`string` - The name to identify the client instance in the events. + |`string | symbol` - The name to identify the client instance in the events. +
_Default:_ `elasticsearch-js` _Default:_ `elasticsearch-js`
|`opaqueIdPrefix` |`opaqueIdPrefix`

2
index.d.ts vendored
View File

@ -88,7 +88,7 @@ interface ClientOptions {
headers?: Record<string, any>; headers?: Record<string, any>;
opaqueIdPrefix?: string; opaqueIdPrefix?: string;
generateRequestId?: generateRequestIdFn; generateRequestId?: generateRequestIdFn;
name?: string; name?: string | symbol;
auth?: BasicAuth | ApiKeyAuth; auth?: BasicAuth | ApiKeyAuth;
cloud?: { cloud?: {
id: string; id: string;

2
lib/Transport.d.ts vendored
View File

@ -52,7 +52,7 @@ export interface RequestEvent<TResponse = Record<string, any>, TContext = unknow
warnings: string[] | null; warnings: string[] | null;
meta: { meta: {
context: TContext; context: TContext;
name: string; name: string | symbol;
request: { request: {
params: TransportRequestParams; params: TransportRequestParams;
options: TransportRequestOptions; options: TransportRequestOptions;

View File

@ -205,7 +205,7 @@ test('Client name', t => {
t.end() t.end()
}) })
t.test('Is present in the event metadata', t => { t.test('Is present in the event metadata (as string)', t => {
t.plan(6) t.plan(6)
const client = new Client({ const client = new Client({
node: 'http://localhost:9200', node: 'http://localhost:9200',
@ -229,6 +229,31 @@ test('Client name', t => {
}) })
}) })
t.test('Is present in the event metadata (as symbol)', t => {
t.plan(6)
const symbol = Symbol('cluster')
const client = new Client({
node: 'http://localhost:9200',
Connection: MockConnection,
name: symbol
})
client.on('request', (err, { meta }) => {
t.error(err)
t.strictEqual(meta.name, symbol)
})
client.on('response', (err, { meta }) => {
t.error(err)
t.strictEqual(meta.name, symbol)
})
client.info((err, { meta }) => {
t.error(err)
t.strictEqual(meta.name, symbol)
})
})
t.test('Sniff and client name', t => { t.test('Sniff and client name', t => {
t.test('sniffOnStart', t => { t.test('sniffOnStart', t => {
t.plan(2) t.plan(2)

View File

@ -300,6 +300,13 @@ expectType<Client>(
}) })
) )
expectType<Client>(
new Client({
node: 'http://localhost:9200',
name: Symbol('foo')
})
)
expectError<errors.ConfigurationError>( expectError<errors.ConfigurationError>(
new Client({ new Client({
node: 'http://localhost:9200', node: 'http://localhost:9200',

View File

@ -1092,3 +1092,26 @@ test('Random selector', t => {
}) })
}) })
}) })
test('name property as string', t => {
t.plan(1)
const client = new Client({
node: 'http://localhost:9200',
name: 'client-name'
})
t.strictEqual(client.name, 'client-name')
})
test('name property as symbol', t => {
t.plan(1)
const symbol = Symbol('client-name')
const client = new Client({
node: 'http://localhost:9200',
name: symbol
})
t.strictEqual(client.name, symbol)
})