import {z} from 'zod' export type CreateApiApiV3SubmissionsIdFilesArgs = {id: string, 'If-Match': string, body: BodyInit}; export type CreateApiApiV3SubmissionsIdFilesOptions = {baseUrl: string, submissionBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const submissionApiFile = z.object({name: z.string().min(1), sizeBytes: z.number().int().gte(0), sha256: z.string(), state: z.literal("complete"), mediaType: z.string().min(1)}).and(z.record(z.string(), z.unknown())); export const submissionApiManifest = z.object({submissionId: z.string(), revision: z.number().int().gte(0), files: z.array(submissionApiFile)}).and(z.record(z.string(), z.unknown())); 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 }; /** * One image per request. Server computes digest. Serialize uploads; after a lost response GET manifest and reconcile name/digest and ETag. Same name/content at current revision is a retry success; differing content is 409. Incomplete files are never visible. File.name equals the original multipart filename used in Encounter.mediaAssetN. Reject unsafe names or names requiring cleaning; never silently rename. Same-content retries do not advance revision. Pilot: JPEG/PNG, matching extension, max 200 files and 200 MiB completed bytes per draft; exceeding either returns 413. At most one additional bounded retry candidate exists during upload. Staging must be configured. Serialize uploads; processing contention returns 429. * * POST /api/v3/submissions/{id}/files */ export const createApiApiV3SubmissionsIdFiles = async (args: CreateApiApiV3SubmissionsIdFilesArgs, options: CreateApiApiV3SubmissionsIdFilesOptions) => { const {baseUrl, fetch: fetchFn = fetch, submissionBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['If-Match'] != null) headers.set("If-Match", String(args['If-Match'])) if (submissionBearer !== undefined) headers.set('authorization', `Bearer ${submissionBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/submissions/${encodeURIComponent(String(args.id))}/files${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(submissionApiManifest, await res.json()) };