import {z} from 'zod' export type Language = 'en-us' | 'es-mx'; export type Orientation = 'landscape' | 'portrait'; export type GameType = 'POLL' | 'PREDICTION' | 'INSTANT_POLL' | 'TRIVIA' | 'SURVEY' | 'THIS_THAT'; export type Side = 'A' | 'B'; export type CreateGameDto = {language: Language, orientation?: Orientation | undefined, gameType: GameType, correctAnswer?: Side | undefined, startTime: string, lastJoinTime: string, endTime: string, gameTitle: string, sourceUrl: string, categoryId: string, sideA: string, sideB: string, videoAssetId?: string | undefined, text: string, isAd: boolean, isPrivate?: boolean | undefined, bingSearch?: string | undefined, bingSearchClaim?: string | undefined, metadata?: Record | undefined, urls?: Array | undefined, questionVideoAssetId?: string | undefined, landscapeVideoAssetId?: string | undefined, audioAssetId?: string | undefined}; export type CreateApiAutogameGameArgs = {creatorId: string, idempotencyKey: string, skipRelevanceCheck?: boolean | undefined, body: CreateGameDto}; export type CreateApiAutogameGameOptions = {baseUrl: string, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const upload = z.object({uploadUrl: z.string(), resourceUrl: z.string()}); 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 /autogame/game */ export const createApiAutogameGame = async (args: CreateApiAutogameGameArgs, options: CreateApiAutogameGameOptions) => { 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.creatorId != null) query.append("creatorId", String(args.creatorId)) if (args.idempotencyKey != null) query.append("idempotencyKey", String(args.idempotencyKey)) if (args.skipRelevanceCheck != null) query.append("skipRelevanceCheck", String(args.skipRelevanceCheck)) if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/autogame/game${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(upload, await res.json()) };