import {z} from 'zod' export type CreateApiBatchBatchIdNewsIdMediaArgs = {body: {data: Array<{id: string, type: 'audio' | 'photo' | 'graphic' | 'news' | 'video' | 'publication-issue', meta: {is_primary?: boolean | undefined}}>}}; export type CreateApiBatchBatchIdNewsIdMediaOptions = {baseUrl?: string | undefined, DVIDS?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiBatchBatchIdNewsIdMediaResponse = z.object({data: z.array(z.object({id: z.string(), type: z.enum(["audio", "photo", "graphic", "news", "video", "publication-issue"]), meta: z.object({is_primary: 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 }; /** * Add the specified media resources to the media collection for the news story * * POST /batch/{batch_id}/news/{id}/media */ export const createApiBatchBatchIdNewsIdMedia = async (args: CreateApiBatchBatchIdNewsIdMediaArgs, options?: CreateApiBatchBatchIdNewsIdMediaOptions) => { const {baseUrl = "https://submitapi.dvidshub.net", fetch: fetchFn = fetch, DVIDS} = 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 (DVIDS !== undefined) headers.set('authorization', `Bearer ${DVIDS}`) const url = `${baseUrl.replace(/\/+$/, '')}/batch/${encodeURIComponent(String(args.batch_id))}/news/${encodeURIComponent(String(args.id))}/media${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(createApiBatchBatchIdNewsIdMediaResponse, await res.json()) };