import {z} from 'zod' export type GetApiApiV1AnalyticsLeaderboardArgs = {period?: ('daily' | 'weekly' | 'monthly' | 'all-time') | undefined, metric?: ('volume' | 'bids' | 'success-rate' | 'activity') | undefined, limit?: number | undefined}; export type GetApiApiV1AnalyticsLeaderboardOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1AnalyticsLeaderboardResponse = z.object({type: z.string(), properties: z.object({leaderboard: z.object({type: z.string(), items: z.object({type: z.string(), properties: z.object({organization: z.object({type: z.string(), properties: z.object({id: z.object({type: z.string()}), name: z.object({type: z.string()})})}), metrics: z.object({type: z.string(), properties: z.object({bidCount: z.object({type: z.string()}), volume: z.object({type: z.string()}), successRate: z.object({type: z.string()}), avgBidTime: z.object({type: z.string()})})}), rank: z.object({type: z.string()}), score: z.object({type: z.string()})})})}), period: z.object({type: z.string()}), metric: z.object({type: z.string()}), totalEntries: z.object({type: 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 }; /** * Get organization leaderboard * * GET /api/v1/analytics/leaderboard */ export const getApiApiV1AnalyticsLeaderboard = async (args: GetApiApiV1AnalyticsLeaderboardArgs, options?: GetApiApiV1AnalyticsLeaderboardOptions) => { const {baseUrl = "https://xga-auction-system.workers.dev", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period != null) query.append("period", String(args.period)) if (args.metric != null) query.append("metric", String(args.metric)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/analytics/leaderboard${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(getApiApiV1AnalyticsLeaderboardResponse, await res.json()) };