import {z} from 'zod' export type GetApiV1GameplaysIdArgs = {id: string}; export type GetApiV1GameplaysIdOptions = {baseUrl: string, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const gameType = z.enum(["POLL", "PREDICTION", "INSTANT_POLL", "TRIVIA", "SURVEY", "THIS_THAT"]); export const gameplayStates = z.enum(["waiting_for_match", "matched", "declared_winner", "declared_loser", "won", "lost", "canceled"]); export const side = z.enum(["A", "B"]); export const ticketType = z.enum(["white", "black", "free"]); export const rewardType = z.literal("microsoft"); export const achievementEntity = z.object({id: z.string(), userId: z.string(), createdAt: z.string(), expireAt: z.string(), achievementId: z.string()}); export const videoUrls = z.object({hls: z.string(), mp4360: z.string(), mp4540: z.string(), mp4720: z.string(), poster: z.string()}); export const gameStates = z.enum(["new", "active", "waiting_for_declaration", "declaring_winner", "winner_declared", "changing_game_result", "processing_payouts", "ended", "canceled", "refunded", "quarantined"]); export const language = z.enum(["en-us", "es-mx"]); export const orientation = z.enum(["landscape", "portrait"]); export const creator = z.object({avatar: z.string(), id: z.string(), partnerName: z.string(), avatarAssetId: z.string(), profileColor: z.string(), userName: z.string(), createdAt: z.string(), updatedAt: z.string()}); export const category = z.object({language: language, id: z.string(), categoryName: z.string(), createdAt: z.string(), updatedAt: z.string(), deletedAt: z.string(), sticker: z.string().optional(), icon: z.string().optional()}); export const game = z.object({audioUrl: z.string(), videoUrls: videoUrls, questionVideoUrls: videoUrls, landscapeVideoUrls: videoUrls, resultUrls: videoUrls, state: gameStates, language: language, gameType: gameType, orientation: orientation.optional(), correctAnswer: side.optional(), id: z.string(), creatorId: z.string(), creator: creator, startTime: z.string(), endTime: z.string(), lastJoinTime: z.string(), gameTitle: z.string(), sourceUrl: z.string(), sideA: z.string(), sideB: z.string(), videoAssetId: z.string().optional(), questionVideoAssetId: z.string().optional(), landscapeVideoAssetId: z.string().optional(), audioAssetId: z.string().optional(), resultAssetId: z.string().optional(), text: z.string(), categoryId: z.string(), category: category, isAd: z.boolean(), isPrivate: z.boolean().optional(), bingSearch: z.string().optional(), bingSearchClaim: z.string().optional(), metadata: z.object({}).optional(), createdAt: z.string(), updatedAt: z.string(), version: z.number(), urls: z.array(z.string()).optional()}); export const gameplay = z.object({gameType: gameType, state: gameplayStates, side: side, ticketType: ticketType, rewardType: rewardType.optional(), achievements: z.array(achievementEntity).optional(), game: game, id: z.string(), userId: z.string(), gameId: z.string(), quantity: z.number(), matched: z.number(), rewardClaimed: z.boolean().optional(), createdAt: z.string(), opponents: z.object({}), returnUrl: z.string(), sideAPercent: z.number().optional(), majorityBonusWon: z.boolean().optional(), newLeague: z.enum(["level1", "level2", "level3", "level4"]).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 /v1/gameplays/{id} */ export const getApiV1GameplaysId = async (args: GetApiV1GameplaysIdArgs, options: GetApiV1GameplaysIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/gameplays/${encodeURIComponent(String(args.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(gameplay, await res.json()) };