import url from 'url' ; import { HttpStart } from 'opensearch-dashboards/public' ; import { CREATE_PATH , getGotoPath } from '../../common/short_url_routes' ; export async function shortenUrl ( absoluteUrl , { basePath , post } ) { const parsedUrl = url . parse ( absoluteUrl ) ; if ( ! parsedUrl || ! parsedUrl . path ) { return ; } const path = parsedUrl . path . replace ( basePath , '' ) ; const hash = parsedUrl . hash ? parsedUrl . hash : '' ; const relativeUrl = path + hash ; const body = JSON . stringify ( { url : relativeUrl } ) ; const resp = await post ( CREATE_PATH , { body } ) ; return url . format ( { protocol : parsedUrl . protocol , host : parsedUrl . host , pathname : `${ basePath }${ getGotoPath ( resp . urlId ) }` , } ) ; }