import {z} from 'zod' export type MaskDimensions = {heightPct: number, widthPct: number}; export type MaskPoint = {xPct: number, yPct: number}; export type TransitionStyle = 'smooth' | 'hard_cut'; export type AddClipMaskRequest = {dimensions: MaskDimensions, durationMs: number, intensity?: number | undefined, point: MaskPoint, startTimeMs: number, transition?: TransitionStyle | undefined}; export type CreateApiV1VideosIdClipsClipIdHighlightsArgs = {id: string, clipId: string, body: AddClipMaskRequest}; export type CreateApiV1VideosIdClipsClipIdHighlightsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const maskDimensionsOutput = z.object({heightPct: z.number().gte(0).lte(100), widthPct: z.number().gte(0).lte(100)}); export const maskPointOutput = z.object({xPct: z.number().gte(0).lte(100), yPct: z.number().gte(0).lte(100)}); export const transitionStyle = z.enum(["smooth", "hard_cut"]); export const clipMask = z.object({dimensions: maskDimensionsOutput, durationMs: z.number().gte(0), id: z.string(), intensity: z.number().gte(0).lte(1), point: maskPointOutput, startTimeMs: z.number().gte(0), transition: transitionStyle, type: z.enum(["blur", "highlight"])}); export const clipMaskResponse = z.object({mask: clipMask}); 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 }; /** * Add a highlight to a clip * * POST /v1/videos/{id}/clips/{clipId}/highlights */ export const createApiV1VideosIdClipsClipIdHighlights = async (args: CreateApiV1VideosIdClipsClipIdHighlightsArgs, options?: CreateApiV1VideosIdClipsClipIdHighlightsOptions) => { const {baseUrl = "https://api.tella.com", 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(/\/+$/, '')}/v1/videos/${encodeURIComponent(String(args.id))}/clips/${encodeURIComponent(String(args.clipId))}/highlights${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(clipMaskResponse, await res.json()) };