import {z} from 'zod' export type GetApiV2AutogameArticleDraftsIdArgs = {id: string}; export type GetApiV2AutogameArticleDraftsIdOptions = {baseUrl: string, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const gameDraftState = z.enum(["sqa-to-review", "video-to-review", "video-ready", "approved", "rejected", "published"]); export const gameType = z.enum(["POLL", "PREDICTION", "INSTANT_POLL", "TRIVIA", "SURVEY", "THIS_THAT"]); export const articleSourceType = z.enum(["widget", "manual", "blob"]); export const articleEntity = z.object({sourceType: articleSourceType, id: z.string(), articleKey: z.string(), partnerId: z.string(), url: z.string(), hostName: z.string(), rawOpenAiChat: z.array(z.object({})).optional(), createdAt: z.string(), updatedAt: z.string(), drafts: z.array(z.lazy(() => gameDraftEntity))}); export const side = z.enum(["A", "B"]); export const imageSearch = z.object({searchId: z.string(), selected: z.number()}); export const synopsis = z.object({text: z.string(), images_query: z.string(), image_type: z.string(), image_search: imageSearch}); export const overlay = z.object({start_chunk: z.number(), end_chunk: z.number(), images_query: z.string(), image_type: z.string(), image_search: imageSearch}); export const question = z.object({text: z.string(), images_query: z.string(), image_type: z.string(), image_search: imageSearch}); export const answers = z.object({text: z.string()}); export const gameDetail = z.object({correctAnswer: side.optional(), synopsis: z.array(synopsis), overlays: z.array(overlay), question: z.array(question), answers: z.array(answers)}); export const gameDraftEntity: z.ZodType = z.object({articleId: z.string(), url: z.string(), createdAt: z.string(), updatedAt: z.string(), state: gameDraftState, lock: z.number(), userId: z.string().optional(), manualOverrides: z.object({}).optional(), version: z.number(), gameIds: z.array(z.string()), systemMessage: z.string().optional(), systemMessageName: z.string().optional(), gameType: gameType.optional(), article: articleEntity, id: z.string(), gameDetail: gameDetail, text: 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 /v2/autogame/article/drafts/{id} */ export const getApiV2AutogameArticleDraftsId = async (args: GetApiV2AutogameArticleDraftsIdArgs, options: GetApiV2AutogameArticleDraftsIdOptions) => { 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(/\/+$/, '')}/v2/autogame/article/drafts/${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(gameDraftEntity, await res.json()) };