feat(api): add Riot account services

This commit is contained in:
dahyeon committed 2026-04-07 02:36:00 +09:00
1 parent 54db90b0df
commit 5361dc8adb
4 files changed
+396

No files matched your search

@@ -0,0 +1,128 @@
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<RiotAccountDto> {
return this.riotClientService.get<RiotAccountDto>(
`/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<RiotAccountDto> {
return this.riotClientService.get<RiotAccountDto>(
`/riot/account/v1/accounts/by-puuid/${encodeURIComponent(puuid)}`,
{
region: options.region,
signal: options.signal,
},
parseRiotAccountDto,
);
}
public async getRegionByPuuid(
puuid: string,
options: RiotAccountLookupOptions = {},
): Promise<RiotAccountRegionDto> {
return this.riotClientService.get<RiotAccountRegionDto>(
`/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<string, unknown> {
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';
}
@@ -0,0 +1,129 @@
import { Injectable } from '@nestjs/common';
import { RiotClientService } from './riot.client.service';
import {
RiotChampionMasteryDto,
RiotPlatformLookupOptions,
} from './riot.types';
interface MasteriesByPuuidOptions extends RiotPlatformLookupOptions {
count?: number;
}
const DEFAULT_TOP_MASTERY_COUNT = 10;
const MAX_TOP_MASTERY_COUNT = 10;
@Injectable()
export class RiotChampionMasteryService {
constructor(private readonly riotClientService: RiotClientService) {}
public async getByPuuid(
puuid: string,
options: RiotPlatformLookupOptions = {},
): Promise<RiotChampionMasteryDto[]> {
return this.riotClientService.get<RiotChampionMasteryDto[]>(
`/lol/champion-mastery/v4/champion-masteries/by-puuid/${encodeURIComponent(puuid)}`,
{
platform: options.platform ?? 'kr',
signal: options.signal,
},
parseMasteries,
);
}
public async getTopByPuuid(
puuid: string,
options: MasteriesByPuuidOptions = {},
): Promise<RiotChampionMasteryDto[]> {
const count = options.count ?? DEFAULT_TOP_MASTERY_COUNT;
if (
!Number.isInteger(count) ||
count <= 0 ||
count > MAX_TOP_MASTERY_COUNT
) {
throw new RangeError('count must be an integer between 1 and 10.');
}
return this.riotClientService.get<RiotChampionMasteryDto[]>(
`/lol/champion-mastery/v4/champion-masteries/by-puuid/${encodeURIComponent(puuid)}/top`,
{
platform: options.platform ?? 'kr',
signal: options.signal,
query: { count },
},
parseMasteries,
);
}
}
function parseMasteries(value: unknown): RiotChampionMasteryDto[] {
if (!Array.isArray(value)) {
throw new TypeError('Champion masteries response must be an array.');
}
return value.map((entry, index) => parseEntry(entry, index));
}
function parseEntry(value: unknown, index: number): RiotChampionMasteryDto {
if (!isRecord(value)) {
throw new TypeError(`championMasteries[${index}] must be an object.`);
}
const puuid = value.puuid;
const championId = value.championId;
const championLevel = value.championLevel;
const championPoints = value.championPoints;
if (typeof puuid !== 'string') {
throw new TypeError(`championMasteries[${index}].puuid must be a string.`);
}
if (typeof championId !== 'number' || !Number.isFinite(championId)) {
throw new TypeError(
`championMasteries[${index}].championId must be a number.`,
);
}
if (typeof championLevel !== 'number' || !Number.isFinite(championLevel)) {
throw new TypeError(
`championMasteries[${index}].championLevel must be a number.`,
);
}
if (typeof championPoints !== 'number' || !Number.isFinite(championPoints)) {
throw new TypeError(
`championMasteries[${index}].championPoints must be a number.`,
);
}
return {
...value,
puuid,
championId,
championLevel,
championPoints,
lastPlayTime:
typeof value.lastPlayTime === 'number' ? value.lastPlayTime : undefined,
championPointsSinceLastLevel:
typeof value.championPointsSinceLastLevel === 'number'
? value.championPointsSinceLastLevel
: undefined,
championPointsUntilNextLevel:
typeof value.championPointsUntilNextLevel === 'number'
? value.championPointsUntilNextLevel
: undefined,
markRequiredForNextLevel:
typeof value.markRequiredForNextLevel === 'number'
? value.markRequiredForNextLevel
: undefined,
tokensEarned:
typeof value.tokensEarned === 'number' ? value.tokensEarned : undefined,
championSeasonMilestone:
typeof value.championSeasonMilestone === 'number'
? value.championSeasonMilestone
: undefined,
milestoneGrades: Array.isArray(value.milestoneGrades)
? (value.milestoneGrades as unknown[]).filter(
(v): v is string => typeof v === 'string',
)
: undefined,
nextSeasonMilestone: isRecord(value.nextSeasonMilestone)
? value.nextSeasonMilestone
: undefined,
};
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
@@ -0,0 +1,79 @@
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;
}
@@ -0,0 +1,60 @@
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;
}