import {z} from 'zod' export type VideoAttributionModel = 'video_generation:1' | 'video_rmbg:1' | 'video_eraser:1' | 'video_mask:1' | 'video_increase_res:1'; export type Agent = string; export type Sync = boolean; export type WebhookUrl = string; export type CreateApiV2VideoAttributionByVideoArgs = {body: {video: string, model: VideoAttributionModel, agent?: Agent | undefined, sync?: Sync | undefined, webhook_url?: WebhookUrl | undefined}}; export type CreateApiV2VideoAttributionByVideoOptions = {baseUrl?: string | undefined, apiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const syncSuccessResponse = z.object({result: z.object({content: z.string()}), request_id: 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 }; /** * Attribution by video URL * * POST /v2/video/attribution/by_video */ export const createApiV2VideoAttributionByVideo = async (args: CreateApiV2VideoAttributionByVideoArgs, options?: CreateApiV2VideoAttributionByVideoOptions) => { const {baseUrl = "https://engine.prod.bria-api.com", fetch: fetchFn = fetch, apiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiToken !== undefined) headers.set("api_token", apiToken) const url = `${baseUrl.replace(/\/+$/, '')}/v2/video/attribution/by_video${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(syncSuccessResponse, await res.json()) };