import {z} from 'zod' export type ChapterModel = {title: string, start?: number | undefined, end?: number | undefined}; export type MomentModel = {label: string, timestamp?: number | undefined}; export type MetaTagModel = {property?: (string | null) | undefined, value?: (string | null) | undefined}; export type UpdateVideoModel = {title?: (string | null) | undefined, collectionId?: (string | null) | undefined, chapters?: (Array | null) | undefined, moments?: (Array | null) | undefined, metaTags?: (Array | null) | undefined}; export type CreateApiLibraryLibraryIdVideosVideoIdArgs = {libraryId: number, videoId: string, body: UpdateVideoModel}; export type CreateApiLibraryLibraryIdVideosVideoIdOptions = {baseUrl?: string | undefined, AccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const statusModel = z.object({success: z.boolean().optional(), message: z.string().nullable().optional(), statusCode: z.number().int().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 }; /** * Update Video * * POST /library/{libraryId}/videos/{videoId} */ export const createApiLibraryLibraryIdVideosVideoId = async (args: CreateApiLibraryLibraryIdVideosVideoIdArgs, options?: CreateApiLibraryLibraryIdVideosVideoIdOptions) => { 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))}${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(statusModel, await res.json()) };