import {z} from 'zod' export type CreateTrackRequest = {url: string, type: 'text' | 'audio', text_type?: ('subtitles' | 'chapters') | undefined, language_code: string, name?: string | undefined, closed_captions?: boolean | undefined, passthrough?: string | undefined}; export type CreateApiVideoV1AssetsASSETIDTracksArgs = {ASSET_ID: string, body: CreateTrackRequest}; export type CreateApiVideoV1AssetsASSETIDTracksOptions = {baseUrl?: string | undefined, accessToken?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const track = z.object({id: z.string().optional(), type: z.enum(["video", "audio", "text"]).optional(), duration: z.number().optional(), max_width: z.number().int().optional(), max_height: z.number().int().optional(), max_frame_rate: z.number().optional(), max_channels: z.number().int().optional(), text_type: z.enum(["subtitles", "chapters"]).optional(), text_source: z.enum(["uploaded", "embedded", "generated_live", "generated_live_final", "generated_vod"]).optional(), language_code: z.string().optional(), name: z.string().optional(), closed_captions: z.boolean().optional(), passthrough: z.string().optional(), status: z.enum(["preparing", "ready", "errored", "deleted", "skipped"]).optional(), primary: z.boolean().optional(), auto_language_confidence: z.number().optional()}); export const createTrackResponse = z.object({data: track}); 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 an asset track * * POST /video/v1/assets/{ASSET_ID}/tracks * * Also accepts auth: authorizationToken */ export const createApiVideoV1AssetsASSETIDTracks = async (args: CreateApiVideoV1AssetsASSETIDTracksArgs, options?: CreateApiVideoV1AssetsASSETIDTracksOptions) => { const {baseUrl = "https://api.mux.com", fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (accessToken !== undefined) headers.set('authorization', `Basic ${btoa(`${accessToken.username}:${accessToken.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/video/v1/assets/${encodeURIComponent(String(args.ASSET_ID))}/tracks${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(createTrackResponse, await res.json()) };