import {z} from 'zod' export type EsportsV2PlayerTimespan = '30d' | '60d' | '90d' | 'all'; export type GetApiValorantV2EsportsVlrPlayersPlayerIdArgs = {player: number, timespan?: EsportsV2PlayerTimespan | undefined}; export type GetApiValorantV2EsportsVlrPlayersPlayerIdOptions = {baseUrl?: string | undefined, apiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const esportsV2AgentPerformanceStats = z.object({acs: z.number(), adr: z.number(), apr: z.number(), assists: z.number().int().gte(0), deaths: z.number().int().gte(0), fdpr: z.number(), first_deaths: z.number().int().gte(0), first_kills: z.number().int().gte(0), fkpr: z.number(), kast: z.number(), kd: z.number(), kills: z.number().int().gte(0), kpr: z.number(), rating: z.number()}); export const esportsV2AgentUsage = z.object({count: z.number().int().gte(0), percentage: z.number(), rounds: z.number().int().gte(0)}); export const esportsV2PlayerAgentStats = z.object({agent: z.string(), stats: esportsV2AgentPerformanceStats, usage: esportsV2AgentUsage}); export const esportsV2Country = z.object({code: z.string(), name: z.string()}); export const esportsV2PlayerTeam = z.object({id: z.number().int().gte(0), logo: z.string(), name: z.string()}); export const esportsV2PlacementEvent = z.object({id: z.number().int().gte(0), name: z.string(), slug: z.string()}); export const esportsV2PlacementEntry = z.object({placement: z.string(), prize: z.string().nullable().optional(), stage: z.string(), team: z.string().nullable().optional()}); export const esportsV2EventPlacement = z.object({event: esportsV2PlacementEvent, placements: z.array(esportsV2PlacementEntry), year: z.string()}); export const esportsV2Social = z.object({platform: z.string(), url: z.string()}); export const esportsV2Player = z.object({agent_stats: z.array(esportsV2PlayerAgentStats), avatar: z.string().nullable().optional(), country: esportsV2Country.nullable().optional(), current_teams: z.array(esportsV2PlayerTeam), event_placements: z.array(esportsV2EventPlacement), id: z.number().int().gte(0), name: z.string(), past_teams: z.array(esportsV2PlayerTeam), real_name: z.string().nullable().optional(), socials: z.array(esportsV2Social), total_winnings: z.string().nullable().optional()}); export const esportsV2PlayerResponse = z.object({data: esportsV2Player, status: z.number().int().gte(0)}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get VLR player (v2) * * GET /valorant/v2/esports/vlr/players/{player_id} * * Also accepts auth: api_key_query */ export const getApiValorantV2EsportsVlrPlayersPlayerId = async (args: GetApiValorantV2EsportsVlrPlayersPlayerIdArgs, options?: GetApiValorantV2EsportsVlrPlayersPlayerIdOptions) => { const {baseUrl = "https://api.henrikdev.xyz", fetch: fetchFn = fetch, apiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.timespan != null) query.append("timespan", String(args.timespan)) if (apiKeyHeader !== undefined) headers.set("Authorization", apiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/valorant/v2/esports/vlr/players/${encodeURIComponent(String(args.player_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(esportsV2PlayerResponse, await res.json()) };