import { HttpSetup } from 'src/core/public' ; import { IndexPatternMissingIndices } from '../../../common/index_patterns/lib' ; import { GetFieldsOptions , IIndexPatternsApiClient } from '../../../common/index_patterns/types' ; const API_BASE_URL = `/api/index_patterns/` ; export class IndexPatternsApiClient implements IIndexPatternsApiClient { private http : HttpSetup ; constructor ( http ) { this . http = http ; } private _request ( url , query ) { return this . http . fetch ( url , { query , } ) . catch ( ( resp ) => { if ( resp . body . statusCode === 404 && resp . body . attributes ?. code === 'no_matching_indices' ) { throw new IndexPatternMissingIndices ( resp . body . message ) ; } throw new Error ( resp . body . message || resp . body . error || `${ resp . body . statusCode } Response` ) ; } ) ; } private _getUrl ( path ) { return API_BASE_URL + path . filter ( Boolean ) . map ( encodeURIComponent ) . join ( '/' ) ; } getFieldsForTimePattern ( options = { } ) { const { pattern , lookBack , metaFields } = options ; const url = this . _getUrl ( [ '_fields_for_time_pattern' ] ) ; return this . _request ( url , { pattern , look_back : lookBack , meta_fields : metaFields , } ) . then ( ( resp ) => resp . fields ) ; } getFieldsForWildcard ( options = { } ) { const { pattern , metaFields , type , params } = options ; let url ; let query ; if ( type ) { url = this . _getUrl ( [ type , '_fields_for_wildcard' ] ) ; query = { pattern , meta_fields : metaFields , params : JSON . stringify ( params ) , } ; } else { url = this . _getUrl ( [ '_fields_for_wildcard' ] ) ; query = { pattern , meta_fields : metaFields , } ; } return this . _request ( url , query ) . then ( ( resp ) => resp . fields ) ; } }