// Licensed to Elasticsearch B.V under one or more agreements. // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information import { URL } from 'url' import { expectType, expectError } from 'tsd' import { TransportGetConnectionOptions } from '../../lib/Transport' import { Client, Serializer, Connection, ConnectionPool, Transport, errors } from '../../' /** * `node` option */ expectType( new Client({ node: 'http://localhost:9200' }) ) expectType( new Client({ nodes: ['http://localhost:9200', 'http://localhost:9200'] }) ) expectType( new Client({ node: { url: new URL('http://localhost:9200'), id: 'my-node' } }) ) expectType( new Client({ nodes: [{ url: new URL('http://localhost:9200'), id: 'my-node-1' }, { url: new URL('http://localhost:9201'), id: 'my-node-2' }] }) ) expectError( // @ts-expect-error new Client({ node: 42 }) ) expectError( // @ts-expect-error new Client({ node: { url: 'http://localhost:9200', id: 'my-node' } }) ) /** * `maxRetries` option */ expectType( new Client({ node: 'http://localhost:9200', maxRetries: 5 }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', maxRetries: 'five' }) ) /** * `requestTimeout` option */ expectType( new Client({ node: 'http://localhost:9200', requestTimeout: 5 }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', requestTimeout: 'five' }) ) /** * `pingTimeout` option */ expectType( new Client({ node: 'http://localhost:9200', pingTimeout: 5 }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', pingTimeout: 'five' }) ) /** * `sniffInterval` option */ expectType( new Client({ node: 'http://localhost:9200', sniffInterval: 5 }) ) expectType( new Client({ node: 'http://localhost:9200', sniffInterval: false }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', sniffInterval: 'five' }) ) /** * `sniffOnStart` option */ expectType( new Client({ node: 'http://localhost:9200', sniffOnStart: true }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', sniffOnStart: 'no' }) ) /** * `sniffEndpoint` option */ expectType( new Client({ node: 'http://localhost:9200', sniffEndpoint: '/custom' }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', sniffEndpoint: false }) ) /** * `sniffOnConnectionFault` option */ expectType( new Client({ node: 'http://localhost:9200', sniffOnConnectionFault: true }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', sniffOnConnectionFault: 'yes' }) ) /** * `resurrectStrategy` option */ expectType( new Client({ node: 'http://localhost:9200', resurrectStrategy: 'ping' }) ) expectType( new Client({ node: 'http://localhost:9200', resurrectStrategy: 'optimistic' }) ) expectType( new Client({ node: 'http://localhost:9200', resurrectStrategy: 'none' }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', resurrectStrategy: 'custom' }) ) /** * `suggestCompression` option */ expectType( new Client({ node: 'http://localhost:9200', suggestCompression: true }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', suggestCompression: 'no' }) ) /** * `compression` option */ expectType( new Client({ node: 'http://localhost:9200', compression: 'gzip' }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', compression: 'deflate' }) ) /** * `headers` option */ expectType( new Client({ node: 'http://localhost:9200', headers: { foo: 'bar' } }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', headers: 'foo=bar' }) ) /** * `opaqueIdPrefix` option */ expectType( new Client({ node: 'http://localhost:9200', opaqueIdPrefix: 'foo-' }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', opaqueIdPrefix: 42 }) ) /** * `name` option */ expectType( new Client({ node: 'http://localhost:9200', name: 'foo' }) ) expectType( new Client({ node: 'http://localhost:9200', name: Symbol('foo') }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', name: 42 }) ) /** * `auth` option */ expectType( new Client({ node: 'http://localhost:9200', auth: { username: 'username', password: 'password' } }) ) expectType( new Client({ node: 'http://localhost:9200', auth: { apiKey: 'abcd' } }) ) expectType( new Client({ node: 'http://localhost:9200', auth: { apiKey: { api_key: 'foo', id: 'bar' } } }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', auth: 'password' }) ) /** * `cloud` option */ expectType( new Client({ cloud: { id: 'localhost:9200' } }) ) expectError( // @ts-expect-error new Client({ cloud: { id: 42 } }) ) /** * `agent` option */ expectType( new Client({ node: 'http://localhost:9200', agent: { keepAlive: true, keepAliveMsecs: 42, maxSockets: 42, maxFreeSockets: 42 } }) ) expectType( new Client({ node: 'http://localhost:9200', agent: false }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', agent: { // @ts-expect-error keepAlive: 'yes', // @ts-expect-error keepAliveMsecs: true, // @ts-expect-error maxSockets: 'all', maxFreeSockets: null } }) ) /** * `ssl` option */ expectType( new Client({ node: 'http://localhost:9200', ssl: { ca: 'cert', rejectUnauthorized: true } }) ) expectError( new Client({ node: 'http://localhost:9200', ssl: { // @ts-expect-error ca: 42, // @ts-expect-error rejectUnauthorized: 'yes' } }) ) /** * `generateRequestId` option */ expectType( new Client({ node: 'http://localhost:9200', generateRequestId (params, options) { return 'id' } }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', generateRequestId: 'id' }) ) /** * `nodeSelector` option */ expectType( new Client({ node: 'http://localhost:9200', nodeSelector (connections) { return connections[0] } }) ) expectType( new Client({ node: 'http://localhost:9200', nodeSelector: 'round-robin' }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', nodeSelector (connections) { return 'id' } }) ) /** * `nodeFilter` option */ expectType( new Client({ node: 'http://localhost:9200', nodeFilter (connection) { return true } }) ) expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', nodeFilter (connection) { return 'id' } }) ) /** * `Serializer` option */ { class CustomSerializer extends Serializer { deserialize (str: string) { return super.deserialize(str) } } expectType( new Client({ node: 'http://localhost:9200', Serializer: CustomSerializer }) ) } { class CustomSerializer { deserialize (str: string) { return JSON.parse(str) } } expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', Serializer: CustomSerializer }) ) } /** * `Connection` option */ { class CustomConnection extends Connection { close () { return super.close() } } expectType( new Client({ node: 'http://localhost:9200', Connection: CustomConnection }) ) } { class CustomConnection { close () { return Promise.resolve() } } expectError( new Client({ node: 'http://localhost:9200', // @ts-expect-error Connection: CustomConnection }) ) } /** * `ConnectionPool` option */ { class CustomConnectionPool extends ConnectionPool { empty () { return super.empty() } } expectType( new Client({ node: 'http://localhost:9200', ConnectionPool: CustomConnectionPool }) ) } { class CustomConnectionPool { empty () { return this } } expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', ConnectionPool: CustomConnectionPool }) ) } /** * `Transport` option */ { class CustomTransport extends Transport { getConnection (opts: TransportGetConnectionOptions) { return super.getConnection(opts) } } expectType( new Client({ node: 'http://localhost:9200', Transport: CustomTransport }) ) } { class CustomTransport { getConnection (opts: TransportGetConnectionOptions) { return null } } expectError( // @ts-expect-error new Client({ node: 'http://localhost:9200', Transport: CustomTransport }) ) } /** * `context` option */ expectType( new Client({ node: 'http://localhost:9200', context: { hello: 'world' } }) ) expectError( new Client({ node: 'http://localhost:9200', context: 'hello world' }) )