import {z} from 'zod' export type GetApiBatchBatchIdUploadArgs = {page?: number | undefined, limit?: number | undefined}; export type GetApiBatchBatchIdUploadOptions = {baseUrl?: string | undefined, DVIDS?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiBatchBatchIdUploadResponse = z.object({links: z.object({self: z.string(), first: z.string(), last: z.string(), prev: z.string().nullable().optional(), next: z.string().nullable().optional()}), data: z.array(z.object({id: z.string(), type: z.literal("batch-upload"), attributes: z.object({expires_at: z.string().optional(), max_file_size: z.number().int().optional(), use_cdn: z.boolean().optional(), http_method: z.enum(["POST", "PUT"]).optional(), upload_url: z.string().optional(), multipart_form_upload_params: z.object({}).nullable().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 batch uploads with paging and filtering * * GET /batch/{batch_id}/upload */ export const getApiBatchBatchIdUpload = async (args: GetApiBatchBatchIdUploadArgs, options?: GetApiBatchBatchIdUploadOptions) => { 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.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/${encodeURIComponent(String(args.batch_id))}/upload${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(getApiBatchBatchIdUploadResponse, await res.json()) };