import {z} from 'zod' export type ValorantAffinity = 'na' | 'eu' | 'ap' | 'kr' | 'br' | 'latam'; export type GetApiValorantV1ByPuuidMmrAffinityPuuidArgs = {affinity: ValorantAffinity, puuid: string}; export type GetApiValorantV1ByPuuidMmrAffinityPuuidOptions = {baseUrl?: string | undefined, apiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const mMRDataImages = z.object({large: z.string(), small: z.string(), triangle_down: z.string(), triangle_up: z.string()}); export const mMRV1Data = z.object({currenttier: z.number().int(), currenttierpatched: z.string(), elo: z.number().int(), images: mMRDataImages, mmr_change_to_last_game: z.number().int(), name: z.string(), old: z.boolean(), ranking_in_tier: z.number().int(), tag: z.string()}); export const mMRV1Response = z.object({data: mMRV1Data, 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 MMR by PUUID (v1) * * GET /valorant/v1/by-puuid/mmr/{affinity}/{puuid} * * Also accepts auth: api_key_query */ export const getApiValorantV1ByPuuidMmrAffinityPuuid = async (args: GetApiValorantV1ByPuuidMmrAffinityPuuidArgs, options?: GetApiValorantV1ByPuuidMmrAffinityPuuidOptions) => { 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 (apiKeyHeader !== undefined) headers.set("Authorization", apiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/valorant/v1/by-puuid/mmr/${encodeURIComponent(String(args.affinity))}/${encodeURIComponent(String(args.puuid))}${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(mMRV1Response, await res.json()) };