Files
lolympus-tournament-support/apps/api/src/modules/riot/riot-summoner.service.ts
T

61 lines
1.8 KiB
TypeScript

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<RiotSummonerDto> {
return this.riotClientService.get<RiotSummonerDto>(
`/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<string, unknown> {
return typeof value === 'object' && value !== null;
}