import type { CoreSetup , KibanaRequest , LifecycleResponseFactory , OnPreRoutingToolkit , } from 'src/core/server' ; import { getSpaceIdFromPath } from '../../../common' ; export interface OnRequestInterceptorDeps { http : CoreSetup [ 'http' ] ; } export function initSpacesOnRequestInterceptor ( { http } ) { http . registerOnPreRouting ( async function spacesOnPreRoutingHandler ( request , response , toolkit ) { const serverBasePath = http . basePath . serverBasePath ; const path = request . url . pathname ; const { spaceId , pathHasExplicitSpaceIdentifier } = getSpaceIdFromPath ( path , serverBasePath ) ; if ( pathHasExplicitSpaceIdentifier ) { const reqBasePath = `/s/${ spaceId }` ; http . basePath . set ( request , reqBasePath ) ; const newPathname = path . substr ( reqBasePath . length ) || '/' ; return toolkit . rewriteUrl ( `${ newPathname }${ request . url . search }` ) ; } return toolkit . next ( ) ; } ) ; }