import {z} from 'zod' export type VerificationBankTransferCode = {id?: string | undefined, type?: ('BSB' | 'IFSC' | 'ROUTING_NUMBER' | 'SORT_CODE') | undefined}; export type VerificationAccount = {accountName?: string | undefined, accountType: 'SAVINGS' | 'CHECKING', accountNumber: string, bankTransferCode: VerificationBankTransferCode}; export type Money = {amount: number, currency: 'AUD' | 'BRL' | 'CAD' | 'EUR' | 'GBP' | 'HKD' | 'IDR' | 'INR' | 'JPY' | 'NZD' | 'SGD' | 'USD' | 'ZAR' | 'CNY' | 'VND' | 'MYR' | 'CHF'}; export type VerificationTransaction = {amount: Money, baseType: 'CREDIT' | 'DEBIT'}; export type UpdateVerification = {accountId?: number | undefined, reason?: ('DATA_NOT_AVAILABLE' | 'ACCOUNT_HOLDER_MISMATCH' | 'FULL_ACCOUNT_NUMBER_AND_BANK_TRANSFER_CODE_NOT_AVAILABLE' | 'FULL_ACCOUNT_NUMBER_NOT_AVAILABLE' | 'BANK_TRANSFER_CODE_NOT_AVAILABLE' | 'EXPIRED' | 'DATA_MISMATCH' | 'INSTRUCTION_GENERATION_ERROR') | undefined, verificationStatus?: ('INITIATED' | 'DEPOSITED' | 'SUCCESS' | 'FAILED') | undefined, providerAccountId?: number | undefined, verificationType?: ('MATCHING' | 'CHALLENGE_DEPOSIT') | undefined, account?: VerificationAccount | undefined, transaction: Array, verificationDate?: string | undefined, verificationId?: number | undefined}; export type UpdateVerificationRequest = {verification?: UpdateVerification | undefined}; export type UpdateApiVerificationArgs = {body: UpdateVerificationRequest}; export type UpdateApiVerificationOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const verificationBankTransferCode = z.object({id: z.string().optional(), type: z.enum(["BSB", "IFSC", "ROUTING_NUMBER", "SORT_CODE"]).optional()}); export const verificationAccount = z.object({accountName: z.string().optional(), accountType: z.enum(["SAVINGS", "CHECKING"]), accountNumber: z.string().min(3).max(17), bankTransferCode: verificationBankTransferCode}); export const verification = z.object({accountId: z.number().int().optional(), reason: z.enum(["DATA_NOT_AVAILABLE", "ACCOUNT_HOLDER_MISMATCH", "FULL_ACCOUNT_NUMBER_AND_BANK_TRANSFER_CODE_NOT_AVAILABLE", "FULL_ACCOUNT_NUMBER_NOT_AVAILABLE", "BANK_TRANSFER_CODE_NOT_AVAILABLE", "EXPIRED", "DATA_MISMATCH", "INSTRUCTION_GENERATION_ERROR"]).optional(), verificationStatus: z.enum(["INITIATED", "DEPOSITED", "SUCCESS", "FAILED"]).optional(), providerAccountId: z.number().int().optional(), verificationType: z.enum(["MATCHING", "CHALLENGE_DEPOSIT"]).optional(), account: verificationAccount.optional(), verificationDate: z.string().optional(), verificationId: z.number().int().optional()}); export const verificationResponse = z.object({verification: z.array(verification).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 }; /** * Verify Challenge Deposit * * PUT /verification */ export const updateApiVerification = async (args: UpdateApiVerificationArgs, options: UpdateApiVerificationOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json;charset=UTF-8") const url = `${baseUrl.replace(/\/+$/, '')}/verification${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(verificationResponse, await res.json()) };