import {z} from 'zod' export type CreateApiBatchArgs = {body: {data: {id: string, type: 'batch', attributes: {created_at?: string | undefined, closed_at?: (string | null) | undefined, closed?: boolean | undefined, send_confirmation_email?: boolean | undefined}, relationships?: ({audio?: ({data: Array<{id: string, type: 'audio'}>, links?: {related: string} | undefined}) | undefined, graphic?: ({data: Array<{id: string, type: 'graphic'}>, links?: {related: string} | undefined}) | undefined, 'multipart-upload'?: ({data: Array<{id: string, type: 'batch-multipart-upload'}>, links?: {related: string} | undefined}) | undefined, news?: ({data: Array<{id: string, type: 'news'}>, links?: {related: string} | undefined}) | undefined, photo?: ({data: Array<{id: string, type: 'photo'}>, links?: {related: string} | undefined}) | undefined, 'publication-issue'?: ({data: Array<{id: string, type: 'publication-issue'}>, links?: {related: string} | undefined}) | undefined, upload?: ({data: Array<{id: string, type: 'batch-upload'}>, links?: {related: string} | undefined}) | undefined, video?: ({data: Array<{id: string, type: 'video'}>, links?: {related: string} | undefined}) | undefined}) | undefined}}}; export type CreateApiBatchOptions = {baseUrl?: string | undefined, DVIDS?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiBatchResponse = z.object({data: z.object({id: z.string(), type: z.literal("batch"), attributes: z.object({created_at: z.string().optional(), closed_at: z.string().nullable().optional(), closed: z.boolean().optional(), send_confirmation_email: z.boolean().optional()}), relationships: z.object({audio: z.object({data: z.array(z.object({id: z.string(), type: z.literal("audio")})), links: z.object({related: z.string()}).optional()}).optional(), graphic: z.object({data: z.array(z.object({id: z.string(), type: z.literal("graphic")})), links: z.object({related: z.string()}).optional()}).optional(), "multipart-upload": z.object({data: z.array(z.object({id: z.string(), type: z.literal("batch-multipart-upload")})), links: z.object({related: z.string()}).optional()}).optional(), news: z.object({data: z.array(z.object({id: z.string(), type: z.literal("news")})), links: z.object({related: z.string()}).optional()}).optional(), photo: z.object({data: z.array(z.object({id: z.string(), type: z.literal("photo")})), links: z.object({related: z.string()}).optional()}).optional(), "publication-issue": z.object({data: z.array(z.object({id: z.string(), type: z.literal("publication-issue")})), links: z.object({related: z.string()}).optional()}).optional(), upload: z.object({data: z.array(z.object({id: z.string(), type: z.literal("batch-upload")})), links: z.object({related: z.string()}).optional()}).optional(), video: z.object({data: z.array(z.object({id: z.string(), type: z.literal("video")})), links: z.object({related: z.string()}).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 }; /** * Create a new batch to submit media assets. * * POST /batch */ export const createApiBatch = async (args: CreateApiBatchArgs, options?: CreateApiBatchOptions) => { 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${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(createApiBatchResponse, await res.json()) };