import {z} from 'zod' export type CreateApiV1AssetsArgs = {body: {filename: string, content_type: string, size_bytes: number}}; export type CreateApiV1AssetsOptions = {baseUrl?: string | undefined, ApiKeyBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const asset = z.object({id: z.string().optional(), object: z.string().optional(), asset_id: z.string().optional(), upload_url: z.string().optional(), upload_headers: z.record(z.string(), z.string()).optional(), status: z.enum(["uploading", "processing", "ready", "deleted"]).optional(), asset_type: z.string().optional(), url: z.string().optional(), content_type: z.string().optional(), size_bytes: z.number().int().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 a HeyGen asset upload (for auto-mode file_asset_ids) * * POST /v1/assets */ export const createApiV1Assets = async (args: CreateApiV1AssetsArgs, options?: CreateApiV1AssetsOptions) => { const {baseUrl = "https://api.vivideo.ai", fetch: fetchFn = fetch, ApiKeyBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyBearer !== undefined) headers.set('authorization', `Bearer ${ApiKeyBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/assets${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(asset, await res.json()) };