Allow the client name to be a symbol (#1254)

This commit is contained in:
Tomas Della Vedova
2020-07-13 12:38:16 +02:00
committed by GitHub
parent 9d30778614
commit 1a7727588e
6 changed files with 59 additions and 4 deletions

View File

@ -178,7 +178,7 @@ function generateRequestId (params, options) {
----
|`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`
|`opaqueIdPrefix`

2
index.d.ts vendored
View File

@ -88,7 +88,7 @@ interface ClientOptions {
headers?: Record<string, any>;
opaqueIdPrefix?: string;
generateRequestId?: generateRequestIdFn;
name?: string;
name?: string | symbol;
auth?: BasicAuth | ApiKeyAuth;
cloud?: {
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;
meta: {
context: TContext;
name: string;
name: string | symbol;
request: {
params: TransportRequestParams;
options: TransportRequestOptions;

View File

@ -205,7 +205,7 @@ test('Client name', t => {
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)
const client = new Client({
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('sniffOnStart', t => {
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>(
new Client({
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)
})