import {z} from 'zod' export type AnnotationPropertiesId = number; 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 Raw = string | null; export type DataPoint = string; export type Field = 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 Parent = number | null; export type AnnotationUpdate = {rectangles?: Rectangles | undefined, document?: DocumentMetaPropertiesIdentifier | undefined, pageIndex?: PageIndex | undefined, raw?: Raw | undefined, parsed?: (string | null | number | null | number | null | boolean | null | {[key: string]: unknown} | null | Array | null) | undefined, isClientVerified?: IsClientVerified | undefined, dataPoint?: DataPoint | undefined, field?: Field | undefined, parent?: Parent | undefined, validationResults?: Array | undefined}; export type PatchApiV3AnnotationsIdArgs = {id: AnnotationPropertiesId, body: AnnotationUpdate}; export type PatchApiV3AnnotationsIdOptions = {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 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 }; /** * Update an annotation * * PATCH /v3/annotations/{id} */ export const patchApiV3AnnotationsId = async (args: PatchApiV3AnnotationsIdArgs, options?: PatchApiV3AnnotationsIdOptions) => { 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/${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(annotation, await res.json()) };