-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
storeCodeFromRoute.ts
49 lines (36 loc) · 1.47 KB
/
storeCodeFromRoute.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
38
39
40
41
42
43
44
45
46
47
48
49
import { RawLocation } from 'vue-router'
import config from 'config'
import { LocalizedRoute } from './types'
const getNormalizedPath = (matchedRouteOrUrl) => {
const matchingPath = matchedRouteOrUrl && (matchedRouteOrUrl.path || matchedRouteOrUrl)
return matchingPath && (matchingPath.length > 0 && matchingPath[0] !== '/') ? `/${matchingPath}` : matchingPath
}
const getUrl = (matchedRouteOrUrl) => {
const normalizedPath = getNormalizedPath(matchedRouteOrUrl)
if (matchedRouteOrUrl && typeof matchedRouteOrUrl === 'object') {
if (matchedRouteOrUrl['host']) {
return matchedRouteOrUrl['host'] + normalizedPath
}
return ''
}
return matchedRouteOrUrl
}
const isMatchingByPath = (matchedRouteOrUrl, store) => {
const normalizedPath = getNormalizedPath(matchedRouteOrUrl)
return normalizedPath.startsWith(`${store.url}/`) || normalizedPath === store.url
}
const isMatchingByDomainAndPath = (matchedRouteOrUrl, store) => {
const url = getUrl(matchedRouteOrUrl)
return url.startsWith(`${store.url}/`) || url === store.url
}
const storeCodeFromRoute = (matchedRouteOrUrl: LocalizedRoute | RawLocation | string): string => {
if (!matchedRouteOrUrl) return ''
for (let storeCode of config.storeViews.mapStoreUrlsFor) {
const store = config.storeViews[storeCode]
if (isMatchingByPath(matchedRouteOrUrl, store) || isMatchingByDomainAndPath(matchedRouteOrUrl, store)) {
return storeCode
}
}
return ''
}
export default storeCodeFromRoute