File size: 581 Bytes
c2b7eb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { isAbsoluteUrl } from './isAbsoluteUrl'

const withoutTrailingSlash = (url: string) => url.replace(/\/$/, '')
const withoutLeadingSlash = (url: string) => url.replace(/^\//, '')

export function joinUrls(
  base: string | undefined,
  url: string | undefined,
): string {
  if (!base) {
    return url!
  }
  if (!url) {
    return base
  }

  if (isAbsoluteUrl(url)) {
    return url
  }

  const delimiter = base.endsWith('/') || !url.startsWith('?') ? '/' : ''
  base = withoutTrailingSlash(base)
  url = withoutLeadingSlash(url)

  return `${base}${delimiter}${url}`
}