import {z} from 'zod' export type GetApiBatchArgs = {closed?: boolean | undefined, page?: number | undefined, limit?: number | undefined}; export type GetApiBatchOptions = {baseUrl?: string | undefined, DVIDS?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiBatchResponse = z.object({data: z.array(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()})).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 }; /** * Get a list of batches with paging and filtering * * GET /batch */ export const getApiBatch = async (args: GetApiBatchArgs, options?: GetApiBatchOptions) => { 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 (args.closed != null) query.append("closed", String(args.closed)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (DVIDS !== undefined) headers.set('authorization', `Bearer ${DVIDS}`) const url = `${baseUrl.replace(/\/+$/, '')}/batch${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(getApiBatchResponse, await res.json()) };