import {z} from 'zod' export type SubmissionApiCommit = {validationId: string}; export type CreateApiApiV3SubmissionsIdCommitArgs = {id: string, 'If-Match': string, 'Idempotency-Key': string, body: SubmissionApiCommit}; export type CreateApiApiV3SubmissionsIdCommitOptions = {baseUrl: string, submissionBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const submissionApiAccepted = z.object({submissionId: z.string(), operationId: z.string(), importTaskId: z.string(), acceptedRevision: z.number().int().gte(0), statusUrl: z.string().min(1)}).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 }; /** * Atomically freezes a validated draft and accepts at most one execution. Same keyed request returns the original acceptance before checking stale If-Match. A different key cannot queue a second execution. Invalid validation returns 422 VALIDATION_INVALID; stale validation/config returns 409 VALIDATION_STALE; a different key after acceptance returns 409 ALREADY_COMMITTED. After admission shutdown recover via GET status/results; mutation retries may return 503 instead of replaying 202. * * POST /api/v3/submissions/{id}/commit */ export const createApiApiV3SubmissionsIdCommit = async (args: CreateApiApiV3SubmissionsIdCommitArgs, options: CreateApiApiV3SubmissionsIdCommitOptions) => { const {baseUrl, fetch: fetchFn = fetch, submissionBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['If-Match'] != null) headers.set("If-Match", String(args['If-Match'])) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (submissionBearer !== undefined) headers.set('authorization', `Bearer ${submissionBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/submissions/${encodeURIComponent(String(args.id))}/commit${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(submissionApiAccepted, await res.json()) };