import {z} from 'zod' export type CreateApiV2RosterFamilyWarMetricsQueryArgs = {server_id: string, body: {playerTags: Array, metricId: 'war.hit_rate' | 'war.regular_hit_rate' | 'war.cwl_hit_rate' | 'war.hit_rate_confidence' | 'war.missed_attacks' | 'war.participation', parameters?: Record | undefined}}; export type CreateApiV2RosterFamilyWarMetricsQueryOptions = {baseUrl?: string | undefined, UserToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2RosterFamilyWarMetricsQueryResponse = z.object({metricId: z.string(), parameters: z.object({}), rows: z.array(z.object({playerTag: z.string(), value: z.unknown(), attackCount: z.union([z.number(), z.literal("NaN"), z.literal("Infinity"), z.literal("-Infinity")])})), evaluatedAt: z.string()}); 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 }; /** * Evaluate war history for current members of configured server clans * * POST /v2/roster/family/war-metrics/query * * Also accepts auth: BotToken */ export const createApiV2RosterFamilyWarMetricsQuery = async (args: CreateApiV2RosterFamilyWarMetricsQueryArgs, options?: CreateApiV2RosterFamilyWarMetricsQueryOptions) => { const {baseUrl = "https://api.clashk.ing", fetch: fetchFn = fetch, UserToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.server_id != null) query.append("server_id", String(args.server_id)) if (UserToken !== undefined) headers.set('authorization', `Bearer ${UserToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/roster/family/war-metrics/query${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV2RosterFamilyWarMetricsQueryResponse, await res.json()) };