import {z} from 'zod' export type KycPersonLookupRequest = {dni_or_nie: string, finalidad: string}; export type CreateApiApiV1PersonsLookupArgs = {'X-CSRF-Token': string, body: KycPersonLookupRequest}; export type CreateApiApiV1PersonsLookupOptions = {baseUrl?: string | undefined, cookieAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const jsonValue: z.ZodType = z.union([z.string(), z.number(), z.boolean(), z.array(z.lazy(() => jsonValue)), z.record(z.string(), z.lazy(() => jsonValue))]).nullable(); export const kycPersonLookupResults = z.object({aeat_morosos: z.array(jsonValue), claims_evidence: z.array(jsonValue), borme_facts: z.array(jsonValue)}); export const kycUsage = z.object({used: z.number().int(), max: z.number().int(), remaining: z.number().int(), window_days: z.number().int(), unlimited: z.boolean()}); export const kycPersonLookupResponse = z.object({ok: z.boolean(), status: z.string(), error: z.string().nullable().optional(), message: z.string().nullable().optional(), query_redacted: z.string().nullable().optional(), query_type: z.string().nullable().optional(), matches_count: z.number().int().optional(), sources_matched: z.array(z.string()).optional(), results: kycPersonLookupResults.optional(), finalidad: z.string().nullable().optional(), usage: kycUsage.optional(), credits_charged: z.number().int().optional(), request_id: z.string(), disclaimer: z.string().nullable().optional(), correction_url: z.string().nullable().optional(), audit_url: z.string().nullable().optional()}); 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 }; /** * Run an authenticated KYC documentary lookup * * POST /api/v1/persons/lookup */ export const createApiApiV1PersonsLookup = async (args: CreateApiApiV1PersonsLookupArgs, options?: CreateApiApiV1PersonsLookupOptions) => { const {baseUrl = "https://openmercantil.es", fetch: fetchFn = fetch, cookieAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-CSRF-Token'] != null) headers.set("X-CSRF-Token", String(args['X-CSRF-Token'])) if (cookieAuth !== undefined) cookies.push("ob_sess=" + encodeURIComponent(cookieAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/persons/lookup${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(kycPersonLookupResponse, await res.json()) };