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