import {z} from 'zod' export type LeaderboardVisibility = 'general' | 'personal'; export type LeaderboardUpdate = {start_at?: number | undefined, end_at?: number | undefined, name?: string | undefined, description?: string | undefined, visibility?: LeaderboardVisibility | undefined}; export type UpdateApiV1LeaderboardsLeaderboardIdArgs = {leaderboard_id: string, body: LeaderboardUpdate}; export type UpdateApiV1LeaderboardsLeaderboardIdOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const leaderboardVisibility = z.enum(["general", "personal"]); export const leaderboardRead = z.object({name: z.string().max(255), description: z.string().max(512), visibility: leaderboardVisibility.optional(), start_at: z.number().int().optional(), end_at: z.number().int().optional(), id: z.string().max(32)}); 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 }; /** * Update Leaderboard * * PUT /v1/leaderboards/{leaderboard_id} */ export const updateApiV1LeaderboardsLeaderboardId = async (args: UpdateApiV1LeaderboardsLeaderboardIdArgs, options?: UpdateApiV1LeaderboardsLeaderboardIdOptions) => { const {baseUrl = "https://api.aghanim.com/s2s", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/leaderboards/${encodeURIComponent(String(args.leaderboard_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(leaderboardRead, await res.json()) };