import { format as urlFormat , parse as urlParse , UrlWithParsedQuery , UrlWithStringQuery , } from 'url' ; import { ReportingConfig } from '../../' ; import { TaskPayloadPNG } from '../png/types' ; import { TaskPayloadPDF } from '../printable_pdf/types' ; import { getAbsoluteUrlFactory } from './get_absolute_url' ; import { validateUrls } from './validate_urls' ; function isPngJob ( job ) { return ( job as TaskPayloadPNG ) . relativeUrl !== undefined ; } function isPdfJob ( job ) { return ( job as TaskPayloadPDF ) . objects !== undefined ; } export function getFullUrls ( config , job ) { const [ basePath , protocol , hostname , port ] = [ config . kbnConfig . get ( 'server' , 'basePath' ) , config . get ( 'kibanaServer' , 'protocol' ) , config . get ( 'kibanaServer' , 'hostname' ) , config . get ( 'kibanaServer' , 'port' ) , ] as string [ ] ; const getAbsoluteUrl = getAbsoluteUrlFactory ( { basePath , protocol , hostname , port } ) ; let relativeUrls = [ ] ; if ( isPngJob ( job ) ) { relativeUrls = [ job . relativeUrl ] ; } else if ( isPdfJob ( job ) ) { relativeUrls = job . objects . map ( ( obj ) => obj . relativeUrl ) ; } else { throw new Error ( `No valid URL fields found in Job Params! Expected \`job.relativeUrl\` or \`job.objects[{ relativeUrl }]\`` ) ; } validateUrls ( relativeUrls ) ; const urls = relativeUrls . map ( ( relativeUrl ) => { const parsedRelative = urlParse ( relativeUrl ) ; const jobUrl = getAbsoluteUrl ( { path : parsedRelative . pathname === null ? undefined : parsedRelative . pathname , hash : parsedRelative . hash === null ? undefined : parsedRelative . hash , search : parsedRelative . search === null ? undefined : parsedRelative . search , } ) ; const parsed = urlParse ( jobUrl , true ) ; if ( parsed . hash == null ) { throw new Error ( 'No valid hash in the URL! A hash is expected for the application to route to the intended visualization.' ) ; } if ( ! job . forceNow ) { return jobUrl ; } const visualizationRoute = urlParse ( parsed . hash . replace ( /^#/ , '' ) , true ) ; const transformedHash = urlFormat ( { pathname : visualizationRoute . pathname , query : { ... visualizationRoute . query , forceNow : job . forceNow , } , } ) ; return urlFormat ( { ... parsed , hash : transformedHash , } ) ; } ) ; return urls ; }