import {z} from 'zod' export type FreepikMagnificStyleTransferRequest = {engine?: ('balanced' | 'definio' | 'illusio' | '3d_cartoon' | 'colorful_anime' | 'caricature' | 'real' | 'super_real' | 'softy') | undefined, fixed_generation?: boolean | undefined, flavor?: ('faithful' | 'gen_z' | 'psychedelia' | 'detaily' | 'clear' | 'donotstyle' | 'donotstyle_sharp') | undefined, image: string, is_portrait?: boolean | undefined, portrait_beautifier?: ('beautify_face' | 'beautify_face_max') | undefined, portrait_style?: ('standard' | 'pop' | 'super_pop') | undefined, prompt?: string | undefined, reference_image: string, structure_strength?: number | undefined, style_strength?: number | undefined, webhook_url?: string | undefined}; export type CreateApiProxyFreepikV1AiImageStyleTransferArgs = {body: FreepikMagnificStyleTransferRequest}; export type CreateApiProxyFreepikV1AiImageStyleTransferOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const freepikTaskData = z.object({generated: z.array(z.string()).optional(), status: z.enum(["CREATED", "IN_PROGRESS", "COMPLETED", "FAILED"]).optional(), task_id: z.string().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 }; /** * Style transfer an image * * POST /proxy/freepik/v1/ai/image-style-transfer */ export const createApiProxyFreepikV1AiImageStyleTransfer = async (args: CreateApiProxyFreepikV1AiImageStyleTransferArgs, options?: CreateApiProxyFreepikV1AiImageStyleTransferOptions) => { const {baseUrl = "https://api.comfy.org", 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(/\/+$/, '')}/proxy/freepik/v1/ai/image-style-transfer${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(freepikTaskData, await res.json()) };