From 342bc2565e90bae674a2c7a56102413c799cd0a3 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: Wed, 15 Apr 2026 01:30:18 +0900 Subject: [PATCH] feat(api): implement Riot Account Service --- .../src/modules/riot/riot-account.service.ts | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 apps/api/src/modules/riot/riot-account.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..cd85bcd7 --- /dev/null +++ b/apps/api/src/modules/riot/riot-account.service.ts @@ -0,0 +1,80 @@ +import { Injectable } from '@nestjs/common'; +import { RiotClientService } from './riot.client.service'; +import { + RiotAccountDto, + RiotAccountLookupOptions, +} 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, + ); + } +} + +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 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'; +}