import {z} from 'zod' export type GetApiV3AiImageGenerationsGenerationRequestIdArgs = {generationRequestId: string}; export type GetApiV3AiImageGenerationsGenerationRequestIdOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const previewUrl = z.object({preview_size: z.string().nullable().optional(), image_url: z.string().nullable().optional(), width: z.number().int().optional(), height: z.number().int().optional()}); export const imageDataResponse = z.object({index: z.number().int().optional(), is_blocked: z.boolean().optional(), preview_urls: z.array(previewUrl).nullable().optional(), url: z.string().nullable()}); export const assetDetail = z.object({id: z.string().nullable().optional()}); export const imageGenerationsResponse = z.object({generation_request_id: z.string().nullable().optional(), seed: z.number().int().nullable().optional(), results: z.array(imageDataResponse).nullable().optional(), original_asset: assetDetail.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 generated images from a previous generation request * * GET /v3/ai/image-generations/{generationRequestId} * * Also accepts auth: OAuth2 */ export const getApiV3AiImageGenerationsGenerationRequestId = async (args: GetApiV3AiImageGenerationsGenerationRequestIdArgs, options: GetApiV3AiImageGenerationsGenerationRequestIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("Api-Key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v3/ai/image-generations/${encodeURIComponent(String(args.generationRequestId))}${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(imageGenerationsResponse, await res.json()) };