Allow access to ElasticsearchClient's child function from core's TS interface (#126731)

* Expose child from core ElasticsearchClient

* Update docs

* Fix typecheck

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Mike Côté 2022-03-07 11:38:58 -05:00 committed by GitHub
parent 004f0d4daa
commit a4febd7709
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 11 additions and 31 deletions

View file

@ -9,5 +9,5 @@ Client used to query the elasticsearch cluster.
<b>Signature:</b>
```typescript
export declare type ElasticsearchClient = Omit<Client, 'connectionPool' | 'serializer' | 'extend' | 'child' | 'close' | 'diagnostic'>;
export declare type ElasticsearchClient = Omit<Client, 'connectionPool' | 'serializer' | 'extend' | 'close' | 'diagnostic'>;
```

View file

@ -15,7 +15,7 @@ import type { Client } from '@elastic/elasticsearch';
*/
export type ElasticsearchClient = Omit<
Client,
'connectionPool' | 'serializer' | 'extend' | 'child' | 'close' | 'diagnostic'
'connectionPool' | 'serializer' | 'extend' | 'close' | 'diagnostic'
>;
/**

View file

@ -886,7 +886,7 @@ export { EcsEventOutcome }
export { EcsEventType }
// @public
export type ElasticsearchClient = Omit<Client, 'connectionPool' | 'serializer' | 'extend' | 'child' | 'close' | 'diagnostic'>;
export type ElasticsearchClient = Omit<Client, 'connectionPool' | 'serializer' | 'extend' | 'close' | 'diagnostic'>;
// @public
export type ElasticsearchClientConfig = Pick<ElasticsearchConfig, 'customHeaders' | 'compression' | 'sniffOnStart' | 'sniffOnConnectionFault' | 'requestHeadersWhitelist' | 'sniffInterval' | 'hosts' | 'username' | 'password' | 'serviceAccountToken'> & {

View file

@ -9,7 +9,6 @@ import { Client } from '@elastic/elasticsearch';
import { loggingSystemMock } from 'src/core/server/mocks';
import { elasticsearchServiceMock } from '../../../../../src/core/server/mocks';
import { createWrappedScopedClusterClientFactory } from './wrap_scoped_cluster_client';
import { ElasticsearchClientWithChild } from '../types';
const esQuery = {
body: { query: { bool: { filter: { range: { '@timestamp': { gte: 0 } } } } } },
@ -41,9 +40,7 @@ describe('wrapScopedClusterClient', () => {
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient();
const childClient = elasticsearchServiceMock.createElasticsearchClient();
(
scopedClusterClient.asInternalUser as unknown as jest.Mocked<ElasticsearchClientWithChild>
).child.mockReturnValue(childClient as unknown as Client);
scopedClusterClient.asInternalUser.child.mockReturnValue(childClient as unknown as Client);
const asInternalUserWrappedSearchFn = childClient.search;
const wrappedSearchClient = createWrappedScopedClusterClientFactory({
@ -62,9 +59,7 @@ describe('wrapScopedClusterClient', () => {
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient();
const childClient = elasticsearchServiceMock.createElasticsearchClient();
(
scopedClusterClient.asCurrentUser as unknown as jest.Mocked<ElasticsearchClientWithChild>
).child.mockReturnValue(childClient as unknown as Client);
scopedClusterClient.asCurrentUser.child.mockReturnValue(childClient as unknown as Client);
const asCurrentUserWrappedSearchFn = childClient.search;
const wrappedSearchClient = createWrappedScopedClusterClientFactory({
@ -83,9 +78,7 @@ describe('wrapScopedClusterClient', () => {
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient();
const childClient = elasticsearchServiceMock.createElasticsearchClient();
(
scopedClusterClient.asInternalUser as unknown as jest.Mocked<ElasticsearchClientWithChild>
).child.mockReturnValue(childClient as unknown as Client);
scopedClusterClient.asInternalUser.child.mockReturnValue(childClient as unknown as Client);
const asInternalUserWrappedSearchFn = childClient.search;
const wrappedSearchClient = createWrappedScopedClusterClientFactory({
@ -106,9 +99,7 @@ describe('wrapScopedClusterClient', () => {
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient();
const childClient = elasticsearchServiceMock.createElasticsearchClient();
(
scopedClusterClient.asInternalUser as unknown as jest.Mocked<ElasticsearchClientWithChild>
).child.mockReturnValue(childClient as unknown as Client);
scopedClusterClient.asInternalUser.child.mockReturnValue(childClient as unknown as Client);
const asInternalUserWrappedSearchFn = childClient.search;
asInternalUserWrappedSearchFn.mockRejectedValueOnce(new Error('something went wrong!'));
@ -127,9 +118,7 @@ describe('wrapScopedClusterClient', () => {
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient();
const childClient = elasticsearchServiceMock.createElasticsearchClient();
(
scopedClusterClient.asInternalUser as unknown as jest.Mocked<ElasticsearchClientWithChild>
).child.mockReturnValue(childClient as unknown as Client);
scopedClusterClient.asInternalUser.child.mockReturnValue(childClient as unknown as Client);
const asInternalUserWrappedSearchFn = childClient.search;
// @ts-ignore incomplete return type
asInternalUserWrappedSearchFn.mockResolvedValue({});
@ -156,9 +145,7 @@ describe('wrapScopedClusterClient', () => {
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient();
const childClient = elasticsearchServiceMock.createElasticsearchClient();
(
scopedClusterClient.asInternalUser as unknown as jest.Mocked<ElasticsearchClientWithChild>
).child.mockReturnValue(childClient as unknown as Client);
scopedClusterClient.asInternalUser.child.mockReturnValue(childClient as unknown as Client);
const asInternalUserWrappedSearchFn = childClient.search;
// @ts-ignore incomplete return type
asInternalUserWrappedSearchFn.mockResolvedValue({ took: 333 });

View file

@ -21,7 +21,7 @@ import type {
AggregationsAggregate,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { IScopedClusterClient, ElasticsearchClient, Logger } from 'src/core/server';
import { ElasticsearchClientWithChild, RuleExecutionMetrics } from '../types';
import { RuleExecutionMetrics } from '../types';
import { Alert as Rule } from '../types';
type RuleInfo = Pick<Rule, 'name' | 'alertTypeId' | 'id'> & { spaceId: string };
@ -87,8 +87,7 @@ function wrapScopedClusterClient(opts: WrapScopedClusterClientOpts): IScopedClus
function wrapEsClient(opts: WrapEsClientOpts): ElasticsearchClient {
const { esClient, ...rest } = opts;
// Core hides access to .child via TS
const wrappedClient = (esClient as ElasticsearchClientWithChild).child({});
const wrappedClient = esClient.child({});
// Mutating the functions we want to wrap
wrappedClient.search = getWrappedSearchFn({ esClient: wrappedClient, ...rest });

View file

@ -5,12 +5,10 @@
* 2.0.
*/
import { Client } from '@elastic/elasticsearch';
import type {
IRouter,
RequestHandlerContext,
SavedObjectReference,
ElasticsearchClient,
IUiSettingsClient,
} from 'src/core/server';
import type { PublicMethodsOf } from '@kbn/utility-types';
@ -48,10 +46,6 @@ import { IAbortableClusterClient } from './lib/create_abortable_es_client_factor
export type WithoutQueryAndParams<T> = Pick<T, Exclude<keyof T, 'query' | 'params'>>;
export type SpaceIdToNamespaceFunction = (spaceId?: string) => string | undefined;
export interface ElasticsearchClientWithChild extends ElasticsearchClient {
child: Client['child'];
}
/**
* @public
*/