65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import { getHeader, defineEventHandler } from 'h3'
|
|
import { useRuntimeConfig } from 'nuxt/app'
|
|
import type {
|
|
GameDataResponse,
|
|
GameDataValue,
|
|
} from '../layers/types/api/gameData'
|
|
|
|
export default defineEventHandler(async event => {
|
|
const config = useRuntimeConfig()
|
|
const baseDomain = (config.public.baseDomain || '.onstove.com') as string
|
|
const stoveApiBaseUrl = config.public.stoveApiUrl
|
|
const apiUrl = `${stoveApiBaseUrl}/pub-comm/v1.0/template/game`
|
|
let gameAlias = ''
|
|
|
|
try {
|
|
// 미들웨어에서 설정한 gameAlias가 있다면 우선 사용
|
|
if (event.context.gameAlias) {
|
|
gameAlias = event.context.gameAlias
|
|
} else {
|
|
const host = getHeader(event, 'host') || ''
|
|
const isGameAliasExtractable = host.includes(baseDomain)
|
|
|
|
if (isGameAliasExtractable) {
|
|
const subdomain = host.split('.')[0]
|
|
if (subdomain && subdomain !== 'www') {
|
|
gameAlias = subdomain
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('gameAlias extraction error: ', error)
|
|
}
|
|
|
|
try {
|
|
const queryParams: Record<string, string> = {
|
|
game_alias: gameAlias,
|
|
}
|
|
|
|
const response = await $fetch<GameDataResponse>(apiUrl, {
|
|
query: queryParams,
|
|
})
|
|
|
|
if (response?.code === 0 && 'value' in response) {
|
|
event.context.gameData = response.value
|
|
|
|
// lang_codes를 사용해서 동적으로 i18n 설정 업데이트
|
|
if (
|
|
response.value.lang_codes &&
|
|
Array.isArray(response.value.lang_codes)
|
|
) {
|
|
event.context.availableLocales = response.value.lang_codes
|
|
event.context.defaultLocale =
|
|
response.value.default_lang_code ||
|
|
response.value.lang_codes[0] ||
|
|
'ko'
|
|
}
|
|
|
|
return response.value as GameDataValue
|
|
}
|
|
} catch (error) {
|
|
console.error(error)
|
|
return {}
|
|
}
|
|
})
|