feat(riot): implement Riot Account Service
This commit is contained in:
1 parent
09d70aaf2f
commit
b899cfdae1
1 file changed
+80
@@ -0,0 +1,80 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { RiotClientService } from './riot.client.service';
|
||||
import {
|
||||
RiotAccountDto,
|
||||
RiotAccountLookupOptions,
|
||||
} 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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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 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';
|
||||
}
|
||||
Reference in new issue
Block a user