import {z} from 'zod' export type Annotations = Array; export type Passed = boolean | null; export type RuleSlug = string; export type Message = string; export type DocumentMetaPropertiesIdentifier = string; export type ValidationResultCreate = {annotations: Annotations, passed?: Passed | undefined, ruleSlug: RuleSlug, message: Message, document: DocumentMetaPropertiesIdentifier}; export type CreateApiV3ValidationResultsBatchCreateArgs = {body: Array}; export type CreateApiV3ValidationResultsBatchCreateOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const documentMetaPropertiesIdentifier = z.string(); export const validationResult = z.object({id: z.number().int().gte(1), annotations: z.array(z.number().int()), passed: z.boolean().nullable(), ruleSlug: z.string(), message: z.string(), document: documentMetaPropertiesIdentifier}); export const createApiV3ValidationResultsBatchCreateResponse = z.array(validationResult); 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 validation results * * POST /v3/validation_results/batch_create */ export const createApiV3ValidationResultsBatchCreate = async (args: CreateApiV3ValidationResultsBatchCreateArgs, options?: CreateApiV3ValidationResultsBatchCreateOptions) => { 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/validation_results/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(createApiV3ValidationResultsBatchCreateResponse, await res.json()) };