import {z} from 'zod' export type GameExpand = 'creators' | 'categories'; export type GetApiV1CreatorGamesGameIdArgs = {gameId: string, _expand?: GameExpand | undefined, expand?: Array | undefined}; export type GetApiV1CreatorGamesGameIdOptions = {baseUrl: string, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; 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 side = z.enum(["A", "B"]); export const sideStatsByTicketTypeEntity = z.object({gameplays: z.number(), tickets: z.number(), matched: z.number()}); export const sideStatsEntity = z.object({whiteTickets: sideStatsByTicketTypeEntity, blackTickets: sideStatsByTicketTypeEntity, freeTickets: sideStatsByTicketTypeEntity}); export const statsEntity = z.object({sideA: sideStatsEntity, sideB: sideStatsEntity}); export const gameType = z.enum(["POLL", "PREDICTION", "INSTANT_POLL", "TRIVIA", "SURVEY", "THIS_THAT"]); 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 slushingEntity = z.object({whiteTickets: z.number(), blackTickets: z.number(), blackTicketsGameplays: z.number()}); export const videoUrls = z.object({hls: z.string(), mp4360: z.string(), mp4540: z.string(), mp4720: z.string(), poster: 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 gameEntity = z.object({state: gameStates, winningSide: side.optional(), stats: statsEntity.optional(), gameType: gameType, language: language, orientation: orientation.optional(), correctAnswer: side.optional(), id: z.string(), creatorId: z.string(), creator: creator, version: z.number(), startTime: z.string(), lastJoinTime: z.string(), endTime: z.string(), createdAt: z.string(), updatedAt: z.string(), slushing: slushingEntity.optional(), resultChanged: z.boolean().optional(), hostEarnings: z.number(), isPendingResult: z.boolean(), isFinalResult: z.boolean(), votePercentageSideA: z.number(), votePercentageSideB: z.number(), gameTitle: z.string(), isAd: z.boolean(), isPrivate: z.boolean(), bingSearch: z.string(), bingSearchClaim: z.string().optional(), metadata: z.object({}).optional(), questionVideoUrls: videoUrls.optional(), landscapeVideoUrls: videoUrls.optional(), sideA: z.string(), sideB: z.string(), sourceUrl: z.string(), text: z.string(), videoUrls: videoUrls.optional(), resultUrls: videoUrls.optional(), categoryId: z.string(), category: category, urls: z.array(z.string()).optional(), audioAssetId: 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 }; /** * GET /v1/creator/games/{gameId} */ export const getApiV1CreatorGamesGameId = async (args: GetApiV1CreatorGamesGameIdArgs, options: GetApiV1CreatorGamesGameIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args._expand != null) query.append("_expand", String(args._expand)) for (const item of args.expand ?? []) query.append("expand", String(item)) if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/creator/games/${encodeURIComponent(String(args.gameId))}${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(gameEntity, await res.json()) };