Updated type definitions (#882)

* Updated type definitions

* Updated test
This commit is contained in:
Tomas Della Vedova
2019-06-19 09:14:19 +02:00
committed by delvedor
parent a4f893d563
commit 39bbd77bec
3 changed files with 39 additions and 7 deletions

22
index.d.ts vendored
View File

@ -20,7 +20,7 @@
/// <reference types="node" /> /// <reference types="node" />
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import { SecureContextOptions } from 'tls'; import { ConnectionOptions as TlsConnectionOptions } from 'tls';
import Transport, { import Transport, {
ApiResponse, ApiResponse,
RequestEvent, RequestEvent,
@ -31,6 +31,7 @@ import Transport, {
generateRequestIdFn, generateRequestIdFn,
TransportRequestCallback TransportRequestCallback
} from './lib/Transport'; } from './lib/Transport';
import { URL } from 'url';
import Connection, { AgentOptions, agentFn } from './lib/Connection'; import Connection, { AgentOptions, agentFn } from './lib/Connection';
import ConnectionPool, { ResurrectEvent } from './lib/ConnectionPool'; import ConnectionPool, { ResurrectEvent } from './lib/ConnectionPool';
import Serializer from './lib/Serializer'; import Serializer from './lib/Serializer';
@ -72,8 +73,22 @@ interface ClientExtends {
} }
// /Extend API // /Extend API
interface NodeOptions {
url: URL;
id?: string;
agent?: AgentOptions;
ssl?: TlsConnectionOptions;
headers?: anyObject;
roles?: {
master: boolean;
data: boolean;
ingest: boolean;
ml: boolean;
}
}
interface ClientOptions { interface ClientOptions {
node?: string | string[]; node?: string | string[] | NodeOptions | NodeOptions[];
nodes?: string | string[]; nodes?: string | string[];
Connection?: typeof Connection; Connection?: typeof Connection;
ConnectionPool?: typeof ConnectionPool; ConnectionPool?: typeof ConnectionPool;
@ -89,7 +104,7 @@ interface ClientOptions {
resurrectStrategy?: 'ping' | 'optimistic' | 'none'; resurrectStrategy?: 'ping' | 'optimistic' | 'none';
suggestCompression?: boolean; suggestCompression?: boolean;
compression?: 'gzip'; compression?: 'gzip';
ssl?: SecureContextOptions; ssl?: TlsConnectionOptions;
agent?: AgentOptions | agentFn; agent?: AgentOptions | agentFn;
nodeFilter?: nodeFilterFn; nodeFilter?: nodeFilterFn;
nodeSelector?: nodeSelectorFn | string; nodeSelector?: nodeSelectorFn | string;
@ -327,5 +342,6 @@ export {
ResurrectEvent, ResurrectEvent,
RequestParams, RequestParams,
ClientOptions, ClientOptions,
NodeOptions,
ClientExtendsCallbackOptions ClientExtendsCallbackOptions
}; };

6
lib/Connection.d.ts vendored
View File

@ -22,13 +22,13 @@
import { URL } from 'url'; import { URL } from 'url';
import { inspect, InspectOptions } from 'util'; import { inspect, InspectOptions } from 'util';
import * as http from 'http'; import * as http from 'http';
import { SecureContextOptions } from 'tls'; import { ConnectionOptions as TlsConnectionOptions } from 'tls';
export declare type agentFn = () => any; export declare type agentFn = () => any;
interface ConnectionOptions { interface ConnectionOptions {
url: URL; url: URL;
ssl?: SecureContextOptions; ssl?: TlsConnectionOptions;
id?: string; id?: string;
headers?: any; headers?: any;
agent?: AgentOptions | agentFn; agent?: AgentOptions | agentFn;
@ -59,7 +59,7 @@ export default class Connection {
ML: string; ML: string;
}; };
url: URL; url: URL;
ssl: SecureContextOptions | null; ssl: TlsConnectionOptions | null;
id: string; id: string;
headers: any; headers: any;
deadCount: number; deadCount: number;

View File

@ -27,13 +27,29 @@ import {
ResurrectEvent, ResurrectEvent,
events, events,
errors, errors,
ClientExtendsCallbackOptions ClientExtendsCallbackOptions,
NodeOptions
} from '../../index' } from '../../index'
import { TransportRequestParams, TransportRequestOptions } from '../../lib/Transport' import { TransportRequestParams, TransportRequestOptions } from '../../lib/Transport'
import { URL } from 'url'
const client = new Client({ node: 'http://localhost:9200' }) const client = new Client({ node: 'http://localhost:9200' })
const nodeOpts: NodeOptions = {
url: new URL('http://localhost:9200'),
id: 'winteriscoming',
headers: { 'foo': 'bar' },
roles: {
master: false,
data: true,
ingest: false,
ml: false
}
}
const client2 = new Client({ node: nodeOpts })
client.on(events.RESPONSE, (err: errors.ElasticsearchClientError | null, request: RequestEvent) => { client.on(events.RESPONSE, (err: errors.ElasticsearchClientError | null, request: RequestEvent) => {
if (err) console.log(err) if (err) console.log(err)
const { body, statusCode } = request const { body, statusCode } = request