import {z} from 'zod' export type GetApiApiV1ClientRunReviewArgs = {access_key?: string | undefined, region: 'us' | 'eu' | 'tw' | 'kr' | 'cn', realm: string, name: string, dungeonId: number, keyLevel: number, clearTimeMs: number, affixes?: string | undefined, itemLevelMean?: number | undefined, scope?: ('realm' | 'region' | 'world') | undefined, specId?: number | undefined, completedAt?: string | undefined}; export type GetApiApiV1ClientRunReviewOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const current = z.object({percentile: z.number().gte(0).lte(100), populationCount: z.number().int(), generatedAt: z.string()}).nullable(); export const historical = z.object({percentile: z.number().gte(0).lte(100), populationCount: z.number().int(), generatedAt: z.string(), period: z.number().int(), locked: z.boolean()}).nullable(); export const itemLevel = z.object({average: z.number(), min: z.number(), maxExclusive: z.number(), current: current, historical: historical}).nullable(); export const type = z.literal("world"); export const slug = z.literal("world"); export const scope3 = z.object({type: type, id: z.string().nullable(), slug: slug}); export const world = z.object({current: current, historical: historical, itemLevel: itemLevel, scope: scope3}); export const type1 = z.literal("region"); export const scope4 = z.object({type: type1, id: z.number().int(), slug: z.string()}); export const region = z.object({current: current, historical: historical, itemLevel: itemLevel, scope: scope4}).nullable(); export const type2 = z.literal("subregion"); export const scope5 = z.object({type: type2, id: z.number().int(), slug: z.string()}); export const model4 = z.object({current: current, historical: historical, itemLevel: itemLevel, scope: scope5}); export const subRegions = z.array(model4); export const scopes = z.object({world: world, region: region, subRegions: subRegions}); export const keystonePace = z.object({current: current, historical: historical, itemLevel: itemLevel, scopes: scopes}).nullable(); export const runReviewPastRun = z.object({completedAt: z.string(), keyLevel: z.number().int(), clearTimeMs: z.number().int(), timed: z.boolean(), score: z.number().nullable()}); export const pastRuns = z.array(runReviewPastRun); export const scope = z.enum(["realm", "region", "world"]); export const runReviewRivalEntry = z.object({rank: z.number().int(), name: z.string(), realm: z.string(), score: z.number(), scoreColor: z.string().nullable(), classId: z.number().int().nullable(), specId: z.number().int().nullable(), isSelf: z.boolean()}); export const entries2 = z.array(runReviewRivalEntry); export const rivals1 = z.object({scope: scope, specId: z.number().int().nullable(), entries: entries2}).nullable(); export const runReviewResponse = z.object({percentile: z.number().nullable(), historicalPercentile: z.number().nullable(), historicalLocked: z.boolean(), keystonePace: keystonePace, runScore: z.number().nullable(), pastRuns: pastRuns, rivals: rivals1}); 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 }; /** * Aggregate review payload for a completed Mythic+ run: Keystone Pace, past dungeon runs, and nearby spec leaderboard rivals * * GET /api/v1/client/run-review */ export const getApiApiV1ClientRunReview = async (args: GetApiApiV1ClientRunReviewArgs, options?: GetApiApiV1ClientRunReviewOptions) => { const {baseUrl = "http://raider.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.access_key != null) query.append("access_key", String(args.access_key)) if (args.region != null) query.append("region", String(args.region)) if (args.realm != null) query.append("realm", String(args.realm)) if (args.name != null) query.append("name", String(args.name)) if (args.dungeonId != null) query.append("dungeonId", String(args.dungeonId)) if (args.keyLevel != null) query.append("keyLevel", String(args.keyLevel)) if (args.clearTimeMs != null) query.append("clearTimeMs", String(args.clearTimeMs)) if (args.affixes != null) query.append("affixes", String(args.affixes)) if (args.itemLevelMean != null) query.append("itemLevelMean", String(args.itemLevelMean)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.specId != null) query.append("specId", String(args.specId)) if (args.completedAt != null) query.append("completedAt", String(args.completedAt)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/client/run-review${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(runReviewResponse, await res.json()) };