import { intersection , isObject } from 'lodash' ; import { Auditor } from '../../audit_trail' ; import { Headers } from '../../http/router' ; import { LegacyAPICaller , LegacyCallAPIOptions } from './api_types' ; /**
 * Serves the same purpose as "normal" `ClusterClient` but exposes additional
 * `callAsCurrentUser` method that doesn't use credentials of the OpenSearch Dashboards internal
 * user (as `callAsInternalUser` does) to request OpenSearch API, but rather
 * passes HTTP headers extracted from the current user request to the API.
 *
 * See  LegacyScopedClusterClient .
 *
 *  deprecated Use  IScopedClusterClient .
 *  public export type ILegacyScopedClusterClient = Pick < LegacyScopedClusterClient , 'callAsCurrentUser' | 'callAsInternalUser' > ; deprecated Use  IScopedClusterClient .
 *  public export class LegacyScopedClusterClient implements ILegacyScopedClusterClient { constructor ( private readonly internalAPICaller , private readonly scopedAPICaller , private readonly headers ? , private readonly auditor ? ) { this . callAsCurrentUser = this . callAsCurrentUser . bind ( this ) ; this . callAsInternalUser = this . callAsInternalUser . bind ( this ) ; } /**
     * Calls specified `endpoint` with provided `clientParams` on behalf of the
     * OpenSearch Dashboards internal user.
     * See  LegacyAPICaller .
     *
     *  param endpoint param clientParams param options public callAsInternalUser ( endpoint , clientParams = { } , options ? ) { if ( this . auditor ) { this . auditor . add ( { message : endpoint , type : 'opensearch.call.internalUser' , } ) ; } return this . internalAPICaller ( endpoint , clientParams , options ) ; } /**
     * Calls specified `endpoint` with provided `clientParams` on behalf of the
     * user initiated request to the OpenSearch Dashboards server (via HTTP request headers).
     * See  LegacyAPICaller .
     *
     *  param endpoint param clientParams param options public callAsCurrentUser ( endpoint , clientParams = { } , options ? ) { const defaultHeaders = this . headers ; if ( defaultHeaders !== undefined ) { const customHeaders = clientParams . headers ; if ( isObject ( customHeaders ) ) { const duplicates = intersection ( Object . keys ( defaultHeaders ) , Object . keys ( customHeaders ) ) ; duplicates . forEach ( ( duplicate ) => { if ( defaultHeaders [ duplicate ] !== ( customHeaders as any ) [ duplicate ] ) { throw Error ( `Cannot override default header ${ duplicate }.` ) ; } } ) ; } clientParams . headers = Object . assign ( { } , clientParams . headers , this . headers ) ; } if ( this . auditor ) { this . auditor . add ( { message : endpoint , type : 'opensearch.call.currentUser' , } ) ; } return this . scopedAPICaller ( endpoint , clientParams , options ) ; } }