import { DEFAULT_SPACE_ID } from '../constants' ; const spaceContextRegex = /^\/s\/([a-z0-9_\-]+)/ ; param requestBasePath param serverBasePath returns private export function getSpaceIdFromPath ( requestBasePath ? , serverBasePath ? ) { if ( requestBasePath == null ) requestBasePath = '/' ; if ( serverBasePath == null ) serverBasePath = '/' ; const pathToCheck = stripServerBasePath ( requestBasePath , serverBasePath ) ; const matchResult = pathToCheck . match ( spaceContextRegex ) ; if ( ! matchResult || matchResult . length === 0 ) { return { spaceId : DEFAULT_SPACE_ID , pathHasExplicitSpaceIdentifier : false , } ; } const [ , spaceId ] = matchResult ; if ( ! spaceId ) { throw new Error ( `Unable to determine Space ID from request path: ${ requestBasePath }` ) ; } return { spaceId , pathHasExplicitSpaceIdentifier : true , } ; } param basePath param spaceId param requestedPath returns export function addSpaceIdToPath ( basePath = '/' , spaceId = '' , requestedPath = '' ) { if ( requestedPath && ! requestedPath . startsWith ( '/' ) ) { throw new Error ( `path must start with a /` ) ; } const normalizedBasePath = basePath . endsWith ( '/' ) ? basePath . slice ( 0 , - 1 ) : basePath ; if ( spaceId && spaceId !== DEFAULT_SPACE_ID ) { return `${ normalizedBasePath }/s/${ spaceId }${ requestedPath }` ; } return `${ normalizedBasePath }${ requestedPath }` || '/' ; } function stripServerBasePath ( requestBasePath , serverBasePath ) { if ( serverBasePath && serverBasePath !== '/' && requestBasePath . startsWith ( serverBasePath ) ) { return requestBasePath . substr ( serverBasePath . length ) ; } return requestBasePath ; }