import type { TransportRequestOptions } from '@elastic/elasticsearch/lib/Transport' ; import { get } from 'lodash' ; import { set } from '@elastic/safer-lodash-set' ; import { OpenSearchClient } from '../../../opensearch' ; import { migrationRetryCallCluster } from '../../../opensearch/client/retry_call_cluster' ; import { Logger } from '../../../logging' ; const methods = [ 'bulk' , 'cat.templates' , 'clearScroll' , 'count' , 'indices.create' , 'indices.delete' , 'indices.deleteTemplate' , 'indices.get' , 'indices.getAlias' , 'indices.refresh' , 'indices.updateAliases' , 'reindex' , 'search' , 'scroll' , 'tasks.get' , ] as const ; type MethodName = typeof methods [ number ] ; export interface MigrationOpenSearchClient { bulk : OpenSearchClient [ 'bulk' ] ; cat : { templates : OpenSearchClient [ 'cat' ] [ 'templates' ] ; } ; clearScroll : OpenSearchClient [ 'clearScroll' ] ; count : OpenSearchClient [ 'count' ] ; indices : { create : OpenSearchClient [ 'indices' ] [ 'create' ] ; delete : OpenSearchClient [ 'indices' ] [ 'delete' ] ; deleteTemplate : OpenSearchClient [ 'indices' ] [ 'deleteTemplate' ] ; get : OpenSearchClient [ 'indices' ] [ 'get' ] ; getAlias : OpenSearchClient [ 'indices' ] [ 'getAlias' ] ; refresh : OpenSearchClient [ 'indices' ] [ 'refresh' ] ; updateAliases : OpenSearchClient [ 'indices' ] [ 'updateAliases' ] ; } ; reindex : OpenSearchClient [ 'reindex' ] ; search : OpenSearchClient [ 'search' ] ; scroll : OpenSearchClient [ 'scroll' ] ; tasks : { get : OpenSearchClient [ 'tasks' ] [ 'get' ] ; } ; } export function createMigrationOpenSearchClient ( client , log , delay ? ) { return methods . reduce ( ( acc , key ) => { set ( acc , key , async ( params ? , options ? ) => { const fn = get ( client , key ) ; if ( ! fn ) { throw new Error ( `unknown OpenSearchClient client method [${ key }]` ) ; } return await migrationRetryCallCluster ( ( ) => fn . call ( client , params , { maxRetries : 0 , ... options } ) , log , delay ) ; } ) ; return acc ; } , { } as MigrationOpenSearchClient ) ; }