fix(web): restore client utilities
This commit is contained in:
1 parent
02222e1333
commit
3dbbf1bfe0
8 files changed
+336
-1
No files matched your search
+1
-1
@@ -1,5 +1,5 @@
|
||||
bin/
|
||||
lib/
|
||||
/lib/
|
||||
dist/
|
||||
node_modules/
|
||||
.pnpm-store/
|
||||
|
||||
@@ -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<unknown> {
|
||||
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<T>(
|
||||
path: string,
|
||||
options: ApiFetchOptions = {},
|
||||
): Promise<T> {
|
||||
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;
|
||||
}
|
||||
@@ -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 <MsalProvider instance={msalInstance}>{children}</MsalProvider>;
|
||||
}
|
||||
@@ -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('<tenant-id>'),
|
||||
);
|
||||
|
||||
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;
|
||||
@@ -0,0 +1,26 @@
|
||||
import {
|
||||
isMsalConfigured,
|
||||
MSAL_API_SCOPE,
|
||||
MSAL_SILENT_REDIRECT_URI,
|
||||
msalInstance,
|
||||
} from './msal.config';
|
||||
|
||||
export async function acquireAccessToken(): Promise<string | null> {
|
||||
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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<Position, string> = {
|
||||
TOP: '탑',
|
||||
JGL: '정글',
|
||||
MID: '미드',
|
||||
ADC: '원딜',
|
||||
SUP: '서폿',
|
||||
};
|
||||
|
||||
export const positionIcon: Record<Position, string> = {
|
||||
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';
|
||||
}
|
||||
@@ -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<RiotLane | string, string> = {
|
||||
TOP: '탑',
|
||||
JUNGLE: '정글',
|
||||
MIDDLE: '미드',
|
||||
BOTTOM: '원딜',
|
||||
UTILITY: '서폿',
|
||||
NONE: '기타',
|
||||
};
|
||||
|
||||
const QUEUE_GROUP_VALUES = new Set<RiotQueueGroup>(
|
||||
QUEUE_GROUP_TABS.map((tab) => tab.value),
|
||||
);
|
||||
|
||||
const RANK_FILTER_VALUES = new Set<RiotRankFilter>(
|
||||
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));
|
||||
}
|
||||
Reference in new issue
Block a user