import { CoreStart , PluginInitializerContext , CoreSetup , Plugin } from 'src/core/public' ; import { fetchStreaming as fetchStreamingStatic , FetchStreamingParams } from './streaming' ; import { removeLeadingSlash } from '../common' ; import { createStreamingBatchedFunction , BatchedFunc , StreamingBatchedFunctionParams , } from './batching/create_streaming_batched_function' ; export interface BfetchPublicSetupDependencies { } export interface BfetchPublicStartDependencies { } export interface BfetchPublicContract { fetchStreaming : ( params : FetchStreamingParams ) => ReturnType < typeof fetchStreamingStatic > ; batchedFunction : < Payload , Result extends object > ( params : StreamingBatchedFunctionParams < Payload , Result > ) => BatchedFunc < Payload , Result > ; } export type BfetchPublicSetup = BfetchPublicContract ; export type BfetchPublicStart = BfetchPublicContract ; export class BfetchPublicPlugin implements Plugin < BfetchPublicSetup , BfetchPublicStart , BfetchPublicSetupDependencies , BfetchPublicStartDependencies > { private contract ! : BfetchPublicContract ; constructor ( private readonly initializerContext ) { } public setup ( core , plugins ) { const { version } = this . initializerContext . env . packageInfo ; const basePath = core . http . basePath . get ( ) ; const fetchStreaming = this . fetchStreaming ( version , basePath ) ; const batchedFunction = this . batchedFunction ( fetchStreaming ) ; this . contract = { fetchStreaming , batchedFunction , } ; return this . contract ; } public start ( core , plugins ) { return this . contract ; } public stop ( ) { } private fetchStreaming = ( version , basePath ) : BfetchPublicSetup [ 'fetchStreaming' ] => ( params ) => fetchStreamingStatic ( { ... params , url : `${ basePath }/${ removeLeadingSlash ( params . url ) }` , headers : { 'Content-Type' : 'application/json' , 'osd-version' : version , ... ( params . headers || { } ) , } , } ) ; private batchedFunction = ( fetchStreaming ) : BfetchPublicContract [ 'batchedFunction' ] => ( params ) => createStreamingBatchedFunction ( { ... params , fetchStreaming : params . fetchStreaming || fetchStreaming , } ) ; }