import {z} from 'zod' export type DocumentMetaPropertiesIdentifier = string; export type GetApiV3AnnotationsArgs = {document: DocumentMetaPropertiesIdentifier}; export type GetApiV3AnnotationsOptions = {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 getApiV3AnnotationsResponse = z.object({count: z.number().int().gte(0), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: 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 }; /** * Get list of all annotations * * GET /v3/annotations */ export const getApiV3Annotations = async (args: GetApiV3AnnotationsArgs, options?: GetApiV3AnnotationsOptions) => { 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 (args.document != null) query.append("document", String(args.document)) if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/annotations${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(getApiV3AnnotationsResponse, await res.json()) };