import {z} from 'zod' export type PBEpisodeUpload = {fileUrl: string, title?: string | undefined, description?: string | undefined, author?: string | undefined, originalFilename?: string | undefined, duration?: number | undefined, fileSize?: number | undefined, publishDate?: string | undefined, explicit?: boolean | undefined}; export type UpdateApiPodcastBotPodcastsPodcastKeyEpisodesEpisodeKeyUploadArgs = {podcastKey: string, episodeKey: string, body: PBEpisodeUpload}; export type UpdateApiPodcastBotPodcastsPodcastKeyEpisodesEpisodeKeyUploadOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const pBEpisodeUploadResult = z.object({episodeKey: z.string().optional(), status: z.string().optional(), alreadyExisted: 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 }; /** * Ingest an uploaded audio file as the episode with this key: copy it into the podcast's storage and run the post-recording jobs * * PUT /podcast-bot/podcasts/{podcastKey}/episodes/{episodeKey}/upload */ export const updateApiPodcastBotPodcastsPodcastKeyEpisodesEpisodeKeyUpload = async (args: UpdateApiPodcastBotPodcastsPodcastKeyEpisodesEpisodeKeyUploadArgs, options?: UpdateApiPodcastBotPodcastsPodcastKeyEpisodesEpisodeKeyUploadOptions) => { const {baseUrl = "https://api.zeno.fm", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/podcast-bot/podcasts/${encodeURIComponent(String(args.podcastKey))}/episodes/${encodeURIComponent(String(args.episodeKey))}/upload${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(pBEpisodeUploadResult, await res.json()) };