Add apex league Riot client methods

This commit is contained in:
dahyeon committed 2026-07-03 00:48:59 -07:00
1 parent 8668234065
commit 67b4032ff0
2 files changed
+159

No files matched your search

@@ -178,6 +178,81 @@ describe('RiotApiClient', () => {
]);
});
it('fetches apex League-V4 lists by queue without legacy summoner ids', async () => {
const fetchMock = createFetchMock().mockResolvedValue(
jsonResponse({
tier: 'CHALLENGER',
queue: 'RANKED_SOLO_5x5',
entries: [
{
puuid: 'seed-puuid-1',
leaguePoints: 1200,
rank: 'I',
wins: 300,
losses: 210,
veteran: true,
inactive: false,
freshBlood: false,
hotStreak: true,
},
],
}),
);
const { client, waitForSlot } = createClient(fetchMock);
await expect(
client.getChallengerLeagueByQueue('RANKED_SOLO_5x5', 'kr'),
).resolves.toEqual({
tier: 'CHALLENGER',
queueType: 'RANKED_SOLO_5x5',
entries: [
{
puuid: 'seed-puuid-1',
tier: 'CHALLENGER',
queueType: 'RANKED_SOLO_5x5',
rank: 'I',
leaguePoints: 1200,
wins: 300,
losses: 210,
veteran: true,
inactive: false,
freshBlood: false,
hotStreak: true,
},
],
});
expect(waitForSlot).toHaveBeenCalledWith('kr');
expect(fetchMock).toHaveBeenCalledWith(
'https://kr.api.riotgames.com/lol/league/v4/challengerleagues/by-queue/RANKED_SOLO_5x5',
expect.any(Object),
);
});
it('rejects apex League-V4 list entries without puuid', async () => {
const fetchMock = createFetchMock().mockResolvedValue(
jsonResponse({
tier: 'MASTER',
queue: 'RANKED_SOLO_5x5',
entries: [
{
leaguePoints: 200,
rank: 'I',
wins: 120,
losses: 100,
},
],
}),
);
const { client } = createClient(fetchMock);
await expect(
client.getMasterLeagueByQueue('RANKED_SOLO_5x5', 'kr'),
).rejects.toThrow(
'Riot apex league entry response was missing string field "puuid".',
);
});
it('honors Retry-After for 429 responses and retries', async () => {
const sleep = jest
.fn<Promise<void>, [number]>()
@@ -47,6 +47,16 @@ export interface RiotLeagueEntryDto {
wins: number;
}
export interface RiotApexLeagueEntryDto extends RiotLeagueEntryDto {
puuid: string;
}
export interface RiotApexLeagueListDto {
entries: RiotApexLeagueEntryDto[];
queueType: string;
tier: string;
}
export interface RiotMatchIdsParams {
count?: number;
queue?: number;
@@ -265,6 +275,35 @@ export class RiotApiClient {
return this.parseLeagueEntries(response);
}
public async getChallengerLeagueByQueue(
queueType = 'RANKED_SOLO_5x5',
platformRoute: RiotPlatformRoute = 'kr',
): Promise<RiotApexLeagueListDto> {
return this.getApexLeagueByQueue(
'challengerleagues',
queueType,
platformRoute,
);
}
public async getGrandmasterLeagueByQueue(
queueType = 'RANKED_SOLO_5x5',
platformRoute: RiotPlatformRoute = 'kr',
): Promise<RiotApexLeagueListDto> {
return this.getApexLeagueByQueue(
'grandmasterleagues',
queueType,
platformRoute,
);
}
public async getMasterLeagueByQueue(
queueType = 'RANKED_SOLO_5x5',
platformRoute: RiotPlatformRoute = 'kr',
): Promise<RiotApexLeagueListDto> {
return this.getApexLeagueByQueue('masterleagues', queueType, platformRoute);
}
public async getMatchIds(
puuid: string,
params: RiotMatchIdsParams = {},
@@ -308,6 +347,18 @@ export class RiotApiClient {
return this.parseMatchPayload(response, 'match timeline');
}
private async getApexLeagueByQueue(
endpointName: 'challengerleagues' | 'grandmasterleagues' | 'masterleagues',
queueType: string,
platformRoute: RiotPlatformRoute,
): Promise<RiotApexLeagueListDto> {
const response = await this.requestJson(
platformRoute,
`${platformBaseUrl(platformRoute)}/lol/league/v4/${endpointName}/by-queue/${encodePathValue(queueType)}`,
);
return this.parseApexLeagueList(response);
}
private async requestJson(
routingValue: RiotPlatformRoute | RiotRegionalRoute,
url: string,
@@ -427,6 +478,39 @@ export class RiotApiClient {
});
}
private parseApexLeagueList(value: unknown): RiotApexLeagueListDto {
const record = requireRecord(value, 'apex league list');
const tier = requireString(record, 'tier', 'apex league list');
const queueType = requireString(record, 'queue', 'apex league list');
const entries = record.entries;
if (!Array.isArray(entries)) {
throw new RiotApiError(
'Riot apex league list response was missing array field "entries".',
);
}
return {
tier,
queueType,
entries: entries.map((entry) => {
const entryRecord = requireRecord(entry, 'apex league entry');
return {
puuid: requireString(entryRecord, 'puuid', 'apex league entry'),
queueType,
tier,
rank: requireString(entryRecord, 'rank', 'apex league entry'),
leaguePoints: numberOrFallback(entryRecord, 'leaguePoints', 0),
wins: numberOrFallback(entryRecord, 'wins', 0),
losses: numberOrFallback(entryRecord, 'losses', 0),
veteran: optionalBoolean(entryRecord, 'veteran', false),
inactive: optionalBoolean(entryRecord, 'inactive', false),
freshBlood: optionalBoolean(entryRecord, 'freshBlood', false),
hotStreak: optionalBoolean(entryRecord, 'hotStreak', false),
};
}),
};
}
private parseMatchIds(value: unknown): string[] {
if (!Array.isArray(value)) {
throw new RiotApiError('Riot match id response had an unexpected shape.');