import {z} from 'zod' export type GetApiGamesetPlaysGamesetsGameSetIdTopPlayersArgs = {gameSetId: string, limit?: number | undefined, cursor?: string | undefined, withExternalId?: boolean | undefined}; export type GetApiGamesetPlaysGamesetsGameSetIdTopPlayersOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const rankedPlayerStandings = z.object({userId: z.string(), externalId: z.string().optional(), username: z.string(), avatarUrl: z.string(), gamesPlayedCount: z.number(), correctAnswerCount: z.number()}); export const topPlayersForGameSetResponseEntity = z.object({items: z.array(rankedPlayerStandings), cursor: z.string().optional()}); 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 }; /** * List the top players of a game set (leaderboard) * * GET /gameset/plays/gamesets/{gameSetId}/top-players */ export const getApiGamesetPlaysGamesetsGameSetIdTopPlayers = async (args: GetApiGamesetPlaysGamesetsGameSetIdTopPlayersArgs, options: GetApiGamesetPlaysGamesetsGameSetIdTopPlayersOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.withExternalId != null) query.append("withExternalId", String(args.withExternalId)) const url = `${baseUrl.replace(/\/+$/, '')}/gameset/plays/gamesets/${encodeURIComponent(String(args.gameSetId))}/top-players${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(topPlayersForGameSetResponseEntity, await res.json()) };