89 lines
2.7 KiB
TypeScript
89 lines
2.7 KiB
TypeScript
import { getHeader, getRequestHost, type H3Event } from 'h3'
|
|
|
|
/**
|
|
* 게임 도메인을 가져오는 컴포저블 함수
|
|
* 서버와 클라이언트 환경에서 모두 동작
|
|
* @param event - H3 이벤트 객체 (서버 미들웨어에서 직접 전달 가능)
|
|
* @returns 게임 도메인 문자열
|
|
*/
|
|
export const getGameDomain = (event?: H3Event): string => {
|
|
try {
|
|
let host = ''
|
|
|
|
// 클라이언트 환경에서는 window.location.host를 사용
|
|
if (import.meta.client) {
|
|
host = (window.location.host || '').split(':')[0]
|
|
}
|
|
|
|
// 서버 환경에서는 event 객체를 사용
|
|
if (import.meta.server) {
|
|
if (!event) return ''
|
|
|
|
// 미들웨어에서 설정한 gameDomain이 있다면 우선 사용
|
|
if (event.context.gameDomain) {
|
|
host = event.context.gameDomain
|
|
} else {
|
|
const serverHost =
|
|
getHeader(event, 'host') || getRequestHost(event) || ''
|
|
host = serverHost.split(':')[0]
|
|
}
|
|
}
|
|
|
|
if (!host) return ''
|
|
|
|
// dev2 호스트명인 경우 l9-dev.onstove.com을 사용
|
|
if (host === 'samplegame-dev2.onstove.com') {
|
|
return 'l9-dev.onstove.com'
|
|
}
|
|
|
|
return host
|
|
} catch (error) {
|
|
console.error('getGameDomain error:', error)
|
|
return ''
|
|
}
|
|
}
|
|
|
|
/**
|
|
* URL에서 언어 코드를 추출하는 함수
|
|
* @param url - URL 문자열
|
|
* @returns 언어 코드 문자열
|
|
*/
|
|
export const getPathLocale = (url?: string): string => {
|
|
const targetUrl = url || (import.meta.client ? window.location.pathname : '')
|
|
const cleanTargetUrl = targetUrl.endsWith('/')
|
|
? targetUrl.slice(0, -1)
|
|
: targetUrl
|
|
|
|
return cleanTargetUrl.split('/')[1]
|
|
}
|
|
|
|
/**
|
|
* URL에서 언어 코드 이후의 경로를 추출하는 함수
|
|
* @param url - URL 문자열
|
|
* @returns 언어 코드 이후의 경로 문자열
|
|
*/
|
|
export const getPathAfterLanguage = (url?: string): string => {
|
|
const targetUrl = url || (import.meta.client ? window.location.pathname : '')
|
|
const cleanTargetUrl = targetUrl.endsWith('/')
|
|
? targetUrl.slice(0, -1)
|
|
: targetUrl
|
|
|
|
// URL에서 언어 코드 패턴을 찾아서 그 뒤의 경로를 추출
|
|
// 예: /ko/about/story -> /about/story
|
|
// 예: /ko -> "" (빈 문자열)
|
|
const languagePattern = /^\/[a-z]{2}(-[a-z]{2})?\/(.+)$/
|
|
const match = cleanTargetUrl.match(languagePattern)
|
|
if (match && match[2]) {
|
|
return `/${match[2]}`
|
|
} else {
|
|
// 언어 코드만 있고 뒤에 아무것도 없는 경우 (예: /ko, /en, /zh-tw, /zh-cn)
|
|
const languageOnlyPattern = /^\/[a-z]{2}(-[a-z]{2})?$/
|
|
if (languageOnlyPattern.test(cleanTargetUrl)) {
|
|
return ''
|
|
} else {
|
|
// 언어 코드가 없는 경우 원본 경로 그대로 반환 (이미 /로 시작)
|
|
return cleanTargetUrl
|
|
}
|
|
}
|
|
}
|