import {z} from 'zod' export type FileSubtitleCue = {start_ms: number, end_ms: number, text: string}; export type FileSubtitleStoreRequest = {data: {type: string, attributes: {language: 'en' | 'da' | 'de' | 'sv' | 'no' | 'nn' | 'fi' | 'nl' | 'fr' | 'es' | 'it' | 'pt' | 'pl' | 'ru' | 'uk' | 'cs' | 'sk' | 'ro' | 'hu' | 'el' | 'tr' | 'bg' | 'hr' | 'sr' | 'sl' | 'et' | 'lv' | 'lt' | 'is' | 'ga' | 'ca' | 'id' | 'ms' | 'vi' | 'th' | 'ar' | 'he' | 'hi' | 'fa' | 'af' | 'zh' | 'ja' | 'ko', label?: (string | null) | undefined, is_default?: boolean | undefined, cues?: Array | undefined, srt?: string | undefined}}}; export type CreateApiDamFilesFileIdSubtitlesArgs = {file_id: string, body: FileSubtitleStoreRequest}; export type CreateApiDamFilesFileIdSubtitlesOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fileSubtitleAttributes = z.object({language: z.string().optional(), label: z.string().nullable().optional(), source: z.enum(["manual", "import", "ai"]).optional(), is_default: z.boolean().optional(), version: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const fileSubtitle = z.object({id: z.string().optional(), type: z.string().optional(), attributes: fileSubtitleAttributes.optional(), relationships: z.object({cues: z.object({links: z.object({related: z.string().optional()}).optional()}).optional()}).optional(), links: z.object({self: z.string().optional()}).optional()}); export const fileSubtitleResponse = z.object({data: fileSubtitle.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 }; /** * Adds a subtitle track to a DAM file * * POST /dam/files/{file_id}/subtitles */ export const createApiDamFilesFileIdSubtitles = async (args: CreateApiDamFilesFileIdSubtitlesArgs, options: CreateApiDamFilesFileIdSubtitlesOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/dam/files/${encodeURIComponent(String(args.file_id))}/subtitles${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(fileSubtitleResponse, await res.json()) };