import {z} from 'zod' export type CreateStaticRenditionRequest = {resolution: 'highest' | 'audio-only' | '2160p' | '1440p' | '1080p' | '720p' | '540p' | '480p' | '360p' | '270p', passthrough?: string | undefined}; export type CreateApiVideoV1AssetsASSETIDStaticRenditionsArgs = {ASSET_ID: string, body: CreateStaticRenditionRequest}; export type CreateApiVideoV1AssetsASSETIDStaticRenditionsOptions = {baseUrl?: string | undefined, accessToken?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const staticRendition = z.object({name: z.enum(["low.mp4", "medium.mp4", "high.mp4", "highest.mp4", "audio.m4a", "capped-1080p.mp4", "2160p.mp4", "1440p.mp4", "1080p.mp4", "720p.mp4", "540p.mp4", "480p.mp4", "360p.mp4", "270p.mp4"]).optional(), ext: z.enum(["mp4", "m4a"]).optional(), height: z.number().int().optional(), width: z.number().int().optional(), bitrate: z.number().int().optional(), filesize: z.string().optional(), type: z.enum(["standard", "advanced"]).optional(), status: z.enum(["ready", "preparing", "skipped", "errored"]).optional(), resolution_tier: z.enum(["2160p", "1440p", "1080p", "720p", "audio-only"]).optional(), resolution: z.enum(["highest", "audio-only", "2160p", "1440p", "1080p", "720p", "540p", "480p", "360p", "270p"]).optional(), id: z.string().optional(), passthrough: z.string().optional()}); export const createStaticRenditionResponse = z.object({data: staticRendition}); 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 static rendition for an asset * * POST /video/v1/assets/{ASSET_ID}/static-renditions * * Also accepts auth: authorizationToken */ export const createApiVideoV1AssetsASSETIDStaticRenditions = async (args: CreateApiVideoV1AssetsASSETIDStaticRenditionsArgs, options?: CreateApiVideoV1AssetsASSETIDStaticRenditionsOptions) => { 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))}/static-renditions${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(createStaticRenditionResponse, await res.json()) };