import {z} from 'zod' export type GetApiApiLeaderboardByChurchChurchIdArgs = {church_id: number, ca_session?: (string | null) | undefined}; export type GetApiApiLeaderboardByChurchChurchIdOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const leaderboardEntry = z.object({rank: z.number().int(), user_id: z.string(), first_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), profile_picture: z.string().nullable().optional(), streak_count: z.number().int().optional(), church_id: z.number().int().nullable().optional(), church_name: z.string().nullable().optional()}); export const leaderboardResponse = z.object({success: z.boolean(), entries: z.array(leaderboardEntry), total_count: z.number().int(), user_rank: z.number().int().nullable().optional(), user_entry: leaderboardEntry.nullable().optional(), context_name: z.string().nullable().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 }; /** * Get Leaderboard By Church * * GET /api/leaderboard/by-church/{church_id} */ export const getApiApiLeaderboardByChurchChurchId = async (args: GetApiApiLeaderboardByChurchChurchIdArgs, options: GetApiApiLeaderboardByChurchChurchIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() const cookies: string[] = [] headers.set('accept', "application/json") if (args.ca_session != null) cookies.push("ca_session=" + encodeURIComponent(String(args.ca_session))) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) if (cookies.length > 0) headers.set('cookie', cookies.join('; ')) const url = `${baseUrl.replace(/\/+$/, '')}/api/leaderboard/by-church/${encodeURIComponent(String(args.church_id))}${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(leaderboardResponse, await res.json()) };