import {z} from 'zod' export type ResourceId = string; export type TransformationAssetStatus = 'ready' | 'draft' | 'published' | 'disabled'; export type AudioVariantType = 'verbatim' | 'summary' | 'custom'; export type TransformationAssetMetadata = {title?: string | undefined, description?: string | undefined, source_url?: string | undefined, thumbnail_url?: string | undefined, external_id?: string | undefined, author?: string | undefined, published_at?: string | undefined, content?: string | undefined} | {article_audio_variant_id?: string | undefined, media_id?: string | undefined, variant_type?: AudioVariantType | undefined} | {title?: string | undefined, segments: Array<{text: string, searchText: string}>} | {media_title: string, media_id?: (string | null) | undefined} | {title?: string | undefined, content?: string | undefined}; export type TransformationAssetUpdate = {metadata: {name?: string | undefined, status?: TransformationAssetStatus | undefined, asset_metadata?: TransformationAssetMetadata | undefined}}; export type PatchApiV2SitesSiteIdTransformationAssetsAssetIdArgs = {site_id: ResourceId, asset_id: ResourceId, body: TransformationAssetUpdate}; export type PatchApiV2SitesSiteIdTransformationAssetsAssetIdOptions = {baseUrl?: string | undefined, v2ApiSecret?: string | undefined, fetch?: typeof fetch | undefined}; export const resourceId = z.string().min(8).max(8); export const transformationAssetType = z.enum(["article", "audio", "segmented_script", "studio_generated_video", "text"]); export const transformationAssetStatus = z.enum(["ready", "draft", "published", "disabled"]); export const audioVariantType = z.enum(["verbatim", "summary", "custom"]); export const transformationAssetMetadata = z.union([z.object({title: z.string().optional(), description: z.string().optional(), source_url: z.string().optional(), thumbnail_url: z.string().optional(), external_id: z.string().optional(), author: z.string().optional(), published_at: z.string().optional(), content: z.string().optional()}), z.object({article_audio_variant_id: z.string().optional(), media_id: z.string().optional(), variant_type: audioVariantType.optional()}), z.object({title: z.string().optional(), segments: z.array(z.object({text: z.string(), searchText: z.string()}))}), z.object({media_title: z.string(), media_id: z.string().nullable().optional()}), z.object({title: z.string().optional(), content: z.string().optional()})]); export const transformationAssetResource = z.object({id: resourceId.optional(), site_id: resourceId.optional(), type: z.literal("asset").optional(), created: z.string().optional(), last_modified: z.string().optional(), asset_type: transformationAssetType.optional(), published_version_id: resourceId.optional(), produced_by_transformation_run_id: resourceId.optional(), metadata: z.object({name: z.string().optional(), status: transformationAssetStatus.optional(), asset_metadata: transformationAssetMetadata.optional()}).optional(), relationships: z.object({project: z.object({id: resourceId.optional()}).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 }; /** * Update an asset * * PATCH /v2/sites/{site_id}/transformation_assets/{asset_id}/ */ export const patchApiV2SitesSiteIdTransformationAssetsAssetId = async (args: PatchApiV2SitesSiteIdTransformationAssetsAssetIdArgs, options?: PatchApiV2SitesSiteIdTransformationAssetsAssetIdOptions) => { const {baseUrl = "https://api.jwplayer.com", fetch: fetchFn = fetch, v2ApiSecret} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (v2ApiSecret !== undefined) headers.set("Authorization", v2ApiSecret) const url = `${baseUrl.replace(/\/+$/, '')}/v2/sites/${encodeURIComponent(String(args.site_id))}/transformation_assets/${encodeURIComponent(String(args.asset_id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(transformationAssetResource, await res.json()) };