import {z} from 'zod' export type GetApiApiV1ClientCharacterRivalsArgs = {access_key?: string | undefined, region: 'us' | 'eu' | 'tw' | 'kr' | 'cn', realm: string, name: string, scope?: ('realm' | 'region' | 'world') | undefined, specId?: number | undefined}; export type GetApiApiV1ClientCharacterRivalsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const scope = z.enum(["realm", "region", "world"]); export const characterRivalEntry = 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 entries = z.array(characterRivalEntry); export const rivals = z.object({scope: scope, specId: z.number().int().nullable(), entries: entries}).nullable(); export const characterRivalsResponse = z.object({rivals: rivals}); 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 }; /** * Rivals leaderboard window (self +/- 2) around a character. Defaults to the class leaderboard; pass specId for the spec leaderboard. * * GET /api/v1/client/character-rivals */ export const getApiApiV1ClientCharacterRivals = async (args: GetApiApiV1ClientCharacterRivalsArgs, options?: GetApiApiV1ClientCharacterRivalsOptions) => { 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.scope != null) query.append("scope", String(args.scope)) if (args.specId != null) query.append("specId", String(args.specId)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/client/character-rivals${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(characterRivalsResponse, await res.json()) };