import {z} from 'zod' export type UserJourneyId = string; export type BusinessApplicantId = string; export type BankDocumentVerificationDeclaredData = {registered_name: string, trading_name: string}; export type BankDocumentVerificationBase = {user_journey_id?: UserJourneyId | undefined, applicant_id?: BusinessApplicantId | undefined, webhook_url?: string | undefined, declared_data?: BankDocumentVerificationDeclaredData | undefined}; export type BankDocumentVerificationInputPost = BankDocumentVerificationBase; export type CreateApiV2BankDocumentVerificationsArgs = {body: BankDocumentVerificationInputPost}; export type CreateApiV2BankDocumentVerificationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bankDocumentVerificationId = z.string(); export const bankDocumentVerificationStatus = z.enum(["created", "quality_checks_in_progress", "checks_in_progress", "approved", "declined", "retry_required", "inconclusive"]); export const responseCode = z.number().int().gte(10000).lte(69999); export const responseSummary = z.string(); export const responseCodes = z.object({code: responseCode, summary: responseSummary}); export const riskLabel = z.enum(["multiple_faces_detected", "mcc_not_confident", "risky_document_format", "no_uk_footprint"]); export const riskLabels = z.array(riskLabel); export const accountTypes = z.enum(["checking", "savings"]); export const bankDocumentType = z.enum(["bank_statement", "bank_letter", "bank_check"]); export const countryCodeIso2 = z.string(); export const address = z.object({address_line1: z.string().max(250).optional(), address_line2: z.string().max(250).optional(), city: z.string().max(50).optional(), state: z.string().max(50).optional(), zip: z.string().max(50).optional(), country: countryCodeIso2.optional()}); export const bankDocument = z.object({bank_account_number: z.string().max(255).optional(), bank_routing_number: z.string().max(255).optional(), account_type: accountTypes.optional(), company_name: z.string().min(2).max(510).optional(), bank_name: z.string().min(2).max(510).optional(), issue_date: z.string().optional(), document_type: bankDocumentType.optional(), company_address: address.optional()}); export const halLink = z.object({href: z.string()}); export const bankDocumentVerificationLinks = z.object({self: halLink}); export const userJourneyId = z.string(); export const businessApplicantId = z.string(); export const bankDocumentVerificationDeclaredData = z.object({registered_name: z.string().min(2).max(255), trading_name: z.string().min(2).max(255)}); export const bankDocumentVerificationOutputPost = z.object({id: bankDocumentVerificationId.optional(), status: bankDocumentVerificationStatus.optional(), response_codes: z.array(responseCodes).optional(), risk_labels: riskLabels.optional(), bank_document: bankDocument.optional(), _links: bankDocumentVerificationLinks.optional(), user_journey_id: userJourneyId.optional(), applicant_id: businessApplicantId.optional(), webhook_url: z.string().optional(), declared_data: bankDocumentVerificationDeclaredData.optional(), created_on: z.string(), modified_on: 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 a Bank Document Verification * * POST /v2/bank-document-verifications */ export const createApiV2BankDocumentVerifications = async (args: CreateApiV2BankDocumentVerificationsArgs, options?: CreateApiV2BankDocumentVerificationsOptions) => { const {baseUrl = "https://api.ubble.ai", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/bank-document-verifications${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(bankDocumentVerificationOutputPost, await res.json()) };