-
Notifications
You must be signed in to change notification settings - Fork 26k
/
resolve-url.ts
37 lines (30 loc) · 1.04 KB
/
resolve-url.ts
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
28
29
30
31
32
33
34
35
36
37
import path from '../../../shared/lib/isomorphic/path'
function isStringOrURL(icon: any): icon is string | URL {
return typeof icon === 'string' || icon instanceof URL
}
function resolveUrl(url: null | undefined, metadataBase: URL | null): null
function resolveUrl(url: string | URL, metadataBase: URL | null): URL
function resolveUrl(
url: string | URL | null | undefined,
metadataBase: URL | null
): URL | null
function resolveUrl(
url: string | URL | null | undefined,
metadataBase: URL | null
): URL | null {
if (url instanceof URL) return url
if (!url) return null
try {
// If we can construct a URL instance from url, ignore metadataBase
const parsedUrl = new URL(url)
return parsedUrl
} catch (_) {}
if (!metadataBase) {
metadataBase = new URL(`http://localhost:${process.env.PORT || 3000}`)
}
// Handle relative or absolute paths
const basePath = metadataBase.pathname || ''
const joinedPath = path.join(basePath, url)
return new URL(joinedPath, metadataBase)
}
export { isStringOrURL, resolveUrl }