import { modifyUrl } from '@osd/std' ; import { ensureRawRequest , OpenSearchDashboardsRequest , LegacyRequest } from './router' ; public export class BasePath { private readonly basePathCache = new WeakMap < LegacyRequest , string > ( ) ; /**
     * returns the server's basePath
     *
     * See  BasePath . get for getting the basePath value for a specific request
      public readonly serverBasePath : string ; internal constructor ( serverBasePath = '' ) { this . serverBasePath = serverBasePath ; } /**
     * returns `basePath` value, specific for an incoming request.
     */ public get = ( request ) => { const requestScopePath = this . basePathCache . get ( ensureRawRequest ( request ) ) || '' ; return `${ this . serverBasePath }${ requestScopePath }` ; } ; privateRemarks public set = ( request , requestSpecificBasePath ) => { const rawRequest = ensureRawRequest ( request ) ; if ( this . basePathCache . has ( rawRequest ) ) { throw new Error ( 'Request basePath was previously set. Setting multiple times is not supported.' ) ; } this . basePathCache . set ( rawRequest , requestSpecificBasePath ) ; } ; /**
     * Prepends `path` with the basePath.
     */ public prepend = ( path ) : string => { if ( this . serverBasePath === '' ) return path ; return modifyUrl ( path , ( parts ) => { if ( ! parts . hostname && parts . pathname && parts . pathname . startsWith ( '/' ) ) { parts . pathname = `${ this . serverBasePath }${ parts . pathname }` ; } } ) ; } ; /**
     * Removes the prepended basePath from the `path`.
     */ public remove = ( path ) : string => { if ( this . serverBasePath === '' ) { return path ; } if ( path === this . serverBasePath ) { return '/' ; } if ( path . startsWith ( `${ this . serverBasePath }/` ) ) { return path . slice ( this . serverBasePath . length ) ; } return path ; } ; } /**
 * Access or manipulate the OpenSearch Dashboards base path
 *
 *  BasePath public export type IBasePath = Pick < BasePath , keyof BasePath > ;