import {z} from 'zod' export type BackgroundMode = 'white_background' | 'transparent_background' | 'custom_background' | 'generative_background'; export type GetApiV2RemoveBackgroundArgs = {image_url: string, background?: BackgroundMode | undefined, background_url?: string | undefined, layout?: ('center' | 'crop' | 'none') | undefined, effect?: ('shadow' | 'none') | undefined, background_prompt?: string | undefined, background_negative_prompt?: string | undefined, plate_replacement?: ('image' | 'none') | undefined}; export type GetApiV2RemoveBackgroundOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2RemoveBackgroundResponse = z.object({output_url: z.string().optional(), error: z.string().optional(), error_description: 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 }; /** * Vehicle Background Removal API (image URL) * * GET /v2/remove-background */ export const getApiV2RemoveBackground = async (args: GetApiV2RemoveBackgroundArgs, options?: GetApiV2RemoveBackgroundOptions) => { const {baseUrl = "https://api.vinaudit.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.image_url != null) query.append("image_url", String(args.image_url)) if (args.background != null) query.append("background", String(args.background)) if (args.background_url != null) query.append("background_url", String(args.background_url)) if (args.layout != null) query.append("layout", String(args.layout)) if (args.effect != null) query.append("effect", String(args.effect)) if (args.background_prompt != null) query.append("background_prompt", String(args.background_prompt)) if (args.background_negative_prompt != null) query.append("background_negative_prompt", String(args.background_negative_prompt)) if (args.plate_replacement != null) query.append("plate_replacement", String(args.plate_replacement)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/remove-background${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV2RemoveBackgroundResponse, await res.json()) };