From 5361dc8adb49172fd90a0d11bbe8835ac2f0677f 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: Tue, 7 Apr 2026 02:36:00 +0900 Subject: [PATCH] feat(api): add Riot account services --- .../src/modules/riot/riot-account.service.ts | 128 +++++++++++++++++ .../riot/riot-champion-mastery.service.ts | 129 ++++++++++++++++++ .../src/modules/riot/riot-league.service.ts | 79 +++++++++++ .../src/modules/riot/riot-summoner.service.ts | 60 ++++++++ 4 files changed, 396 insertions(+) create mode 100644 apps/api/src/modules/riot/riot-account.service.ts create mode 100644 apps/api/src/modules/riot/riot-champion-mastery.service.ts create mode 100644 apps/api/src/modules/riot/riot-league.service.ts create mode 100644 apps/api/src/modules/riot/riot-summoner.service.ts diff --git a/apps/api/src/modules/riot/riot-account.service.ts b/apps/api/src/modules/riot/riot-account.service.ts new file mode 100644 index 00000000..58970308 --- /dev/null +++ b/apps/api/src/modules/riot/riot-account.service.ts @@ -0,0 +1,128 @@ +import { Injectable } from '@nestjs/common'; +import { RiotClientService } from './riot.client.service'; +import { + RIOT_PLATFORM_ROUTES, + RiotAccountDto, + RiotAccountLookupOptions, + RiotAccountRegionDto, +} from './riot.types'; + +@Injectable() +export class RiotAccountService { + constructor(private readonly riotClientService: RiotClientService) {} + + public async getByRiotId( + gameName: string, + tagLine: string, + options: RiotAccountLookupOptions = {}, + ): Promise { + return this.riotClientService.get( + `/riot/account/v1/accounts/by-riot-id/${encodeURIComponent(gameName)}/${encodeURIComponent(tagLine)}`, + { + region: options.region, + signal: options.signal, + }, + parseRiotAccountDto, + ); + } + + public async getByPuuid( + puuid: string, + options: RiotAccountLookupOptions = {}, + ): Promise { + return this.riotClientService.get( + `/riot/account/v1/accounts/by-puuid/${encodeURIComponent(puuid)}`, + { + region: options.region, + signal: options.signal, + }, + parseRiotAccountDto, + ); + } + + public async getRegionByPuuid( + puuid: string, + options: RiotAccountLookupOptions = {}, + ): Promise { + return this.riotClientService.get( + `/riot/account/v1/region/by-game/lol/by-puuid/${encodeURIComponent(puuid)}`, + { + region: options.region ?? 'asia', + signal: options.signal, + }, + parseRiotAccountRegionDto, + ); + } +} + +function parseRiotAccountDto(value: unknown): RiotAccountDto { + if (!isRecord(value)) { + throw new TypeError('Riot account response must be an object.'); + } + + const puuid = value.puuid; + const gameName = value.gameName; + const tagLine = value.tagLine; + + if (!isNonEmptyString(puuid)) { + throw new TypeError('Riot account response is missing a valid puuid.'); + } + + if (!isOptionalString(gameName)) { + throw new TypeError('Riot account response has an invalid gameName.'); + } + + if (!isOptionalString(tagLine)) { + throw new TypeError('Riot account response has an invalid tagLine.'); + } + + return { + puuid, + gameName: gameName ?? null, + tagLine: tagLine ?? null, + }; +} + +function parseRiotAccountRegionDto(value: unknown): RiotAccountRegionDto { + if (!isRecord(value)) { + throw new TypeError('Riot account region response must be an object.'); + } + + const puuid = value.puuid; + const game = value.game; + const region = value.region; + + if (!isNonEmptyString(puuid)) { + throw new TypeError( + 'Riot account region response is missing a valid puuid.', + ); + } + if (!isNonEmptyString(game)) { + throw new TypeError( + 'Riot account region response is missing a valid game.', + ); + } + const platformRegion = region as (typeof RIOT_PLATFORM_ROUTES)[number]; + if ( + !isNonEmptyString(region) || + !RIOT_PLATFORM_ROUTES.includes(platformRegion) + ) { + throw new TypeError( + 'Riot account region response is missing a valid region.', + ); + } + + return { puuid, game, region: platformRegion }; +} + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null; +} + +function isNonEmptyString(value: unknown): value is string { + return typeof value === 'string' && value.length > 0; +} + +function isOptionalString(value: unknown): value is string | undefined { + return value === undefined || typeof value === 'string'; +} diff --git a/apps/api/src/modules/riot/riot-champion-mastery.service.ts b/apps/api/src/modules/riot/riot-champion-mastery.service.ts new file mode 100644 index 00000000..abfc58c2 --- /dev/null +++ b/apps/api/src/modules/riot/riot-champion-mastery.service.ts @@ -0,0 +1,129 @@ +import { Injectable } from '@nestjs/common'; +import { RiotClientService } from './riot.client.service'; +import { + RiotChampionMasteryDto, + RiotPlatformLookupOptions, +} from './riot.types'; + +interface MasteriesByPuuidOptions extends RiotPlatformLookupOptions { + count?: number; +} + +const DEFAULT_TOP_MASTERY_COUNT = 10; +const MAX_TOP_MASTERY_COUNT = 10; + +@Injectable() +export class RiotChampionMasteryService { + constructor(private readonly riotClientService: RiotClientService) {} + + public async getByPuuid( + puuid: string, + options: RiotPlatformLookupOptions = {}, + ): Promise { + return this.riotClientService.get( + `/lol/champion-mastery/v4/champion-masteries/by-puuid/${encodeURIComponent(puuid)}`, + { + platform: options.platform ?? 'kr', + signal: options.signal, + }, + parseMasteries, + ); + } + + public async getTopByPuuid( + puuid: string, + options: MasteriesByPuuidOptions = {}, + ): Promise { + const count = options.count ?? DEFAULT_TOP_MASTERY_COUNT; + if ( + !Number.isInteger(count) || + count <= 0 || + count > MAX_TOP_MASTERY_COUNT + ) { + throw new RangeError('count must be an integer between 1 and 10.'); + } + return this.riotClientService.get( + `/lol/champion-mastery/v4/champion-masteries/by-puuid/${encodeURIComponent(puuid)}/top`, + { + platform: options.platform ?? 'kr', + signal: options.signal, + query: { count }, + }, + parseMasteries, + ); + } +} + +function parseMasteries(value: unknown): RiotChampionMasteryDto[] { + if (!Array.isArray(value)) { + throw new TypeError('Champion masteries response must be an array.'); + } + return value.map((entry, index) => parseEntry(entry, index)); +} + +function parseEntry(value: unknown, index: number): RiotChampionMasteryDto { + if (!isRecord(value)) { + throw new TypeError(`championMasteries[${index}] must be an object.`); + } + const puuid = value.puuid; + const championId = value.championId; + const championLevel = value.championLevel; + const championPoints = value.championPoints; + if (typeof puuid !== 'string') { + throw new TypeError(`championMasteries[${index}].puuid must be a string.`); + } + if (typeof championId !== 'number' || !Number.isFinite(championId)) { + throw new TypeError( + `championMasteries[${index}].championId must be a number.`, + ); + } + if (typeof championLevel !== 'number' || !Number.isFinite(championLevel)) { + throw new TypeError( + `championMasteries[${index}].championLevel must be a number.`, + ); + } + if (typeof championPoints !== 'number' || !Number.isFinite(championPoints)) { + throw new TypeError( + `championMasteries[${index}].championPoints must be a number.`, + ); + } + return { + ...value, + puuid, + championId, + championLevel, + championPoints, + lastPlayTime: + typeof value.lastPlayTime === 'number' ? value.lastPlayTime : undefined, + championPointsSinceLastLevel: + typeof value.championPointsSinceLastLevel === 'number' + ? value.championPointsSinceLastLevel + : undefined, + championPointsUntilNextLevel: + typeof value.championPointsUntilNextLevel === 'number' + ? value.championPointsUntilNextLevel + : undefined, + markRequiredForNextLevel: + typeof value.markRequiredForNextLevel === 'number' + ? value.markRequiredForNextLevel + : undefined, + tokensEarned: + typeof value.tokensEarned === 'number' ? value.tokensEarned : undefined, + championSeasonMilestone: + typeof value.championSeasonMilestone === 'number' + ? value.championSeasonMilestone + : undefined, + milestoneGrades: Array.isArray(value.milestoneGrades) + ? (value.milestoneGrades as unknown[]).filter( + (v): v is string => typeof v === 'string', + ) + : undefined, + nextSeasonMilestone: isRecord(value.nextSeasonMilestone) + ? value.nextSeasonMilestone + : undefined, + }; +} + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null; +} diff --git a/apps/api/src/modules/riot/riot-league.service.ts b/apps/api/src/modules/riot/riot-league.service.ts new file mode 100644 index 00000000..a8c4614c --- /dev/null +++ b/apps/api/src/modules/riot/riot-league.service.ts @@ -0,0 +1,79 @@ +import { Injectable } from '@nestjs/common'; +import { RiotClientService } from './riot.client.service'; +import { + RiotLeagueEntryDto, + RiotPlatformLookupOptions, +} from './riot.types'; + +@Injectable() +export class RiotLeagueService { + constructor(private readonly riotClientService: RiotClientService) {} + + public async getEntriesByPuuid( + puuid: string, + options: RiotPlatformLookupOptions = {}, + ): Promise { + return this.riotClientService.get( + `/lol/league/v4/entries/by-puuid/${encodeURIComponent(puuid)}`, + { + platform: options.platform ?? 'kr', + signal: options.signal, + }, + parseLeagueEntries, + ); + } +} + +function parseLeagueEntries(value: unknown): RiotLeagueEntryDto[] { + if (!Array.isArray(value)) { + throw new TypeError('League entries response must be an array.'); + } + return value.map((entry, index) => parseEntry(entry, index)); +} + +function parseEntry(value: unknown, index: number): RiotLeagueEntryDto { + if (!isRecord(value)) { + throw new TypeError(`leagueEntries[${index}] must be an object.`); + } + return { + leagueId: requireString(value.leagueId, `leagueEntries[${index}].leagueId`), + queueType: requireString( + value.queueType, + `leagueEntries[${index}].queueType`, + ), + tier: requireString(value.tier, `leagueEntries[${index}].tier`), + rank: requireString(value.rank, `leagueEntries[${index}].rank`), + puuid: requireString(value.puuid, `leagueEntries[${index}].puuid`), + leaguePoints: requireNumber( + value.leaguePoints, + `leagueEntries[${index}].leaguePoints`, + ), + wins: requireNumber(value.wins, `leagueEntries[${index}].wins`), + losses: requireNumber(value.losses, `leagueEntries[${index}].losses`), + veteran: typeof value.veteran === 'boolean' ? value.veteran : undefined, + inactive: typeof value.inactive === 'boolean' ? value.inactive : undefined, + freshBlood: + typeof value.freshBlood === 'boolean' ? value.freshBlood : undefined, + hotStreak: + typeof value.hotStreak === 'boolean' ? value.hotStreak : undefined, + miniSeries: isRecord(value.miniSeries) ? value.miniSeries : undefined, + }; +} + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null; +} + +function requireString(value: unknown, path: string): string { + if (typeof value !== 'string' || value.length === 0) { + throw new TypeError(`${path} must be a non-empty string.`); + } + return value; +} + +function requireNumber(value: unknown, path: string): number { + if (typeof value !== 'number' || !Number.isFinite(value)) { + throw new TypeError(`${path} must be a finite number.`); + } + return value; +} diff --git a/apps/api/src/modules/riot/riot-summoner.service.ts b/apps/api/src/modules/riot/riot-summoner.service.ts new file mode 100644 index 00000000..30248294 --- /dev/null +++ b/apps/api/src/modules/riot/riot-summoner.service.ts @@ -0,0 +1,60 @@ +import { Injectable } from '@nestjs/common'; +import { RiotClientService } from './riot.client.service'; +import { + RiotPlatformLookupOptions, + RiotSummonerDto, +} from './riot.types'; + +@Injectable() +export class RiotSummonerService { + constructor(private readonly riotClientService: RiotClientService) {} + + public async getByPuuid( + puuid: string, + options: RiotPlatformLookupOptions = {}, + ): Promise { + return this.riotClientService.get( + `/lol/summoner/v4/summoners/by-puuid/${encodeURIComponent(puuid)}`, + { + platform: options.platform ?? 'kr', + signal: options.signal, + }, + parseSummoner, + ); + } +} + +function parseSummoner(value: unknown): RiotSummonerDto { + if (!isRecord(value)) { + throw new TypeError('Riot summoner response must be an object.'); + } + const puuid = value.puuid; + const profileIconId = value.profileIconId; + const revisionDate = value.revisionDate; + const summonerLevel = value.summonerLevel; + if (typeof puuid !== 'string' || puuid.length === 0) { + throw new TypeError('Summoner.puuid must be a non-empty string.'); + } + if (typeof profileIconId !== 'number') { + throw new TypeError('Summoner.profileIconId must be a number.'); + } + if (typeof revisionDate !== 'number') { + throw new TypeError('Summoner.revisionDate must be a number.'); + } + if (typeof summonerLevel !== 'number') { + throw new TypeError('Summoner.summonerLevel must be a number.'); + } + return { + puuid, + profileIconId, + revisionDate, + summonerLevel, + id: typeof value.id === 'string' ? value.id : undefined, + accountId: + typeof value.accountId === 'string' ? value.accountId : undefined, + }; +} + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null; +}