import {z} from 'zod' export type CreateApiDamFilesFileIdVideoCdnEmbedSettingsArgs = {file_id: string, autoplay?: boolean | undefined, loop?: boolean | undefined, muted?: boolean | undefined, hideControls?: boolean | undefined, showDownload?: boolean | undefined, showVideoThumbnail?: boolean | undefined, fitToWindow?: boolean | undefined, subtitles?: boolean | undefined, accessible?: boolean | undefined, width?: number | undefined, height?: number | undefined, videoPlayerPresetId?: number | undefined, color?: string | undefined}; export type CreateApiDamFilesFileIdVideoCdnEmbedSettingsOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const videoEmbedSettings = z.object({data: z.object({cdnFileVideoId: z.number().int().optional(), status: z.enum(["QUEUED", "IN_PROGRESS", "FINISHED", "FAILED"]).optional(), videoSource: z.string().optional(), iframe: z.string().optional(), html: z.string().optional()}).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 }; /** * Activate Video CDN and get embed settings * * POST /dam/files/{file_id}/video-cdn/embed-settings */ export const createApiDamFilesFileIdVideoCdnEmbedSettings = async (args: CreateApiDamFilesFileIdVideoCdnEmbedSettingsArgs, options: CreateApiDamFilesFileIdVideoCdnEmbedSettingsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.autoplay != null) query.append("autoplay", String(args.autoplay)) if (args.loop != null) query.append("loop", String(args.loop)) if (args.muted != null) query.append("muted", String(args.muted)) if (args.hideControls != null) query.append("hideControls", String(args.hideControls)) if (args.showDownload != null) query.append("showDownload", String(args.showDownload)) if (args.showVideoThumbnail != null) query.append("showVideoThumbnail", String(args.showVideoThumbnail)) if (args.fitToWindow != null) query.append("fitToWindow", String(args.fitToWindow)) if (args.subtitles != null) query.append("subtitles", String(args.subtitles)) if (args.accessible != null) query.append("accessible", String(args.accessible)) if (args.width != null) query.append("width", String(args.width)) if (args.height != null) query.append("height", String(args.height)) if (args.videoPlayerPresetId != null) query.append("videoPlayerPresetId", String(args.videoPlayerPresetId)) if (args.color != null) query.append("color", String(args.color)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/dam/files/${encodeURIComponent(String(args.file_id))}/video-cdn/embed-settings${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(videoEmbedSettings, await res.json()) };