import {z} from 'zod' export type PatchApiBatchBatchIdMultipartUploadIdArgs = {body: {data: {id: string, type: 'batch-multipart-upload', attributes: {max_file_size?: number | undefined, max_part_size?: number | undefined, content_type?: string | undefined, parts?: (Array<{etag?: string | undefined, part_number?: number | undefined, size?: number | undefined}>) | undefined}}}}; export type PatchApiBatchBatchIdMultipartUploadIdOptions = {baseUrl?: string | undefined, DVIDS?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiBatchBatchIdMultipartUploadIdResponse = z.object({data: z.object({id: z.string(), type: z.literal("batch-multipart-upload"), attributes: z.object({max_file_size: z.number().int().optional(), max_part_size: z.number().int().optional(), content_type: z.string().optional(), parts: z.array(z.object({etag: z.string().optional(), part_number: z.number().int().gte(1).lte(10000).optional(), size: z.number().int().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 }; /** * Complete a batch multipart upload * * PATCH /batch/{batch_id}/multipart-upload/{id} */ export const patchApiBatchBatchIdMultipartUploadId = async (args: PatchApiBatchBatchIdMultipartUploadIdArgs, options?: PatchApiBatchBatchIdMultipartUploadIdOptions) => { 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))}/multipart-upload/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiBatchBatchIdMultipartUploadIdResponse, await res.json()) };