import { useMemo } from 'react' ; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { HttpFetchError } from 'kibana/public' ; import { KBN_FIELD_TYPES } from '../../../../../../src/plugins/data/public' ; import type { GetTransformsAuditMessagesResponseSchema } from '../../../common/api_schemas/audit_messages' ; import type { DeleteTransformsRequestSchema , DeleteTransformsResponseSchema , } from '../../../common/api_schemas/delete_transforms' ; import type { FieldHistogramsRequestSchema , FieldHistogramsResponseSchema , } from '../../../common/api_schemas/field_histograms' ; import type { StartTransformsRequestSchema , StartTransformsResponseSchema , } from '../../../common/api_schemas/start_transforms' ; import type { StopTransformsRequestSchema , StopTransformsResponseSchema , } from '../../../common/api_schemas/stop_transforms' ; import type { GetTransformNodesResponseSchema , GetTransformsResponseSchema , PostTransformsPreviewRequestSchema , PostTransformsPreviewResponseSchema , PutTransformsRequestSchema , PutTransformsResponseSchema , } from '../../../common/api_schemas/transforms' ; import type { PostTransformsUpdateRequestSchema , PostTransformsUpdateResponseSchema , } from '../../../common/api_schemas/update_transforms' ; import type { GetTransformsStatsResponseSchema } from '../../../common/api_schemas/transforms_stats' ; import { TransformId } from '../../../common/types/transform' ; import { API_BASE_PATH } from '../../../common/constants' ; import { EsIndex } from '../../../common/types/es_index' ; import { useAppDependencies } from '../app_dependencies' ; import { SavedSearchQuery } from './use_search_items' ; export const DEFAULT_SAMPLER_SHARD_SIZE = 5000 ; export interface FieldHistogramRequestConfig { fieldName : string ; type ? : KBN_FIELD_TYPES ; } interface FetchOptions { asSystemRequest ? : boolean ; } export const useApi = ( ) => { const { http } = useAppDependencies ( ) ; return useMemo ( ( ) => ( { async getTransformNodes ( ) { try { return await http . get ( `${ API_BASE_PATH }transforms/_nodes` ) ; } catch ( e ) { return e ; } } , async getTransform ( transformId ) { try { return await http . get ( `${ API_BASE_PATH }transforms/${ transformId }` ) ; } catch ( e ) { return e ; } } , async getTransforms ( fetchOptions = { } ) { try { return await http . get ( `${ API_BASE_PATH }transforms` , fetchOptions ) ; } catch ( e ) { return e ; } } , async getTransformStats ( transformId ) { try { return await http . get ( `${ API_BASE_PATH }transforms/${ transformId }/_stats` ) ; } catch ( e ) { return e ; } } , async getTransformsStats ( fetchOptions = { } ) { try { return await http . get ( `${ API_BASE_PATH }transforms/_stats` , fetchOptions ) ; } catch ( e ) { return e ; } } , async createTransform ( transformId , transformConfig ) { try { return await http . put ( `${ API_BASE_PATH }transforms/${ transformId }` , { body : JSON . stringify ( transformConfig ) , } ) ; } catch ( e ) { return e ; } } , async updateTransform ( transformId , transformConfig ) { try { return await http . post ( `${ API_BASE_PATH }transforms/${ transformId }/_update` , { body : JSON . stringify ( transformConfig ) , } ) ; } catch ( e ) { return e ; } } , async deleteTransforms ( reqBody ) { try { return await http . post ( `${ API_BASE_PATH }delete_transforms` , { body : JSON . stringify ( reqBody ) , } ) ; } catch ( e ) { return e ; } } , async getTransformsPreview ( obj ) { try { return await http . post ( `${ API_BASE_PATH }transforms/_preview` , { body : JSON . stringify ( obj ) , } ) ; } catch ( e ) { return e ; } } , async startTransforms ( reqBody ) { try { return await http . post ( `${ API_BASE_PATH }start_transforms` , { body : JSON . stringify ( reqBody ) , } ) ; } catch ( e ) { return e ; } } , async stopTransforms ( transformsInfo ) { try { return await http . post ( `${ API_BASE_PATH }stop_transforms` , { body : JSON . stringify ( transformsInfo ) , } ) ; } catch ( e ) { return e ; } } , async getTransformAuditMessages ( transformId ) { try { return await http . get ( `${ API_BASE_PATH }transforms/${ transformId }/messages` ) ; } catch ( e ) { return e ; } } , async esSearch ( payload ) { try { return await http . post ( `${ API_BASE_PATH }es_search` , { body : JSON . stringify ( payload ) } ) ; } catch ( e ) { return e ; } } , async getEsIndices ( ) { try { return await http . get ( `/api/index_management/indices` ) ; } catch ( e ) { return e ; } } , async getHistogramsForFields ( indexPatternTitle , fields , query , runtimeMappings ? , samplerShardSize = DEFAULT_SAMPLER_SHARD_SIZE ) { try { return await http . post ( `${ API_BASE_PATH }field_histograms/${ indexPatternTitle }` , { body : JSON . stringify ( { query , fields , samplerShardSize , ... ( runtimeMappings !== undefined ? { runtimeMappings } : { } ) , } ) , } ) ; } catch ( e ) { return e ; } } , } ) , [ http ] ) ; } ;