import {z} from 'zod' export type VopRequestPayeeIdentificationType = 'name' | 'inac' | 'legal_entity_identifier' | 'business_identification_code' | 'bank_identification' | 'central_bank_identification' | 'clearing_house_identification' | 'company_incorporation_number' | 'company_identification' | 'customer_number' | 'duns_number' | 'employee_number' | 'global_location_number' | 'siren' | 'siret' | 'tax_number' | 'business_domain_identification' | 'other_identification' | 'proprietary_code'; export type CommonMetadata = Record; export type VopRequestCreationDetails = {scheme: 'vop', payee_identification_type: VopRequestPayeeIdentificationType, payee_identification: string, payee_account_number: string, payee_additional_information?: string | undefined, requesting_agent_bank_code?: string | undefined, initiated_at?: string | undefined, metadata?: CommonMetadata | undefined}; export type CreateApiPayeeVerificationRequestsArgs = {'Idempotency-Key'?: string | undefined, body: VopRequestCreationDetails}; export type CreateApiPayeeVerificationRequestsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const commonMetadata = z.object({}); export const vopRequestDetails = z.object({id: z.string(), idempotency_key: z.string().optional(), object: z.literal("payee_verification_request"), status: z.enum(["created", "received", "completed", "failed"]), status_details: z.string(), failure_code: z.enum(["business_error", "technical_error", "psp_technical_error"]).nullable().optional(), direction: z.enum(["outgoing", "incoming"]), scheme: z.literal("vop"), request: z.object({payee_identification_type: z.string().optional(), payee_identification: z.string().optional(), sent_identification: z.string().nullable().optional(), payee_account_number: z.string().optional(), additional_information: z.string().optional(), requesting_agent_bank_code: z.string().optional(), responding_agent_bank_code: z.string().optional()}), matching_result: z.enum(["match", "close_match", "no_match", "impossible_match"]), payee_suggested_name: z.string().nullable(), matching_details: z.object({cleaned_identification: z.string().optional(), retrieved_identification: z.string().nullable().optional(), retrieved_identifications: z.array(z.string()).nullable().optional(), internal_account_id: z.string().optional(), matching_score: z.number().int().gte(0).lte(100).optional(), distance: z.enum(["levenshtein", "jarowinkler"]).nullable().optional(), match_threshold: z.number().int().gte(0).lte(100).nullable().optional(), close_match_threshold: z.number().int().gte(0).lte(100).nullable().optional()}).nullable(), scheme_data: z.object({scheme_request_id: z.string().optional(), request_timestamp: z.string().optional(), response_timestamp: z.string().optional()}).nullable(), response_received_at: z.string().nullable().optional(), initiated_at: z.string().nullable().optional(), metadata: commonMetadata.optional(), created_at: 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 }; /** * Send verification payee request * * POST /payee_verification_requests */ export const createApiPayeeVerificationRequests = async (args: CreateApiPayeeVerificationRequestsArgs, options?: CreateApiPayeeVerificationRequestsOptions) => { const {baseUrl = "https://sandbox.numeral.io/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/payee_verification_requests${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(vopRequestDetails, await res.json()) };