80 lines
2.7 KiB
TypeScript
80 lines
2.7 KiB
TypeScript
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<RiotLeagueEntryDto[]> {
|
|
return this.riotClientService.get<RiotLeagueEntryDto[]>(
|
|
`/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<string, unknown> {
|
|
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;
|
|
}
|