import { schema } from '@osd/config-schema' ; import { HttpServiceSetup , RequestHandlerContext } from 'opensearch-dashboards/server' ; import { IndexPatternsFetcher } from './fetcher' ; export function registerRoutes ( http ) { const parseMetaFields = ( metaFields ) => { let parsedFields = [ ] ; if ( typeof metaFields === 'string' ) { parsedFields = JSON . parse ( metaFields ) ; } else { parsedFields = metaFields ; } return parsedFields ; } ; const router = http . createRouter ( ) ; router . get ( { path : '/api/index_patterns/_fields_for_wildcard' , validate : { query : schema . object ( { pattern : schema . string ( ) , meta_fields : schema . oneOf ( [ schema . string ( ) , schema . arrayOf ( schema . string ( ) ) ] , { defaultValue : [ ] , } ) , } ) , } , } , async ( context , request , response ) => { const { callAsCurrentUser } = context . core . opensearch . legacy . client ; const indexPatterns = new IndexPatternsFetcher ( callAsCurrentUser ) ; const { pattern , meta_fields : metaFields } = request . query ; let parsedFields = [ ] ; try { parsedFields = parseMetaFields ( metaFields ) ; } catch ( error ) { return response . badRequest ( ) ; } try { const fields = await indexPatterns . getFieldsForWildcard ( { pattern , metaFields : parsedFields , } ) ; return response . ok ( { body : { fields } , headers : { 'content-type' : 'application/json' , } , } ) ; } catch ( error ) { if ( typeof error === 'object' && ! ! error ?. isBoom && ! ! error ?. output ?. payload && typeof error ?. output ?. payload === 'object' ) { const payload = error ?. output ?. payload ; return response . notFound ( { body : { message : payload . message , attributes : payload , } , } ) ; } else { return response . notFound ( ) ; } } } ) ; router . get ( { path : '/api/index_patterns/_fields_for_time_pattern' , validate : { query : schema . object ( { pattern : schema . string ( ) , interval : schema . maybe ( schema . string ( ) ) , look_back : schema . number ( { min : 1 } ) , meta_fields : schema . oneOf ( [ schema . string ( ) , schema . arrayOf ( schema . string ( ) ) ] , { defaultValue : [ ] , } ) , } ) , } , } , async ( context , request , response ) => { const { callAsCurrentUser } = context . core . opensearch . legacy . client ; const indexPatterns = new IndexPatternsFetcher ( callAsCurrentUser ) ; const { pattern , interval , look_back : lookBack , meta_fields : metaFields } = request . query ; let parsedFields = [ ] ; try { parsedFields = parseMetaFields ( metaFields ) ; } catch ( error ) { return response . badRequest ( ) ; } try { const fields = await indexPatterns . getFieldsForTimePattern ( { pattern , interval : interval ? interval : '' , lookBack , metaFields : parsedFields , } ) ; return response . ok ( { body : { fields } , headers : { 'content-type' : 'application/json' , } , } ) ; } catch ( error ) { return response . notFound ( ) ; } } ) ; }