import {z} from 'zod' export type AnnotationPropertiesId = number; export type GetApiV3AnnotationsIdArgs = {id: AnnotationPropertiesId}; export type GetApiV3AnnotationsIdOptions = {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 }; /** * Get specific annotation * * GET /v3/annotations/{id} */ export const getApiV3AnnotationsId = async (args: GetApiV3AnnotationsIdArgs, options?: GetApiV3AnnotationsIdOptions) => { const {baseUrl = "https://api.affinda.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) 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()) };