70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import {
|
|
getHeader,
|
|
getRequestHost,
|
|
defineEventHandler,
|
|
getRequestURL,
|
|
} from 'h3'
|
|
|
|
export default defineEventHandler(async event => {
|
|
const url = getRequestURL(event)
|
|
|
|
// 정적 자산, API, 파비콘 등은 제외하고 페이지 요청만 처리
|
|
if (
|
|
url.pathname.startsWith('/api/') ||
|
|
url.pathname.startsWith('/_nuxt/') ||
|
|
url.pathname.startsWith('/favicon') ||
|
|
url.pathname.includes('.') ||
|
|
url.pathname.startsWith('/_')
|
|
) {
|
|
return
|
|
}
|
|
|
|
const host =
|
|
(getHeader(event, 'host') || getRequestHost(event)).toString() || ''
|
|
const baseDomain = process.env.BASE_DOMAIN || '.onstove.com'
|
|
const isGameDomainExtractable = host.includes(baseDomain)
|
|
|
|
if (isGameDomainExtractable) {
|
|
const cleanHost = host.split(':')[0]
|
|
event.context.gameDomain = cleanHost
|
|
}
|
|
|
|
// gameData를 직접 가져와서 context에 저장 (API 호출 없이)
|
|
try {
|
|
const config = useRuntimeConfig()
|
|
const stoveApiBaseUrl = config.public.stoveApiUrl
|
|
const apiUrl = `${stoveApiBaseUrl}/pub-comm/v1.0/template/game`
|
|
|
|
// URL의 첫 번째 path를 lang_code로 사용 (파비콘, API 경로 제외)
|
|
const url = getRequestURL(event)
|
|
const pathSegments = url.pathname
|
|
.split('/')
|
|
.filter(
|
|
segment =>
|
|
segment &&
|
|
!segment.includes('favicon') &&
|
|
!segment.includes('api') &&
|
|
!segment.startsWith('_')
|
|
)
|
|
const langCode = pathSegments[0] || 'ko'
|
|
|
|
const queryParams: Record<string, string> = {
|
|
game_domain: event.context.gameDomain || '',
|
|
lang_code: langCode,
|
|
}
|
|
|
|
const response = await $fetch(apiUrl, {
|
|
query: queryParams,
|
|
})
|
|
|
|
// 타입 단언을 사용하여 response의 타입 오류를 해결
|
|
const res = response as { code?: number; value?: unknown }
|
|
|
|
if (res?.code === 0 && res && typeof res === 'object' && 'value' in res) {
|
|
event.context.gameData = res.value
|
|
}
|
|
} catch (error) {
|
|
console.error('gameData load error:', error)
|
|
}
|
|
})
|