import {z} from 'zod' export type ImageColor = {rgb?: Array | undefined, std?: Array | undefined, weight?: number | undefined}; export type UserControls = {artistic_level?: number | undefined, background_color?: ImageColor | undefined, colors?: Array | undefined, no_text?: boolean | undefined}; export type ImageFormat = 'webp' | 'png'; export type TransformModel = 'recraftv2' | 'recraftv2_raster' | 'recraftv2_vector' | 'recraftv3' | 'recraftv3_raster' | 'recraftv3_vector' | 'recraftv4' | 'recraftv4_raster' | 'recraftv4_vector' | 'recraftv4_pro' | 'recraftv4_pro_raster' | 'recraftv4_pro_vector' | 'recraftv4_1' | 'recraftv4_1_raster' | 'recraftv4_1_vector' | 'recraftv4_1_pro' | 'recraftv4_1_pro_raster' | 'recraftv4_1_pro_vector' | 'recraftv4_1_utility' | 'recraftv4_1_utility_raster' | 'recraftv4_1_utility_vector' | 'recraftv4_1_utility_pro' | 'recraftv4_1_utility_pro_raster' | 'recraftv4_1_utility_pro_vector' | 'recraftv4_styles' | 'recraftv4_styles_raster' | 'recraftv4_styles_vector' | 'recraftv4_styles_pro' | 'recraftv4_styles_pro_raster' | 'recraftv4_styles_pro_vector' | 'recraftv4_1_flash' | 'recraftv4_1_flash_raster'; export type ResponseFormat = 'url' | 'b64_json' | 'multipart'; export type ImageSize = '1024x1024' | '1365x1024' | '1024x1365' | '1536x1024' | '1024x1536' | '1820x1024' | '1024x1820' | '1024x2048' | '2048x1024' | '1434x1024' | '1024x1434' | '1024x1280' | '1280x1024' | '1024x1707' | '1707x1024' | '1216x896' | '896x1216' | '1280x832' | '832x1280' | '1152x896' | '896x1152' | '1280x896' | '896x1280' | '832x1344' | '768x1344' | '1344x768' | '768x1536' | '1536x768' | '2048x2048' | '2432x1792' | '1792x2432' | '2560x1664' | '1664x2560' | '2304x1792' | '1792x2304' | '2560x1792' | '1792x2560' | '1664x2688' | '1536x2688' | '2688x1536' | '1536x3072' | '3072x1536' | '1:1' | '2:1' | '1:2' | '3:2' | '2:3' | '4:3' | '3:4' | '5:4' | '4:5' | '6:10' | '14:10' | '10:14' | '16:9' | '9:16'; export type StyleMatch = 'regular' | 'precise' | 'flexible'; export type TextLayoutItem = {bbox: Array>, text: string}; export type TextLayout = Array; export type UpscaleMode = 'upscale4mp' | 'upscale16mp'; export type CreateApiV1ImagesGenerationsVectorArgs = {body: {controls?: UserControls | undefined, image_format?: ImageFormat | undefined, model?: TransformModel | undefined, n?: number | undefined, negative_prompt?: string | undefined, prompt: string, random_seed?: number | undefined, response_format?: ResponseFormat | undefined, size?: ImageSize | undefined, style?: string | undefined, style_id?: string | undefined, style_match?: StyleMatch | undefined, text_layout?: TextLayout | undefined, upscale?: UpscaleMode | undefined, zdr?: boolean | undefined, style_reference_urls?: Array | undefined}}; export type CreateApiV1ImagesGenerationsVectorOptions = {baseUrl?: string | undefined, auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const image = z.object({b64_json: z.string().optional(), image_id: z.string(), revised_prompt: z.string().optional(), url: z.string().optional()}); export const generateImageResponse = z.object({created: z.number().int(), credits: z.number().int(), data: z.array(image), style_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 }; /** * Generate vector image from prompt * * POST /v1/images/generations/vector */ export const createApiV1ImagesGenerationsVector = async (args: CreateApiV1ImagesGenerationsVectorArgs, options?: CreateApiV1ImagesGenerationsVectorOptions) => { const {baseUrl = "https://external.api.recraft.ai", fetch: fetchFn = fetch, auth0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (auth0 !== undefined) headers.set('authorization', `Bearer ${auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/images/generations/vector${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(generateImageResponse, await res.json()) };