import {z} from 'zod' export type GptImage2Request = {type: 'text-to-image', prompt: string, resolution: '1K' | '2K' | '4K', aspect_ratio: 'auto' | '1:1' | '5:4' | '9:16' | '21:9' | '16:9' | '4:3' | '3:2' | '4:5' | '3:4' | '2:3' | '2:1' | '1:2' | '3:1' | '1:3' | '9:21', channel?: ('standard' | 'economy') | undefined, batch_count?: number | undefined} | {type: 'image-to-image', prompt: string, image_urls: Array, resolution: '1K' | '2K' | '4K', aspect_ratio: 'auto' | '1:1' | '5:4' | '9:16' | '21:9' | '16:9' | '4:3' | '3:2' | '4:5' | '3:4' | '2:3' | '2:1' | '1:2' | '3:1' | '1:3' | '9:21', channel?: ('standard' | 'economy') | undefined, batch_count?: number | undefined}; export type CreateApiApiV1ImageGptImage2Args = {'Idempotency-Key'?: string | undefined, body: GptImage2Request}; export type CreateApiApiV1ImageGptImage2Options = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1ImageGptImage2Response = z.object({id: z.string(), status: z.enum(["processing", "completed"]), model: z.literal("gpt-image-2"), credits_used: z.number().optional(), remaining_credits: z.number().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 }; /** * Generate images with GPT Image 2 * * POST /api/v1/image/gpt-image-2 */ export const createApiApiV1ImageGptImage2 = async (args: CreateApiApiV1ImageGptImage2Args, options?: CreateApiApiV1ImageGptImage2Options) => { const {baseUrl = "https://www.seedance2ai.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/image/gpt-image-2${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(createApiApiV1ImageGptImage2Response, await res.json()) };