Files
web-temp/layers/composables/useGds.ts
2025-11-07 16:29:44 +09:00

158 lines
4.1 KiB
TypeScript

import type {
ReqGetGdsClientPolicyTotal,
ResGetGdsClientPolicyTotal,
} from '#layers/types/GdsType'
import { DEFAULT_LOCALE_CODE } from '@/i18n.config'
/**
* GDS
*
* @description https://wiki.smilegate.net/display/SDKAPIDOCU/51-09.+gds
*/
const COUNTRY_GROUPS = {
KOREA: ['KR'] as const,
NORTH_AMERICA: ['US', 'CA'] as const,
TAIWAN_HONG_KONG_MACAU: ['TW', 'HK', 'MO'] as const,
} as const
const ERROR_CODE = {
UNKNOWN: -99999,
} as const
const useGds = () => {
const countryCode = ref('')
const isKorea = ref(false)
const isTaiwanHongKongMacau = ref(false)
const isNorthAmerica = ref(false)
/**
* 국가별 플래그 업데이트
*/
const updateCountryFlags = (nation: string) => {
if (!nation) {
countryCode.value = ''
isKorea.value = false
isTaiwanHongKongMacau.value = false
isNorthAmerica.value = false
return
}
const upperNation = nation.toUpperCase()
countryCode.value = upperNation
isKorea.value = COUNTRY_GROUPS.KOREA.includes(upperNation as any)
isTaiwanHongKongMacau.value =
COUNTRY_GROUPS.TAIWAN_HONG_KONG_MACAU.includes(upperNation as any)
isNorthAmerica.value = COUNTRY_GROUPS.NORTH_AMERICA.includes(
upperNation as any
)
}
/**
* 에러 응답 생성
*/
const createErrorResponse = (message: string): ResGetGdsClientPolicyTotal => {
return {
code: ERROR_CODE.UNKNOWN,
message,
res_code: ERROR_CODE.UNKNOWN,
res_data: undefined as any,
}
}
/**
* Mock 응답 생성 (개발/QA 환경)
*/
const createMockResponse = (
qc: string,
clientLang: string
): ResGetGdsClientPolicyTotal => {
return {
code: 0,
message: '',
res_code: 0,
res_data: {
is_default: true,
nation: qc.toUpperCase(),
regulation: '',
timezone: '',
utc_offset: 0,
lang: clientLang || DEFAULT_LOCALE_CODE,
coverages: [],
},
}
}
/**
* 사전 등록 - 특정 국가 여부 조회 (IP 기반)
*/
const checkCountryByIp = async (
req: ReqGetGdsClientPolicyTotal
): Promise<ResGetGdsClientPolicyTotal> => {
try {
// runType 우선순위: req.runType > runtimeConfig.runType
const runtimeConfig = useRuntimeConfig()
const runType = req.runType || runtimeConfig.public.runType
const stoveApiBaseUrl = runtimeConfig.public.stoveApiUrl
// Mock 모드 (개발/QA 환경)
const isMockMode = runType !== 'live' && req.qc && req.qc !== ''
if (isMockMode) {
const mockQc = req.qc!.toUpperCase()
updateCountryFlags(mockQc)
return createMockResponse(mockQc, req.client_lang || '')
}
// 실제 API 호출
const url = `${stoveApiBaseUrl}/gds/v2/client/policy/total`
const query = {
policy_grp: req.policy_grp || 'onstove',
device_nation: req.device_nation || 'KR',
client_lang: req.client_lang || DEFAULT_LOCALE_CODE,
include_coverages: req.include_coverages ?? false,
}
const res = (await commonFetch('GET', url, {
query,
})) as ResGetGdsClientPolicyTotal
// 성공 응답 처리
if (res.res_code === 0 && res.res_data?.nation) {
updateCountryFlags(res.res_data.nation)
return res
}
// 실패 응답 처리
// eslint-disable-next-line no-console
console.error('[useGds].checkCountryByIp: Invalid response', res)
updateCountryFlags('')
return res
} catch (error) {
// 에러 로깅
// eslint-disable-next-line no-console
console.error('[useGds].checkCountryByIp: Exception', error)
// 상태 초기화
updateCountryFlags('')
// 에러 응답 반환
return createErrorResponse(
error instanceof Error ? error.message : String(error)
)
}
}
return {
// Reactive state
isKorea: readonly(isKorea),
isTaiwanHongKongMacau: readonly(isTaiwanHongKongMacau),
isNorthAmerica: readonly(isNorthAmerica),
countryCode: readonly(countryCode),
// Methods
checkCountryByIp,
}
}
export { useGds }