import {z} from 'zod' export type LeaderboardState = 'OPEN' | 'FULL' | 'AWARDING' | 'ARCHIVED'; export type LeaderboardLeague = 'level1' | 'level2' | 'level3' | 'level4'; export type CreateLeaderboardDto = {state?: LeaderboardState | undefined, league: LeaderboardLeague, imageAssetId: string, distributorId: string, prizeName: string, prizeType: string, prizeValue: number, startDate: string, endDate: string, prizeDrawingDate: string, pointThreshold: number, numberOfPrizes: number, isVerified?: boolean | undefined, prizeNameShort?: string | undefined, imageSmallAssetId?: string | undefined, imageIconAssetId?: string | undefined, prizeTitle: string, prizeShortTitle: string, maxUsersThreshold: number, gameplaysThreshold: number, revenueThreshold?: number | undefined}; export type CreateApiV1AdminLeaderboardsArgs = {'x-idempotency-key': string, body: CreateLeaderboardDto}; export type CreateApiV1AdminLeaderboardsOptions = {baseUrl: string, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const leaderboardState = z.enum(["OPEN", "FULL", "AWARDING", "ARCHIVED"]); export const leaderboardLeague = z.enum(["level1", "level2", "level3", "level4"]); export const leaderboardAdmin = z.object({state: leaderboardState, league: leaderboardLeague, id: z.string(), prizeName: z.string(), prizeType: z.string(), prizeValue: z.number(), imageAssetId: z.string(), imageAssetUrl: z.string().optional(), startDate: z.string(), endDate: z.string(), prizeDrawingDate: z.string(), pointThreshold: z.number(), numberOfPrizes: z.number(), createdAt: z.string(), updatedAt: z.string(), distributorId: z.string(), version: z.number(), isVerified: z.boolean().optional(), prizeNameShort: z.string().optional(), imageSmallAssetId: z.string().optional(), imageSmallAssetUrl: z.string().optional(), imageIconAssetId: z.string().optional(), imageIconAssetUrl: z.string().optional(), prizeTitle: z.string(), prizeShortTitle: z.string(), maxUsersThreshold: z.number(), playerCount: z.number(), gameplays: z.number(), gameplaysThreshold: z.number().optional(), revenueThreshold: z.number()}); 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 }; /** * POST /v1/admin/leaderboards */ export const createApiV1AdminLeaderboards = async (args: CreateApiV1AdminLeaderboardsArgs, options: CreateApiV1AdminLeaderboardsOptions) => { const {baseUrl, fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-idempotency-key'] != null) headers.set("x-idempotency-key", String(args['x-idempotency-key'])) if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/admin/leaderboards${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(leaderboardAdmin, await res.json()) };