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; }