import {z} from 'zod' export type DocumentFieldDetectorCreate = {uid?: string | undefined, warning_message?: string | undefined, category?: ('simple_config' | null) | undefined, exclude_regexps?: (string | null) | undefined, definition_words?: (string | null) | undefined, include_regexps?: (string | null) | undefined, regexps_pre_process_lower?: boolean | undefined, detected_value?: (string | null) | undefined, extraction_hint?: ('TAKE_FIRST' | 'TAKE_SECOND' | 'TAKE_LAST' | 'TAKE_MIN' | 'TAKE_MAX' | null) | undefined, text_part: 'FULL' | 'BEFORE_REGEXP' | 'AFTER_REGEXP' | 'INSIDE_REGEXP', detect_limit_unit: 'NONE' | 'UNIT', detect_limit_count: number, field: string}; export type CreateApiApiV1DocumentDocumentFieldDetectorsArgs = {body: DocumentFieldDetectorCreate}; export type CreateApiApiV1DocumentDocumentFieldDetectorsOptions = {baseUrl: string, AuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const documentFieldDetectorCreate = z.object({uid: z.string().optional(), warning_message: z.string().optional(), category: z.literal("simple_config").nullable().optional(), exclude_regexps: z.string().nullable().optional(), definition_words: z.string().nullable().optional(), include_regexps: z.string().nullable().optional(), regexps_pre_process_lower: z.boolean().optional(), detected_value: z.string().max(256).nullable().optional(), extraction_hint: z.enum(["TAKE_FIRST", "TAKE_SECOND", "TAKE_LAST", "TAKE_MIN", "TAKE_MAX"]).nullable().optional(), text_part: z.enum(["FULL", "BEFORE_REGEXP", "AFTER_REGEXP", "INSIDE_REGEXP"]), detect_limit_unit: z.enum(["NONE", "UNIT"]), detect_limit_count: z.number().int().gte(-2147483648).lte(2147483647), field: z.string()}); 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 }; /** * Create Document Field * * POST /api/v1/document/document-field-detectors/ */ export const createApiApiV1DocumentDocumentFieldDetectors = async (args: CreateApiApiV1DocumentDocumentFieldDetectorsArgs, options: CreateApiApiV1DocumentDocumentFieldDetectorsOptions) => { const {baseUrl, fetch: fetchFn = fetch, AuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AuthToken !== undefined) headers.set("Authorization", AuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/document/document-field-detectors/${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(documentFieldDetectorCreate, await res.json()) };