-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
56 lines (41 loc) · 1.74 KB
/
utils.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const runtimeConfig = useRuntimeConfig()
export function replaceOauthSubdomain(requestSubdomainSegments) {
const segments = [...requestSubdomainSegments]
if (segments[0]?.toLowerCase() === 'oauth') {
segments[0] = 'www'
}
return segments
}
export function addJsonToPath(path) {
if (!path.endsWith('.json')) {
path = `${path}.json`
}
return path
}
export function addZDomain(requestDomainSegments) {
const segments = [...requestDomainSegments]
if (segments.length === 1 && segments[0] !== 'localhost') {
segments.push(...runtimeConfig.public.baseServiceDomain.split('.'))
}
return segments
}
export function constructSiteUrl(siteUrl, requestUrl, {path, search, replaceOauthSubdomain: shouldReplaceOauthSubdomain = false, addJsonToPath: shouldAddJsonToPath = false} = {}) {
path ||= requestUrl.pathname
search ||= requestUrl.searchParams
const requestDomain = requestUrl.hostname
const requestDomainSegments = requestDomain.split('.')
let requestSubdomainSegments = requestDomainSegments.slice(0, -2)
let siteUrlDomainSegments = siteUrl.host.split('.')
if (shouldReplaceOauthSubdomain) {
requestSubdomainSegments = replaceOauthSubdomain(requestSubdomainSegments)
}
if (shouldAddJsonToPath) {
path = addJsonToPath(path)
}
let requestSubdomain = requestSubdomainSegments.join('.')
requestSubdomain = requestSubdomain ? `${requestSubdomain}.` : ''
siteUrlDomainSegments = addZDomain(siteUrlDomainSegments)
let siteUrlHost = siteUrlDomainSegments.join('.')
siteUrlHost = siteUrl.port ? `${siteUrlHost}:${siteUrl.port}` : siteUrlHost
return new URL(`${siteUrl.protocol}//${requestSubdomain}${siteUrlHost}${path}?${search}`);
}