-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathurl.js
92 lines (69 loc) · 2.5 KB
/
url.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
export function getSiteUrl () {
// console.log( 'import.meta.site', import.meta.env )
const hasImportMeta = typeof import.meta !== 'undefined'
const hasImportMetaEnv = hasImportMeta && typeof import.meta.env !== 'undefined'
// Try PUBLIC_URL
if ( typeof process.env.PUBLIC_URL !== 'undefined' ) {
console.log('Has env.PUBLIC_URL')
return process.env.PUBLIC_URL
}
if ( hasImportMetaEnv && typeof import.meta.env.PUBLIC_URL !== 'undefined' ) {
console.log('Has PUBLIC_URL')
return import.meta.env.PUBLIC_URL
}
// Try process.env.URL
if ( typeof process.env.URL !== 'undefined' ) {
console.log('Has env.URL')
return process.env.URL
}
// Try Astro.site.origin
if ( typeof Astro !== 'undefined' ) {
console.log('Has Astro')
return Astro.site.origin
}
// Try URL
if ( hasImportMetaEnv && typeof import.meta.env.URL !== 'undefined' ) {
console.log('Has URL')
return import.meta.env.URL
}
throw new Error('Could not find site URL')
}
export function getApiUrl () {
const hasImportMeta = typeof import.meta !== 'undefined'
const hasImportMetaEnv = hasImportMeta && typeof import.meta.env !== 'undefined'
// Try PUBLIC_API_DOMAIN
if ( typeof process.env.PUBLIC_API_DOMAIN !== 'undefined' ) {
// console.log('Has env.PUBLIC_API_DOMAIN')
return process.env.PUBLIC_API_DOMAIN
}
if ( hasImportMetaEnv && typeof import.meta.env.PUBLIC_API_DOMAIN !== 'undefined' ) {
// console.log('Has PUBLIC_API_DOMAIN')
return import.meta.env.PUBLIC_API_DOMAIN
}
throw new Error('Could not find API URL')
}
export function getPartPartsFromUrl ( urlString ) {
if ( typeof urlString !== 'string' ) throw new Error('urlString must be a string')
const url = new URL( urlString, 'https://doesitarm.com' )
const pathParts = url.pathname
.replace(/^\/+/, '') // Trim slashes from the beginning
.replace(/\/+$/, '') // Trim slashes from the end
.split('/')
return pathParts
}
export function getPathPartsFromAstroRequest ( AstroRequest ) {
// Parse the request url
const url = new URL( AstroRequest.url, 'https://doesitarm.com' )
const [
routeType,
pathSlug,
subSlug
] = getPartPartsFromUrl ( AstroRequest.url )
return {
pathname: url.pathname,
routeType,
pathSlug,
subSlug,
params: Object.fromEntries( url.searchParams )
}
}