import {z} from 'zod' export type WidgetExpand = 'creator' | 'playlist'; export type GetApiV1CreatorWidgetsWidgetIdArgs = {widgetId: string, _expand?: Array | undefined, expand?: Array | undefined, articleUrl?: string | undefined}; export type GetApiV1CreatorWidgetsWidgetIdOptions = {baseUrl: string, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const widgetTypes = z.enum(["inline", "expanding", "mobile-expanding", "docked", "carousel", "sidebar", "playlist"]); 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 language = z.enum(["en-us", "es-mx"]); 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 gameType = z.enum(["POLL", "PREDICTION", "INSTANT_POLL", "TRIVIA", "SURVEY", "THIS_THAT"]); export const orientation = z.enum(["landscape", "portrait"]); export const side = z.enum(["A", "B"]); 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 gameSet = z.object({id: z.string(), partnerId: z.string().optional(), partner: creator, createdAt: z.string(), updatedAt: z.string(), startTime: z.string(), endTime: z.string(), deletedAt: z.string(), games: z.array(game)}); export const playlist = z.object({language: language, gameSet: gameSet.optional(), id: z.string(), totalGames: z.number(), creatorId: z.string(), creator: creator, widgets: z.array(z.lazy(() => widget)), gamesOrder: z.array(z.string()).optional(), games: z.array(game), autofilledGames: z.array(game), autofillCategories: z.array(category).optional(), autofillCreators: z.array(creator).optional(), autofillExcludedCreators: z.array(creator).optional(), autofill: z.boolean()}); export const widget: z.ZodType = z.object({type: widgetTypes, id: z.string(), name: z.string(), creatorId: z.string(), creator: creator, playlistId: z.string(), playlist: playlist, deletedAt: 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 }; /** * GET /v1/creator/widgets/{widgetId} */ export const getApiV1CreatorWidgetsWidgetId = async (args: GetApiV1CreatorWidgetsWidgetIdArgs, options: GetApiV1CreatorWidgetsWidgetIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args._expand ?? []) query.append("_expand", String(item)) for (const item of args.expand ?? []) query.append("expand", String(item)) if (args.articleUrl != null) query.append("articleUrl", String(args.articleUrl)) if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/creator/widgets/${encodeURIComponent(String(args.widgetId))}${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(widget, await res.json()) };