import {z} from 'zod' export type GetApiBatchBatchIdAudioIdOptions = {baseUrl?: string | undefined, DVIDS?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiBatchBatchIdAudioIdResponse = z.object({data: z.object({id: z.string(), type: z.literal("audio"), attributes: z.object({category: z.enum(["b-roll", "briefings", "greetings", "interviews", "newscasts", "recording"]).optional(), title: z.string().max(255), description: z.string().max(65535), instructions: z.string().max(65535).optional(), notes: z.string().max(65535).optional(), tags: z.array(z.string().max(100)).optional(), created_at: z.string(), subdiv: z.string().max(50).optional(), country: z.string().max(2), city: z.string().max(75).optional(), status: z.enum(["unpublished", "published", "uploaded", "needs-approval"]).optional(), id3_artist: z.string().max(100).optional(), id3_album: z.string().max(100).optional(), id3_genre: z.string().max(100).optional(), id3_composer: z.string().max(100).optional(), id3_conductor: z.string().max(100).optional(), id3_track: z.number().int().optional(), id3_disc_number: z.number().int().optional(), id3_year: z.number().int().optional()}), relationships: z.object({authors: z.object({data: z.array(z.object({id: z.string(), type: z.literal("author")}))}).optional(), batch: z.object({data: z.object({id: z.string(), type: z.literal("batch")})}).optional(), batch_upload: z.object({data: z.object({id: z.string(), type: z.enum(["batch-upload", "batch-multipart-upload"])})}), hometowns: z.object({data: z.array(z.object({id: z.string(), type: z.literal("location")}))}).optional(), themes: z.object({data: z.array(z.object({id: z.string(), type: z.literal("theme")}))}).optional(), service_unit: z.object({data: z.object({id: z.string(), type: z.literal("service-unit")})})})})}); 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 }; /** * Get an audio clip by id * * GET /batch/{batch_id}/audio/{id} */ export const getApiBatchBatchIdAudioId = async (options?: GetApiBatchBatchIdAudioIdOptions) => { const {baseUrl = "https://submitapi.dvidshub.net", fetch: fetchFn = fetch, DVIDS} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (DVIDS !== undefined) headers.set('authorization', `Bearer ${DVIDS}`) const url = `${baseUrl.replace(/\/+$/, '')}/batch/${encodeURIComponent(String(args.batch_id))}/audio/${encodeURIComponent(String(args.id))}${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(getApiBatchBatchIdAudioIdResponse, await res.json()) };