import {z} from 'zod' export type GetApiCommunityIdBadgeBadgeIdLeaderboardArgs = {id: string, badgeId: string, metric?: ('time' | 'score' | 'rank' | 'position' | 'points') | undefined, metricDirection?: ('ASC' | 'DESC') | undefined, perPage: unknown, page: unknown}; export type GetApiCommunityIdBadgeBadgeIdLeaderboardOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const communityMemberBadgeLeaderboardResponseDto = z.object({name: z.string(), earnedAt: z.string(), claimedAt: z.string(), wallet: z.string(), username: z.string(), displayName: z.string(), avatar: z.string(), rewards: z.object({}), "metadata.[key]": z.object({})}); export const getApiCommunityIdBadgeBadgeIdLeaderboardResponse = z.array(communityMemberBadgeLeaderboardResponseDto); 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 badge leaderboard * * GET /community/{id}/badge/{badgeId}/leaderboard */ export const getApiCommunityIdBadgeBadgeIdLeaderboard = async (args: GetApiCommunityIdBadgeBadgeIdLeaderboardArgs, options?: GetApiCommunityIdBadgeBadgeIdLeaderboardOptions) => { const {baseUrl = "https://api.openpage.fun/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.metric != null) query.append("metric", String(args.metric)) if (args.metricDirection != null) query.append("metricDirection", String(args.metricDirection)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (args.page != null) query.append("page", String(args.page)) if (apiKey !== undefined) headers.set("X-Api-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/community/${encodeURIComponent(String(args.id))}/badge/${encodeURIComponent(String(args.badgeId))}/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(getApiCommunityIdBadgeBadgeIdLeaderboardResponse, await res.json()) };