WARNING: Version 5.x has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
TypeScript support
editTypeScript support
editThe client offers a first-class support for TypeScript, since it ships the type definitions for every exposed API.
If you are using TypeScript you will be required to use snake_case style to define the API parameters instead of camelCase.
Other than the types for the surface API, the client offers the types for every request method, via the RequestParams
, if you need the types for a search request for instance, you can access them via RequestParams.Search
.
Every API that supports a body, accepts a generics which represents the type of the request body, if you don’t configure anything, it will default to any
.
For example:
import { RequestParams } from '@elastic/elasticsearch' interface SearchBody { query: { match: { foo: string } } } const searchParams: RequestParams.Search<SearchBody> = { index: 'test', body: { query: { match: { foo: 'bar' } } } } // This is valid as well const searchParams: RequestParams.Search = { index: 'test', body: { query: { match: { foo: 'bar' } } } }
You can find the type definiton of a response in ApiResponse
, which accepts a generics as well if you want to specify the body type, otherwise it defaults to any
.
interface SearchResponse<T> { hits: { hits: Array<{ _source: T; }> } } // Define the interface of the source object interface Source { foo: string } client.search(searchParams) .then((response: ApiResponse<SearchResponse<Source>>) => console.log(response)) .catch((err: Error) => {})