import {z} from 'zod' export type PatchApiVideosVideoIdTexttracksTexttrackIdArgs = {texttrack_id: number, video_id: number, body: {active?: boolean | undefined, language?: string | undefined, name?: string | undefined, type?: ('captions' | 'chapters' | 'descriptions' | 'metadata' | 'subtitles') | undefined}}; export type PatchApiVideosVideoIdTexttracksTexttrackIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const textTrack = z.object({active: z.boolean(), hls_link: z.string(), hls_link_expires_time: z.string(), language: z.string().nullable(), link: z.string(), link_expires_time: z.string(), name: z.string().nullable(), type: z.enum(["captions", "subtitles"]).nullable(), uri: 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 }; /** * Edit a text track * * PATCH /videos/{video_id}/texttracks/{texttrack_id} */ export const patchApiVideosVideoIdTexttracksTexttrackId = async (args: PatchApiVideosVideoIdTexttracksTexttrackIdArgs, options?: PatchApiVideosVideoIdTexttracksTexttrackIdOptions) => { const {baseUrl = "https://api.vimeo.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.vimeo.video.texttrack+json") headers.set('accept', "application/vnd.vimeo.video.texttrack+json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/videos/${encodeURIComponent(String(args.video_id))}/texttracks/${encodeURIComponent(String(args.texttrack_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(textTrack, await res.json()) };