From 3dbbf1bfe08c0c601ed7e3085911198a6baed25f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EB=8B=A4=ED=98=84=20=28Dahyeon=20Park=29?= Date: Fri, 8 May 2026 04:18:00 +0900 Subject: [PATCH] fix(web): restore client utilities --- .gitignore | 2 +- apps/web/src/lib/api.ts | 78 +++++++++++++++++++++++ apps/web/src/lib/auth/MsalAppProvider.tsx | 10 +++ apps/web/src/lib/auth/msal.config.ts | 40 ++++++++++++ apps/web/src/lib/auth/token-provider.ts | 26 ++++++++ apps/web/src/lib/ddragon.ts | 71 +++++++++++++++++++++ apps/web/src/lib/league.ts | 63 ++++++++++++++++++ apps/web/src/lib/riot-history.ts | 47 ++++++++++++++ 8 files changed, 336 insertions(+), 1 deletion(-) create mode 100644 apps/web/src/lib/api.ts create mode 100644 apps/web/src/lib/auth/MsalAppProvider.tsx create mode 100644 apps/web/src/lib/auth/msal.config.ts create mode 100644 apps/web/src/lib/auth/token-provider.ts create mode 100644 apps/web/src/lib/ddragon.ts create mode 100644 apps/web/src/lib/league.ts create mode 100644 apps/web/src/lib/riot-history.ts diff --git a/.gitignore b/.gitignore index 4bf14116..605b62c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ bin/ -lib/ +/lib/ dist/ node_modules/ .pnpm-store/ diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.ts new file mode 100644 index 00000000..8a8d1864 --- /dev/null +++ b/apps/web/src/lib/api.ts @@ -0,0 +1,78 @@ +import { API_BASE_URL, getAuthUserId } from '../config'; + +export class ApiError extends Error { + readonly data: unknown; + readonly status: number; + + constructor(status: number, message: string, data: unknown) { + super(message); + this.name = 'ApiError'; + this.status = status; + this.data = data; + } +} + +type ApiFetchOptions = RequestInit & { + auth?: 'none' | 'dev'; +}; + +function resolveApiUrl(path: string) { + if (/^https?:\/\//i.test(path)) { + return path; + } + return `${API_BASE_URL}${path.startsWith('/') ? path : `/${path}`}`; +} + +function resolveErrorMessage(status: number, data: unknown) { + if (typeof data === 'object' && data !== null) { + const message = (data as { message?: unknown }).message; + if (typeof message === 'string') return message; + if (Array.isArray(message)) return message.join(', '); + } + return `API request failed (${status})`; +} + +async function readResponseBody(response: Response): Promise { + const contentType = response.headers.get('content-type') ?? ''; + if (contentType.includes('application/json')) { + return response.json(); + } + + const text = await response.text(); + return text ? text : null; +} + +export async function apiFetch( + path: string, + options: ApiFetchOptions = {}, +): Promise { + const { auth, headers, ...requestOptions } = options; + const requestHeaders = new Headers(headers); + + if (requestOptions.body && !requestHeaders.has('Content-Type')) { + requestHeaders.set('Content-Type', 'application/json'); + } + + if (auth !== 'none' && !requestHeaders.has('Authorization')) { + const authUserId = getAuthUserId(); + if (authUserId) { + requestHeaders.set('x-auth-user-id', authUserId); + } + } + + const response = await fetch(resolveApiUrl(path), { + ...requestOptions, + headers: requestHeaders, + }); + + if (response.status === 204) { + return undefined as T; + } + + const data = await readResponseBody(response); + if (!response.ok) { + throw new ApiError(response.status, resolveErrorMessage(response.status, data), data); + } + + return data as T; +} diff --git a/apps/web/src/lib/auth/MsalAppProvider.tsx b/apps/web/src/lib/auth/MsalAppProvider.tsx new file mode 100644 index 00000000..3034e81b --- /dev/null +++ b/apps/web/src/lib/auth/MsalAppProvider.tsx @@ -0,0 +1,10 @@ +import { MsalProvider } from '@azure/msal-react'; +import { msalInstance } from './msal.config'; + +export function MsalAppProvider({ children }: { children: React.ReactNode }) { + if (!msalInstance) { + return <>{children}; + } + + return {children}; +} diff --git a/apps/web/src/lib/auth/msal.config.ts b/apps/web/src/lib/auth/msal.config.ts new file mode 100644 index 00000000..e455cc9e --- /dev/null +++ b/apps/web/src/lib/auth/msal.config.ts @@ -0,0 +1,40 @@ +import { + PublicClientApplication, + type Configuration, +} from '@azure/msal-browser'; + +const clientId = (import.meta.env.VITE_MSAL_CLIENT_ID ?? '').trim(); +const authority = (import.meta.env.VITE_MSAL_AUTHORITY ?? '').trim(); +const redirectUri = (import.meta.env.VITE_MSAL_REDIRECT_URI ?? '').trim(); + +export const MSAL_API_SCOPE = ( + import.meta.env.VITE_MSAL_API_SCOPE ?? '' +).trim(); + +export const MSAL_SILENT_REDIRECT_URI = ( + import.meta.env.VITE_MSAL_SILENT_REDIRECT_URI ?? + `${window.location.origin}/auth/silent-callback.html` +).trim(); + +export const isMsalConfigured = Boolean( + clientId && + authority && + MSAL_API_SCOPE && + !authority.includes(''), +); + +export const msalConfig: Configuration = { + auth: { + clientId, + authority, + redirectUri: redirectUri || window.location.origin, + postLogoutRedirectUri: window.location.origin, + }, + cache: { + cacheLocation: 'localStorage', + }, +}; + +export const msalInstance = isMsalConfigured + ? new PublicClientApplication(msalConfig) + : null; diff --git a/apps/web/src/lib/auth/token-provider.ts b/apps/web/src/lib/auth/token-provider.ts new file mode 100644 index 00000000..05d63cad --- /dev/null +++ b/apps/web/src/lib/auth/token-provider.ts @@ -0,0 +1,26 @@ +import { + isMsalConfigured, + MSAL_API_SCOPE, + MSAL_SILENT_REDIRECT_URI, + msalInstance, +} from './msal.config'; + +export async function acquireAccessToken(): Promise { + if (!isMsalConfigured || !msalInstance) { + return null; + } + + const accounts = msalInstance.getAllAccounts(); + const account = msalInstance.getActiveAccount() ?? accounts[0]; + if (!account) { + return null; + } + + const response = await msalInstance.acquireTokenSilent({ + account, + scopes: [MSAL_API_SCOPE], + redirectUri: MSAL_SILENT_REDIRECT_URI, + }); + + return response.accessToken; +} diff --git a/apps/web/src/lib/ddragon.ts b/apps/web/src/lib/ddragon.ts new file mode 100644 index 00000000..7c4e31d4 --- /dev/null +++ b/apps/web/src/lib/ddragon.ts @@ -0,0 +1,71 @@ +import type { DdragonManifest } from '../api/types'; + +const FALLBACK_VERSION = '15.24.1'; +const FALLBACK_CDN = 'https://ddragon.leagueoflegends.com/cdn'; + +function cdn(manifest: DdragonManifest | null | undefined) { + return manifest?.cdn ?? FALLBACK_CDN; +} + +function version(manifest: DdragonManifest | null | undefined) { + return manifest?.version ?? FALLBACK_VERSION; +} + +export function championDisplayName( + manifest: DdragonManifest | null | undefined, + championId: number, +) { + return manifest?.champions[String(championId)]?.name ?? null; +} + +export function championIconUrl( + manifest: DdragonManifest | null | undefined, + championId: number, +) { + const champion = manifest?.champions[String(championId)]; + if (!champion) return null; + return `${cdn(manifest)}/${version(manifest)}/img/champion/${champion.key}.png`; +} + +export function profileIconUrl( + manifest: DdragonManifest | null | undefined, + profileIconId: number, +) { + return `${cdn(manifest)}/${version(manifest)}/img/profileicon/${profileIconId}.png`; +} + +export function itemIconUrl( + manifest: DdragonManifest | null | undefined, + itemId: number, +) { + if (!itemId) return null; + return `${cdn(manifest)}/${version(manifest)}/img/item/${itemId}.png`; +} + +export function summonerSpellIconUrl( + manifest: DdragonManifest | null | undefined, + spellId: number, +) { + const spell = manifest?.summonerSpells[String(spellId)]; + if (!spell) return null; + return `${cdn(manifest)}/${version(manifest)}/img/spell/${spell.key}.png`; +} + +export function runeStyleIconUrl( + manifest: DdragonManifest | null | undefined, + styleId: number | undefined, +) { + const style = manifest?.runesReforged.find((entry) => entry.id === styleId); + return style ? `${cdn(manifest)}/img/${style.icon}` : null; +} + +export function runeKeystoneIconUrl( + manifest: DdragonManifest | null | undefined, + runeId: number | undefined, +) { + const rune = manifest?.runesReforged + .flatMap((style) => style.slots.flatMap((slot) => slot.runes)) + .find((entry) => entry.id === runeId); + + return rune ? `${cdn(manifest)}/img/${rune.icon}` : null; +} diff --git a/apps/web/src/lib/league.ts b/apps/web/src/lib/league.ts new file mode 100644 index 00000000..b1fc390c --- /dev/null +++ b/apps/web/src/lib/league.ts @@ -0,0 +1,63 @@ +import type { Position, TeamSide } from '../api/types'; +import adcIcon from '../assets/position/position-bottom.svg'; +import jungleIcon from '../assets/position/position-jungle.svg'; +import midIcon from '../assets/position/position-middle.svg'; +import supportIcon from '../assets/position/position-support.svg'; +import topIcon from '../assets/position/position-top.svg'; + +export const POSITION_ORDER: Position[] = ['TOP', 'JGL', 'MID', 'ADC', 'SUP']; + +export const positionLabel: Record = { + TOP: '탑', + JGL: '정글', + MID: '미드', + ADC: '원딜', + SUP: '서폿', +}; + +export const positionIcon: Record = { + TOP: topIcon, + JGL: jungleIcon, + MID: midIcon, + ADC: adcIcon, + SUP: supportIcon, +}; + +const TEAM_TONES = [ + { + border: 'border-blue-500/20', + bg: 'bg-blue-500/5', + text: 'text-blue-400', + bar: 'bg-blue-500', + }, + { + border: 'border-red-500/20', + bg: 'bg-red-500/5', + text: 'text-red-400', + bar: 'bg-red-500', + }, + { + border: 'border-emerald-500/20', + bg: 'bg-emerald-500/5', + text: 'text-emerald-400', + bar: 'bg-emerald-500', + }, + { + border: 'border-amber-500/20', + bg: 'bg-amber-500/5', + text: 'text-amber-400', + bar: 'bg-amber-500', + }, +]; + +export function teamIndexLabel(teamIndex: number, fallback?: string) { + return fallback?.trim() || `${teamIndex}팀`; +} + +export function teamIndexColorClasses(teamIndex: number) { + return TEAM_TONES[(Math.max(1, teamIndex) - 1) % TEAM_TONES.length]; +} + +export function sideShortLabel(side: TeamSide) { + return side === 'blue' ? 'BLUE' : 'RED'; +} diff --git a/apps/web/src/lib/riot-history.ts b/apps/web/src/lib/riot-history.ts new file mode 100644 index 00000000..4f2c415c --- /dev/null +++ b/apps/web/src/lib/riot-history.ts @@ -0,0 +1,47 @@ +import type { RiotLane, RiotQueueGroup, RiotRankFilter } from '../api/types'; + +export const QUEUE_GROUP_TABS: Array<{ + label: string; + value: RiotQueueGroup; +}> = [ + { label: '소환사의 협곡', value: 'summoners-rift' }, + { label: '칼바람', value: 'aram' }, + { label: 'URF', value: 'urf' }, + { label: '아레나', value: 'arena' }, + { label: '내전', value: 'internal' }, +]; + +export const RANK_FILTER_TABS: Array<{ + label: string; + value: RiotRankFilter; +}> = [ + { label: '전체', value: 'all' }, + { label: '솔로랭크', value: 'solo' }, + { label: '자유랭크', value: 'flex' }, + { label: '일반', value: 'normal' }, +]; + +export const LANE_LABELS: Record = { + TOP: '탑', + JUNGLE: '정글', + MIDDLE: '미드', + BOTTOM: '원딜', + UTILITY: '서폿', + NONE: '기타', +}; + +const QUEUE_GROUP_VALUES = new Set( + QUEUE_GROUP_TABS.map((tab) => tab.value), +); + +const RANK_FILTER_VALUES = new Set( + RANK_FILTER_TABS.map((tab) => tab.value), +); + +export function isRiotQueueGroup(value: string | null): value is RiotQueueGroup { + return Boolean(value && QUEUE_GROUP_VALUES.has(value as RiotQueueGroup)); +} + +export function isRiotRankFilter(value: string | null): value is RiotRankFilter { + return Boolean(value && RANK_FILTER_VALUES.has(value as RiotRankFilter)); +}