import {z} from 'zod' export type GetApiDiscardedVideosVideoIdArgs = {videoId: string}; export type GetApiDiscardedVideosVideoIdOptions = {baseUrl?: string | undefined, apiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const metadata = z.object({key: z.string().optional(), value: z.string().optional()}); export const videoSourceLiveStreamLink = z.object({rel: z.string().optional(), uri: z.string().optional()}); export const videoSourceLiveStream = z.object({liveStreamId: z.string().optional(), links: z.array(videoSourceLiveStreamLink).optional()}); export const videoSource = z.object({uri: z.string().optional(), type: z.string().optional(), liveStream: videoSourceLiveStream.optional()}); export const videoAssets = z.object({hls: z.string().optional(), iframe: z.string().optional(), player: z.string().optional(), thumbnail: z.string().optional(), mp4: z.string().optional()}); export const video = z.object({videoId: z.string(), createdAt: z.string().optional(), title: z.string().optional(), description: z.string().optional(), publishedAt: z.string().optional(), updatedAt: z.string().optional(), discardedAt: z.string().nullable().optional(), deletesAt: z.string().nullable().optional(), discarded: z.boolean().optional(), language: z.string().optional(), languageOrigin: z.enum(["api", "auto"]).nullable().optional(), tags: z.array(z.string()).optional(), metadata: z.array(metadata).optional(), source: videoSource.optional(), assets: videoAssets.optional(), playerId: z.string().optional(), public: z.boolean().optional(), panoramic: z.boolean().optional(), mp4Support: z.boolean().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 }; /** * Retrieve a discarded video object * * GET /discarded/videos/{videoId} */ export const getApiDiscardedVideosVideoId = async (args: GetApiDiscardedVideosVideoIdArgs, options?: GetApiDiscardedVideosVideoIdOptions) => { const {baseUrl = "https://ws.api.video", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKey.username}:${apiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/discarded/videos/${encodeURIComponent(String(args.videoId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(video, await res.json()) };