import {z} from 'zod' export type Seedream5LiteRequest = {type: 'text-to-image', prompt: string, aspect_ratio?: ('1:1' | '4:3' | '3:4' | '16:9' | '9:16' | '2:3' | '3:2' | '21:9') | undefined, quality?: ('2K' | '4K') | undefined, channel?: ('standard' | 'wild') | undefined} | {type: 'image-to-image', prompt: string, aspect_ratio?: ('1:1' | '4:3' | '3:4' | '16:9' | '9:16' | '2:3' | '3:2' | '21:9') | undefined, quality?: ('2K' | '4K') | undefined, channel?: ('standard' | 'wild') | undefined, image_urls: Array}; export type CreateApiApiV1ImageSeedream5LiteArgs = {'Idempotency-Key'?: string | undefined, body: Seedream5LiteRequest}; export type CreateApiApiV1ImageSeedream5LiteOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1ImageSeedream5LiteResponse = z.object({id: z.string(), status: z.enum(["processing", "completed"]), model: z.literal("seedream-5-lite"), 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 Seedream 5 Lite * * POST /api/v1/image/seedream-5-lite */ export const createApiApiV1ImageSeedream5Lite = async (args: CreateApiApiV1ImageSeedream5LiteArgs, options?: CreateApiApiV1ImageSeedream5LiteOptions) => { 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/seedream-5-lite${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(createApiApiV1ImageSeedream5LiteResponse, await res.json()) };