import {z} from 'zod' export type Rectangle = {pageIndex?: number | undefined, x0: number, y0: number, x1: number, y1: number}; export type Rectangles = Array; export type DocumentMetaPropertiesIdentifier = string; export type PageIndex = number | null; export type DataPoint = string; export type Field = string | null; export type Raw = string | null; export type IsClientVerified = boolean; export type ValidationResult = {id: number, annotations: Array, passed: boolean | null, ruleSlug: string, message: string, document: DocumentMetaPropertiesIdentifier}; export type ChangedValidationResults = {created?: Array | undefined, updated?: Array | undefined, deleted?: Array | undefined} | {[key: string]: unknown} | null; export type AnnotationCreate = {rectangles?: Rectangles | undefined, document: DocumentMetaPropertiesIdentifier, pageIndex: PageIndex, dataPoint?: DataPoint | undefined, field?: Field | undefined, raw?: Raw | undefined, parsed?: (string | null | number | null | number | null | boolean | null | {[key: string]: unknown} | null | Array | null) | undefined, isClientVerified?: IsClientVerified | undefined, parent?: (number | null) | undefined, validationResults?: Array | undefined}; export type BatchCreateAnnotationsRequest = Array; export type CreateApiV3AnnotationsBatchCreateArgs = {body: BatchCreateAnnotationsRequest}; export type CreateApiV3AnnotationsBatchCreateOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const rectangle = z.object({pageIndex: z.number().int().gte(0).optional(), x0: z.number(), y0: z.number(), x1: z.number(), y1: z.number()}); export const documentMetaPropertiesIdentifier = z.string(); export const annotationContentType = z.enum(["text", "integer", "float", "decimal", "date", "datetime", "daterange", "boolean", "enum", "location", "phonenumber", "json", "table", "expectedremuneration", "jobtitle", "language", "skill", "yearsexperience", "group", "table_deprecated", "url", "image", "docclf"]); export const annotation = z.object({id: z.number().int().gte(1), rectangle: rectangle, rectangles: z.array(rectangle), document: documentMetaPropertiesIdentifier, pageIndex: z.number().int().gte(0).nullable(), raw: z.string().nullable(), confidence: z.number().nullable(), classificationConfidence: z.number().nullable(), textExtractionConfidence: z.number().nullable(), isVerified: z.boolean(), isClientVerified: z.boolean(), isAutoVerified: z.boolean(), dataPoint: z.string().optional(), field: z.string().nullable().optional(), contentType: annotationContentType, parent: z.number().int().nullable().optional()}).and(z.record(z.string(), z.unknown())).nullable(); export const batchCreateAnnotationsResponse = z.array(annotation); 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 }; /** * Batch create annotations * * POST /v3/annotations/batch_create */ export const createApiV3AnnotationsBatchCreate = async (args: CreateApiV3AnnotationsBatchCreateArgs, options?: CreateApiV3AnnotationsBatchCreateOptions) => { const {baseUrl = "https://api.affinda.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/annotations/batch_create${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(batchCreateAnnotationsResponse, await res.json()) };