import {z} from 'zod' export type ImageGenerationRequest = {model: string, prompt: string, enhance_prompt?: boolean | undefined, go_fast?: boolean | undefined, guidance?: number | undefined, negative_prompt?: string | undefined, num_inference_steps?: number | undefined, seed?: (number | null) | undefined, disable_safety_checker?: boolean | undefined, image?: (string | null) | undefined, strength?: number | undefined, lora_weights?: (string | null) | undefined, lora_scale?: number | undefined, extra_lora_weights?: Array | undefined, extra_lora_scale?: Array | undefined, aspect_ratio?: string | undefined, image_size?: string | undefined, output_format?: string | undefined, output_quality?: number | undefined}; export type CreateApiImagesGenerationsArgs = {body: ImageGenerationRequest}; export type CreateApiImagesGenerationsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const imageGenerationResponse = z.object({created: z.number().int().optional(), data: z.array(z.object({url: z.string().nullable().optional(), b64_json: z.string().nullable().optional()})).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 }; /** * Create Image * * POST /images/generations */ export const createApiImagesGenerations = async (args: CreateApiImagesGenerationsArgs, options?: CreateApiImagesGenerationsOptions) => { const {baseUrl = "https://platform.qubrid.com/v1", 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/images/generations${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(imageGenerationResponse, await res.json()) };