import {z} from 'zod' export type SubtitleGenerationRequest = {data: {type: string, attributes?: ({languages?: (Array<'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'>) | undefined}) | undefined}}; export type CreateApiDamFilesFileIdSubtitleGenerationsArgs = {file_id: string, body: SubtitleGenerationRequest}; export type CreateApiDamFilesFileIdSubtitleGenerationsOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const subtitleGeneration = z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({status: z.enum(["queued", "processing", "completed", "translation_failed", "no_audio_track", "no_speech", "source_missing", "failed"]).optional(), languages: z.array(z.string()).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional(), links: z.object({self: z.string().optional()}).optional()}); export const subtitleGenerationResponse = z.object({data: subtitleGeneration.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 }; /** * Starts AI subtitle generation for a DAM file * * POST /dam/files/{file_id}/subtitle-generations */ export const createApiDamFilesFileIdSubtitleGenerations = async (args: CreateApiDamFilesFileIdSubtitleGenerationsArgs, options: CreateApiDamFilesFileIdSubtitleGenerationsOptions) => { 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))}/subtitle-generations${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(subtitleGenerationResponse, await res.json()) };