import {z} from 'zod' export type CaptionModelAdd = {srclang?: (string | null) | undefined, label?: (string | null) | undefined, captionsFile?: (string | null) | undefined}; export type CreateApiLibraryLibraryIdVideosVideoIdCaptionsSrclangArgs = {libraryId: number, videoId: string, srclang: string, body: CaptionModelAdd}; export type CreateApiLibraryLibraryIdVideosVideoIdCaptionsSrclangOptions = {baseUrl?: string | undefined, AccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const captionValidationModel = z.object({valid: z.boolean().optional(), errorList: z.array(z.string()).nullable().optional(), warningList: z.array(z.string()).nullable().optional(), warningMessage: z.string().nullable().optional()}); export const statusModelOfCaptionValidationModel = z.object({success: z.boolean().optional(), message: z.string().nullable().optional(), statusCode: z.number().int().optional(), data: captionValidationModel.nullable().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 }; /** * Add Caption * * POST /library/{libraryId}/videos/{videoId}/captions/{srclang} */ export const createApiLibraryLibraryIdVideosVideoIdCaptionsSrclang = async (args: CreateApiLibraryLibraryIdVideosVideoIdCaptionsSrclangArgs, options?: CreateApiLibraryLibraryIdVideosVideoIdCaptionsSrclangOptions) => { const {baseUrl = "https://video.bunnycdn.com", fetch: fetchFn = fetch, AccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AccessKey !== undefined) headers.set("AccessKey", AccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/library/${encodeURIComponent(String(args.libraryId))}/videos/${encodeURIComponent(String(args.videoId))}/captions/${encodeURIComponent(String(args.srclang))}${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(statusModelOfCaptionValidationModel, await res.json()) };