diff --git a/docs/configuration.asciidoc b/docs/configuration.asciidoc index dc2664407..f2ca0f0c2 100644 --- a/docs/configuration.asciidoc +++ b/docs/configuration.asciidoc @@ -149,7 +149,9 @@ const client = new Client({ const client = new Client({ node: 'http://localhost:9200', - agent: () => new CustomAgent() + // the function takes as parameter the option + // object passed to the Connection constructor + agent: (opts) => new CustomAgent() }) const client = new Client({ diff --git a/lib/Connection.d.ts b/lib/Connection.d.ts index 00d1e676e..933a6a8eb 100644 --- a/lib/Connection.d.ts +++ b/lib/Connection.d.ts @@ -28,9 +28,9 @@ import * as https from 'https' import * as hpagent from 'hpagent' import { ConnectionOptions as TlsConnectionOptions } from 'tls' -export declare type agentFn = () => any; +export declare type agentFn = (opts: ConnectionOptions) => any; -interface ConnectionOptions { +export interface ConnectionOptions { url: URL; ssl?: TlsConnectionOptions; id?: string; diff --git a/lib/Connection.js b/lib/Connection.js index e1945b131..6bf38c495 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -53,7 +53,7 @@ class Connection { } if (typeof opts.agent === 'function') { - this.agent = opts.agent() + this.agent = opts.agent(opts) } else if (opts.agent === false) { this.agent = undefined } else { diff --git a/test/types/connection.test-d.ts b/test/types/connection.test-d.ts index df8910da6..66e9d257b 100644 --- a/test/types/connection.test-d.ts +++ b/test/types/connection.test-d.ts @@ -20,22 +20,35 @@ import { expectType } from 'tsd' import { URL } from 'url' import { Connection } from '../../' +import { ConnectionOptions } from '../../lib/Connection' -const conn = new Connection({ - url: new URL('http://localhost:9200'), - ssl: { ca: 'string' }, - id: 'id', - headers: {}, - agent: { keepAlive: false }, - status: 'alive', - roles: { master: true }, - auth: { username: 'username', password: 'password' } -}) +{ + const conn = new Connection({ + url: new URL('http://localhost:9200'), + ssl: { ca: 'string' }, + id: 'id', + headers: {}, + agent: { keepAlive: false }, + status: 'alive', + roles: { master: true }, + auth: { username: 'username', password: 'password' } + }) -expectType(conn) -expectType(conn.url) -expectType(conn.id) -expectType>(conn.headers) -expectType(conn.deadCount) -expectType(conn.resurrectTimeout) -expectType(conn.status) + expectType(conn) + expectType(conn.url) + expectType(conn.id) + expectType>(conn.headers) + expectType(conn.deadCount) + expectType(conn.resurrectTimeout) + expectType(conn.status) +} + +{ + const conn = new Connection({ + url: new URL('http://localhost:9200'), + agent (opts) { + expectType(opts) + return 'the agent' + } + }) +} diff --git a/test/unit/connection.test.js b/test/unit/connection.test.js index 0e3e6173a..c83147c8d 100644 --- a/test/unit/connection.test.js +++ b/test/unit/connection.test.js @@ -152,7 +152,7 @@ test('Basic (https with ssl agent)', t => { }) test('Custom http agent', t => { - t.plan(5) + t.plan(6) function handler (req, res) { t.match(req.headers, { @@ -172,7 +172,12 @@ test('Custom http agent', t => { agent.custom = true const connection = new Connection({ url: new URL(`http://localhost:${port}`), - agent: () => agent + agent: opts => { + t.match(opts, { + url: new URL(`http://localhost:${port}`) + }) + return agent + } }) t.true(connection.agent.custom) connection.request({