import { Logger , LegacyAPICaller , OpenSearchClient } from 'opensearch-dashboards/server' ; export type CollectorFormatForBulkUpload < T , U > = ( result : T ) => { type : string ; payload : U ; } ; export type AllowedSchemaTypes = 'keyword' | 'text' | 'number' | 'boolean' | 'long' | 'date' | 'float' ; export interface SchemaField { type : string ; } export type RecursiveMakeSchemaFrom < U > = U extends object ? MakeSchemaFrom < U > : { type : AllowedSchemaTypes ; } ; export type MakeSchemaFrom < Base > = { [ Key in keyof Required < Base > ] : Required < Base > [ Key ] extends Array < infer U > ? { type : 'array' ; items : RecursiveMakeSchemaFrom < U > ; } : RecursiveMakeSchemaFrom < Required < Base > [ Key ] > ; } ; export interface CollectorOptions < T = unknown , U = T > { type : string ; init ? : Function ; schema ? : MakeSchemaFrom < T > ; fetch : ( callCluster : LegacyAPICaller , opensearchClient ? : OpenSearchClient ) => Promise < T > | T ; formatForBulkUpload ? : CollectorFormatForBulkUpload < T , U > ; isReady : ( ) => Promise < boolean > | boolean ; } export class Collector < T = unknown , U = T > { public readonly type : CollectorOptions < T , U > [ 'type' ] ; public readonly init ? : CollectorOptions < T , U > [ 'init' ] ; public readonly fetch : CollectorOptions < T , U > [ 'fetch' ] ; private readonly _formatForBulkUpload ? : CollectorFormatForBulkUpload < T , U > ; public readonly isReady : CollectorOptions < T , U > [ 'isReady' ] ; constructor ( protected readonly log , { type , init , fetch , formatForBulkUpload , isReady , ... options } ) { if ( type === undefined ) { throw new Error ( 'Collector must be instantiated with a options.type string property' ) ; } if ( typeof init !== 'undefined' && typeof init !== 'function' ) { throw new Error ( 'If init property is passed, Collector must be instantiated with a options.init as a function property' ) ; } if ( typeof fetch !== 'function' ) { throw new Error ( 'Collector must be instantiated with a options.fetch function property' ) ; } Object . assign ( this , options ) ; this . type = type ; this . init = init ; this . fetch = fetch ; this . isReady = typeof isReady === 'function' ? isReady : ( ) => true ; this . _formatForBulkUpload = formatForBulkUpload ; } public formatForBulkUpload ( result ) { if ( this . _formatForBulkUpload ) { return this . _formatForBulkUpload ( result ) ; } else { return this . defaultFormatterForBulkUpload ( result ) ; } } protected defaultFormatterForBulkUpload ( result ) { return { type : this . type , payload : ( result as unknown ) as U , } ; } }