import {z} from 'zod' export type CreateApiApiV1GptImage2Args = {Authorization?: string | undefined, body: {prompt: string, ratio?: ('auto' | '1:1' | '16:9' | '9:16' | '4:3' | '3:4' | '2:3' | '3:2' | '4:5' | '5:4' | '21:9') | undefined, image_size?: ('1k' | '2k' | '4k') | undefined, quality?: ('low' | 'medium' | 'high') | undefined, image_list?: (Array<{url?: string | undefined}>) | undefined, custom_callback_url?: string | undefined}}; export type CreateApiApiV1GptImage2Options = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1GptImage2Response = z.object({code: z.number().int(), msg: z.string(), data: z.object({img_uuid: 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 }; /** * GPT Image 2 * * POST /api/v1/gpt-image-2 */ export const createApiApiV1GptImage2 = async (args: CreateApiApiV1GptImage2Args, options?: CreateApiApiV1GptImage2Options) => { const {baseUrl = "https://api.goenhance.ai", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/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(createApiApiV1GptImage2Response, await res.json()) };